Skip to content

Commit

Permalink
Added lark grammar
Browse files Browse the repository at this point in the history
  • Loading branch information
adavoudi committed Feb 12, 2021
1 parent 57eed21 commit 46734f5
Show file tree
Hide file tree
Showing 14 changed files with 4,614 additions and 171 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ install:

script:
- black --check promsql
- pytest --verbose --capture=no promsql
- pytest --verbose --capture=no tests
- pip install .

notifications:
Expand Down
117 changes: 1 addition & 116 deletions bin/promsql-cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,128 +3,13 @@
from promsql.lexer import PromSqlLexer
from promsql.parser import PromSqlParser


class Metric:
def __init__(self, name=None, time_range=None, tags=None, offset=None):
self.name = name
self.time_range = time_range
self.tags = tags

def __str__(self):
return f"Metric: {self.name}, tags: {self.tags}, time_range: {self.time_range}"


class Tag:
def __init__(self, name=None, op=None, value=None):
self.name = name
self.op = op
self.value = value

def __str__(self):
return f"Tag: {self.name} {self.op} {self.value}"


class TagList:
def __init__(self, tags=None):
self.tags = tags

def __str__(self):
return ", ".join([str(item) for item in self.tags])


class TimeRange:
def __init__(self, start=None, end=None):
self.start = start
self.end = end

def __str__(self):
return f"TimeRange: {self.start}, {self.end}"


class Function:
def __init__(self, name=None, params=None):
self.name = name
self.params = params

def __str__(self):
return f"Function: {self.name}, params: {self.params}"


class AggOp:
def __init__(
self,
name=None,
filter_method=None,
label_name_list=None,
params=None,
expr=None,
):
self.name = name
self.filter_method = filter_method
self.label_name_list = label_name_list
self.params = params
self.expr = expr

def __str__(self):
return f"AggOp: {self.name}, filter_method: {self.filter_method}, label_name_list: {self.label_name_list}, expr: {self.expr}, params: {self.params}"


class BinOp:
def __init__(
self,
op=None,
left_expr=None,
right_expr=None,
filter_method=None,
group_side=None,
filter_label_list=None,
group_label_list=None,
has_bool=None,
):
self.op = op
self.left_expr = left_expr
self.right_expr = right_expr
self.filter_method = filter_method
self.group_side = group_side
self.filter_label_list = filter_label_list
self.group_label_list = group_label_list
self.has_bool = has_bool

def __str__(self):
return f"BinOp: {self.op}, left: ({self.left_expr}), right: ({self.right_expr}), filter_method: {self.filter_method}, group_side: {self.group_side}, filter_label_list: {self.filter_label_list}, group_label_list: {self.group_label_list}, has_bool: {self.has_bool}"


class UnaryOp:
def __init__(self, op: None, expr: None):
self.op = op
self.expr = expr


class Parameter:
def __init__(self, value=None):
self.value = value

def __str__(self):
return f"param: {self.value}"


class ParameterList:
def __init__(self, parameters=None):
self.parameters = parameters

def __str__(self):
return ", ".join([str(item) for item in self.parameters])


if __name__ == "__main__":
lexer = PromSqlLexer()
parser = PromSqlParser()
while True:
try:
text = input("calc > ")
text = input("promsql > ")
result = parser.parse(lexer.tokenize(text))
print(result)
# for tok in lexer.tokenize(text):
# print("type=%r, value=%r" % (tok.type, tok.value))
except EOFError:
break
49 changes: 49 additions & 0 deletions lark_main copy.py
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))
22 changes: 22 additions & 0 deletions lark_main.py
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))
Loading

0 comments on commit 46734f5

Please sign in to comment.