-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgherkin-javascript.razor
More file actions
221 lines (199 loc) · 5.83 KB
/
gherkin-javascript.razor
File metadata and controls
221 lines (199 loc) · 5.83 KB
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
@using Berp;
@helper CallProduction(ProductionRule production)
{
switch(production.Type)
{
case ProductionRuleType.Start:
@:startRule(context, '@production.RuleName');
break;
case ProductionRuleType.End:
@:endRule(context, '@production.RuleName');
break;
case ProductionRuleType.Process:
@:build(context, token);
break;
}
}
@helper HandleParserError(IEnumerable<string> expectedTokens, State state)
{<text>
var stateComment = "State: @state.Id - @Raw(state.Comment)";
token.detach();
var expectedTokens = ["@Raw(string.Join("\", \"", expectedTokens))"];
var error = token.isEof ?
Errors.UnexpectedEOFException.create(token, expectedTokens, stateComment) :
Errors.UnexpectedTokenException.create(token, expectedTokens, stateComment);
if (self.stopAtFirstError) throw error;
addError(context, error);
return @state.Id;</text>}
@helper MatchToken(TokenType tokenType)
{<text>match_@(tokenType)(context, token)</text>}
// This file is generated. Do not edit! Edit gherkin-javascript.razor instead.
var Errors = require('./errors');
var AstBuilder = require('./ast_builder');
var TokenScanner = require('./token_scanner');
var TokenMatcher = require('./token_matcher');
var RULE_TYPES = [
'None',
@foreach(var rule in Model.RuleSet.Where(r => !r.TempRule))
{<text> '@rule.Name.Replace("#", "_")', // @rule.ToString(true)
</text>}
];
module.exports = function Parser(builder) {
builder = builder || new AstBuilder();
var self = this;
var context;
this.parse = function(tokenScanner, tokenMatcher) {
if(typeof tokenScanner == 'string') {
tokenScanner = new TokenScanner(tokenScanner);
}
tokenMatcher = tokenMatcher || new TokenMatcher();
builder.reset();
tokenMatcher.reset();
context = {
tokenScanner: tokenScanner,
tokenMatcher: tokenMatcher,
tokenQueue: [],
errors: []
};
startRule(context, "@Model.RuleSet.StartRule.Name");
var state = 0;
var token = null;
while(true) {
token = readToken(context);
state = matchToken(state, token, context);
if(token.isEof) break;
}
endRule(context, "@Model.RuleSet.StartRule.Name");
if(context.errors.length > 0) {
throw Errors.CompositeParserException.create(context.errors);
}
return getResult();
};
function addError(context, error) {
context.errors.push(error);
if (context.errors.length > 10)
throw Errors.CompositeParserException.create(context.errors);
}
function startRule(context, ruleType) {
handleAstError(context, function () {
builder.startRule(ruleType);
});
}
function endRule(context, ruleType) {
handleAstError(context, function () {
builder.endRule(ruleType);
});
}
function build(context, token) {
handleAstError(context, function () {
builder.build(token);
});
}
function getResult() {
return builder.getResult();
}
function handleAstError(context, action) {
handleExternalError(context, true, action)
}
function handleExternalError(context, defaultValue, action) {
if(self.stopAtFirstError) return action();
try {
return action();
} catch (e) {
if(e instanceof Errors.CompositeParserException) {
e.errors.forEach(function (error) {
addError(context, error);
});
} else if(
e instanceof Errors.ParserException ||
e instanceof Errors.AstBuilderException ||
e instanceof Errors.UnexpectedTokenException ||
e instanceof Errors.NoSuchLanguageException
) {
addError(context, e);
} else {
throw e;
}
}
return defaultValue;
}
function readToken(context) {
return context.tokenQueue.length > 0 ?
context.tokenQueue.shift() :
context.tokenScanner.read();
}
function matchToken(state, token, context) {
switch(state) {
@foreach(var state in Model.States.Values.Where(s => !s.IsEndState))
{
@:case @state.Id:
@:return matchTokenAt_@(state.Id)(token, context);
}
default:
throw new Error("Unknown state: " + state);
}
}
@foreach(var state in Model.States.Values.Where(s => !s.IsEndState))
{
<text>
// @Raw(state.Comment)
function matchTokenAt_@(state.Id)(token, context) {
@foreach(var transition in state.Transitions)
{
@:if(@MatchToken(transition.TokenType)) {
if (transition.LookAheadHint != null)
{
@:if(lookahead_@(transition.LookAheadHint.Id)(context, token)) {
}
foreach(var production in transition.Productions)
{
@CallProduction(production)
}
@:return @transition.TargetState;
if (transition.LookAheadHint != null)
{
@:}
}
@:}
}
@HandleParserError(state.Transitions.Select(t => "#" + t.TokenType.ToString()).Distinct(), state)
}
</text>
}
@foreach(var rule in Model.RuleSet.TokenRules)
{
<text>
function match_@(rule.Name.Replace("#", ""))(context, token) {
@if (rule.Name != "#EOF")
{
@:if(token.isEof) return false;
}
return handleExternalError(context, false, function () {
return context.tokenMatcher.match_@(rule.Name.Replace("#", ""))(token);
});
}
</text>
}
@foreach(var lookAheadHint in Model.RuleSet.LookAheadHints)
{
<text>
function lookahead_@(lookAheadHint.Id)(context, currentToken) {
currentToken.detach();
var token;
var queue = [];
var match = false;
do {
token = readToken(context);
token.detach();
queue.push(token);
if (false @foreach(var tokenType in lookAheadHint.ExpectedTokens) { <text>|| @MatchToken(tokenType)</text>}) {
match = true;
break;
}
} while(false @foreach(var tokenType in lookAheadHint.Skip) { <text>|| @MatchToken(tokenType)</text>});
context.tokenQueue = context.tokenQueue.concat(queue);
return match;
}
</text>
}
}