Skip to content

Commit

Permalink
Cleanup of json example.
Browse files Browse the repository at this point in the history
  • Loading branch information
eerimoq committed Jul 22, 2018
1 parent 955d0ca commit 698d541
Showing 1 changed file with 28 additions and 28 deletions.
56 changes: 28 additions & 28 deletions examples/benchmarks/json/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,10 @@ def parse():
?value: dict
| list
| string
| SIGNED_NUMBER -> number
| "true" -> true
| "false" -> false
| "null" -> null
| SIGNED_NUMBER
| "true"
| "false"
| "null"
list : "[" [value ("," value)*] "]"
Expand Down Expand Up @@ -174,32 +174,32 @@ def parse():


def pyparsing_parse():
TRUE = pp.Keyword("true")
FALSE = pp.Keyword("false")
NULL = pp.Keyword("null")

LBRACK, RBRACK, LBRACE, RBRACE, COLON = map(pp.Suppress, "[]{}:")

jsonString = pp.dblQuotedString().setParseAction(pp.removeQuotes)
jsonNumber = pp.pyparsing_common.number()

jsonObject = pp.Forward()
jsonValue = pp.Forward()
jsonElements = pp.delimitedList(jsonValue)
jsonArray = pp.Group(LBRACK + pp.Optional(jsonElements, []) + RBRACK)
jsonValue <<= (jsonString
| jsonNumber
| pp.Group(jsonObject)
| jsonArray
| TRUE
| FALSE
| NULL)
memberDef = pp.Group(jsonString + COLON + jsonValue)
jsonMembers = pp.delimitedList(memberDef)
jsonObject <<= pp.Dict(LBRACE + pp.Optional(jsonMembers) + RBRACE)
TRUE = pp.Keyword('true')
FALSE = pp.Keyword('false')
NULL = pp.Keyword('null')

LBRACK, RBRACK, LBRACE, RBRACE, COLON = map(pp.Suppress, '[]{}:')

string = pp.dblQuotedString().setParseAction(pp.removeQuotes)
number = pp.pyparsing_common.number()

object_ = pp.Forward()
value = pp.Forward()
elements = pp.delimitedList(value)
array = pp.Group(LBRACK + pp.Optional(elements, []) + RBRACK)
value <<= (string
| number
| pp.Group(object_)
| array
| TRUE
| FALSE
| NULL)
member = pp.Group(string + COLON + value)
members = pp.delimitedList(member)
object_ <<= pp.Dict(LBRACE + pp.Optional(members) + RBRACE)

def parse():
jsonValue.parseString(JSON_STRING)
value.parseString(JSON_STRING)

return timeit.timeit(parse, number=ITERATIONS)

Expand Down

0 comments on commit 698d541

Please sign in to comment.