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