Skip to content
This repository has been archived by the owner on Dec 10, 2018. It is now read-only.

Commit

Permalink
Merge pull request #30 from suzaku/faster_parsing
Browse files Browse the repository at this point in the history
Faster parse
  • Loading branch information
lxyu committed Aug 27, 2014
2 parents 51f7114 + 1bbce7a commit 9e0f178
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ pip-log.txt
.project
.pydevproject
*.sublime-workspace
*.sw[op]
35 changes: 29 additions & 6 deletions thriftpy/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ def parse(schema):
"__version__": __version__,
"__python__": __python__[:2],
"__hash__": hashlib.md5(schema.encode('utf-8')).digest(),
"typedefs": {},
"consts": {},
"enums": {},
"structs": [],
"exceptions": [],
"services": [],
}

# constants
Expand Down Expand Up @@ -69,18 +75,15 @@ def parse(schema):

# typedef parser
typedef = _typedef + orig_types("ttype") + identifier("name")
result["typedefs"] = dict((t.name, t.ttype) for t, _, _ in typedef.scanString(schema))

# const parser
const = _const + ttype("ttype") + identifier("name") + EQ + value("value")
result["consts"] = dict((c.name, c.value) for c, _, _ in const.scanString(schema))

# enum parser
enum_value = pa.Group(identifier('name') + pa.Optional(EQ + integer_('value')) + pa.Optional(COMMA))
enum_list = pa.Group(pa.ZeroOrMore(enum_value))("members")
enum = _enum + identifier("name") + LBRACE + enum_list + RBRACE
enum.ignore(single_line_comment)
result["enums"] = dict((e.name, e) for e, _, _ in enum.scanString(schema))

# struct parser
category = _or(*map(pa.Literal, ("required", "optional")))
Expand All @@ -89,12 +92,10 @@ def parse(schema):
struct = _or(_struct, _union) + identifier("name") + LBRACE + struct_members + RBRACE
struct.ignore(single_line_comment)
# struct defines is ordered
result["structs"] = [s for s, _, _ in struct.scanString(schema)]

# exception parser
exception = _exception + identifier("name") + LBRACE + struct_members + RBRACE
exception.ignore(single_line_comment)
result["exceptions"] = [s for s, _, _ in exception.scanString(schema)]

# service parser
ftype = _or(ttype, pa.Keyword("void"))
Expand All @@ -105,7 +106,29 @@ def parse(schema):
service = _service + identifier("name") + LBRACE + service_apis + RBRACE
service.ignore(single_line_comment)
service.ignore(pa.cStyleComment)
result["services"] = [s for s, _, _ in service.scanString(schema)]

parser = pa.OneOrMore(
_or(
pa.Group(typedef)('typedefs*'), pa.Group(const)('consts*'),
pa.Group(enum)('enums*'), pa.Group(struct)('structs*'),
pa.Group(exception)('exceptions*'), pa.Group(service)('services')
)
)

for parse_results, _, _ in parser.scanString(schema):
for res in parse_results:
if res.getName() == 'typedefs':
result[res.getName()][res.name] = res.ttype
elif res.getName() == 'consts':
result[res.getName()][res.name] = res.value
elif res.getName() == 'enums':
result[res.getName()][res.name] = res
elif res.getName() == 'structs':
result[res.getName()].append(res)
elif res.getName() == 'exceptions':
result[res.getName()].append(res)
elif res.getName() == 'services':
result[res.getName()].append(res)

return result

Expand Down

0 comments on commit 9e0f178

Please sign in to comment.