Skip to content

Commit 69b9dcf

Browse files
committed
Work on lalr grammar
1 parent 8523a07 commit 69b9dcf

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

src/dolang/formulas.lark

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
?start: formula
2+
3+
equation_block: _NEWLINE? (equation) (_NEWLINE equation )* _NEWLINE?
4+
5+
?equation: equality | formula
6+
7+
equality: formula "=" formula
8+
9+
?formula: sum
10+
11+
?sum: product
12+
| sum "+" product -> add
13+
| sum "-" product -> sub
14+
| "-" product -> neg
15+
?product: atom|pow
16+
| product "*" (atom|pow) -> mul
17+
| product "/" (atom|pow) -> div
18+
?pow: atom _POW atom -> pow
19+
20+
_POW: "^" | "**"
21+
22+
?atom: NUMBER -> number
23+
| constant
24+
| "(" sum ")"
25+
| call
26+
| variable
27+
28+
!constant: NAME -> constant
29+
30+
variable: cname "[" date_index "]" -> variable
31+
32+
!cname: NAME -> name
33+
34+
?date_index: "t" [SIGNED_INT2] -> date
35+
36+
?call: cname "(" formula ")" -> call
37+
38+
SIGNED_INT2: ("+"|"-") INT
39+
40+
_NEWLINE: NEWLINE
41+
UNICODE_LETTER: /[^\W\d_\$]/
42+
NAME: UNICODE_LETTER ("_"|UNICODE_LETTER|DIGIT)*
43+
44+
%import common.SIGNED_INT
45+
%import common.DIGIT
46+
%import common.INT
47+
%import common.NUMBER
48+
%import common.WS_INLINE
49+
%import common.NEWLINE
50+
%ignore WS_INLINE

src/dolang/grammar.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@
3434

3535
grammar_0 = open(DATA_PATH, "rt", encoding="utf-8").read()
3636

37+
F_PATH = os.path.join(DIR_PATH, "formulas.lark")
38+
39+
grammar_f = open(F_PATH, "rt", encoding="utf-8").read()
40+
41+
42+
3743
# from .grammar_lark import grammar_0
3844

3945
from lark.lark import Lark
@@ -50,6 +56,27 @@
5056
],
5157
)
5258

59+
### replaces date with 0 when missing
60+
class TimeFixer(Transformer):
61+
62+
def date(self, args):
63+
if args[0] is None:
64+
return Tree("date", [Token("NUMBER", "0")])
65+
else:
66+
return Tree("date", [Token("NUMBER", args[0])])
67+
68+
69+
parser_f = Lark(
70+
grammar_f,
71+
start=[
72+
"formula",
73+
"equation_block"
74+
],
75+
parser="lalr",
76+
strict=True,
77+
transformer=TimeFixer()
78+
)
79+
5380

5481
def parse_string(text, start=None):
5582

0 commit comments

Comments
 (0)