forked from cucumber/gherkin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go.razor
309 lines (277 loc) · 7.3 KB
/
parser.go.razor
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
@using Berp;
@helper CallProduction(ProductionRule production)
{
switch(production.Type)
{
case ProductionRuleType.Start:
@:ctxt.startRule(@Raw("RuleType" + production.RuleName.Replace("#", "")));
break;
case ProductionRuleType.End:
@:ctxt.endRule(@Raw("RuleType" + production.RuleName.Replace("#", "")));
break;
case ProductionRuleType.Process:
@:ctxt.build(token);
break;
}
}
@helper HandleParserError(IEnumerable<string> expectedTokens, State state)
{<text>
// var stateComment = "State: @state.Id - @Raw(state.Comment)"
var expectedTokens = []string{"@Raw(string.Join("\", \"", expectedTokens))"}
if line.IsEof() {
err = &parseError{
msg: fmt.Sprintf("unexpected end of file, expected: %s", strings.Join(expectedTokens,", ")),
loc: &Location{Line: line.LineNumber, Column: 0},
}
} else {
err = &parseError{
msg: fmt.Sprintf("expected: %s, got '%s'", strings.Join(expectedTokens,", "), line.LineText),
loc: &Location{Line: line.LineNumber, Column: line.Indent() + 1},
}
}
// if (ctxt.p.stopAtFirstError) throw error;
//ctxt.addError(err)
return @state.Id, err</text>}
@helper MatchToken(TokenType tokenType)
{<text>ctxt.match@(tokenType)(line)</text>}
@helper IsMatchToken(TokenType tokenType)
{<text>ctxt.isMatch@(tokenType)(line)</text>}
@helper TokenConst(Rule rule)
{<text>@Raw("rule" + rule.Name.Replace("#", "Int"))</text>}
//
// This file is generated. Do not edit! Edit parser.go.razor instead.
package gherkin
import (
"fmt"
"strings"
)
type TokenType int
const (
TokenTypeNone TokenType = iota
@foreach(var rule in Model.RuleSet.TokenRules)
{<text> @Raw("TokenType" + rule.Name.Replace("#", ""))
</text>}
)
func tokenTypeForRule(rt RuleType) TokenType {
return TokenTypeNone
}
func (t TokenType) Name() string {
switch t {
@foreach(var rule in Model.RuleSet.TokenRules)
{<text> case @Raw("TokenType" + rule.Name.Replace("#", "")): return "@Raw(rule.Name.Replace("#", ""))"
</text>}
}
return ""
}
func (t TokenType) RuleType() RuleType {
switch t {
@foreach(var rule in Model.RuleSet.TokenRules)
{<text> case @Raw("TokenType" + rule.Name.Replace("#", "")): return @Raw("RuleType" + rule.Name.Replace("#", ""))
</text>}
}
return RuleTypeNone
}
type RuleType int
const (
RuleTypeNone RuleType = iota
@foreach(var rule in Model.RuleSet.Where(r => !r.TempRule))
{<text> @Raw("RuleType" + rule.Name.Replace("#", ""))
</text>}
)
func (t RuleType) IsEOF() bool {
return t == RuleTypeEOF
}
func (t RuleType) Name() string {
switch t {
@foreach(var rule in Model.RuleSet.Where(r => !r.TempRule))
{<text> case @Raw("RuleType" + rule.Name.Replace("#", "")): return "@Raw(rule.Name)"
</text>}
}
return ""
}
type Location struct {
Line int
Column int
}
type parseError struct {
msg string
loc *Location
}
func (a *parseError) Error() string {
return fmt.Sprintf("(%d:%d): %s", a.loc.Line, a.loc.Column, a.msg)
}
type parseErrors []error
func (pe parseErrors) Error() string {
var ret = []string{"Parser errors:"}
for i := range pe {
ret = append(ret, pe[i].Error())
}
return strings.Join(ret,"\n")
}
func (p *parser) Parse(s Scanner, m Matcher) (err error) {
p.builder.Reset()
m.Reset()
ctxt := &parseContext{p,s,p.builder,m,nil,nil}
var state int
ctxt.startRule(@Raw("RuleType" + @Model.RuleSet.StartRule.Name))
for {
gl, eof, err := ctxt.scan()
if err != nil {
ctxt.addError(err)
if p.stopAtFirstError {
break
}
}
state, err = ctxt.match(state, gl)
if err != nil {
ctxt.addError(err)
if p.stopAtFirstError {
break
}
}
if eof {
// done! \o/
break
}
}
ctxt.endRule(@Raw("RuleType" + @Model.RuleSet.StartRule.Name))
if len(ctxt.errors) > 0 {
return ctxt.errors
}
return
}
type parseContext struct {
p *parser
s Scanner
b Builder
m Matcher
queue []*scanResult
errors parseErrors
}
func (ctxt *parseContext) addError(e error) {
ctxt.errors = append(ctxt.errors, e);
// if (p.errors.length > 10)
// throw Errors.CompositeParserException.create(p.errors);
}
type scanResult struct {
line *Line
atEof bool
err error
}
func (ctxt *parseContext) scan() (*Line, bool, error) {
l := len(ctxt.queue)
if l > 0 {
x := ctxt.queue[0]
ctxt.queue = ctxt.queue[1:]
return x.line, x.atEof, x.err
}
return ctxt.s.Scan()
}
func (ctxt *parseContext) startRule(r RuleType) (bool, error) {
ok, err := ctxt.b.StartRule(r)
if err != nil {
ctxt.addError(err)
}
return ok, err
}
func (ctxt *parseContext) endRule(r RuleType) (bool, error) {
ok, err := ctxt.b.EndRule(r)
if err != nil {
ctxt.addError(err)
}
return ok, err
}
func (ctxt *parseContext) build(t *Token) (bool, error) {
ok, err := ctxt.b.Build(t)
if err != nil {
ctxt.addError(err)
}
return ok, err
}
func (ctxt *parseContext) match(state int, line *Line) (newState int, err error) {
switch(state) {
@foreach(var state in Model.States.Values.Where(s => !s.IsEndState))
{
@:case @state.Id:
@:return ctxt.matchAt@(state.Id)(line);
}
default:
return state, fmt.Errorf("Unknown state: %+v", state);
}
}
@foreach(var state in Model.States.Values.Where(s => !s.IsEndState))
{
<text>
// @Raw(state.Comment)
func (ctxt *parseContext) matchAt@(state.Id)(line *Line) (newState int, err error) {
@foreach(var transition in state.Transitions)
{
@:if ok, token, err := @MatchToken(transition.TokenType); ok {
if (transition.LookAheadHint != null)
{
@:if ctxt.lookahead@(transition.LookAheadHint.Id)(line) {
}
foreach(var production in transition.Productions)
{
@CallProduction(production)
}
@:return @transition.TargetState, err;
if (transition.LookAheadHint != null)
{
@:}
}
@:}
}
@HandleParserError(state.Transitions.Select(t => "#" + t.TokenType.ToString()).Distinct(), state)
}
</text>
}
type Matcher interface {
@foreach(var rule in Model.RuleSet.TokenRules)
{<text> Match@(rule.Name.Replace("#", ""))(line *Line) (bool,*Token,error)
</text>}
Reset()
}
@foreach(var rule in Model.RuleSet.TokenRules)
{
<text>
func (ctxt *parseContext) isMatch@(rule.Name.Replace("#", ""))(line *Line) bool {
ok, _, _ := ctxt.match@(rule.Name.Replace("#", ""))(line)
return ok
}
func (ctxt *parseContext) match@(rule.Name.Replace("#", ""))(line *Line) (bool, *Token, error) {
@if (rule.Name != "#EOF")
{
@:if line.IsEof() {
@: return false, nil, nil
@:}
}
return ctxt.m.Match@(rule.Name.Replace("#", ""))(line);
}
</text>
}
@foreach(var lookAheadHint in Model.RuleSet.LookAheadHints)
{
<text>
func (ctxt *parseContext) lookahead@(lookAheadHint.Id)(initialLine *Line) bool {
var queue []*scanResult
var match bool
for {
line, atEof, err := ctxt.scan();
queue = append(queue, &scanResult{line,atEof,err});
if false @foreach(var tokenType in lookAheadHint.ExpectedTokens) { <text>|| @IsMatchToken(tokenType)</text>} {
match = true;
break
}
if !(false @foreach(var tokenType in lookAheadHint.Skip) { <text>|| @IsMatchToken(tokenType)</text>}) {
break
}
if atEof {
break
}
}
ctxt.queue = append(ctxt.queue, queue...)
return match;
}
</text>
}