diff options
author | bbudge@chromium.org <bbudge@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-10-07 22:43:04 +0000 |
---|---|---|
committer | bbudge@chromium.org <bbudge@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-10-07 22:43:04 +0000 |
commit | a2365a2cc33bf237617b0632c927d9987f9d816c (patch) | |
tree | ef43d9037e8d17d3fcaaf8ced2f64b4288f93ddd /ppapi/generators | |
parent | acb2299648c3f60917946b4d87714e09d5815da6 (diff) | |
download | chromium_src-a2365a2cc33bf237617b0632c927d9987f9d816c.zip chromium_src-a2365a2cc33bf237617b0632c927d9987f9d816c.tar.gz chromium_src-a2365a2cc33bf237617b0632c927d9987f9d816c.tar.bz2 |
Allow enums values to be simple arithmetic expressions.
BUG=none
TEST=./generator.py test_parser/enum.idl
Review URL: http://codereview.chromium.org/8161006
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@104588 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ppapi/generators')
-rw-r--r-- | ppapi/generators/idl_lexer.py | 6 | ||||
-rw-r--r-- | ppapi/generators/idl_parser.py | 57 | ||||
-rw-r--r-- | ppapi/generators/test_cgen/enum_typedef.h | 6 | ||||
-rw-r--r-- | ppapi/generators/test_cgen/enum_typedef.idl | 4 | ||||
-rw-r--r-- | ppapi/generators/test_lex.in | 7 | ||||
-rw-r--r-- | ppapi/generators/test_parser/enum.idl | 44 |
6 files changed, 106 insertions, 18 deletions
diff --git a/ppapi/generators/idl_lexer.py b/ppapi/generators/idl_lexer.py index 13498c8..940eea2 100644 --- a/ppapi/generators/idl_lexer.py +++ b/ppapi/generators/idl_lexer.py @@ -64,7 +64,8 @@ class IDLLexer(object): 'STRING', # Operators - 'LSHIFT' + 'LSHIFT', + 'RSHIFT' ] # 'keywords' is a map of string to token type. All SYMBOL tokens are @@ -82,7 +83,7 @@ class IDLLexer(object): # 'literals' is a value expected by lex which specifies a list of valid # literal tokens, meaning the token type and token value are identical. - literals = '"*.(){}[],;:=+-' + literals = '"*.(){}[],;:=+-/~|&^' # Token definitions # @@ -100,6 +101,7 @@ class IDLLexer(object): t_OCT = r'-?0[0-7]+' t_HEX = r'-?0[Xx][0-9A-Fa-f]+' t_LSHIFT = r'<<' + t_RSHIFT = r'>>' # A line ending '\n', we use this to increment the line number def t_LINE_END(self, t): diff --git a/ppapi/generators/idl_parser.py b/ppapi/generators/idl_parser.py index fea256c..60b0050 100644 --- a/ppapi/generators/idl_parser.py +++ b/ppapi/generators/idl_parser.py @@ -413,19 +413,52 @@ class IDLParser(IDLLexer): p[0] = p[1] if self.parse_debug: DumpReduction('integer', p) -# Numbers are integers or floats. - def p_number(self, p): - """number : FLOAT - | HEX - | INT - | OCT""" +# +# Expression +# +# A simple arithmetic expression. +# + precedence = ( + ('left','|','&','^'), + ('left','LSHIFT','RSHIFT'), + ('left','+','-'), + ('left','*','/'), + ('right','UMINUS','~'), + ) + + def p_expression_binop(self, p): + """expression : expression LSHIFT expression + | expression RSHIFT expression + | expression '|' expression + | expression '&' expression + | expression '^' expression + | expression '+' expression + | expression '-' expression + | expression '*' expression + | expression '/' expression""" + p[0] = "%s %s %s" % (str(p[1]), str(p[2]), str(p[3])) + if self.parse_debug: DumpReduction('expression_binop', p) + + def p_expression_unop(self, p): + """expression : '-' expression %prec UMINUS + | '~' expression %prec '~'""" + p[0] = "%s%s" % (str(p[1]), str(p[2])) + if self.parse_debug: DumpReduction('expression_unop', p) + + def p_expression_term(self, p): + "expression : '(' expression ')'" + p[0] = "%s%s%s" % (str(p[1]), str(p[2]), str(p[3])) + if self.parse_debug: DumpReduction('expression_term', p) + + def p_expression_symbol(self, p): + "expression : SYMBOL" p[0] = p[1] - if self.parse_debug: DumpReduction('number', p) + if self.parse_debug: DumpReduction('expression_symbol', p) - def p_number_lshift(self, p): - """number : integer LSHIFT INT""" - p[0] = "%s << %s" % (p[1], p[3]) - if self.parse_debug: DumpReduction('number_lshift', p) + def p_expression_integer(self, p): + "expression : integer" + p[0] = p[1] + if self.parse_debug: DumpReduction('expression_integer', p) # # Array List @@ -523,7 +556,7 @@ class IDLParser(IDLLexer): if self.parse_debug: DumpReduction('enum_block', p) def p_enum_list(self, p): - """enum_list : modifiers SYMBOL '=' number enum_cont + """enum_list : modifiers SYMBOL '=' expression enum_cont | modifiers SYMBOL enum_cont""" if len(p) > 4: val = self.BuildAttribute('VALUE', p[4]) diff --git a/ppapi/generators/test_cgen/enum_typedef.h b/ppapi/generators/test_cgen/enum_typedef.h index e442205..b7ef0c1 100644 --- a/ppapi/generators/test_cgen/enum_typedef.h +++ b/ppapi/generators/test_cgen/enum_typedef.h @@ -22,11 +22,13 @@ * @addtogroup Enums * @{ */ -/* typedef enum { A = 1, B = 2, C = 3 } et1; */ +/* typedef enum { A = 1, B = 2, C = 3, D = A + B, E = ~D } et1; */ typedef enum { A = 1, B = 2, - C = 3 + C = 3, + D = A + B, + E = ~D } et1; /** * @} diff --git a/ppapi/generators/test_cgen/enum_typedef.idl b/ppapi/generators/test_cgen/enum_typedef.idl index b5d0feb..2454104 100644 --- a/ppapi/generators/test_cgen/enum_typedef.idl +++ b/ppapi/generators/test_cgen/enum_typedef.idl @@ -8,8 +8,8 @@ * This file will test that the IDL snippet matches the comment. */ -/* typedef enum { A = 1, B = 2, C = 3 } et1; */ -enum et1 { A=1, B=2, C=3 }; +/* typedef enum { A = 1, B = 2, C = 3, D = A + B, E = ~D } et1; */ +enum et1 { A=1, B=2, C=3, D=A+B, E=~D }; /* typedef int32_t i; */ typedef int32_t i; diff --git a/ppapi/generators/test_lex.in b/ppapi/generators/test_lex.in index 283a185..e0a000a 100644 --- a/ppapi/generators/test_lex.in +++ b/ppapi/generators/test_lex.in @@ -22,6 +22,13 @@ COMMENT /*MULTI LINE*/ = = + + - - +/ / +~ ~ +| | +& & +^ ^ +<< << +>> >> FLOAT 1.1 FLOAT 1e1 diff --git a/ppapi/generators/test_parser/enum.idl b/ppapi/generators/test_parser/enum.idl index a33f224..0924728 100644 --- a/ppapi/generators/test_parser/enum.idl +++ b/ppapi/generators/test_parser/enum.idl @@ -62,3 +62,47 @@ enum Es8 { /* OK EnumItem(E15) */ E15 = 0x400 }; + +/* OK Enum(Es9) */ +enum Es9 { + /* OK EnumItem(Es9_1) */ + Es9_1 = 0, + /* OK EnumItem(Es9_2) */ + Es9_2 = Es9_1, + /* OK EnumItem(Es9_3) */ + Es9_3 = Es9_1 << Es9_2, + /* OK EnumItem(Es9_3a) */ + /* FAIL Unexpected symbol Es9_2 after symbol Es9_1. */ + Es9_3a = Es9_1 Es9_2, + /* OK EnumItem(Es9_4) */ + Es9_4 = Es9_1 >> Es9_2, + /* OK EnumItem(Es9_5) */ + Es9_5 = Es9_1 | Es9_2, + /* OK EnumItem(Es9_6) */ + Es9_6 = Es9_1 & Es9_2, + /* OK EnumItem(Es9_7) */ + Es9_7 = Es9_1 ^ Es9_2, + /* OK EnumItem(Es9_8) */ + Es9_8 = Es9_1 + Es9_2, + /* OK EnumItem(Es9_9) */ + Es9_9 = Es9_1 - Es9_2, + /* OK EnumItem(Es9_10) */ + Es9_10 = Es9_1 * Es9_2, + /* OK EnumItem(Es9_11) */ + Es9_11 = Es9_1 / Es9_2, + /* OK EnumItem(Es9_12) */ + Es9_12 = -Es9_1, + /* OK EnumItem(Es9_13) */ + Es9_13 = ~Es9_1, + /* OK EnumItem(Es9_14) */ + Es9_14 = (Es9_1), + /* OK EnumItem(Es9_14a) */ + /* FAIL Unexpected ,. */ + Es9_14a = (Es9_1, + /* OK EnumItem(Es9_15) */ + Es9_15 = (Es9_1 + Es9_2) << Es9_3 + 1, + /* OK EnumItem(Es9_16) */ + Es9_16 = Es9_1 + -Es9_2 +}; + + |