-
Notifications
You must be signed in to change notification settings - Fork 2
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
14 changed files
with
4,614 additions
and
171 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
from lark import Lark | ||
|
||
from lark import Transformer | ||
|
||
|
||
class TreeToJson(Transformer): | ||
def string(self, s): | ||
(s,) = s | ||
return s[1:-1] | ||
|
||
def number(self, n): | ||
(n,) = n | ||
return float(n) | ||
|
||
list = list | ||
pair = tuple | ||
dict = dict | ||
|
||
null = lambda self, _: None | ||
true = lambda self, _: True | ||
false = lambda self, _: False | ||
|
||
|
||
json_parser = Lark( | ||
r""" | ||
value: dict | ||
| list | ||
| ESCAPED_STRING | ||
| SIGNED_NUMBER | ||
| "true" | "false" | "null" | ||
list : "[" [value ("," value)*] "]" | ||
dict : "{" [pair ("," pair)*] "}" | ||
pair : ESCAPED_STRING ":" value | ||
%import common.ESCAPED_STRING | ||
%import common.SIGNED_NUMBER | ||
%import common.WS | ||
%ignore WS | ||
""", | ||
start="value", | ||
) | ||
|
||
text = '{"key": ["item0", "item1", 3.14]}' | ||
tree = json_parser.parse(text) | ||
|
||
print(TreeToJson().transform(tree)) |
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,22 @@ | ||
from lark import Lark | ||
from lark import Transformer | ||
|
||
grammar = open("promsql.lark", "r").read() | ||
|
||
class MyTransformer(Transformer): | ||
def number_literal(self, items): | ||
return "ALIREZA" | ||
def pair(self, key_value): | ||
k, v = key_value | ||
return k, v | ||
def dict(self, items): | ||
return dict(items) | ||
|
||
|
||
parser = Lark(grammar, start="start") | ||
|
||
text = '((- a ^ 2 * b + c) > 1) and d or e' | ||
tree = parser.parse(text) | ||
print(MyTransformer().transform(tree).pretty()) | ||
|
||
# print(TreeToJson().transform(tree)) |
Oops, something went wrong.