File tree Expand file tree Collapse file tree 2 files changed +77
-0
lines changed
Expand file tree Collapse file tree 2 files changed +77
-0
lines changed Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 3434
3535grammar_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
3945from lark .lark import Lark
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
5481def parse_string (text , start = None ):
5582
You can’t perform that action at this time.
0 commit comments