Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated integer regexp to allow for negatives #40

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Updated integer regexp to allow for negatives #40

wants to merge 1 commit into from

Conversation

arecker
Copy link

@arecker arecker commented Nov 27, 2014

Found this while looking for TODOs

Implemented this pattern instead. Added a unit test and it seems to be working.

@@ -73,8 +73,7 @@ def _unescape_string(s):

class JavaScriptLexer(purplex.Lexer):
"""Lexer for a subset of JavaScript."""
# TODO: Negative integers
INTEGER = purplex.TokenDef(r'\d+')
INTEGER = purplex.TokenDef(r'[+-]?(?<!\.)\b[0-9]+\b(?!\.[0-9])')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain what the regular expression is doing? Why not just [-+]?\d+?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I can't explain it : ) I'm not the best with these. I was going off of the SO link I found + the unit test. I linked it in the PR. There is an explation there.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[+-]?

one of +, -, or nothing

(?<!\.)

Negative lookbehind, makes sure that a literal dot will not be matched at this position

\b

“the boundary between a word char (\w) and something that is not a word char”

[0-9]+

One or more digit

\b

-''-

(?!\.[0-9])

Negative lookahead, will make sure a literal dot followed by a digit is not matched


All in all, it means that it will match things like 42, +42, and -42, but not floats like 42.6 or .42

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants