diff options
author | Konstanty Bialkowski <konstanty@ieee.org> | 2015-04-19 20:09:21 +1000 |
---|---|---|
committer | Konstanty Bialkowski <konstanty@ieee.org> | 2015-04-20 11:59:33 +1000 |
commit | 3bdbfdc32d6d9e97b867527f7e5be6a712d1edb6 (patch) | |
tree | 732f509adb56a6e2ec6373fa45c34275773fbd25 /pycparser | |
parent | 126492f006edad4dbffda54466e1db679b6700c9 (diff) | |
download | pycparser-3bdbfdc32d6d9e97b867527f7e5be6a712d1edb6.tar.gz |
Allow binary constants (e.g.: 0b01010)
- Add lexer and parser tests.
Diffstat (limited to 'pycparser')
-rw-r--r-- | pycparser/c_lexer.py | 9 | ||||
-rw-r--r-- | pycparser/c_parser.py | 1 |
2 files changed, 9 insertions, 1 deletions
diff --git a/pycparser/c_lexer.py b/pycparser/c_lexer.py index e17a2ea..e8d2af0 100644 --- a/pycparser/c_lexer.py +++ b/pycparser/c_lexer.py @@ -129,7 +129,7 @@ class CLexer(object): 'TYPEID', # constants - 'INT_CONST_DEC', 'INT_CONST_OCT', 'INT_CONST_HEX', + 'INT_CONST_DEC', 'INT_CONST_OCT', 'INT_CONST_HEX', 'INT_CONST_BIN', 'FLOAT_CONST', 'HEX_FLOAT_CONST', 'CHAR_CONST', 'WCHAR_CONST', @@ -183,12 +183,15 @@ class CLexer(object): hex_prefix = '0[xX]' hex_digits = '[0-9a-fA-F]+' + bin_prefix = '0[bB]' + bin_digits = '[01]+' # integer constants (K&R2: A.2.5.1) integer_suffix_opt = r'(([uU]ll)|([uU]LL)|(ll[uU]?)|(LL[uU]?)|([uU][lL])|([lL][uU]?)|[uU])?' decimal_constant = '(0'+integer_suffix_opt+')|([1-9][0-9]*'+integer_suffix_opt+')' octal_constant = '0[0-7]*'+integer_suffix_opt hex_constant = hex_prefix+hex_digits+integer_suffix_opt + bin_constant = bin_prefix+bin_digits+integer_suffix_opt bad_octal_constant = '0[0-7]*[89]' @@ -419,6 +422,10 @@ class CLexer(object): def t_INT_CONST_HEX(self, t): return t + @TOKEN(bin_constant) + def t_INT_CONST_BIN(self, t): + return t + @TOKEN(bad_octal_constant) def t_BAD_CONST_OCT(self, t): msg = "Invalid octal constant" diff --git a/pycparser/c_parser.py b/pycparser/c_parser.py index 4eb26b6..4273051 100644 --- a/pycparser/c_parser.py +++ b/pycparser/c_parser.py @@ -1573,6 +1573,7 @@ class CParser(PLYParser): """ constant : INT_CONST_DEC | INT_CONST_OCT | INT_CONST_HEX + | INT_CONST_BIN """ p[0] = c_ast.Constant( 'int', p[1], self._coord(p.lineno(1))) |