/
GrammarParser2.n2
190 lines (146 loc) · 9.65 KB
/
GrammarParser2.n2
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
using N2;
syntax module GrammarParser2
{
token field OpenBrace = "(";
token field CloseBrace = ")";
token field OpenBrace = "{";
token field CloseBrace = "}";
token field OpenSquareBrace = "[";
token field CloseSquareBrace = "]";
token field SingleQuote = "\'";
token field Quote = "\"";
token field Semicolon = ";";
token field Comma = ",";
token field Colon = ":";
token field Assign = "=";
token field Prefix = "\\u";
token field DodDot = "..";
token field Op = "+", "-", "*", "/", "?", "^", "%", "&", "!";
token field Backslash = "\\";
span class Char;
span class String;
span class Number;
span class Keyword = ['a'..'z']['a'..'z']+;
span class Operator = ['+', '-', '/', '*', '^', '?', ':']+;
span class OpenBrace = "(" | "{" | "[";
span class CloseBrace = ")" | "}" | "]";
braces "(", ")";
braces "{", "}";
braces "[", "]";
[StartRule]
syntax Start = s OpenNamespace* TopDeclaration* !Any;
[Ast( _, Namespace, _)]
syntax OpenNamespace = "using"s QIdentifier ";"s;
token Any = ['\u0000'..'\uFFFF'];
token NewLineCharacter = '\n'
| '\r'
| '\u2028' /* line separator */
| '\u2029'; /* paragraph separator */
token NewLine = "\r\n" | NewLineCharacter;
token Whitespace = [Zs]
| '\t'
| '\v' /* vertial tab */
| '\f'; /* form feed */
token Space = Whitespace | NewLine;
token Spaces = Space+;
syntax IgnoreToken;
[Ast( Start, Body, NewLine)]
syntax SingleLineComment is IgnoreToken = "//" (!NewLine Any)* NewLine?;
[Ast( Start, Body, End)]
syntax MultiLineComment is IgnoreToken = "/*" (!"*/" Any)* "*/";
syntax SpaceToken is IgnoreToken = Spaces;
void s = IgnoreToken*; /* optional spacer */
void S = !IdentifierPartCharacters s; /* identifier break spacer */
token LetterCharacter = ['A'..'Z', 'a'..'z', 'а'..'я', 'А'..'Я'];//[Lu, Ll, Lt, Lm, Lo, Nl];
token CombiningCharacter = [Mn, Mc];
token DecimalDigitCharacter = [Nd];
token ConnectingCharacter = [Pc];
token FormattingCharacter = [Cf];
token IdentifierStartCharacter = LetterCharacter | "_";
token IdentifierPartCharacters = LetterCharacter | DecimalDigitCharacter | ConnectingCharacter | CombiningCharacter | FormattingCharacter;
token IdentifierBody = IdentifierStartCharacter IdentifierPartCharacters*;
token HexDigit = ['0'..'9', 'a'..'f', 'A'..'F'];
token ReservedStringChar = "\"" | "\\";
token ReservedCharChar = "\'" | "\\";
alias Name = Identifier;
alias QualifiedName = QIdentifier;
alias Char = Any;
syntax TopDeclaration;
syntax SyntaxModule is TopDeclaration = "syntax"S "module"S Name "{"s Declaration* "}"s;
[SpanClass(String)]
syntax StringLiteral = "\"" StringPart* "\"" s;
syntax StringPart;
syntax UnicodeEscapeSequenceStringPart is StringPart = "\\u" HexDigit HexDigit HexDigit HexDigit;
syntax EscapeSequenceStringPart is StringPart = "\\" Char;
syntax SimpleStringPart is StringPart = Chars { alias Chars = (!ReservedStringChar Any)+; }
[SpanClass(Char)]
syntax CharLiteral = "\'" CharPart "\'" s;
syntax CharPart;
syntax SimpleCharPart is CharPart = !ReservedCharChar Char;
syntax UnicodeEscapeSequenceCharPart is CharPart = "\\u" HexDigit HexDigit HexDigit HexDigit;
syntax EscapeSequenceCharPart is CharPart = "\\" Char;
token Digits = ['0'..'9']+;
[SpanClass(Number)]
syntax Number = Digits s;
syntax Identifier = IdentifierBody s;
syntax QIdentifier = Names { alias Names = (Identifier; "."s)+; }
syntax RuleAttribute;
syntax StartAttribute is RuleAttribute = AttributeName S { alias AttributeName = "StartRule"; }
syntax AstAttribute is RuleAttribute = AttributeName S "("s Names ")"s { alias AttributeName = "Ast"; alias Names = (Identifier; ","s)*; }
syntax SpanClassAttribute is RuleAttribute = AttributeName S "("s Class ")"s { alias AttributeName = "SpanClass"; alias Class = QIdentifier; }
syntax RuleAttributeList = "["s (RuleAttribute; ","s)+ "]"s;
syntax RuleAttributes = RuleAttributeList*;
syntax Declaration;
alias GrammarName = QIdentifier;
syntax ExtentionRuleDecl is Declaration = RuleAttributes "syntax"S Name "is"S BaseName "="s Rule BodyDeclaration { alias BaseName = QIdentifier; }
syntax SimpleRuleDecl is Declaration = RuleAttributes "syntax"S Name "="s Rule BodyDeclaration;
syntax TokenRuleDecl is Declaration = RuleAttributes "token"S Name "="s TokenRule ";"s;
syntax VoidRuleDecl is Declaration = RuleAttributes "void"S Name "="s Rule ";"s;
syntax Alias is Declaration = "alias"S Name "="s Rule ";"s;
syntax TokenField is Declaration = "token"S "field"S Name "="s (StringLiteral; ","s)+ ";"s;
syntax AliasSyntaxModuleDecl is Declaration = "using"S Alias "="s GrammarName ";"s { alias Alias = Identifier; }
syntax ImportSyntaxModuleDecl is Declaration = "using"S GrammarName ";"s;
syntax ExtentionPointRuleDecl is Declaration = RuleAttributes "syntax"S Name ";"s;
syntax SpanClassDecl is Declaration = "span"S "class"S Name MatchTokens ";"s { alias MatchTokens = ("="s TokenRule)?; }
syntax BracePairDecl is Declaration = "braces"S Open ","s Close ";"s { alias Open = TokenRule; alias Close = TokenRule; }
syntax OptionEmitDebugSources is Declaration = "option"S "EmitDebugSources"S "="s Value ";"s { alias Value = Identifier; }
syntax BodyDeclaration;
syntax EmptyBodyDeclaration is BodyDeclaration = ";"s;
syntax BlokBodyDeclaration is BodyDeclaration = "{"s BodyMemberDeclaration* "}"s;
syntax BodyMemberDeclaration;
syntax InnerAliasDeclaration is BodyMemberDeclaration = "alias"S Name "="s Rule ";"s; // TODO: Create "Once" attribute which prevent use rule more then one times.
syntax PrecedenceDeclaration is BodyMemberDeclaration = "precedence"S Precedence RightAssociative ";"s { alias Precedence = Number; alias RightAssociative = ("right-associative"S)?; } // TODO: make precedence table
syntax Range;
syntax SingleCharRange is Range = CharLiteral;
syntax CharRange is Range = CharLiteral ".."s CharLiteral;
syntax UnicodeRange is Range = Name;
syntax TokenRule;
alias SeparatorToken = TokenRule;
syntax ChoiceTokenRule is TokenRule = LeftRule RightRules { alias LeftRule = TokenRule : 10; alias RightRules = ("|"s TokenRule : 10)+; }
syntax SequenceTokenRule is TokenRule = LeftRule RightRules { alias LeftRule = TokenRule : 20; alias RightRules = (TokenRule : 20)+; }
syntax OptionalTokenRule is TokenRule = TokenRule "?"s { precedence 30; }
syntax ZeroOrManyTokenRule is TokenRule = TokenRule "*"s { precedence 30; }
syntax OneOrManyTokenRule is TokenRule = TokenRule "+"s { precedence 30; }
syntax CharTokenRule is TokenRule = CharLiteral;
syntax StringTokenRule is TokenRule = StringLiteral;
syntax CallTokenRule is TokenRule = QualifiedName;
syntax RoundsTokenRule is TokenRule = "("s TokenRule ")"s;
syntax RangeTokenRule is TokenRule = "["s (Range; ","s)+ "]"s;
syntax ZeroOrManyWithSeparatorTokenRule is TokenRule = "("s TokenRule ";"s SeparatorToken ")"s "*"s;
syntax OneOrManyWithSeparatorTokenRule is TokenRule = "("s TokenRule ";"s SeparatorToken ")"s "+"s;
syntax Rule;
alias SeparatorRule = Rule;
syntax SequenceRule is Rule = LeftRule RightRules { alias LeftRule = Rule : 10; alias RightRules = (Rule : 10)+; }
syntax NotRule is Rule = "!"s Rule : 20;
syntax AndRule is Rule = "&"s Rule : 20;
syntax OptionalRule is Rule = Rule : 30 "?"s;
syntax ZeroOrManyRule is Rule = Rule : 30 "*"s;
syntax OneOrManyRule is Rule = Rule : 30 "+"s;
syntax CharRule is Rule = CharLiteral;
syntax StringRule is Rule = StringLiteral;
syntax RoundsRule is Rule = "("s Rule ")"s;
syntax CallRule is Rule = QualifiedName BindingPower { alias BindingPower = (":"s Number)?; }
syntax ZeroOrManyWithSeparatorRule is Rule = "("s Rule ";"s SeparatorRule ")"s "*"s;
syntax OneOrManyWithSeparatorRule is Rule = "("s Rule ";"s SeparatorRule ")"s "+"s;
syntax Scope is Rule = Name "{"s Rule "}"s;
}