-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
143 lines (129 loc) · 3.77 KB
/
Program.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
using System;
using BenchmarkDotNet.Running;
/*
The grammer
primaryExpression : IDENTIFIER
NUMBER
STRING_LITERAL
'(' expression ')'
postfixExpression: primaryExpression ( . IDENTIFER | [expression] (argList?))*
multiplicativeExpression: postfixExpression (*|/ postfixExpression)*
expression: multiplicativeExpression
*/
namespace Parser
{
class Program
{
static void Main(string[] args)
{
string[] expressions = {
"[1, 2].length + [].length",
"a + +b",
"f.g(x) != f(g(x))",
"!!!!++a.b != 0",
"-2 + a == 3 + 4",
"a != b^2",
"fun[a+2](a).b.c + c",
"func(2).a.b + 3",
"1 + exp(2, 3)",
"a + div(3 + 2",
"add(1, 2)",
"\"hel lo\" + 'world'",
"'hello' + 'world'",
"_test + 5.5",
"hello+world",
"1+2",
"a+1",
"a+b*1+2",
"(a+b)*1+2",
"(1)+2",
"(1/2"
};
string[] ids = {
"_1",
"1a",
"a1",
"@@a",
"__",
"a",
"_a",
"$b",
"_a1"
};
//TestPureParser(expressions);
//TestParser(expressions);
//TestLexer(expressions);
BenchmarkRunner.Run<Benchmarks>();
//TestAdhoc();
}
static void printToken(Token t)
{
Console.WriteLine($" <{(t.Kind)}/{t.Text}>");
}
static void TestPureParser(string[] expressions)
{
var parser = new Parser();
foreach (var e in expressions)
{
Console.WriteLine(e);
var result = parser.Parse(e);
if (result.IsSuccess)
{
Console.WriteLine(result.Value);
}
else
{
Console.WriteLine(result.ErrorMessage);
}
}
}
static void TestAdhoc()
{
// var parser = new Parser().parseStringLiteral2;
// var e = "\"abc\\\"abc\"";
// {
// Console.WriteLine(e);
// var result = parser(new Parser.InputReader(e));
// if (result.IsSuccess)
// {
// Console.WriteLine(result.Value);
// }
// else
// {
// Console.WriteLine(result.ErrorMessage);
// }
// }
}
static void TestParser(string[] expressions)
{
foreach (var e in expressions)
{
var parser = new ExpressionParser();
try
{
Console.WriteLine(e);
var exp = parser.ParseExpression(e);
Console.WriteLine(exp);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
static void TestLexer(string[] expressions)
{
foreach (var e in expressions)
{
var lexer = new ExpressionLexer(e);
Console.WriteLine($"{e}:");
Token token = null;
do {
token = lexer.NextToken();
printToken(token);
lexer.EatToken();
} while (token.Kind != TokenKind.EOF);
}
}
}
}