Skip to content

Commit

Permalink
Support for octal numbers with added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
r888800009 committed Nov 3, 2023
1 parent f5f8bf8 commit a764fe0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
5 changes: 3 additions & 2 deletions pyclibrary/c_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1735,10 +1735,11 @@ def print_parse_results(pr, depth=0, name=''):
int_strip = lambda t: t[0].rstrip('UL')
hexint = Regex(r'[+-]?\s*0[xX][{}]+[UL]*'.format(hexnums)).setParseAction(int_strip)
decint = Regex(r'[+-]?\s*[0-9]+[UL]*').setParseAction(int_strip)
integer = (hexint | decint)
octal = Regex(r'[+-]?\s*0[0-7]+[UL]*').setParseAction(lambda t: int_strip(t).replace('0', '0o', 1))
integer = (hexint | octal | decint)
integer.setParseAction(wrap_int)
# in eval expr would not match identifier, it would match a number cause error
integer_in_expr = (hexint | decint)
integer_in_expr = (hexint | octal | decint )
# The floating regex is ugly but it is because we do not want to match
# integer to it.
floating = Regex(r'[+-]?\s*((((\d(\.\d*)?)|(\.\d+))[eE][+-]?\d+)|((\d\.\d*)|(\.\d+)))')
Expand Down
5 changes: 5 additions & 0 deletions tests/headers/macros/macro_values.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
#define MACRO_H2 - 0x000001U
#define MACRO_H3 0X000002UL

// Octal values
#define MACRO_OCT1 + 010
#define MACRO_OCT2 -03000U
#define MACRO_OCT3 02UL

// Bit shifted hexadecimal values
#define MACRO_SH1 (0x000000 << 1)
#define MACRO_SH2 (0x000001U << 2)
Expand Down
11 changes: 11 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,17 @@ def test_values(self):
macros['MACRO_H3'] == '0X000002UL' and
values['MACRO_H3'] == 2)

# Octal integer
assert ('MACRO_OCT1' in macros and
macros['MACRO_OCT1'] == '+ 010' and
values['MACRO_OCT1'] == 0o10)
assert ('MACRO_OCT2' in macros and
macros['MACRO_OCT2'] == '-03000U' and
values['MACRO_OCT2'] == -0o3000)
assert ('MACRO_OCT3' in macros and
macros['MACRO_OCT3'] == '02UL' and
values['MACRO_OCT3'] == 0o2)

# Bit shifted hexadecimal integer
assert ('MACRO_SH1' in macros and
macros['MACRO_SH1'] == '(0x000000 << 1)' and
Expand Down

0 comments on commit a764fe0

Please sign in to comment.