-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCatGrammar.cs
275 lines (272 loc) · 10.4 KB
/
CatGrammar.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
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
/// Dedicated to the public domain by Christopher Diggins
/// http://creativecommons.org/licenses/publicdomain/
using System;
using System.Collections.Generic;
using System.Text;
using Peg;
namespace Cat
{
/// <summary>
/// Contains grammar rules. Any circular references require the usage of a "Delay" function which prevents
/// the parser construction to lead to a stack overflow.
/// </summary>
public class CatGrammar : Peg.Grammar
{
public static Rule CatAstNode(AstLabel label, Rule x)
{
return new AstNodeRule(label, x);
}
public static Rule UntilEndOfLine()
{
return NoFail(WhileNot(AnyChar(), NL()), "expected a new line");
}
public static Rule LineComment()
{
return Seq(CharSeq("//"), UntilEndOfLine());
}
public static Rule BlockComment()
{
return Seq(CharSeq("/*"), NoFail(WhileNot(AnyChar(), CharSeq("*/")), "expected a new line"));
}
public static Rule MetaDataContent()
{
return CatAstNode(AstLabel.MetaDataContent, UntilEndOfLine());
}
public static Rule MetaDataLabel()
{
return CatAstNode(AstLabel.MetaDataLabel, Seq(Star(CharSet(" \t")), Ident(), SingleChar(':')));
}
public static Rule MetaDataEntry()
{
return Seq(Opt(MetaDataLabel()), Star(CharSet(" \t")), MetaDataContent());
}
public static Rule StartMetaDataBlock()
{
return Seq(WS(), CharSeq("{{"), UntilEndOfLine());
}
public static Rule EndMetaDataBlock()
{
return Seq(WS(), CharSeq("}}"), UntilEndOfLine());
}
public static Rule MetaDataBlock()
{
return Seq(CatAstNode(AstLabel.MetaDataBlock, Seq(StartMetaDataBlock(), WhileNot(MetaDataEntry(), EndMetaDataBlock()))), WS());
}
public static Rule Comment()
{
return Choice(BlockComment(), LineComment());
}
public static Rule WS()
{
return Star(Choice(CharSet(" \t\n\r"), Comment()));
}
public static Rule CatIdentChar()
{
//return Choice(IdentNextChar(), CharSet("~`!@#$%^&*-+=|:;<>.?/"));
return Choice(IdentNextChar(), CharSet("~`!@#$%^&*-+=|:;<>.?/"));
}
public static Rule CatIdent()
{
return Plus(CatIdentChar());
}
public static Rule Token(string s)
{
return Token(CharSeq(s));
}
public static Rule Token(Rule r)
{
return Seq(r, WS());
}
public static Rule Word(string s)
{
return Seq(CharSeq(s), EOW(), WS());
}
public static Rule Quote()
{
// Note the usage of Delay which breaks circular references in the grammar
return CatAstNode(AstLabel.Quote, Seq(Token("["), Star(Delay(Expr)), NoFail(Token("]"), "missing ']'")));
}
public static Rule IntegerLiteral()
{
return CatAstNode(AstLabel.Int, Seq(Opt(SingleChar('-')), Plus(Digit()), Not(CharSet("."))));
}
public static Rule EscapeChar()
{
return Seq(SingleChar('\\'), AnyChar());
}
public static Rule StringCharLiteral()
{
return Choice(EscapeChar(), NotChar('"'));
}
public static Rule CharLiteral()
{
return CatAstNode(AstLabel.Char, Seq(SingleChar('\''), StringCharLiteral(), SingleChar('\'')));
}
public static Rule StringLiteral()
{
return CatAstNode(AstLabel.String, Seq(SingleChar('\"'), Star(StringCharLiteral()), SingleChar('\"')));
}
public static Rule FloatLiteral()
{
return CatAstNode(AstLabel.Float, Seq(Opt(SingleChar('-')), Plus(Digit()), SingleChar('.'), Plus(Digit())));
}
public static Rule HexValue()
{
return CatAstNode(AstLabel.Hex, Plus(HexDigit()));
}
public static Rule HexLiteral()
{
return Seq(CharSeq("0x"), NoFail(HexValue(), "expected at least one hexadecimal digit"));
}
public static Rule BinaryValue()
{
return CatAstNode(AstLabel.Bin, Plus(BinaryDigit()));
}
public static Rule BinaryLiteral()
{
return Seq(CharSeq("0b"), NoFail(BinaryValue(), "expected at least one binary digit"));
}
public static Rule NumLiteral()
{
return Choice(HexLiteral(), BinaryLiteral(), FloatLiteral(), IntegerLiteral());
}
public static Rule Literal()
{
return Choice(StringLiteral(), CharLiteral(), NumLiteral());
}
public static Rule Symbol()
{
// The "()" together is treated as a single symbol
return Choice(CharSeq("()"), CharSet("(),"));
}
public static Rule Name()
{
return Token(CatAstNode(AstLabel.Name, Choice(Symbol(), CatIdent())));
}
public static Rule Lambda()
{
return CatAstNode(AstLabel.Lambda, Seq(CharSeq("\\"), NoFail(Seq(Param(), CharSeq("."), Choice(Delay(Lambda),
NoFail(Quote(), "expected a quotation or lambda expression"))), "expected a lambda expression")));
}
public static Rule Expr()
{
return Token(Choice(Lambda(), Literal(), Quote(), Name()));
}
public static Rule Statement()
{
return Delay(FxnDef);
}
public static Rule CodeBlock()
{
return Seq(Token("{"), Star(Choice(Statement(), Expr())), NoFail(Token("}"), "missing '}'"));
}
public static Rule Param()
{
return Token(CatAstNode(AstLabel.Param, Ident()));
}
public static Rule Params()
{
return Seq(Token("("), Star(Param()), NoFail(Token(")"), "missing ')'"));
}
public static Rule TypeVar()
{
return CatAstNode(AstLabel.TypeVar, Seq(Opt(CharSeq("$")), LowerCaseLetter(), Star(IdentNextChar())));
}
public static Rule StackVar()
{
return CatAstNode(AstLabel.StackVar, Seq(Opt(CharSeq("$")), UpperCaseLetter(), Star(IdentNextChar())));
}
public static Rule TypeOrStackVar()
{
return Seq(SingleChar('\''), NoFail(Choice(TypeVar(), StackVar()), "invalid type or stack variable name"), WS());
}
public static Rule TypeName()
{
return Token(CatAstNode(AstLabel.TypeName, Ident()));
}
public static Rule TypeComponent()
{
return Choice(TypeName(), TypeOrStackVar(), Delay(FxnType));
}
public static Rule Production()
{
return CatAstNode(AstLabel.Stack, Token(Star(TypeComponent())));
}
public static Rule Consumption()
{
return CatAstNode(AstLabel.Stack, Token(Star(TypeComponent())));
}
public static Rule Arrow()
{
return CatAstNode(AstLabel.Arrow, Choice(Token("->"), Token("~>")));
}
public static Rule FxnType()
{
return CatAstNode(AstLabel.FxnType, Seq(Token("("), Production(), NoFail(Arrow(), "expected either -> or ~>"), Consumption(), NoFail(Token(")"), "expected closing paranthesis")));
}
public static Rule TypeDecl()
{
return Seq(Token(":"), NoFail(FxnType(), "expected function type declaration"), WS());
}
public static Rule FxnDef()
{
return CatAstNode(AstLabel.Def, Seq(Word("define"),
NoFail(Name(), "expected name"), Opt(Params()), Opt(TypeDecl()), Opt(MetaDataBlock()), Star(Delay(FxnDef)), NoFail(CodeBlock(), "expected a code block")));
}
public static Rule FxnDecl()
{
return CatAstNode(AstLabel.Decl, Seq(Word("declare"), NoFail(Name(), "expected name"),
Opt(Seq(TypeDecl(), Opt(MetaDataBlock())))));
}
#region macros
public static Rule MacroTypeVarName()
{
return CatAstNode(AstLabel.MacroTypeVarName, Seq(LowerCaseLetter(), Star(IdentNextChar())));
}
public static Rule MacroTypeVar()
{
return CatAstNode(AstLabel.MacroTypeVar, MacroTypeVarName());
}
public static Rule MacroStackVarName()
{
return CatAstNode(AstLabel.MacroStackVarName, Seq(UpperCaseLetter(), Star(IdentNextChar())));
}
public static Rule MacroStackVar()
{
return CatAstNode(AstLabel.MacroStackVar, Seq(MacroStackVarName(), WS(), Opt(TypeDecl())));
}
public static Rule MacroVar()
{
return Seq(SingleChar('$'), NoFail(Choice(MacroTypeVar(), MacroStackVar()), "expected a valid macro type variable or stack variable"));
}
public static Rule MacroName()
{
return CatAstNode(AstLabel.MacroName, Choice(Symbol(), CatIdent()));
}
public static Rule MacroTerm()
{
return Token(Choice(MacroQuote(), MacroVar(), MacroName()));
}
public static Rule MacroQuote()
{
return CatAstNode(AstLabel.MacroQuote, Seq(Token("["), Star(Delay(MacroTerm)), NoFail(Token("]"), "missing ']'")));
}
public static Rule MacroPattern()
{
return CatAstNode(AstLabel.MacroPattern, Seq(Token("{"), Star(MacroTerm()), NoFail(Token("}"), "missing '}'")));
}
public static Rule MacroDef()
{
return CatAstNode(AstLabel.MacroRule, Seq(Word("rule"), Seq(MacroPattern(), Token("=>"), MacroPattern())));
}
public static Rule MacroProp()
{
return CatAstNode(AstLabel.MacroProp, Seq(Word("rule"), Seq(MacroPattern(), Token("=="), MacroPattern())));
}
#endregion
public static Rule CatProgram()
{
return Seq(WS(), Star(Choice(MetaDataBlock(), FxnDef(), MacroDef(), Expr())), WS(), NoFail(EndOfInput(), "expected macro or function defintion"));
}
}
}