-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlili_parser.py
More file actions
99 lines (68 loc) · 2.39 KB
/
lili_parser.py
File metadata and controls
99 lines (68 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import sys
import lark
from lark import Lark, Transformer, v_args
from data import *
LiLi_grammar = r"""
?start: expr*
?expr: term_value
| "'" expr -> quote
| sexpr
sexpr: "(" expr+ ")"
?term_value:"\#t" -> true
| "\#f" -> false
| SIGNED_INT -> int
| SIGNED_FLOAT -> float
| string -> string
| SYMBOL -> symbol
DIGIT: "0".."9"
LCASE_LETTER: "a".."z"
UCASE_LETTER: "A".."Z"
LETTER: UCASE_LETTER | LCASE_LETTER
SYMBOL_SPEC_CHARS : "<"|">"|"-"|"_"|"*"|"+"|"-"|"/"|"?"
ALL_SYMBOL_CHARS : DIGIT|LETTER|SYMBOL_SPEC_CHARS
INT: DIGIT+
DECIMAL: INT "." INT? | "." INT
// float = /-?\d+(\.\d+)?([eE][+-]?\d+)?/
_EXP: ("e"|"E") SIGNED_INT
FLOAT: INT _EXP | DECIMAL _EXP?
SYMBOL.0 : ALL_SYMBOL_CHARS+
SPACE.0 : (" "|"\t"|"\n"|",")+
COMMENT.0 : /;[^\n]*/
string.1: ESCAPED_STRING
SIGNED_INT.1: ["+"|"-"] INT
SIGNED_FLOAT.1: ["+"|"-"] FLOAT
%import common.ESCAPED_STRING
%ignore SPACE
%ignore COMMENT
"""
class _LiLiTransformer(Transformer):
def float(self, value):
return FLOAT(float(value[0].value))
def int(self, value):
return INTEGER(int(value[0].value))
def true(self, value):
return BOOLEAN(True)
def false(self, value):
return BOOLEAN(False)
def quote(self, expr):
return LIST(tuple([SYMBOL("quote")]+expr))
def string(self, value):
assert len(value) == 1
return STRING(value[0].value[1:-1]) # Strip escaped
def symbol(self, value):
assert len(value) == 1
return SYMBOL(value[0].value)
def sexpr(self, values):
return LIST(tuple(values))
def start(self, exprs):
return exprs
_LiLi_parser = Lark(LiLi_grammar, parser='lalr',
# Using the basic lexer isn't required, and isn't usually recommended.
# But, it's good enough for JSON, and it's slightly faster.
lexer='basic',
# Disabling propagate_positions and placeholders slightly improves speed
propagate_positions=False,
maybe_placeholders=False,
# Using an internal transformer is faster and more memory efficient
transformer=_LiLiTransformer()) # =TreeToJson())
parse = _LiLi_parser.parse