-
Notifications
You must be signed in to change notification settings - Fork 11
/
Rule.n
227 lines (207 loc) · 8.98 KB
/
Rule.n
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
using Nitra.Typing;
using Nemerle;
using Nemerle.Imperative;
using Nemerle.Collections;
using Nemerle.Extensions;
using Nemerle.Text;
using Nemerle.Utility;
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace Nitra.Model
{
public variant PrecedenceReference
{
| Zero
| Name { symbol : SymbolRef[PrecedenceSymbol] }
}
[Record, StructuralEquality]
public class BindingPowerInfo
{
public Symbol : PrecedenceReference;
public IsStrong : bool;
public IsZero : bool { get { !IsStrong && Symbol is PrecedenceReference.Zero } }
public Name : string
{
get
{
(if (IsStrong) "+" else "") + (if (Symbol is Name(symbol)) symbol.ToString() else "0")
}
}
public override ToString() : string
{
Name
}
public static Zero : BindingPowerInfo = BindingPowerInfo(PrecedenceReference.Zero(), false);
}
[Record, StructuralEquality]
public variant Rule : Nitra.Located
{
[EqualsIgnore]
public override Location : Location { get; }
public Type : RuleType;
public FieldName : string;
| Sequence { Rules : list[Rule]; }
| Call
{
RuleRef : RuleRef;
BindingPower : BindingPowerInfo;
[RecordIgnore, EqualsIgnore] public SpanClass : SpanClassSymbol { get; }
public this(loc : Nitra.Location, type : RuleType, fieldName : string, ruleRef : RuleRef, bindingPower : BindingPowerInfo, spanClass : SpanClassSymbol)
{
this(loc, type, fieldName, ruleRef, bindingPower);
this.SpanClass = spanClass;
}
}
| Repeat {
MinCount : int; MaxCount : option[int]; Rule : Rule;
public IsOptimizedList : bool { get { this.Type is RuleType.Chars } }
public IsDynamicSyntaxExtentionPoint : bool { get { this.Rule is Rule.Call(RuleRef = RuleRef.Some(Symbol = RuleDefSymbol(Options = RuleDefinitionOptions(IsDynamicSyntaxExtentionPoint = true)))) } }
}
| RepeatWithSeparator { MinCount : int; MaxCount : option[int]; Rule : Rule; Separator : Rule; HangingSeparator : bool; }
//| Choice { rules : list[RegularRule]; }
//| Chars {
// chars : list[RangeSet];
// public IsString : bool { get { chars.ForAll(_.IsSingleChar) } }
// }
| String
{
Str : string;
/// This token is important in a error recovery.
[RecordIgnore, EqualsIgnore] public IsKeyToken : bool { get; internal set; }
/// This token can be parsed by the rule used to parse an identifier. All keywords IsKeyToken.
[RecordIgnore, EqualsIgnore] public IsKeyword : bool { get; internal set; }
[RecordIgnore, EqualsIgnore] public SpanClass : SpanClassSymbol { get; }
public this(loc : Nitra.Location, fieldName : string, str : string)
{
this(loc, RuleType.Chars(Some(str)), fieldName, str);
}
public this(loc : Nitra.Location, fieldName : string, str : string, isKeyword : bool, spanClass : SpanClassSymbol)
{
this(loc, RuleType.Chars(Some(str)), fieldName, str);
this.IsKeyword = isKeyword;
this.IsKeyToken = isKeyword;
this.SpanClass = spanClass;
}
}
| Not { Rule : Rule; }
| And { Rule : Rule; }
public this(originalRule : Rule)
{
this.Location = originalRule.Location;
this.Type = originalRule.Type;
this.FieldName = originalRule.FieldName;
}
public UpdateFieldName(newFieldName : string) : Rule
{
if (FieldName == newFieldName)
this
else match (this)
{
| Sequence(rules) => Rule.Sequence(Location, Type, newFieldName, rules)
| Call(ruleRef, bindingPower) as rule => Rule.Call(Location, Type, newFieldName, ruleRef, bindingPower, rule.SpanClass)
| Repeat(minCount, maxCount, rule) => Rule.Repeat(Location, Type, newFieldName, minCount, maxCount, rule)
| RepeatWithSeparator(minCount, maxCount, rule, separator, hangingSeparator) => Rule.RepeatWithSeparator(Location, Type, newFieldName, minCount, maxCount, rule, separator, hangingSeparator)
| String(Str = str, IsKeyToken = isKeyToken, SpanClass = spanClass) => Rule.String(Location, newFieldName, str, isKeyToken, spanClass)
| Not(rule) => Rule.Not(Location, Type, newFieldName, rule)
| And(rule) => Rule.And(Location, Type, newFieldName, rule)
//| Choice(rules) => Rule.Choice(Location, Type, newFieldName, rules)
//| Chars(chars) => Rule.Chars(Location, Type, newFieldName, chars)
}
}
public Priority : int
{
get
{
match (this)
{
//| Chars => 5
| String => 5
| Call => 5
| Repeat => 4
| RepeatWithSeparator => 4
| Not => 3
| And => 3
| Sequence => 2
//| Choice => 1
}
}
}
public override ToString() : string
{
def p(rule : Rule)
{
if (rule.Priority <= this.Priority)
$"($rule)"
else
rule.ToString()
}
def makeCallText(name, fieldName)
{
if (string.IsNullOrEmpty(fieldName))
name.ToString()
else
fieldName
}
match (this)
{
| Sequence(rules) => $<#..$(rules; " "; p)#>
| Call as call when call.BindingPower.IsZero => makeCallText(call.RuleRef, FieldName)
| Call(name, bp) => $<#$(makeCallText(name, FieldName)) : $bp#>
| Repeat(0, None, rule) => $"$(p(rule))*"
| Repeat(1, None, rule) => $"$(p(rule))+"
| Repeat(min, None, rule) => $"$(p(rule)){$min,}"
| Repeat(0, Some(1), rule) => $"$(p(rule))?"
| Repeat(min, Some(max), rule) when min == max => $"$(p(rule)){$min}"
| Repeat(min, Some(max), rule) => $"$(p(rule)){$min, $max}"
| RepeatWithSeparator(0, None, rule, separator, false) => $"($(p(rule)); $(p(separator)))*"
| RepeatWithSeparator(0, None, rule, separator, true) => $"($(p(rule)); $(p(separator)); ?)*"
| RepeatWithSeparator(1, None, rule, separator, false) => $"($(p(rule)); $(p(separator)))+"
| RepeatWithSeparator(1, None, rule, separator, true) => $"($(p(rule)); $(p(separator)); ?)+"
| RepeatWithSeparator => assert(false);
| String(str) => $<#'$str'#>
| Not(rule) => $"!$(p(rule))"
| And(rule) => $"&$(p(rule))"
//| Choice(rules) => $<#..$(rules; " | ")#>
//| Chars as stringRule when stringRule.IsString =>
// def buffer = StringBuilder();
// _ = buffer.Append("''");
// foreach (rangeSet in stringRule.chars)
// _ = buffer.Append(Utils.EscapeStringChar(rangeSet.Ranges.Head.from));
// _ = buffer.Append("''");
// buffer.ToString()
//
//| Chars([rangeSet]) => $"[$rangeSet]"
//| Chars(rangeSets) =>
// def buffer = StringBuilder();
// _ = buffer.Append('(');
// foreach (rangeSet in rangeSets with index)
// {
// when (index > 0)
// _ = buffer.Append(" | ");
// _ = buffer.AppendRangeSet(rangeSet)
// }
// _ = buffer.Append(')');
// buffer.ToString()
}
}
public SubRules : list[Rule]
{
get
{
match (this)
{
| Rule.Sequence(rules) => rules
| _ => [this]
}
}
}
public static ReferenceEqualityComparer : IEqualityComparer[Rule] = RuleReferenceEqualityComparer();
private sealed class RuleReferenceEqualityComparer : EqualityComparer[Rule]
{
public override Equals(x : Rule, y : Rule) : bool { ReferenceEquals(x, y) }
public override GetHashCode(obj : Rule) : int { if (ReferenceEquals(obj, null)) 0 else obj.GetHashCode() }
}
}
}