Skip to content

Commit 0ebac81

Browse files
committed
feat: print as mly without actions
1 parent 705a7d8 commit 0ebac81

File tree

12 files changed

+16155
-12509
lines changed

12 files changed

+16155
-12509
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.3.37
2+
3+
- feat: Print as mly without actions
4+
15
## 0.3.23
26

37
- fix: fix bug in small_int_set which causes building incorrect lr1 automaton

boot/moonyacc.js

Lines changed: 11518 additions & 12506 deletions
Large diffs are not rendered by default.

moon.mod.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "moonbitlang/yacc",
3-
"version": "0.3.33",
3+
"version": "0.3.37",
44
"deps": {
55
"moonbitlang/x": "0.4.23",
66
"Yoorkin/ArgParser": "0.1.9"

src/lib/ast/print.mbt

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
///|
2+
pub fn print_spec(
3+
spec : ParserSpec,
4+
out : StringBuilder,
5+
actions~ : Bool = false
6+
) -> Unit {
7+
let mut header = None
8+
let mut trailer = None
9+
let start_decls = []
10+
let normal_decls = []
11+
let prec_decls = []
12+
for decl in spec.decls {
13+
match decl {
14+
Start(_) as decl => start_decls.push(decl)
15+
Header(code, _, _) => header = Some(code)
16+
Trailer(code, _, _) => trailer = Some(code)
17+
Left(_) | Right(_) | Nonassoc(_) as decl => prec_decls.push(decl)
18+
decl => normal_decls.push(decl)
19+
}
20+
}
21+
for decl in start_decls {
22+
match decl {
23+
Start(symbols) => {
24+
out.write_string("%start")
25+
if not(actions) {
26+
out.write_string("<unit>")
27+
}
28+
for symbol in symbols {
29+
out.write_string(" ")
30+
out.write_string(symbol)
31+
}
32+
out.write_string("\n")
33+
}
34+
_ => panic()
35+
}
36+
}
37+
if not(start_decls.is_empty()) {
38+
out.write_string("\n")
39+
}
40+
for decl in normal_decls {
41+
match decl {
42+
Start(_) | Header(_) | Trailer(_) | Left(_) | Right(_) | Nonassoc(_) =>
43+
panic()
44+
Token(symbols, type_~) => {
45+
out.write_string("%token")
46+
if actions && type_ is Some(type_) {
47+
out.write_string("<\{type_}>")
48+
}
49+
for symbol in symbols {
50+
out.write_string(" ")
51+
out.write_string(symbol)
52+
}
53+
out.write_string("\n")
54+
}
55+
Token1(symbol, type_~, image~) => {
56+
out.write_string("%token")
57+
if actions && type_ is Some(type_) {
58+
out.write_string("<\{type_}>")
59+
}
60+
out.write_string(" ")
61+
out.write_string(symbol)
62+
out.write_string(" ")
63+
out.write_string(image)
64+
out.write_string("\n")
65+
}
66+
Type(symbols, type_~) =>
67+
if actions {
68+
out.write_string("%type")
69+
out.write_string("<\{type_}>")
70+
for symbol in symbols {
71+
out.write_string(" ")
72+
out.write_string(symbol)
73+
}
74+
out.write_string("\n")
75+
}
76+
Derive(traits~, type_~) =>
77+
if actions {
78+
out.write_string("%derive")
79+
out.write_string("<\{type_}>")
80+
out.write_string(" ")
81+
out.write_string(traits)
82+
out.write_string("\n")
83+
}
84+
Position(type_~) =>
85+
if actions {
86+
out.write_string("%position")
87+
out.write_string("<\{type_}>")
88+
out.write_string("\n")
89+
}
90+
}
91+
}
92+
if not(prec_decls.is_empty()) {
93+
out.write_string("\n")
94+
}
95+
for decl in prec_decls {
96+
match decl {
97+
Left(symbols) | Right(symbols) | Nonassoc(symbols) => {
98+
out.write_string(
99+
match decl {
100+
Left(_) => "%left"
101+
Right(_) => "%right"
102+
Nonassoc(_) => "%nonassoc"
103+
_ => panic()
104+
},
105+
)
106+
for symbol in symbols {
107+
out.write_string(" ")
108+
match symbol {
109+
Symbol(symbol) => out.write_string(symbol)
110+
Image(image) => out.write_string(image)
111+
}
112+
}
113+
out.write_string("\n")
114+
}
115+
_ => panic()
116+
}
117+
}
118+
if actions && header is Some(code) {
119+
out.write_string("\n\n%{\{code}%}\n")
120+
}
121+
out.write_string("\n%%\n\n")
122+
for rule in spec.rules {
123+
if rule.inline {
124+
out.write_string("%inline ")
125+
}
126+
out.write_string(rule.nonterminal)
127+
if actions && not(rule.generic_params.is_empty()) {
128+
out.write_string("[")
129+
for index, param in rule.generic_params {
130+
if index > 0 {
131+
out.write_string(", ")
132+
}
133+
out.write_string(param)
134+
}
135+
out.write_string("]")
136+
}
137+
if not(rule.params.is_empty()) {
138+
out.write_string("(")
139+
for index, param in rule.params {
140+
if index > 0 {
141+
out.write_string(", ")
142+
}
143+
out.write_string(param.0)
144+
if actions && param.1 is Some(type_) {
145+
out.write_string(" : \{type_}")
146+
}
147+
}
148+
out.write_string(")")
149+
}
150+
if actions && rule.type_ is Some(type_) {
151+
out.write_string(" -> \{type_}")
152+
}
153+
fn term_to_string(term : Term) {
154+
match term {
155+
Symbol(symbol, ..) => symbol
156+
Image(image, ..) => image
157+
RuleCall(symbol, args, ..) => {
158+
let args = args.iter().map(term_to_string).join(", ")
159+
"\{symbol}(\{args})"
160+
}
161+
}
162+
}
163+
164+
for index, clause in rule.clauses {
165+
if index == 0 {
166+
out.write_string("\n : ")
167+
} else {
168+
out.write_string("\n | ")
169+
}
170+
for index, clause_no_action in clause.0 {
171+
if index > 0 {
172+
out.write_string("\n | ")
173+
}
174+
for index, item in clause_no_action.items {
175+
if index > 0 {
176+
out.write_string(" ")
177+
}
178+
if actions && item.binder is Some(binder) {
179+
out.write_string("\{binder}=\{term_to_string(item.term)}")
180+
} else {
181+
out.write_string(term_to_string(item.term))
182+
}
183+
}
184+
if clause_no_action.prec is Some(prec) {
185+
match prec {
186+
Symbol(symbol) => out.write_string(" %prec \{symbol}")
187+
Image(image) => out.write_string(" %prec \{image}")
188+
}
189+
}
190+
if not(actions) {
191+
out.write_string(" {}")
192+
}
193+
}
194+
if actions {
195+
if clause.1.code is Some(code) {
196+
out.write_string(" {\{code.code}}")
197+
} else {
198+
out.write_string(" {}")
199+
}
200+
}
201+
}
202+
out.write_string("\n ;\n\n")
203+
}
204+
if actions && trailer is Some(code) {
205+
out.write_string("%{\{code}%}\n")
206+
}
207+
}

0 commit comments

Comments
 (0)