Skip to content

Commit

Permalink
Add hex value parsing to set default
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Toepfer authored and Christian Toepfer committed Nov 7, 2019
1 parent 4ac9dd7 commit c2faed3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
14 changes: 13 additions & 1 deletion pyrobuf/parse_proto.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class Parser(object):
('COMMA', r','),
('SKIP', r'\s'),
('SEMICOLON', r';'),
('HEXVALUE', r'(0x[0-9A-Fa-f]+)'),
('NUMERIC', r'(-?[0-9]*\.?[0-9]+(?:[eE][-+]?[0-9]+)?)'),
('STRING', r'("(?:\\.|[^"\\])*"|\'(?:\\.|[^"\\])*\')'),
('BOOLEAN', r'(true|false)'),
Expand Down Expand Up @@ -546,8 +547,12 @@ def _parse_default(self, field, tokens):
# This will get updated later
field.default = token.full_name
return
elif token.token_type == 'HEXVALUE':
assert field.type in self.scalars.difference({'bool', 'enum'}), \
"attempting to set hex value as default for non-numeric field on line {}: '{}'".format(
token.line + 1, self.lines[token.line])
elif token.token_type == 'NUMERIC':
assert field.type in self.scalars, \
assert field.type in self.scalars.difference({'bool', 'enum'}), \
"attempting to set numeric as default for non-numeric field on line {}: '{}'".format(
token.line + 1, self.lines[token.line])
if field.type not in self.floats:
Expand Down Expand Up @@ -907,6 +912,13 @@ def __init__(self, line, value):
self.line = line
self.value = float(value)

class HexValue(Token):
token_type = 'HEXVALUE'

def __init__(self, line, value):
self.line = line
self.value = int(value, 16)

class String(Token):
token_type = 'STRING'

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import sys


VERSION = "0.9.0.6"
VERSION = "0.9.0.7"
HERE = os.path.dirname(os.path.abspath(__file__))
PYROBUF_DEFS_PXI = "pyrobuf_defs.pxi"
PYROBUF_LIST_PXD = "pyrobuf_list.pxd"
Expand Down

0 comments on commit c2faed3

Please sign in to comment.