aboutsummaryrefslogtreecommitdiff
path: root/driver/semantic_action_test.go
blob: d480a0ad1397ca37376abc370370fde815d23069 (plain) (blame)
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package driver

import (
	"fmt"
	"strings"
	"testing"

	"github.com/nihei9/vartan/grammar"
	"github.com/nihei9/vartan/spec"
)

type testSemAct struct {
	gram   *spec.CompiledGrammar
	actLog []string
}

func (a *testSemAct) Shift(tok VToken, recovered bool) {
	t := a.gram.ParsingTable.Terminals[tok.TerminalID()]
	if recovered {
		a.actLog = append(a.actLog, fmt.Sprintf("shift/%v/recovered", t))
	} else {
		a.actLog = append(a.actLog, fmt.Sprintf("shift/%v", t))
	}
}

func (a *testSemAct) Reduce(prodNum int, recovered bool) {
	lhsSym := a.gram.ParsingTable.LHSSymbols[prodNum]
	lhsText := a.gram.ParsingTable.NonTerminals[lhsSym]
	if recovered {
		a.actLog = append(a.actLog, fmt.Sprintf("reduce/%v/recovered", lhsText))
	} else {
		a.actLog = append(a.actLog, fmt.Sprintf("reduce/%v", lhsText))
	}
}

func (a *testSemAct) Accept() {
	a.actLog = append(a.actLog, "accept")
}

func (a *testSemAct) TrapAndShiftError(cause VToken, popped int) {
	a.actLog = append(a.actLog, fmt.Sprintf("trap/%v/shift/error", popped))
}

func (a *testSemAct) MissError(cause VToken) {
	a.actLog = append(a.actLog, "miss")
}

func TestParserWithSemanticAction(t *testing.T) {
	specSrcWithErrorProd := `
#name test;

seq
    : seq elem semicolon
	| elem semicolon
    | error star star semicolon
	| error semicolon #recover
	;
elem
    : char char char
	;

ws #skip
    : "[\u{0009}\u{0020}]+";
semicolon
    : ';';
star
    : '*';
char
    : "[a-z]";
`

	specSrcWithoutErrorProd := `
#name test;

seq
    : seq elem semicolon
	| elem semicolon
	;
elem
    : char char char
	;

ws #skip
    : "[\u{0009}\u{0020}]+";
semicolon
    : ';';
char
    : "[a-z]";
`

	tests := []struct {
		caption string
		specSrc string
		src     string
		actLog  []string
	}{
		{
			caption: "when an input contains no syntax error, the driver calls `Shift`, `Reduce`, and `Accept`.",
			specSrc: specSrcWithErrorProd,
			src:     `a b c; d e f;`,
			actLog: []string{
				"shift/char",
				"shift/char",
				"shift/char",
				"reduce/elem",
				"shift/semicolon",
				"reduce/seq",

				"shift/char",
				"shift/char",
				"shift/char",
				"reduce/elem",
				"shift/semicolon",
				"reduce/seq",

				"accept",
			},
		},
		{
			caption: "when a grammar has `error` symbol, the driver calls `TrapAndShiftError`.",
			specSrc: specSrcWithErrorProd,
			src:     `a; b !; c d !; e ! * *; h i j;`,
			actLog: []string{
				"shift/char",
				"trap/1/shift/error",
				"shift/semicolon",
				"reduce/seq/recovered",

				"shift/char",
				"trap/2/shift/error",
				"shift/semicolon",
				"reduce/seq/recovered",

				"shift/char",
				"shift/char",
				"trap/3/shift/error",
				"shift/semicolon",
				"reduce/seq/recovered",

				"shift/char",
				"trap/2/shift/error",
				"shift/star",
				"shift/star",
				// When the driver shifts three times, it recovers from an error.
				"shift/semicolon/recovered",
				"reduce/seq",

				"shift/char",
				"shift/char",
				"shift/char",
				"reduce/elem",
				"shift/semicolon",
				"reduce/seq",

				// Even if the input contains syntax errors, the driver calls `Accept` when the input is accepted
				// according to the error production.
				"accept",
			},
		},
		{
			caption: "when the input doesn't meet the error production, the driver calls `MissError`.",
			specSrc: specSrcWithErrorProd,
			src:     `a !`,
			actLog: []string{
				"shift/char",
				"trap/1/shift/error",

				"miss",
			},
		},
		{
			caption: "when a syntax error isn't trapped, the driver calls `MissError`.",
			specSrc: specSrcWithoutErrorProd,
			src:     `a !`,
			actLog: []string{
				"shift/char",

				"miss",
			},
		},
	}
	for _, tt := range tests {
		t.Run(tt.caption, func(t *testing.T) {
			ast, err := spec.Parse(strings.NewReader(tt.specSrc))
			if err != nil {
				t.Fatal(err)
			}

			b := grammar.GrammarBuilder{
				AST: ast,
			}
			g, err := b.Build()
			if err != nil {
				t.Fatal(err)
			}

			gram, _, err := grammar.Compile(g)
			if err != nil {
				t.Fatal(err)
			}

			toks, err := NewTokenStream(gram, strings.NewReader(tt.src))
			if err != nil {
				t.Fatal(err)
			}

			semAct := &testSemAct{
				gram: gram,
			}
			p, err := NewParser(toks, NewGrammar(gram), SemanticAction(semAct))
			if err != nil {
				t.Fatal(err)
			}

			err = p.Parse()
			if err != nil {
				t.Fatal(err)
			}

			if len(semAct.actLog) != len(tt.actLog) {
				t.Fatalf("unexpected action log; want: %+v, got: %+v", tt.actLog, semAct.actLog)
			}

			for i, e := range tt.actLog {
				if semAct.actLog[i] != e {
					t.Fatalf("unexpected action log; want: %+v, got: %+v", tt.actLog, semAct.actLog)
				}
			}
		})
	}
}