-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
288 additions
and
193 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
"""Based on | ||
https://github.com/lark-parser/lark/blob/master/docs/json_tutorial.md. | ||
""" | ||
|
||
import timeit | ||
|
||
from lark import Lark | ||
|
||
|
||
LARK_JSON_GRAMMAR = r""" | ||
?value: dict | ||
| list | ||
| string | ||
| SIGNED_NUMBER | ||
| "true" | ||
| "false" | ||
| "null" | ||
list : "[" [value ("," value)*] "]" | ||
dict : "{" [pair ("," pair)*] "}" | ||
pair : string ":" value | ||
string : ESCAPED_STRING | ||
%import common.ESCAPED_STRING | ||
%import common.SIGNED_NUMBER | ||
%import common.WS | ||
%ignore WS | ||
""" | ||
|
||
|
||
def parse_lalr(json_string, iterations): | ||
parser = Lark(LARK_JSON_GRAMMAR, | ||
start='value', | ||
lexer='standard', | ||
parser='lalr') | ||
|
||
def _parse(): | ||
parser.parse(json_string) | ||
|
||
return timeit.timeit(_parse, number=iterations) | ||
|
||
|
||
def parse_earley(json_string, iterations): | ||
parser = Lark(LARK_JSON_GRAMMAR, | ||
start='value', | ||
lexer='standard', | ||
parser='earley') | ||
|
||
def _parse(): | ||
parser.parse(json_string) | ||
|
||
return timeit.timeit(_parse, number=iterations) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
"""Based on | ||
https://github.com/drhagen/parsita/blob/master/examples/json.py. | ||
""" | ||
|
||
import timeit | ||
|
||
from parsita import TextParsers | ||
from parsita import lit | ||
from parsita import reg | ||
from parsita import rep | ||
from parsita import repsep | ||
|
||
|
||
class JsonStringParsers(TextParsers, whitespace=None): | ||
quote = lit(r'\"') | ||
reverse_solidus = lit(r'\\') | ||
solidus = lit(r'\/') | ||
backspace = lit(r'\b') | ||
form_feed = lit(r'\f') | ||
line_feed = lit(r'\n') | ||
carriage_return = lit(r'\r') | ||
tab = lit(r'\t') | ||
uni = reg(r'\\u([0-9a-fA-F]{4})') | ||
|
||
escaped = (quote | reverse_solidus | solidus | backspace | form_feed | | ||
line_feed | carriage_return | tab | uni) | ||
unescaped = reg(r'[\u0020-\u0021\u0023-\u005B\u005D-\U0010FFFF]+') | ||
|
||
string = '"' >> rep(escaped | unescaped) << '"' > ''.join | ||
|
||
|
||
class JsonParsers(TextParsers, whitespace=r'[ \t\n\r]*'): | ||
number = reg(r'-?(0|[1-9][0-9]*)(\.[0-9]+)?([eE][-+]?[0-9]+)?') | ||
|
||
false = lit('false') | ||
true = lit('true') | ||
null = lit('null') | ||
|
||
string = JsonStringParsers.string | ||
|
||
array = '[' >> repsep(value, ',') << ']' | ||
|
||
entry = string << ':' & value | ||
obj = '{' >> repsep(entry, ',') << '}' | ||
|
||
value = (number | ||
| false | ||
| true | ||
| null | ||
| string | ||
| array | ||
| obj) | ||
|
||
|
||
def parse(json_string, iterations): | ||
def _parse(): | ||
JsonParsers.value.parse(json_string) | ||
|
||
return timeit.timeit(_parse, number=iterations) |
Oops, something went wrong.