-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTypedBNF.lark
194 lines (147 loc) · 5.47 KB
/
TypedBNF.lark
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
%mkrepl
%%
from _tbnf.src.apis import *
from tbnf.basic import mkpos
import json.decoder
def add(xs, x):
xs.append(x)
return xs
def unesc(x, f=json.decoder.py_scanstring):
"""from the raw form of a double quoted string to a python string,
e.g.,
unesc('"asd"') == "asd"
"""
return f(x, 1)[0]
%%
start : list{toplevel} -> $1
### MACROS
empty{a} : -> []
| a -> $1
slist{sep, a} : a -> [$1]
| slist{sep, a} sep a -> add($1, $3)
list{a} : a -> [$1]
| list{a} a -> add($1, $2)
# empty or separated list
eslist{sep, a} : empty{slist{sep, a}} -> $1
# empty or list
elist{a} : empty{list{a}} -> $1
### Common
identstr : CNAMEPLUS -> str($1)
### Types
typ : arrow_typ -> $1
%%
def process_tparam(kvs):
out = []
for i, (k, v) in enumerate(kvs):
if k == '_':
out.append((f"value{i+1}", v))
else:
out.append((k, v))
return out
%%
arrow_typ : typ2 "->" arrow_typ -> MK_TFun([("value", $1)], $3)
| "(" eslist{",", param_type} ")" "->" arrow_typ -> MK_TFun(process_tparam($2), $5)
| type_product -> MK_TTuple($1)
typ2 : CNAMEPLUS -> MK_TConst(str($1))
| tvar -> $1
| typ2 "<" slist{",", typ} ">" -> MK_TApp($1, $3)
type_product : type_product "*" typ2 -> add($1, $3)
| typ2 -> [$1]
top_typ : "<" slist{",", tvar_str} ">" typ -> MK_Poly($2, $4)
| typ -> MK_Mono($1)
tvar : "'" CNAMEPLUS -> MK_TVar(str($2))
tvar_str : "'" CNAMEPLUS -> str($2)
param_type : CNAMEPLUS ":" typ -> (str($1), $3)
| typ -> ("_", $1)
### Toplevels
type_params : "<" slist{",", tvar_str} ">" -> $2
| -> []
field_ann : CNAMEPLUS ":" typ -> (str($1), $3, mkpos($1))
field_anns : -> (False, []) ## adt only
| "(" eslist{",", field_ann} ")" -> (True, $2) ## record only
decl : "extern" "var" identstr ":" top_typ -> MK_Declvar($3, $5, mkpos($2))
| "case" identstr ":" typ -> MK_Declctor($2, $4, mkpos($1))
| "extern" "type" identstr type_params field_anns ->\
MK_Decltype(True, $5[0], $3, $4, $5[1], mkpos($1))
| "type" identstr type_params field_anns ->\
MK_Decltype(False, $4[0], $2, $3, $4[1], mkpos($1))
toplevel :def -> $1
| decl -> $1
| lexerdef -> $1
| "ignore" slist{",", identstr} -> MK_Defignore($2, mkpos($1))
def : CNAMEPLUS "(" slist{",", identstr} ")" productions ->\
MK_Defmacro(str($1), $3, $5, mkpos($1))
| CNAMEPLUS productions -> \
MK_Defrule(str($1), $2, mkpos($1))
productions : ":" production -> [(mkpos($1), $2)]
| productions "|" production -> add($1, (mkpos($2), $3))
production : elist{psym} "{" expr "}" -> MK_production($1, $3)
psym : "<" identstr ">" -> MK_Term(str($2), False)
| ESCAPED_STRING -> MK_Term(unesc(str($1)), True)
| identstr -> MK_Nonterm($1)
| CNAMEPLUS "(" slist{",", psym} ")" -> MK_Macrocall(str($1), $3, mkpos($1))
### Simpler language
ann: identstr ":" typ -> ($1, $3)
func_parameters : "(" eslist{",", ann} ")" -> $2
expr : "let" identstr "=" expr "in" expr -> \
MK_expr(MK_ELet($2, $4, $6), mkpos($1))
| "fun" func_parameters "->" expr -> \
MK_expr(MK_EFun($2, $4), mkpos($1))
| call -> $1
| call ";" expr -> MK_expr(MK_ELet("_", $1, $3), $1.pos)
call : call "(" eslist{",", expr} ")" -> MK_expr(MK_EApp($1, $3), $1.pos)
| atomexp -> $1
snd{a, b} : a b -> $2
atomexp : INT -> MK_expr(MK_EInt(int($1)), mkpos($1))
| FLOAT -> MK_expr(MK_EFlt(float($1)), mkpos($1))
| ESCAPED_STRING -> MK_expr(MK_EStr(unesc($1)), mkpos($1))
| "$" INT -> MK_expr(MK_ESlot(int($2)), mkpos($1))
| "[" eslist{",", expr} "]" -> MK_expr(MK_EList($2), mkpos($1))
| "(" ")" -> MK_expr(MK_ETuple([]), mkpos($1))
| "(" slist{",", expr} ")" -> MK_expr(MK_ETuple($2), mkpos($1))
| CNAMEPLUS -> MK_expr(MK_EVar(str($1)), mkpos($1))
| atomexp "." CNAMEPLUS -> MK_expr(MK_EField($1, str($3)), $1.pos)
| "true" -> MK_expr(MK_EBool(True), mkpos($1))
| "false" -> MK_expr(MK_EBool(False), mkpos($1))
### Lexical definitions
%%
def u2i(ch):
return int(ch[-4:], 16)
def to_unicode(ch):
i = ord(ch)
return i
# return fr"\u{i:04X}"
%%
lexer : slist{"|", lexer_and} -> MK_LOr($1)
lexer_and : list{lexer_atomexpr} -> MK_LSeq($1)
lexer_atomexpr :\
lexer_atomexpr "+" -> MK_LPlus($1)
| lexer_atomexpr "*" -> MK_LStar($1)
| lexer_atomexpr "?" -> MK_LOptional($1)
| lexer_atom -> $1
lexer_atom : ESCAPED_STRING -> MK_LStr(unesc(str($1)))
| "!" lexer_atom -> MK_LNot($2)
| "(" lexer ")" -> MK_LGroup($2)
| "\\d" -> MK_LNumber
| "[" UNICODE "-" UNICODE "]" -> MK_LRange(u2i($2), u2i($4))
| "[" /./ "-" /./ "]" -> MK_LRange(ord($2), ord($4))
| ("_" | ".") -> MK_LWildcard
| CNAMEPLUS -> MK_LRef(str($1))
lexerdef : CNAMEPLUS "=" lexer ";" -> MK_Deflexer(str($1), $3, mkpos($1))
UCODE : DIGIT | /[a-fA-F]/
UNICODE : "\\" "u" UCODE UCODE UCODE UCODE
LCASE_LETTER: "a".."z"
UCASE_LETTER: "A".."Z"
LETTER: UCASE_LETTER | LCASE_LETTER
WORD: LETTER+
CNAMEPLUS: ("_"|LETTER) ("_"|"-"|LETTER|DIGIT)*
%import common.ESCAPED_STRING
%import common.FLOAT
%import common.INT
%import common.WS
%import common.DIGIT
%import common.C_COMMENT
%import common.CPP_COMMENT
%ignore WS
%ignore C_COMMENT
%ignore CPP_COMMENT