Skip to content

Commit c2faed3

Browse files
Christian ToepferChristian Toepfer
authored andcommitted
Add hex value parsing to set default
1 parent 4ac9dd7 commit c2faed3

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

pyrobuf/parse_proto.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class Parser(object):
3333
('COMMA', r','),
3434
('SKIP', r'\s'),
3535
('SEMICOLON', r';'),
36+
('HEXVALUE', r'(0x[0-9A-Fa-f]+)'),
3637
('NUMERIC', r'(-?[0-9]*\.?[0-9]+(?:[eE][-+]?[0-9]+)?)'),
3738
('STRING', r'("(?:\\.|[^"\\])*"|\'(?:\\.|[^"\\])*\')'),
3839
('BOOLEAN', r'(true|false)'),
@@ -546,8 +547,12 @@ def _parse_default(self, field, tokens):
546547
# This will get updated later
547548
field.default = token.full_name
548549
return
550+
elif token.token_type == 'HEXVALUE':
551+
assert field.type in self.scalars.difference({'bool', 'enum'}), \
552+
"attempting to set hex value as default for non-numeric field on line {}: '{}'".format(
553+
token.line + 1, self.lines[token.line])
549554
elif token.token_type == 'NUMERIC':
550-
assert field.type in self.scalars, \
555+
assert field.type in self.scalars.difference({'bool', 'enum'}), \
551556
"attempting to set numeric as default for non-numeric field on line {}: '{}'".format(
552557
token.line + 1, self.lines[token.line])
553558
if field.type not in self.floats:
@@ -907,6 +912,13 @@ def __init__(self, line, value):
907912
self.line = line
908913
self.value = float(value)
909914

915+
class HexValue(Token):
916+
token_type = 'HEXVALUE'
917+
918+
def __init__(self, line, value):
919+
self.line = line
920+
self.value = int(value, 16)
921+
910922
class String(Token):
911923
token_type = 'STRING'
912924

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import sys
1111

1212

13-
VERSION = "0.9.0.6"
13+
VERSION = "0.9.0.7"
1414
HERE = os.path.dirname(os.path.abspath(__file__))
1515
PYROBUF_DEFS_PXI = "pyrobuf_defs.pxi"
1616
PYROBUF_LIST_PXD = "pyrobuf_list.pxd"

0 commit comments

Comments
 (0)