Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 1 | __author__ = "Dusan (Ph4r05) Klinec" |
Sapan Bhatia | b122587 | 2017-03-29 20:47:47 +0200 | [diff] [blame] | 2 | |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 3 | __copyright__ = "Copyright (C) 2014 Dusan (ph4r05) Klinec" |
| 4 | __license__ = "Apache License, Version 2.0" |
| 5 | __version__ = "1.0" |
| 6 | |
| 7 | import ply.lex as lex |
| 8 | import ply.yacc as yacc |
| 9 | from .model import * |
| 10 | |
Sapan Bhatia | b122587 | 2017-03-29 20:47:47 +0200 | [diff] [blame] | 11 | import pdb |
Sapan Bhatia | 64c7251 | 2017-06-23 02:32:45 -0700 | [diff] [blame] | 12 | from helpers import LexHelper, LU |
| 13 | from logicparser import FOLParser, FOLLexer |
Sapan Bhatia | ad79fee | 2017-06-26 23:35:57 -0700 | [diff] [blame] | 14 | import ast |
| 15 | |
| 16 | class PythonError(Exception): |
| 17 | pass |
Sapan Bhatia | b122587 | 2017-03-29 20:47:47 +0200 | [diff] [blame] | 18 | |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 19 | class ProtobufLexer(object): |
| 20 | keywords = ('double', 'float', 'int32', 'int64', 'uint32', 'uint64', 'sint32', 'sint64', |
| 21 | 'fixed32', 'fixed64', 'sfixed32', 'sfixed64', 'bool', 'string', 'bytes', |
Sapan Bhatia | 78fee77 | 2017-04-21 19:00:48 +0200 | [diff] [blame] | 22 | 'message', 'required', 'optional', 'repeated', 'enum', 'extensions', 'max', 'extend', |
Sapan Bhatia | d85f7f0 | 2017-07-13 21:25:23 -0400 | [diff] [blame] | 23 | 'to', 'package', '_service', 'rpc', 'returns', 'true', 'false', 'option', 'import', 'manytoone', 'manytomany', 'onetoone', 'policy', 'map', 'reduce') |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 24 | |
| 25 | tokens = [ |
Sapan Bhatia | 64c7251 | 2017-06-23 02:32:45 -0700 | [diff] [blame] | 26 | 'POLICYBODY', |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 27 | 'NAME', |
| 28 | 'NUM', |
| 29 | 'STRING_LITERAL', |
Sapan Bhatia | 78fee77 | 2017-04-21 19:00:48 +0200 | [diff] [blame] | 30 | #'LINE_COMMENT', 'BLOCK_COMMENT', |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 31 | 'LBRACE', 'RBRACE', 'LBRACK', 'RBRACK', |
Dusan Klinec | c9b031a | 2014-11-10 13:21:08 +0100 | [diff] [blame] | 32 | 'LPAR', 'RPAR', 'EQ', 'SEMI', 'DOT', |
Sapan Bhatia | 78fee77 | 2017-04-21 19:00:48 +0200 | [diff] [blame] | 33 | 'ARROW', 'COLON', 'COMMA', 'SLASH', |
Sapan Bhatia | 64c7251 | 2017-06-23 02:32:45 -0700 | [diff] [blame] | 34 | 'DOUBLECOLON', |
Dusan Klinec | aa9ff47 | 2014-11-10 18:02:03 +0100 | [diff] [blame] | 35 | 'STARTTOKEN' |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 36 | ] + [k.upper() for k in keywords] |
Sapan Bhatia | 64c7251 | 2017-06-23 02:32:45 -0700 | [diff] [blame] | 37 | |
| 38 | |
Sapan Bhatia | 5d76b24 | 2017-07-19 12:48:15 -0400 | [diff] [blame^] | 39 | t_POLICYBODY = r'< (.|\n)*? [^-]>' |
Sapan Bhatia | 64c7251 | 2017-06-23 02:32:45 -0700 | [diff] [blame] | 40 | |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 41 | literals = '()+-*/=?:,.^|&~!=[]{};<>@%' |
| 42 | |
Sapan Bhatia | 87792a1 | 2017-04-10 19:35:05 -0700 | [diff] [blame] | 43 | t_NUM = r'[+-]?\d+(\.\d+)?' |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 44 | t_STRING_LITERAL = r'\"([^\\\n]|(\\.))*?\"' |
| 45 | |
| 46 | t_ignore_LINE_COMMENT = '//.*' |
| 47 | def t_BLOCK_COMMENT(self, t): |
| 48 | r'/\*(.|\n)*?\*/' |
| 49 | t.lexer.lineno += t.value.count('\n') |
| 50 | |
| 51 | t_LBRACE = '{' |
| 52 | t_RBRACE = '}' |
| 53 | t_LBRACK = '\\[' |
| 54 | t_RBRACK = '\\]' |
Sapan Bhatia | 64c7251 | 2017-06-23 02:32:45 -0700 | [diff] [blame] | 55 | |
| 56 | |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 57 | t_LPAR = '\\(' |
| 58 | t_RPAR = '\\)' |
| 59 | t_EQ = '=' |
| 60 | t_SEMI = ';' |
Sapan Bhatia | b122587 | 2017-03-29 20:47:47 +0200 | [diff] [blame] | 61 | t_ARROW = '\\-\\>' |
| 62 | t_COLON = '\\:' |
Sapan Bhatia | 78fee77 | 2017-04-21 19:00:48 +0200 | [diff] [blame] | 63 | t_SLASH = '\\/' |
Sapan Bhatia | b122587 | 2017-03-29 20:47:47 +0200 | [diff] [blame] | 64 | t_COMMA = '\\,' |
Dusan Klinec | a4fae11 | 2014-11-10 08:50:27 +0100 | [diff] [blame] | 65 | t_DOT = '\\.' |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 66 | t_ignore = ' \t\f' |
Dusan Klinec | aa9ff47 | 2014-11-10 18:02:03 +0100 | [diff] [blame] | 67 | t_STARTTOKEN = '\\+' |
Sapan Bhatia | 64c7251 | 2017-06-23 02:32:45 -0700 | [diff] [blame] | 68 | t_DOUBLECOLON = '\\:\\:' |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 69 | |
| 70 | def t_NAME(self, t): |
Sapan Bhatia | 78fee77 | 2017-04-21 19:00:48 +0200 | [diff] [blame] | 71 | '[A-Za-z_$][A-Za-z0-9_+$]*' |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 72 | if t.value in ProtobufLexer.keywords: |
Dusan Klinec | a4fae11 | 2014-11-10 08:50:27 +0100 | [diff] [blame] | 73 | #print "type: %s val %s t %s" % (t.type, t.value, t) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 74 | t.type = t.value.upper() |
| 75 | return t |
| 76 | |
| 77 | def t_newline(self, t): |
| 78 | r'\n+' |
| 79 | t.lexer.lineno += len(t.value) |
| 80 | |
| 81 | def t_newline2(self, t): |
| 82 | r'(\r\n)+' |
| 83 | t.lexer.lineno += len(t.value) / 2 |
| 84 | |
| 85 | def t_error(self, t): |
| 86 | print("Illegal character '{}' ({}) in line {}".format(t.value[0], hex(ord(t.value[0])), t.lexer.lineno)) |
| 87 | t.lexer.skip(1) |
| 88 | |
Dusan Klinec | c9b031a | 2014-11-10 13:21:08 +0100 | [diff] [blame] | 89 | |
Sapan Bhatia | b122587 | 2017-03-29 20:47:47 +0200 | [diff] [blame] | 90 | def srcPort(x): |
| 91 | if (x): |
| 92 | return [FieldDirective(Name('port'),x)] |
| 93 | else: |
| 94 | return [] |
| 95 | |
| 96 | |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 97 | class ProtobufParser(object): |
| 98 | tokens = ProtobufLexer.tokens |
Dusan Klinec | aa9ff47 | 2014-11-10 18:02:03 +0100 | [diff] [blame] | 99 | offset = 0 |
| 100 | lh = LexHelper() |
Sapan Bhatia | 64c7251 | 2017-06-23 02:32:45 -0700 | [diff] [blame] | 101 | fol_lexer = lex.lex(module=FOLLexer())#, optimize=1) |
| 102 | fol_parser = yacc.yacc(module=FOLParser(), start='goal') |
Dusan Klinec | aa9ff47 | 2014-11-10 18:02:03 +0100 | [diff] [blame] | 103 | |
| 104 | def setOffset(self, of): |
| 105 | self.offset = of |
| 106 | self.lh.offset = of |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 107 | |
| 108 | def p_empty(self, p): |
| 109 | '''empty :''' |
| 110 | pass |
| 111 | |
| 112 | def p_field_modifier(self,p): |
| 113 | '''field_modifier : REQUIRED |
| 114 | | OPTIONAL |
| 115 | | REPEATED''' |
Dusan Klinec | aa9ff47 | 2014-11-10 18:02:03 +0100 | [diff] [blame] | 116 | p[0] = LU.i(p,1) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 117 | |
| 118 | def p_primitive_type(self, p): |
| 119 | '''primitive_type : DOUBLE |
| 120 | | FLOAT |
| 121 | | INT32 |
| 122 | | INT64 |
| 123 | | UINT32 |
| 124 | | UINT64 |
| 125 | | SINT32 |
| 126 | | SINT64 |
| 127 | | FIXED32 |
| 128 | | FIXED64 |
| 129 | | SFIXED32 |
| 130 | | SFIXED64 |
| 131 | | BOOL |
| 132 | | STRING |
| 133 | | BYTES''' |
Dusan Klinec | aa9ff47 | 2014-11-10 18:02:03 +0100 | [diff] [blame] | 134 | p[0] = LU.i(p,1) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 135 | |
Sapan Bhatia | b122587 | 2017-03-29 20:47:47 +0200 | [diff] [blame] | 136 | def p_link_type(self, p): |
| 137 | '''link_type : ONETOONE |
| 138 | | MANYTOONE |
| 139 | | MANYTOMANY''' |
| 140 | p[0] = LU.i(p,1) |
| 141 | |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 142 | def p_field_id(self, p): |
| 143 | '''field_id : NUM''' |
Dusan Klinec | aa9ff47 | 2014-11-10 18:02:03 +0100 | [diff] [blame] | 144 | p[0] = LU.i(p,1) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 145 | |
| 146 | def p_rvalue(self, p): |
| 147 | '''rvalue : NUM |
| 148 | | TRUE |
| 149 | | FALSE''' |
Dusan Klinec | aa9ff47 | 2014-11-10 18:02:03 +0100 | [diff] [blame] | 150 | p[0] = LU.i(p,1) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 151 | |
Sapan Bhatia | b122587 | 2017-03-29 20:47:47 +0200 | [diff] [blame] | 152 | def p_rvalue3(self, p): |
| 153 | '''rvalue : STRING_LITERAL''' |
| 154 | p[0] = Name(LU.i(p, 1)) |
| 155 | self.lh.set_parse_object(p[0], p) |
| 156 | p[0].deriveLex() |
| 157 | |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 158 | def p_rvalue2(self, p): |
| 159 | '''rvalue : NAME''' |
Dusan Klinec | aa9ff47 | 2014-11-10 18:02:03 +0100 | [diff] [blame] | 160 | p[0] = Name(LU.i(p, 1)) |
| 161 | self.lh.set_parse_object(p[0], p) |
| 162 | p[0].deriveLex() |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 163 | |
Sapan Bhatia | b122587 | 2017-03-29 20:47:47 +0200 | [diff] [blame] | 164 | def p_field_directives2(self, p): |
| 165 | '''field_directives : empty''' |
| 166 | p[0] = [] |
| 167 | |
| 168 | def p_field_directives(self, p): |
| 169 | '''field_directives : LBRACK field_directive_times RBRACK''' |
| 170 | p[0] = p[2] |
| 171 | #self.lh.set_parse_object(p[0], p) |
| 172 | |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 173 | def p_field_directive(self, p): |
Sapan Bhatia | b122587 | 2017-03-29 20:47:47 +0200 | [diff] [blame] | 174 | '''field_directive : NAME EQ rvalue''' |
| 175 | p[0] = FieldDirective(Name(LU.i(p, 1)), LU.i(p, 3)) |
Dusan Klinec | aa9ff47 | 2014-11-10 18:02:03 +0100 | [diff] [blame] | 176 | self.lh.set_parse_object(p[0], p) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 177 | |
Sapan Bhatia | a368602 | 2017-06-24 07:24:19 -0700 | [diff] [blame] | 178 | def p_policy_opt_explicit(self, p): |
| 179 | '''policy_opt : DOUBLECOLON NAME''' |
| 180 | p[0] = p[2] |
| 181 | |
Sapan Bhatia | a368602 | 2017-06-24 07:24:19 -0700 | [diff] [blame] | 182 | def p_policy_opt_empty(self, p): |
| 183 | '''policy_opt : empty''' |
| 184 | p[0] = None |
Sapan Bhatia | 87792a1 | 2017-04-10 19:35:05 -0700 | [diff] [blame] | 185 | |
| 186 | def p_csv_expr(self, p): |
| 187 | '''csv_expr : LPAR csv RPAR''' |
| 188 | p[0] = p[2] |
| 189 | |
| 190 | def p_csv_expr2(self, p): |
| 191 | '''csv_expr : empty''' |
| 192 | p[0] = [] |
| 193 | |
| 194 | def p_csv2(self, p): |
| 195 | '''csv : empty''' |
| 196 | |
| 197 | def p_csv(self, p): |
Sapan Bhatia | 2ddf83a | 2017-06-10 04:31:40 -0700 | [diff] [blame] | 198 | '''csv : dotname |
| 199 | | csv COMMA dotname''' |
Sapan Bhatia | 87792a1 | 2017-04-10 19:35:05 -0700 | [diff] [blame] | 200 | |
| 201 | if len(p) == 2: |
| 202 | p[0] = [LU(p,1)] |
| 203 | else: |
| 204 | p[0] = p[1] + [LU(p,3)] |
| 205 | |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 206 | def p_field_directive_times(self, p): |
| 207 | '''field_directive_times : field_directive_plus''' |
| 208 | p[0] = p[1] |
| 209 | |
| 210 | def p_field_directive_times2(self, p): |
| 211 | '''field_directive_times : empty''' |
| 212 | p[0] = [] |
| 213 | |
| 214 | def p_field_directive_plus(self, p): |
| 215 | '''field_directive_plus : field_directive |
Sapan Bhatia | b122587 | 2017-03-29 20:47:47 +0200 | [diff] [blame] | 216 | | field_directive_plus COMMA field_directive''' |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 217 | if len(p) == 2: |
Dusan Klinec | a9f6d36 | 2014-11-10 21:07:08 +0100 | [diff] [blame] | 218 | p[0] = [LU(p,1)] |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 219 | else: |
Sapan Bhatia | b122587 | 2017-03-29 20:47:47 +0200 | [diff] [blame] | 220 | p[0] = p[1] + [LU(p,3)] |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 221 | |
Dusan Klinec | a4fae11 | 2014-11-10 08:50:27 +0100 | [diff] [blame] | 222 | def p_dotname(self, p): |
| 223 | '''dotname : NAME |
| 224 | | dotname DOT NAME''' |
Dusan Klinec | aa9ff47 | 2014-11-10 18:02:03 +0100 | [diff] [blame] | 225 | if len(p) == 2: |
| 226 | p[0] = [LU(p,1)] |
| 227 | else: |
| 228 | p[0] = p[1] + [LU(p,3)] |
Dusan Klinec | a4fae11 | 2014-11-10 08:50:27 +0100 | [diff] [blame] | 229 | |
| 230 | # Hack for cases when there is a field named 'message' or 'max' |
| 231 | def p_fieldName(self, p): |
Sapan Bhatia | 78fee77 | 2017-04-21 19:00:48 +0200 | [diff] [blame] | 232 | '''field_name : STARTTOKEN |
| 233 | | NAME |
Dusan Klinec | a4fae11 | 2014-11-10 08:50:27 +0100 | [diff] [blame] | 234 | | MESSAGE |
| 235 | | MAX''' |
Dusan Klinec | aa9ff47 | 2014-11-10 18:02:03 +0100 | [diff] [blame] | 236 | p[0] = Name(LU.i(p,1)) |
| 237 | self.lh.set_parse_object(p[0], p) |
| 238 | p[0].deriveLex() |
Dusan Klinec | a4fae11 | 2014-11-10 08:50:27 +0100 | [diff] [blame] | 239 | |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 240 | def p_field_type(self, p): |
| 241 | '''field_type : primitive_type''' |
Dusan Klinec | aa9ff47 | 2014-11-10 18:02:03 +0100 | [diff] [blame] | 242 | p[0] = FieldType(LU.i(p,1)) |
| 243 | self.lh.set_parse_object(p[0], p) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 244 | |
| 245 | def p_field_type2(self, p): |
Dusan Klinec | a4fae11 | 2014-11-10 08:50:27 +0100 | [diff] [blame] | 246 | '''field_type : dotname''' |
Dusan Klinec | aa9ff47 | 2014-11-10 18:02:03 +0100 | [diff] [blame] | 247 | p[0] = DotName(LU.i(p, 1)) |
| 248 | self.lh.set_parse_object(p[0], p) |
| 249 | p[0].deriveLex() |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 250 | |
Sapan Bhatia | 78fee77 | 2017-04-21 19:00:48 +0200 | [diff] [blame] | 251 | def p_slash_name(self, p): |
Sapan Bhatia | 2ddf83a | 2017-06-10 04:31:40 -0700 | [diff] [blame] | 252 | '''slash_name : SLASH dotname''' |
Sapan Bhatia | 78fee77 | 2017-04-21 19:00:48 +0200 | [diff] [blame] | 253 | p[0] = p[2] |
| 254 | #self.lh.set_parse_object(p[0], p) |
| 255 | |
| 256 | def p_slash_name2(self, p): |
| 257 | '''slash_name : empty''' |
| 258 | p[0] = None |
| 259 | |
Sapan Bhatia | b122587 | 2017-03-29 20:47:47 +0200 | [diff] [blame] | 260 | def p_colon_fieldname(self, p): |
| 261 | '''colon_fieldname : COLON field_name''' |
| 262 | p[0] = p[2] |
| 263 | self.lh.set_parse_object(p[0], p) |
| 264 | |
| 265 | def p_colon_fieldname2(self, p): |
| 266 | '''colon_fieldname : empty''' |
| 267 | p[0] = None |
| 268 | |
| 269 | # TODO: Add directives to link definition |
| 270 | def p_link_definition(self, p): |
Sapan Bhatia | d70c378 | 2017-06-28 22:47:22 -0700 | [diff] [blame] | 271 | '''link_definition : field_modifier link_type field_name policy_opt ARROW dotname slash_name colon_fieldname EQ field_id field_directives SEMI''' |
Sapan Bhatia | b122587 | 2017-03-29 20:47:47 +0200 | [diff] [blame] | 272 | p[0] = LinkSpec( |
Sapan Bhatia | d70c378 | 2017-06-28 22:47:22 -0700 | [diff] [blame] | 273 | FieldDefinition(LU.i(p,1), Name('int32'), LU.i(p, 3), LU.i(p,4), LU.i(p, 10), [FieldDirective(Name('type'), Name('link')), FieldDirective(Name('model'),LU.i(p, 6))] + srcPort(LU.i(p,8)) + LU.i(p,11)), |
| 274 | LinkDefinition(LU.i(p,2), LU.i(p,3), LU.i(p,6), LU.i(p,7), LU.i(p,8))) |
Sapan Bhatia | b122587 | 2017-03-29 20:47:47 +0200 | [diff] [blame] | 275 | |
| 276 | self.lh.set_parse_object(p[0], p) |
| 277 | |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 278 | # Root of the field declaration. |
| 279 | def p_field_definition(self, p): |
Sapan Bhatia | a368602 | 2017-06-24 07:24:19 -0700 | [diff] [blame] | 280 | '''field_definition : field_modifier field_type field_name policy_opt EQ field_id field_directives SEMI''' |
| 281 | p[0] = FieldDefinition(LU.i(p,1), LU.i(p,2), LU.i(p, 3), LU.i(p,4), LU.i(p,6), LU.i(p,7)) |
Dusan Klinec | aa9ff47 | 2014-11-10 18:02:03 +0100 | [diff] [blame] | 282 | self.lh.set_parse_object(p[0], p) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 283 | |
| 284 | # Root of the enum field declaration. |
| 285 | def p_enum_field(self, p): |
Dusan Klinec | a4fae11 | 2014-11-10 08:50:27 +0100 | [diff] [blame] | 286 | '''enum_field : field_name EQ NUM SEMI''' |
Dusan Klinec | aa9ff47 | 2014-11-10 18:02:03 +0100 | [diff] [blame] | 287 | p[0] = EnumFieldDefinition(LU.i(p, 1), LU.i(p,3)) |
| 288 | self.lh.set_parse_object(p[0], p) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 289 | |
| 290 | def p_enum_body_part(self, p): |
| 291 | '''enum_body_part : enum_field |
| 292 | | option_directive''' |
| 293 | p[0] = p[1] |
| 294 | |
| 295 | def p_enum_body(self, p): |
| 296 | '''enum_body : enum_body_part |
| 297 | | enum_body enum_body_part''' |
| 298 | if len(p) == 2: |
| 299 | p[0] = [p[1]] |
| 300 | else: |
| 301 | p[0] = p[1] + [p[2]] |
| 302 | |
| 303 | def p_enum_body_opt(self, p): |
| 304 | '''enum_body_opt : empty''' |
| 305 | p[0] = [] |
| 306 | |
| 307 | def p_enum_body_opt2(self, p): |
| 308 | '''enum_body_opt : enum_body''' |
| 309 | p[0] = p[1] |
| 310 | |
Sapan Bhatia | ad79fee | 2017-06-26 23:35:57 -0700 | [diff] [blame] | 311 | def p_reduce_definition(self, p): |
| 312 | '''reduce_definition : REDUCE NAME POLICYBODY''' |
| 313 | ltxt = p[3].lstrip('<').rstrip('>') |
| 314 | l = ast.parse(ltxt).body[0] |
| 315 | if not isinstance(l, ast.Expr): |
| 316 | raise PythonError("reduce operator needs to be an expression") |
| 317 | elif not isinstance(l.value, ast.Lambda): |
| 318 | raise PythonError("reduce operator needs to be a lambda") |
| 319 | |
| 320 | p[0] = ReduceDefinition(Name(LU.i(p, 2)), ltxt) |
| 321 | self.lh.set_parse_object(p[0], p) |
| 322 | |
| 323 | def p_map_definition(self, p): |
| 324 | '''map_definition : MAP NAME POLICYBODY''' |
| 325 | ltxt = p[3].lstrip('<').rstrip('>') |
| 326 | l = ast.parse(ltxt).body[0] |
| 327 | if not isinstance(l, ast.Expr): |
| 328 | raise PythonError("map operator needs to be an expression") |
| 329 | elif not isinstance(l.value, ast.Lambda): |
| 330 | raise PythonError("map operator needs to be a lambda") |
| 331 | |
| 332 | p[0] = MapDefinition(Name(LU.i(p, 2)), ltxt) |
| 333 | self.lh.set_parse_object(p[0], p) |
| 334 | |
Sapan Bhatia | 64c7251 | 2017-06-23 02:32:45 -0700 | [diff] [blame] | 335 | def p_policy_definition(self, p): |
| 336 | '''policy_definition : POLICY NAME POLICYBODY''' |
| 337 | fol = self.fol_parser.parse(p[3], lexer = self.fol_lexer) |
| 338 | p[0] = PolicyDefinition(Name(LU.i(p, 2)), fol) |
| 339 | self.lh.set_parse_object(p[0], p) |
| 340 | |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 341 | # Root of the enum declaration. |
| 342 | # enum_definition ::= 'enum' ident '{' { ident '=' integer ';' }* '}' |
| 343 | def p_enum_definition(self, p): |
| 344 | '''enum_definition : ENUM NAME LBRACE enum_body_opt RBRACE''' |
Dusan Klinec | aa9ff47 | 2014-11-10 18:02:03 +0100 | [diff] [blame] | 345 | p[0] = EnumDefinition(Name(LU.i(p, 2)), LU.i(p,4)) |
| 346 | self.lh.set_parse_object(p[0], p) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 347 | |
| 348 | def p_extensions_to(self, p): |
| 349 | '''extensions_to : MAX''' |
| 350 | p[0] = ExtensionsMax() |
Dusan Klinec | aa9ff47 | 2014-11-10 18:02:03 +0100 | [diff] [blame] | 351 | self.lh.set_parse_object(p[0], p) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 352 | |
| 353 | def p_extensions_to2(self, p): |
| 354 | '''extensions_to : NUM''' |
Dusan Klinec | aa9ff47 | 2014-11-10 18:02:03 +0100 | [diff] [blame] | 355 | p[0] = LU.i(p, 1) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 356 | |
| 357 | # extensions_definition ::= 'extensions' integer 'to' integer ';' |
| 358 | def p_extensions_definition(self, p): |
| 359 | '''extensions_definition : EXTENSIONS NUM TO extensions_to SEMI''' |
Dusan Klinec | aa9ff47 | 2014-11-10 18:02:03 +0100 | [diff] [blame] | 360 | p[0] = ExtensionsDirective(LU.i(p,2), LU.i(p,4)) |
| 361 | self.lh.set_parse_object(p[0], p) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 362 | |
| 363 | # message_extension ::= 'extend' ident '{' message_body '}' |
| 364 | def p_message_extension(self, p): |
| 365 | '''message_extension : EXTEND NAME LBRACE message_body RBRACE''' |
Dusan Klinec | aa9ff47 | 2014-11-10 18:02:03 +0100 | [diff] [blame] | 366 | p[0] = MessageExtension(Name(LU.i(p, 2)), LU.i(p,4)) |
| 367 | self.lh.set_parse_object(p[0], p) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 368 | |
| 369 | def p_message_body_part(self, p): |
| 370 | '''message_body_part : field_definition |
Sapan Bhatia | b122587 | 2017-03-29 20:47:47 +0200 | [diff] [blame] | 371 | | link_definition |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 372 | | enum_definition |
Sapan Bhatia | 4a159ac | 2017-04-29 20:10:05 +0200 | [diff] [blame] | 373 | | option_directive |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 374 | | message_definition |
| 375 | | extensions_definition |
| 376 | | message_extension''' |
| 377 | p[0] = p[1] |
| 378 | |
| 379 | # message_body ::= { field_definition | enum_definition | message_definition | extensions_definition | message_extension }* |
| 380 | def p_message_body(self, p): |
| 381 | '''message_body : empty''' |
| 382 | p[0] = [] |
| 383 | |
| 384 | # message_body ::= { field_definition | enum_definition | message_definition | extensions_definition | message_extension }* |
| 385 | def p_message_body2(self, p): |
| 386 | '''message_body : message_body_part |
| 387 | | message_body message_body_part''' |
| 388 | if len(p) == 2: |
| 389 | p[0] = [p[1]] |
| 390 | else: |
| 391 | p[0] = p[1] + [p[2]] |
| 392 | |
| 393 | # Root of the message declaration. |
| 394 | # message_definition = MESSAGE_ - ident("messageId") + LBRACE + message_body("body") + RBRACE |
| 395 | def p_message_definition(self, p): |
Sapan Bhatia | a368602 | 2017-06-24 07:24:19 -0700 | [diff] [blame] | 396 | '''message_definition : MESSAGE NAME policy_opt csv_expr LBRACE message_body RBRACE''' |
| 397 | p[0] = MessageDefinition(Name(LU.i(p, 2)), LU.i(p,3), LU.i(p, 4), LU.i(p,6)) |
Dusan Klinec | aa9ff47 | 2014-11-10 18:02:03 +0100 | [diff] [blame] | 398 | self.lh.set_parse_object(p[0], p) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 399 | |
| 400 | # method_definition ::= 'rpc' ident '(' [ ident ] ')' 'returns' '(' [ ident ] ')' ';' |
| 401 | def p_method_definition(self, p): |
| 402 | '''method_definition : RPC NAME LPAR NAME RPAR RETURNS LPAR NAME RPAR''' |
Dusan Klinec | aa9ff47 | 2014-11-10 18:02:03 +0100 | [diff] [blame] | 403 | p[0] = MethodDefinition(Name(LU.i(p, 2)), Name(LU.i(p, 4)), Name(LU.i(p, 8))) |
| 404 | self.lh.set_parse_object(p[0], p) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 405 | |
| 406 | def p_method_definition_opt(self, p): |
| 407 | '''method_definition_opt : empty''' |
| 408 | p[0] = [] |
| 409 | |
| 410 | def p_method_definition_opt2(self, p): |
| 411 | '''method_definition_opt : method_definition |
| 412 | | method_definition_opt method_definition''' |
| 413 | if len(p) == 2: |
| 414 | p[0] = [p[1]] |
| 415 | else: |
| 416 | p[0] = p[1] + [p[2]] |
| 417 | |
| 418 | # service_definition ::= 'service' ident '{' method_definition* '}' |
| 419 | # service_definition = SERVICE_ - ident("serviceName") + LBRACE + ZeroOrMore(Group(method_definition)) + RBRACE |
| 420 | def p_service_definition(self, p): |
Sapan Bhatia | b122587 | 2017-03-29 20:47:47 +0200 | [diff] [blame] | 421 | '''service_definition : _SERVICE NAME LBRACE method_definition_opt RBRACE''' |
Dusan Klinec | aa9ff47 | 2014-11-10 18:02:03 +0100 | [diff] [blame] | 422 | p[0] = ServiceDefinition(Name(LU.i(p, 2)), LU.i(p,4)) |
| 423 | self.lh.set_parse_object(p[0], p) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 424 | |
| 425 | # package_directive ::= 'package' ident [ '.' ident]* ';' |
| 426 | def p_package_directive(self,p): |
Dusan Klinec | a4fae11 | 2014-11-10 08:50:27 +0100 | [diff] [blame] | 427 | '''package_directive : PACKAGE dotname SEMI''' |
Dusan Klinec | aa9ff47 | 2014-11-10 18:02:03 +0100 | [diff] [blame] | 428 | p[0] = PackageStatement(Name(LU.i(p, 2))) |
| 429 | self.lh.set_parse_object(p[0], p) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 430 | |
| 431 | # import_directive = IMPORT_ - quotedString("importFileSpec") + SEMI |
| 432 | def p_import_directive(self, p): |
| 433 | '''import_directive : IMPORT STRING_LITERAL SEMI''' |
Dusan Klinec | aa9ff47 | 2014-11-10 18:02:03 +0100 | [diff] [blame] | 434 | p[0] = ImportStatement(Literal(LU.i(p,2))) |
| 435 | self.lh.set_parse_object(p[0], p) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 436 | |
| 437 | def p_option_rvalue(self, p): |
| 438 | '''option_rvalue : NUM |
| 439 | | TRUE |
| 440 | | FALSE''' |
Dusan Klinec | aa9ff47 | 2014-11-10 18:02:03 +0100 | [diff] [blame] | 441 | p[0] = LU(p, 1) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 442 | |
| 443 | def p_option_rvalue2(self, p): |
| 444 | '''option_rvalue : STRING_LITERAL''' |
Dusan Klinec | aa9ff47 | 2014-11-10 18:02:03 +0100 | [diff] [blame] | 445 | p[0] = Literal(LU(p,1)) |
| 446 | |
| 447 | def p_option_rvalue3(self, p): |
| 448 | '''option_rvalue : NAME''' |
| 449 | p[0] = Name(LU.i(p,1)) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 450 | |
| 451 | # option_directive = OPTION_ - ident("optionName") + EQ + quotedString("optionValue") + SEMI |
| 452 | def p_option_directive(self, p): |
| 453 | '''option_directive : OPTION NAME EQ option_rvalue SEMI''' |
Dusan Klinec | aa9ff47 | 2014-11-10 18:02:03 +0100 | [diff] [blame] | 454 | p[0] = OptionStatement(Name(LU.i(p, 2)), LU.i(p,4)) |
| 455 | self.lh.set_parse_object(p[0], p) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 456 | |
Sapan Bhatia | ff86b01 | 2017-06-11 14:44:15 -0700 | [diff] [blame] | 457 | # topLevelStatement = Group(message_definition | message_extension | enum_definition | service_definition | import_directive | option_directive | package_definition) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 458 | def p_topLevel(self,p): |
| 459 | '''topLevel : message_definition |
| 460 | | message_extension |
| 461 | | enum_definition |
Sapan Bhatia | 64c7251 | 2017-06-23 02:32:45 -0700 | [diff] [blame] | 462 | | policy_definition |
Sapan Bhatia | 89bbaa5 | 2017-06-28 22:58:15 -0700 | [diff] [blame] | 463 | | map_definition |
| 464 | | reduce_definition |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 465 | | service_definition |
| 466 | | import_directive |
Sapan Bhatia | ff86b01 | 2017-06-11 14:44:15 -0700 | [diff] [blame] | 467 | | package_directive |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 468 | | option_directive''' |
| 469 | p[0] = p[1] |
| 470 | |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 471 | def p_statements2(self, p): |
| 472 | '''statements : topLevel |
| 473 | | statements topLevel''' |
| 474 | if len(p) == 2: |
| 475 | p[0] = [p[1]] |
| 476 | else: |
| 477 | p[0] = p[1] + [p[2]] |
| 478 | |
| 479 | def p_statements(self, p): |
| 480 | '''statements : empty''' |
| 481 | p[0] = [] |
| 482 | |
| 483 | # parser = Optional(package_directive) + ZeroOrMore(topLevelStatement) |
Dusan Klinec | c9b031a | 2014-11-10 13:21:08 +0100 | [diff] [blame] | 484 | def p_protofile(self, p): |
Sapan Bhatia | ff86b01 | 2017-06-11 14:44:15 -0700 | [diff] [blame] | 485 | '''protofile : statements''' |
| 486 | p[0] = ProtoFile(LU.i(p,1)) |
Dusan Klinec | aa9ff47 | 2014-11-10 18:02:03 +0100 | [diff] [blame] | 487 | self.lh.set_parse_object(p[0], p) |
Dusan Klinec | c9b031a | 2014-11-10 13:21:08 +0100 | [diff] [blame] | 488 | |
| 489 | # Parsing starting point |
| 490 | def p_goal(self, p): |
Dusan Klinec | aa9ff47 | 2014-11-10 18:02:03 +0100 | [diff] [blame] | 491 | '''goal : STARTTOKEN protofile''' |
Dusan Klinec | c9b031a | 2014-11-10 13:21:08 +0100 | [diff] [blame] | 492 | p[0] = p[2] |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 493 | |
| 494 | def p_error(self, p): |
| 495 | print('error: {}'.format(p)) |
| 496 | |
| 497 | class ProtobufAnalyzer(object): |
| 498 | |
| 499 | def __init__(self): |
Sapan Bhatia | b122587 | 2017-03-29 20:47:47 +0200 | [diff] [blame] | 500 | self.lexer = lex.lex(module=ProtobufLexer())#, optimize=1) |
Sapan Bhatia | 4460911 | 2017-05-15 00:00:25 +0200 | [diff] [blame] | 501 | self.parser = yacc.yacc(module=ProtobufParser(), start='goal', debug=0, outputdir='/tmp')#optimize=1) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 502 | |
| 503 | def tokenize_string(self, code): |
| 504 | self.lexer.input(code) |
| 505 | for token in self.lexer: |
| 506 | print(token) |
| 507 | |
| 508 | def tokenize_file(self, _file): |
| 509 | if type(_file) == str: |
| 510 | _file = file(_file) |
| 511 | content = '' |
| 512 | for line in _file: |
| 513 | content += line |
| 514 | return self.tokenize_string(content) |
| 515 | |
Dusan Klinec | aa9ff47 | 2014-11-10 18:02:03 +0100 | [diff] [blame] | 516 | def parse_string(self, code, debug=0, lineno=1, prefix='+'): |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 517 | self.lexer.lineno = lineno |
Dusan Klinec | aa9ff47 | 2014-11-10 18:02:03 +0100 | [diff] [blame] | 518 | self.parser.offset = len(prefix) |
Dusan Klinec | ccaa0d9 | 2014-11-09 03:21:31 +0100 | [diff] [blame] | 519 | return self.parser.parse(prefix + code, lexer=self.lexer, debug=debug) |
| 520 | |
| 521 | def parse_file(self, _file, debug=0): |
| 522 | if type(_file) == str: |
| 523 | _file = file(_file) |
| 524 | content = '' |
| 525 | for line in _file: |
| 526 | content += line |
| 527 | return self.parse_string(content, debug=debug) |