/
tlmeta.tlg
170 lines (145 loc) · 6.46 KB
/
tlmeta.tlg
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
#!/usr/bin/env tl
parserParser = parser {
sp: " " | "\t"
nl: "\r\n" | "\n\r" | "\n" | "\r"
slcomment: "//" (!nl _)*
icomment: "/*" (!"*/" _)* ("*/"|end)
comment: (slcomment (nl|end) | icomment)
ws: (sp | nl | comment)*
name: ls=alpha+ -> ls.toChar
escape: "\\\"" -> "\""
| "\\n" -> "\n"
| "\\r" -> "\r"
| "\\t" -> "\t"
| "\\$" -> "\$"
| "\\\\" -> "\\"
| !"\"" l=_ -> l.toChar
text: "\"" ts=escape* @"a closing '\"'" "\"" -> ts.join
cscape: "\\]" -> "]"
| "\\n" -> "\n"
| "\\r" -> "\r"
| "\\t" -> "\t"
| "\\$" -> "\$"
| "\\\\" -> "\\"
| !"]" l=_ -> l.toChar
char: "[" ts=cscape* @"closing ']'" "]" -> ts.join
act: "->". ts=(!"\n" _)* -> ts.toChar
cond: "?"."(". ts=(!")" _)* ")" -> ts.toChar
term: "(". t=or @"a closing ')'" .")" -> { type = "Sub", term = t }
| ".." -> { type = "Wsnl" }
| "." -> { type = "Ws" }
| "_" -> { type = "Any" }
| c=cond -> { type = "Cond", act = c }
| n=name !":" -> { type = "Call", rule = n }
| t=text -> { type = "Text", text = t }
| t=char -> { type = "Char", text = t }
post: "!" @"a term" t=term -> { type = "Not", term = t }
| "&" @"a term" t=term -> { type = "Ahead", term = t }
| t=term "*" -> { type = "Star", term = t }
| t=term "+" -> { type = "Plus", term = t }
| t=term "?" -> { type = "Opt", term = t }
| term
named: n=name "=" @"a term" t=post -> { type = "Bind", name = n, term = t }
| "@" t=text -> { type = "NoReturn", msg = t }
| post
and: h=named rs=(.named)* .a=act? -> { type = "And", terms = [h].cat(rs), act = a }
or: @"some terms"
h=and rs=(."|". and)* -> { type = "Or", terms = [h].cat(rs) }
rule: .n=name @":" .":". t=or -> { type = "Rule", name = n, term = t }
parser: n=name ."="."parser"."{"
rs=rule*
@"a closing '}'" ."}" -> { n = n, rs = rs }
start: b=(!(name ."="."parser"."{") _)*
p=parser
a=_* -> { before = b.toChar, after = a.toChar, rules = p.rs, name = p.n }
}
# emitting
emit = r, buf, ind, name ->
{ r.type == "Or" }: goto emitOr r, buf, ind
{ r.type == "And" }: goto emitAnd r, buf, ind
{ r.type == "Sub" }: goto emitSub r, buf, ind
{ r.type == "Not" }: goto emitNot r, buf, ind
{ r.type == "Opt" }: goto emitOpt r, buf, ind
{ r.type == "Star"}: goto emitStar r, buf, ind
{ r.type == "Plus"}: goto emitPlus r, buf, ind
{ r.type == "Ahead"}: goto emitAhead r, buf, ind
{ r.type == "Cond" }: buf.write("$ind if not ($(r.act)): error(_in, _nr, \"if not ($(r.act.escape))\")\n")
{ r.type == "Char" }: buf.write("$ind _in, _v = pChar(_in, _nr, \"$(r.text.escape)\")\n")
{ r.type == "NoReturn"}: buf.write("$ind _nr = \"$(r.msg.escape)\"\n")
{ r.type == "Call" }: buf.write("$ind _in, _v = rule_$(r.rule)(_in, _nr)\n")
{ r.type == "Bind" }: emit(r.term, buf, ind); buf.write("$(ind) $(r.name) = _v; pAct(_in, _v)\n")
{ r.type == "Text" }: buf.write("$ind _in, _v = pText(_in, _nr, \"$(r.text.escape)\")\n")
{ r.type == "Ws" }: buf.write("$ind _in, _v = rule_ws(_in, _nr)\n")
{ r.type == "Wsnl" }: buf.write("$ind _in, _v = rule_wsnl(_in, _nr)\n")
{ r.type == "Any" }: buf.write("$ind _in, _v = rule_any(_in, _nr)\n")
{ true }: throw "InternalError emit unknown: $(r.type)"
emitAhead = r, buf, ind ->
buf.write("$ind _in, _v = pAhead(_in, _nr, (_in, _nr ->\n")
emit r.term, buf, ind.cat(" ")
buf.write("$ind ))\n")
emitNot = r, buf, ind ->
buf.write("$ind _in, _v = pNot(_in, _nr, (_in, _nr ->\n")
emit r.term, buf, ind.cat(" ")
buf.write("$ind ))\n")
emitOpt = r, buf, ind ->
buf.write("$ind _in, _v = pOpt(_in, _nr, (_in, _nr ->\n")
emit r.term, buf, ind.cat(" ")
buf.write("$ind ))\n")
emitStar = r, buf, ind ->
buf.write("$ind _in, _v = pStar(_in, _nr, (_in, _nr ->\n")
emit r.term, buf, ind.cat(" ")
buf.write("$ind ))\n")
emitPlus = r, buf, ind ->
buf.write("$ind _in, _v = pPlus(_in, _nr, (_in, _nr ->\n")
emit r.term, buf, ind.cat(" ")
buf.write("$ind ))\n")
emitAnd = r, buf, ind ->
var $n = 0
# TODO maybe collect all binds, and "call" r.act
r.terms.each: r -> emit r, buf, ind
if r.act: buf.write("$ind _in, _v = pAct(_in, ($(r.act)))\n")
emitOr = r, buf, ind ->
if r.terms.size == 0: return
if r.terms.size == 1: return emit(r.terms[1], buf, ind)
buf.write("$ind _in, _v = pOr(_in, _nr, _name\n")
r.terms.each: r ->
buf.write("$ind ,(_in, _nr ->\n")
emit r, buf, ind.cat(" ")
buf.write("$ind )\n")
buf.write("$ind )\n")
emitSub = r, buf, ind ->
buf.write("$ind _in, _v = (_in, _nr ->\n")
emit r.term, buf, ind.cat(" ")
buf.write("$ind )(_in, _nr)\n")
emitRule = r, buf ->
name = r.name
buf.write("rule_$name = (_in, _nr ->\n")
buf.write(" trace(_in, \" >> TRY $name\", _nr)\n")
buf.write(" this = _in.oop\n")
buf.write(" _name = \"$name\"\n")
buf.write(" _pos = _in.pos\n")
buf.write(" _hit = _tllookup(this, _name, _pos)\n")
buf.write(" if _hit: return _hit.in, _hit.v\n")
buf.write(" catch: e -> pHandle(e, _nr, _name)\n")
emit r.term, buf, ""
buf.write(" trace(_in, \" << !OK! $name\")\n")
buf.write(" goto _tlcache(this, _name, _pos, _in, _v)\n")
buf.write(")\n")
if args.size != 2: throw "Usage: <input.tl> <output.tl>"
base = io.File(fileFromModulePath("tlmeta-base.tl", ".")).readString
ast = parserParser(io.File(args[1]).readString)
buf = io.Buffer.new
buf.write(ast.before)
buf.write("\n# --- tlmeta-base.tl ---\n")
buf.write(base)
buf.write("\n# --- parser ---\n")
var $hasstart = false
ast.rules.each: r ->
if r.name == "start": $hasstart = true
emitRule r, buf
start = (if $hasstart: "start") or ast.rules[1].name
buf.write("$(ast.name)$("") = text -> pStart(rule_$start, text)\n")
buf.write("\n# ---\n")
buf.write(ast.after)
io.File(args[2]).write(buf)
print "written '$(args[2])'"