-
-
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.
Added pyleri and textx to benchmark.
- Loading branch information
Showing
5 changed files
with
186 additions
and
15 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
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
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,59 @@ | ||
"""Based on | ||
https://github.com/transceptor-technology/pyleri/blob/master/examples/json_grammar.py. | ||
""" | ||
|
||
import timeit | ||
|
||
from pyleri import Ref | ||
from pyleri import Choice | ||
from pyleri import Grammar | ||
from pyleri import Regex | ||
from pyleri import Keyword | ||
from pyleri import Sequence | ||
from pyleri import List | ||
|
||
|
||
class JsonGrammar(Grammar): | ||
START = Ref() | ||
|
||
# JSON strings should be enclosed in double quotes. | ||
# A backslash can be used as escape character. | ||
r_string = Regex('(")(?:(?=(\\\?))\\2.)*?\\1') | ||
|
||
# JSON does not support floats or integers prefixed with a + sign | ||
# and floats must start with a number, for example .5 is not allowed | ||
# but should be written like 0.5 | ||
r_float = Regex('-?[0-9]+\.?[0-9]+') | ||
r_integer = Regex('-?[0-9]+') | ||
|
||
k_true = Keyword('true') | ||
k_false = Keyword('false') | ||
k_null = Keyword('null') | ||
|
||
json_map_item = Sequence(r_string, ':', START) | ||
|
||
json_map = Sequence('{', List(json_map_item), '}') | ||
json_array = Sequence('[', List(START), ']') | ||
|
||
START = Choice(r_string, | ||
r_float, | ||
r_integer, | ||
k_true, | ||
k_false, | ||
k_null, | ||
json_map, | ||
json_array) | ||
|
||
|
||
def parse_time(json_string, iterations): | ||
grammar = JsonGrammar() | ||
|
||
def _parse(): | ||
grammar.parse(json_string) | ||
|
||
return timeit.timeit(_parse, number=iterations) | ||
|
||
|
||
def parse(json_string): | ||
return JsonGrammar().parse(json_string) |
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,50 @@ | ||
"""Based on | ||
https://github.com/igordejanovic/textX/tree/master/examples/json. | ||
""" | ||
|
||
import timeit | ||
|
||
from textx import metamodel_from_str | ||
|
||
|
||
GRAMMAR = '''\ | ||
/* | ||
A grammar for JSON data-interchange format. | ||
See: http://www.json.org/ | ||
*/ | ||
File: | ||
Array | Object | ||
; | ||
Array: | ||
"[" values*=Value[','] "]" | ||
; | ||
Value: | ||
STRING | FLOAT | BOOL | Object | Array | "null" | ||
; | ||
Object: | ||
"{" members*=Member[','] "}" | ||
; | ||
Member: | ||
key=STRING ':' value=Value | ||
; | ||
''' | ||
|
||
|
||
def parse_time(json_string, iterations): | ||
parser = metamodel_from_str(GRAMMAR) | ||
|
||
def _parse(): | ||
parser.model_from_str(json_string) | ||
|
||
return timeit.timeit(_parse, number=iterations) | ||
|
||
|
||
def parse(json_string): | ||
parser = metamodel_from_str(GRAMMAR) | ||
|
||
return parser.model_from_str(json_string) |