forked from gfwilliams/tiny-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparserResults.cpp
More file actions
204 lines (173 loc) · 4.34 KB
/
parserResults.cpp
File metadata and controls
204 lines (173 loc) · 4.34 KB
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
/*
* File: parserResults.cpp
* Author: ghernan
*
* Created on November 30, 2016, 7:56 PM
*/
#include "ascript_pch.hpp"
#include "parserResults.h"
#include "utils.h"
#include "ScriptException.h"
#include <stdarg.h>
/**
* If the current result is an error, executes the parse function.
* If not, it doesn't call the parse function.
* @param parseFn
* @return
*/
ExprResult ExprResult::orElse(ParseFunction parseFn)
{
if (ok())
return *this;
ExprResult r = parseFn(token);
r.m_initialToken = m_initialToken;
if (r.ok() || r.errorDesc.position > this->errorDesc.position)
return r;
else
return *this;
}
/**
* If the current result is a success, executes the parse function and returns
* its result.
* Version for regular parse functions.
* @param parseFn
* @return
*/
ExprResult ExprResult::then(ParseFunction parseFn)
{
if (error())
return *this;
ExprResult r = parseFn(token);
r.m_initialToken = m_initialToken;
return r;
}
/**
* If the current result is a success, executes the parse function and returns
* its result.
* Version for chained parse functions. Chained parse functions receive the current
* result as parameter.
* @param parseFn
* @return
*/
ExprResult ExprResult::then(ChainParseFunction parseFn)
{
if (error())
return *this;
ExprResult r = parseFn(token, result);
r.m_initialToken = m_initialToken;
return r;
}
/**
* Requires that the current token complies with certain condition.
* The 'condition' is a function which receives a token, and return a boolean
* value.
* If the check is successful, advances to the next token in the result.
* @param checkFn Token check function.
* @return
*/
ExprResult ExprResult::require(TokenCheck checkFn)
{
if (error())
return *this;
if (checkFn(token))
{
ExprResult r (token.next(), result);
r.m_initialToken = m_initialToken;
return r;
}
else
return getError("Unexpected token: '%s'", token.text().c_str());
}
/**
* Requires that the current token is of the specified type.
* If the check is successful, advances to the next token in the result.
* @param tokenType Token type
* @return
*/
ExprResult ExprResult::require(int tokenType)
{
if (error())
return *this;
if (token.type() == tokenType)
{
ExprResult r (token.next(), result);
r.m_initialToken = m_initialToken;
return r;
}
else
return getError("Unexpected token: '%s'", token.text().c_str());
}
/**
* Requires that the current token is an identifier with the given text.
* @param text
* @return
*/
ExprResult ExprResult::requireId(const char* text)
{
if (error())
return *this;
ExprResult r = require(LEX_ID);
if (r.ok() && token.text() == text)
return r;
else
return getError("Unexpected id. '%s' expected, '%s' found", text, token.text().c_str());
}
/**
* Skips next token. Forwards the previous result
* @return
*/
ExprResult ExprResult::skip()
{
if (error())
return *this;
ExprResult r(token.next(), result);
r.m_initialToken = m_initialToken;
return r;
}
/**
* Gets an error result located at the current token.
* @param format
* @param ...
* @return
*/
ExprResult ExprResult::getError(const char* format, ...)
{
va_list args;
char buffer[2048];
va_start(args, format);
vsprintf_s(buffer, format, args);
va_end(args);
ExprResult result (token, ParseError(token.getPosition(), buffer));
result.m_initialToken = m_initialToken;
return result;
}
/**
* Throws a script exception if it is an error result.
*/
void ExprResult::throwIfError()const
{
if (error())
errorAt(errorDesc.position, "%s", errorDesc.text.c_str());
}
/**
* Transforms a 'ExprResult' into a 'ParseResult'
* If it is an error result, throws an exception, because the ParseResult
* structure doesn't contain error information
* @return
*/
ParseResult ExprResult::toParseResult()const
{
throwIfError();
return ParseResult(token, result);
}
/**
* If the result is an error, relocates it to the initial token
* @return
*/
ExprResult ExprResult::final()const
{
if (ok())
return *this;
else
return ExprResult(m_initialToken, errorDesc);
}