-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathRuleParser.cs
229 lines (188 loc) · 7.83 KB
/
RuleParser.cs
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
using System;
using sly.lexer;
using sly.parser.syntax.grammar;
namespace sly.parser.generator
{
public class RuleParser<IN> where IN : struct
{
#region rules grammar
[Production("rule : IDENTIFIER COLON clauses")]
public GrammarNode<IN> Root(Token<EbnfTokenGeneric> name, Token<EbnfTokenGeneric> discarded, ClauseSequence<IN> clauses)
{
var rule = new Rule<IN>();
rule.NonTerminalName = name.Value;
rule.Clauses = clauses.Clauses;
return rule;
}
[Production("clauses : clause clauses")]
public GrammarNode<IN> Clauses(IClause<IN> clause, ClauseSequence<IN> clauses)
{
var list = new ClauseSequence<IN>(clause);
if (clauses != null) list.AddRange(clauses);
return list;
}
[Production("clauses : clause ")]
public ClauseSequence<IN> SingleClause(IClause<IN> clause)
{
return new ClauseSequence<IN>(clause);
}
[Production("clause : IDENTIFIER ZEROORMORE")]
public IClause<IN> ZeroMoreClause(Token<EbnfTokenGeneric> id, Token<EbnfTokenGeneric> discarded)
{
var innerClause = BuildTerminalOrNonTerimal(id.Value, true);
return new ZeroOrMoreClause<IN>(innerClause);
}
[Production("clause : IDENTIFIER ONEORMORE")]
public IClause<IN> OneMoreClause(Token<EbnfTokenGeneric> id, Token<EbnfTokenGeneric> discarded)
{
var innerClause = BuildTerminalOrNonTerimal(id.Value);
return new OneOrMoreClause<IN>(innerClause);
}
[Production("clause : IDENTIFIER OPTION")]
public IClause<IN> OptionClause(Token<EbnfTokenGeneric> id, Token<EbnfTokenGeneric> discarded)
{
var innerClause = BuildTerminalOrNonTerimal(id.Value);
return new OptionClause<IN>(innerClause);
}
[Production("clause : IDENTIFIER DISCARD ")]
public IClause<IN> SimpleDiscardedClause(Token<EbnfTokenGeneric> id, Token<EbnfTokenGeneric> discard)
{
var clause = BuildTerminalOrNonTerimal(id.Value, true);
return clause;
}
[Production("clause : choiceclause DISCARD")]
public IClause<IN> AlternateDiscardedClause(ChoiceClause<IN> choices, Token<EbnfTokenGeneric> discarded)
{
choices.IsDiscarded = true;
return choices;
}
[Production("clause : choiceclause")]
public IClause<IN> AlternateClause(ChoiceClause<IN> choices)
{
choices.IsDiscarded = false;
return choices;
}
[Production("choiceclause : LCROG choices RCROG ")]
public IClause<IN> AlternateChoices(Token<EbnfTokenGeneric> discardleft, IClause<IN> choices, Token<EbnfTokenGeneric> discardright)
{
return choices;
}
[Production("choices : IDENTIFIER ")]
public IClause<IN> ChoicesOne(Token<EbnfTokenGeneric> head)
{
var choice = BuildTerminalOrNonTerimal(head.Value);
return new ChoiceClause<IN>(choice);
}
[Production("choices : IDENTIFIER OR choices ")]
public IClause<IN> ChoicesMany(Token<EbnfTokenGeneric> head, Token<EbnfTokenGeneric> discardOr, ChoiceClause<IN> tail)
{
var headClause = BuildTerminalOrNonTerimal(head.Value);
return new ChoiceClause<IN>(headClause,tail.Choices);
}
[Production("clause : IDENTIFIER ")]
public IClause<IN> SimpleClause(Token<EbnfTokenGeneric> id)
{
var clause = BuildTerminalOrNonTerimal(id.Value);
return clause;
}
#region groups
[Production("clause : LPAREN groupclauses RPAREN ")]
public GroupClause<IN> Group(Token<EbnfTokenGeneric> discardLeft, GroupClause<IN> clauses,
Token<EbnfTokenGeneric> discardRight)
{
return clauses;
}
[Production("clause : choiceclause ONEORMORE ")]
public IClause<IN> ChoiceOneOrMore(ChoiceClause<IN> choices,Token<EbnfTokenGeneric> discardOneOrMore)
{
return new OneOrMoreClause<IN>(choices);
}
[Production("clause : choiceclause ZEROORMORE ")]
public IClause<IN> ChoiceZeroOrMore(ChoiceClause<IN> choices,Token<EbnfTokenGeneric> discardZeroOrMore)
{
return new ZeroOrMoreClause<IN>(choices);
}
[Production("clause : choiceclause OPTION ")]
public IClause<IN> ChoiceOptional(ChoiceClause<IN> choices,Token<EbnfTokenGeneric> discardOption)
{
return new OptionClause<IN>(choices);
}
[Production("clause : LPAREN groupclauses RPAREN ONEORMORE ")]
public IClause<IN> GroupOneOrMore(Token<EbnfTokenGeneric> discardLeft, GroupClause<IN> clauses,
Token<EbnfTokenGeneric> discardRight, Token<EbnfTokenGeneric> oneZeroOrMore)
{
return new OneOrMoreClause<IN>(clauses);
}
[Production("clause : LPAREN groupclauses RPAREN ZEROORMORE ")]
public IClause<IN> GroupZeroOrMore(Token<EbnfTokenGeneric> discardLeft, GroupClause<IN> clauses,
Token<EbnfTokenGeneric> discardRight, Token<EbnfTokenGeneric> discardZeroOrMore)
{
return new ZeroOrMoreClause<IN>(clauses);
}
[Production("clause : LPAREN groupclauses RPAREN OPTION ")]
public IClause<IN> GroupOptional(Token<EbnfTokenGeneric> discardLeft, GroupClause<IN> group,
Token<EbnfTokenGeneric> discardRight, Token<EbnfTokenGeneric> option)
{
return new OptionClause<IN>(group);
}
[Production("groupclauses : groupclause groupclauses")]
public GroupClause<IN> GroupClauses(GroupClause<IN> group, GroupClause<IN> groups)
{
if (groups != null) group.AddRange(groups);
return group;
}
[Production("groupclauses : groupclause")]
public GroupClause<IN> GroupClausesOne(GroupClause<IN> group)
{
return group;
}
[Production("groupclause : IDENTIFIER ")]
public GroupClause<IN> GroupClause(Token<EbnfTokenGeneric> id)
{
var clause = BuildTerminalOrNonTerimal(id.Value);
return new GroupClause<IN>(clause);
}
[Production("groupclause : IDENTIFIER DISCARD ")]
public GroupClause<IN> GroupClauseDiscarded(Token<EbnfTokenGeneric> id, Token<EbnfTokenGeneric> discarded)
{
var clause = BuildTerminalOrNonTerimal(id.Value, true);
return new GroupClause<IN>(clause);
}
[Production("groupclause : choiceclause ")]
public GroupClause<IN> GroupChoiceClause(ChoiceClause<IN> choices)
{
return new GroupClause<IN>(choices);
}
#endregion
private IClause<IN> BuildTerminalOrNonTerimal(string name, bool discard = false)
{
var token = default(IN);
IClause<IN> clause;
var isTerminal = false;
var b = Enum.TryParse(name, out token);
if (b)
{
isTerminal = true;
}
if (isTerminal)
clause = new TerminalClause<IN>(token, discard);
else
{
if (name == "INDENT")
{
clause = new IndentTerminalClause<IN>(IndentationType.Indent,discard);
}
else if (name == "UINDENT")
{
clause = new IndentTerminalClause<IN>(IndentationType.UnIndent,discard);
}
else
{
clause = new NonTerminalClause<IN>(name);
}
}
return clause;
}
#endregion
}
}