-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy paththrift_parser.py
208 lines (162 loc) · 5.9 KB
/
thrift_parser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# protobuf_thrift_thrift_parser.py
#
# simple thrift_thrift_parser for parsing protobuf .proto files
#
# Copyright 2010, Paul McGuire
#
from pyparsing import (
Word,
alphas,
alphanums,
Regex,
Suppress,
Forward,
Group,
oneOf,
ZeroOrMore,
Optional,
delimitedList,
Keyword,
restOfLine,
quotedString,
Dict,
cStyleComment,
)
ident = Word(alphas + "_", alphanums + "_.").setName("identifier")
integer = Regex(r"[+-]?\d+")
integer = Regex(r"[-+]?([0-9]*\.[0-9]+|[0-9]+)")
# (0x)?[0-9abcdef]+
hex_integer = Regex(r"[+-]?(?:0x)[0-9a-z]+")
LBRACE, RBRACE, LBRACK, RBRACK, LPAR, RPAR, EQ, SEMI, COMMA, COLON, L1, R1, Q = map(Suppress, "{}[]()=;,:<>\"")
kwds = """typedef namespace exception struct union required optional repeated enum extensions extends extend
to package service rpc returns true false option import syntax throws list map set void const"""
for kw in kwds.split():
exec("%s_ = Keyword('%s')" % (kw.upper(), kw))
messageBody = Forward()
messageDefn = STRUCT_ - ident("messageId") + LBRACE + messageBody("body") + RBRACE
unionDefn = UNION_ - ident("messageId") + LBRACE + messageBody("body") + RBRACE
ts = (
oneOf(
"""double float int32 int64 uint32 uint64 sint32 sint64
fixed32 fixed64 sfixed32 sfixed64 bool string bytes i16 i32 Text i64 Bytes void string"""
)
| ident
)
listType = (LIST_ | SET_) + L1 + ts + R1
mapType = MAP_ + L1 + ts + COMMA + ts + R1
#typespec = ZeroOrMore(Group(MAP_ | LIST_ | SET_)) + ZeroOrMore(L1) + ts + ZeroOrMore(COMMA + ts) + ZeroOrMore(R1)
ts2 = Forward()
ts2 << (listType | mapType | ts)
listType1 = (LIST_ | SET_) + L1 + ts2 + R1
mapType1 = MAP_ + L1 + ts2 + COMMA + ts2 + R1
typespec = Forward()
typespec << (listType1 | mapType1 | ts2)
floatV = Regex(r"[-+]?([0-9]*\.[0-9]+|[0-9]+)")
rvalue = integer | TRUE_ | FALSE_ | ident
P2 = "{" + ZeroOrMore((ZeroOrMore(Q) + ident + ZeroOrMore(Q) + COLON + (rvalue | "{}"))) + "}"
quoted = (Q + rvalue + Q)
fieldDirective = LBRACK + Group(ident + EQ + rvalue) + RBRACK
fieldDefn = (
integer("fieldint")
+ COLON
+ ZeroOrMore(REQUIRED_ | OPTIONAL_ | REPEATED_)("fieldQualifier")
+ typespec
+ ident("ident")
+ ZeroOrMore("=" + (rvalue | quoted | ident | hex_integer | P2 ))
+ ZeroOrMore(COMMA)
+ ZeroOrMore(SEMI)
)
fieldDefn2 = (
integer("fieldint")
+ COLON
- ident
+ ident("ident")
+ ZeroOrMore(COMMA)
)
versionDefn = CONST_ + typespec + ident("ident") + EQ + P2
# enumDefn ::= 'enum' ident '{' { ident '=' integer ';' }* '}'
enumDefn = (
ENUM_("typespec")
- ident("name")
+ LBRACE
+ Dict(
ZeroOrMore(
Group(
ident + ZeroOrMore(EQ + (hex_integer | integer)) + ZeroOrMore(fieldDirective) + ZeroOrMore(COMMA) + ZeroOrMore(SEMI)
)
)
)("values")
+ RBRACE
)
# extensionsDefn ::= 'extensions' integer 'to' integer ';'
extensionsDefn = EXTENSIONS_ - integer + TO_ + integer + SEMI
# messageExtension ::= 'extend' ident '{' messageBody '}'
messageExtension = EXTEND_ - ident + LBRACE + messageBody + RBRACE
# messageBody ::= { fieldDefn | enumDefn | messageDefn | extensionsDefn | messageExtension }*
messageBody << Group(
ZeroOrMore(
Group(fieldDefn | enumDefn | messageDefn | extensionsDefn | messageExtension)
)
)
# packageDirective ::= 'package' ident [ '.' ident]* ';'
packageDirective = Group(PACKAGE_ - delimitedList(ident, ".", combine=True) + SEMI)
namespaceDefn = NAMESPACE_ - ident("messageId") + delimitedList(ident, ".", combine=True)
exceptionDefn = EXCEPTION_ - ident("messageId") + LBRACE + messageBody("body") + RBRACE
exceptionsDefn = LPAR + fieldDefn + ZeroOrMore(fieldDefn) + RPAR
# methodDefn ::= 'void' ident '(' [ ident ] ')' 'returns' '(' [ ident ] ')' ';'
methodDefn = (
typespec("typespec")
- ident("methodName")
+ LPAR
+ ZeroOrMore(Group(fieldDefn))
+ RPAR
+ ZeroOrMore(THROWS_ + Group(exceptionsDefn))
)
methodDefn2 = (
ident("ident")
+ VOID_
- ident("methodName")
+ LPAR
+ ZeroOrMore(Group(fieldDefn))
+ RPAR
+ ZeroOrMore(THROWS_ + Group(exceptionsDefn))
)
# serviceDefn ::= 'service' ident '{' methodDefn* '}'
serviceDefn = (
SERVICE_ - ident("serviceName") + LBRACE + ZeroOrMore(Group(methodDefn)) + RBRACE
)
typeDefn = TYPEDEF_ - typespec("typespec") + ident("ident")
comment = "//" + restOfLine | cStyleComment
comment1 = "#" + restOfLine
importDirective = IMPORT_ - (quotedString("importFileSpec")) + SEMI
optionDirective = (
OPTION_
- ident("optionName")
+ EQ
+ (quotedString("optionValue") | TRUE_ | FALSE_ | ident)
+ SEMI
)
topLevelStatement = Group(messageDefn | unionDefn | messageExtension | enumDefn | serviceDefn | namespaceDefn | typeDefn | exceptionDefn | versionDefn)
thrift_parser = Optional(packageDirective) + ZeroOrMore(topLevelStatement)
thrift_parser.ignore(comment)
thrift_parser.ignore(comment1)
thrift_parser.ignore("option " + restOfLine)
thrift_parser.ignore("import " + restOfLine)
thrift_parser.ignore("syntax " + restOfLine)
thrift_parser.ignore("service " + restOfLine)
thrift_parser.ignore("const " + restOfLine)
thrift_parser.ignore("namespace " + restOfLine)
thrift_parser.ignore("include " + restOfLine)
#thrift_thrift_parser.ignore("map<" + restOfLine) # don't handle map<x, x> currently, TBD
# contents = open("../hbase/hbase-thrift/src/main/resources/org/apache/hadoop/hbase/thrift/Hbase.thrift").read()
# contents = contents.replace(" 0x", " x")
contents ='''
const string VERSION = "20.1.0"
struct CounterColumn {
1: required binary name = 0.0,
2: required i64 value
}
'''
#contents = open("cassandra/interface/cassandra.thrift").read()
# r = thrift_parser.parseString(contents)
# print(r)