-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.cs
182 lines (166 loc) · 6.33 KB
/
parser.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
using System.Collections.Generic;
namespace pineapple
{
class parser
{
public struct Variable
{
public int LineNum;
public string Name;
}
public struct Assignment : Statement
{
public int LineNum;
public Variable Variable;
public string String;
public System.Type ValueType()
{
return this.GetType();
}
}
public struct Print : Statement
{
public int LineNum;
public Variable Variable;
public System.Type ValueType()
{
return this.GetType();
}
}
public interface Statement
{
System.Type ValueType();
};
public struct SourceCode
{
public int LineNum;
public List<Statement> Statements;
}
//Name ::= [_A-Za-z][_0-9A-Za-z]*
static public (string, string) parseName(ref lexer.Lexer Lexer)
{
var (_, name) = Lexer.NextTokenIs(lexer.TOKEN_NAME);
return (name, null);
}
//String ::= '"' '"' Ignored | '"' StringCharacter '"' Ignored
static public (string, string) parseString(ref lexer.Lexer Lexer)
{
string str = "";
switch (Lexer.LookAhead())
{
case lexer.TOKEN_DUOQUOTE:
Lexer.NextTokenIs(lexer.TOKEN_DUOQUOTE);
Lexer.LookAheadAndSkip(lexer.TOKEN_IGNORED);
return (str, null);
case lexer.TOKEN_QUOTE:
Lexer.NextTokenIs(lexer.TOKEN_QUOTE);
str = Lexer.scanBeforeToken(lexer.tokenNameMap[lexer.TOKEN_QUOTE]);
Lexer.NextTokenIs(lexer.TOKEN_QUOTE);
Lexer.LookAheadAndSkip(lexer.TOKEN_IGNORED);
return (str, null);
default:
return (null, "parseString(): not a string.");
}
}
//Variable ::= "$" Name Ignored
static public (Variable, string) parseVariable(ref lexer.Lexer Lexer)
{
Variable variable = new Variable();
string error;
variable.LineNum = Lexer.GetLineNum();
Lexer.NextTokenIs(lexer.TOKEN_VAR_PREFIX);
(variable.Name, error) = parseName(ref Lexer);
if (error != null)
return (variable, error);
Lexer.LookAheadAndSkip(lexer.TOKEN_IGNORED);
return (variable, null);
}
//Assignment ::= Variable Ignored "=" Ignored String Ignored
static public (Assignment, string) parseAssignment(ref lexer.Lexer Lexer)
{
Assignment assignment = new Assignment();
string error;
assignment.LineNum = Lexer.GetLineNum();
(assignment.Variable, error) = parseVariable(ref Lexer);
if (error != null)
return (assignment, error);
Lexer.LookAheadAndSkip(lexer.TOKEN_IGNORED);
Lexer.NextTokenIs(lexer.TOKEN_EQUAL);
Lexer.LookAheadAndSkip(lexer.TOKEN_IGNORED);
(assignment.String, error) = parseString(ref Lexer);
if (error != null)
return (assignment, error);
Lexer.LookAheadAndSkip(lexer.TOKEN_IGNORED);
return (assignment, null);
}
//Print ::= "print" "(" Ignored Variable Ignored ")" Ignored
static public (Print, string) parsePrint(ref lexer.Lexer Lexer)
{
Print print = new Print();
string error;
print.LineNum = Lexer.GetLineNum();
Lexer.NextTokenIs(lexer.TOKEN_PRINT);
Lexer.NextTokenIs(lexer.TOKEN_LEFT_PAREN);
Lexer.LookAheadAndSkip(lexer.TOKEN_IGNORED);
(print.Variable, error) = parseVariable(ref Lexer);
if (error != null)
return (print, error);
Lexer.LookAheadAndSkip(lexer.TOKEN_IGNORED);
Lexer.NextTokenIs(lexer.TOKEN_RIGHT_PAREN);
Lexer.LookAheadAndSkip(lexer.TOKEN_IGNORED);
return (print, null);
}
//Statement ::= Print | Assignment
static public (List<Statement>, string) parseStatements(ref lexer.Lexer Lexer)
{
List<Statement> statements = new List<Statement>();
while (!isSourceCodeEnd(Lexer.LookAhead()))
{
var (statement, error) = parseStatement(ref Lexer);
if (error != null)
return (null, error);
statements.Add(statement);
}
return (statements, null);
}
static public (Statement, string) parseStatement(ref lexer.Lexer Lexer)
{
Lexer.LookAheadAndSkip(lexer.TOKEN_IGNORED);
switch (Lexer.LookAhead())
{
case lexer.TOKEN_PRINT:
return parsePrint(ref Lexer);
case lexer.TOKEN_VAR_PREFIX:
return parseAssignment(ref Lexer);
default:
return (null, "parseStatement(): unknown Statement.");
}
}
static private bool isSourceCodeEnd(int token)
{
if (token == lexer.TOKEN_EOF)
return true;
return false;
}
//SourceCode ::= Statement+
static public (SourceCode, string) parseSourceCode(ref lexer.Lexer Lexer)
{
SourceCode sourceCode = new SourceCode();
string error;
sourceCode.LineNum = Lexer.GetLineNum();
(sourceCode.Statements, error) = parseStatements(ref Lexer);
if (error != null)
return (sourceCode, error);
return (sourceCode, null);
}
static public (SourceCode, string) parse(string code)
{
lexer.Lexer Lexer = lexer.NewLexer(code);
var (sourceCode, error) = parseSourceCode(ref Lexer);
if (error != null)
return (sourceCode, error);
Lexer.NextTokenIs(lexer.TOKEN_EOF);
return (sourceCode, null);
}
}
}