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

Parse hexadecimal notation #63

Merged
merged 2 commits into from
Nov 2, 2019
Merged
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
11 changes: 10 additions & 1 deletion edn_format/edn_lex.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ def __str__(self):
'CHAR',
'STRING',
'INTEGER',
'HEX_INTEGER',
'FLOAT',
'RATIO',
'SYMBOL',
Expand Down Expand Up @@ -215,13 +216,21 @@ def t_RATIO(t):


def t_INTEGER(t):
r"""[+-]?\d+N?"""
# "No integer other than 0 may begin with 0."
# https://github.com/edn-format/edn#integers
r"""[+-]?(?:0(?!x)|[1-9]\d*)N?"""
if t.value.endswith('N'):
t.value = t.value[:-1]
t.value = int(t.value)
return t


def t_HEX_INTEGER(t):
r"""[+-]?0x[A-F0-9]+"""
t.value = int(t.value, 16)
return t


def t_COMMENT(t):
r'[;][^\n]*'
pass # ignore
Expand Down
1 change: 1 addition & 0 deletions edn_format/edn_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def _tag_decorator(fn_or_cls):
def p_term_leaf(p):
"""term : CHAR
| STRING
| HEX_INTEGER
| INTEGER
| FLOAT
| KEYWORD
Expand Down
5 changes: 5 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ def test_lexer(self):
"true")
self.check_lex("[LexToken(INTEGER,123,1,0)]",
"123")
self.check_lex("[LexToken(HEX_INTEGER,1,1,0)]",
"0x1")
self.check_lex(
"[LexToken(INTEGER,456,1,0), " +
"LexToken(SYMBOL,None,1,4), " +
Expand Down Expand Up @@ -93,6 +95,8 @@ def check_dumps(self, expected_output, actual_input, **kw):
def test_parser_single_expressions(self):
for expected, edn_string in (
(1, "1"),
(16768115, "0xFFDC73"),
(Symbol("xFF"), "xFF"),
(Symbol("a*b"), 'a*b'),
("ab", '"ab"'),
('a"b', r'"a\"b"'),
Expand Down Expand Up @@ -138,6 +142,7 @@ def test_parser_multiple_expressions(self):
([], " ,,,, ,, , "),
([1], ",,,,,,,1,,,,,,,,,"),
([1, 2], "1 2"),
([0, Symbol("x1"), 1], "0 x1 0x1"),
([1, 2], "1 2"),
([True, 42, False, Symbol('end')], "true 42 false end"),
([Symbol("a*b"), 42], 'a*b 42'),
Expand Down