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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions hangups/javascript.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

FLOAT = purplex.TokenDef(r'[-+]?\d*[.]\d+')

NULL = purplex.TokenDef(r'null')
Expand Down
1 change: 1 addition & 0 deletions hangups/test/test_javascript.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
@pytest.mark.parametrize('input_,expected', [
# simple types
('12', 12),
('-1', -1),
('null', None),
('true', True),
('false', False),
Expand Down