-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQStatement.cpp
More file actions
125 lines (114 loc) · 3.67 KB
/
Copy pathQStatement.cpp
File metadata and controls
125 lines (114 loc) · 3.67 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
#include <assert.h>
#include <iostream>
#include "FileLexer.h"
#include "Type.h"
#include "Expression.h"
#include "CompilerHelpers.h"
#include "logging.h"
SET_LOG_CAT( LOG_CAT_ALL );
SET_LOG_LEVEL( LOG_LVL_NOISE );
using namespace QLang;
using namespace std;
// Of two parse-attempt errors, return the one that progressed further into the
// source (greater line, then greater column). A statement that is neither a
// valid declaration nor a valid expression should report the deeper, more
// specific cause — the attempt that consumed more input before failing — not a
// generic "Unexpected token" at column 1 (U2, FR-006 / R4).
static const CompileError &deeperError( const CompileError &a, const CompileError &b )
{
const SourceLocation &la = a.getLocation();
const SourceLocation &lb = b.getLocation();
if ( lb.line > la.line || ( lb.line == la.line && lb.col > la.col ) )
return b;
return a;
}
Statement *Statement::Parse( Lexer &l, Scope *scope )
{
TRACE_BEGIN( LOG_LVL_INFO );
Statement *statement = nullptr;
int pos = l.getCurrentPos();
LOG( "Saving position: %d", pos );
switch ( l.peekSymbol() )
{
case Lexer::KEYWORD_WHILE:
statement = WhileStatement::Parse( l, scope );
break;
case Lexer::KEYWORD_IF:
statement = IfStatement::Parse( l, scope );
break;
case Lexer::KEYWORD_FOR:
statement = ForInStatement::Parse( l, scope );
break;
case Lexer::KEYWORD_RETURN:
statement = ReturnStatement::Parse( l, scope );
break;
case Lexer::KEYWORD_BREAK:
statement = BreakStatement::Parse( l, scope );
break;
case Lexer::KEYWORD_CONTINUE:
statement = ContinueStatement::Parse( l, scope );
break;
case Lexer::KEYWORD_MATCH:
statement = MatchExpression::Parse( l, scope );
break;
case Lexer::KEYWORD_SPAWN:
statement = SpawnStatement::Parse( l, scope );
break;
case Lexer::KEYWORD_WAIT:
statement = WaitStatement::Parse( l, scope );
break;
case Lexer::KEYWORD_WAIT_ALL:
statement = WaitAllStatement::Parse( l, scope );
break;
case Lexer::KEYWORD_ASSERT:
statement = AssertStatement::Parse( l, scope );
break;
case Lexer::KEYWORD_ON:
statement = EventHandler::Parse( l, scope );
break;
case '{':
statement = Block::Parse( l, scope );
break;
case ';':
l.getSymbol();
break;
default:
try {
statement = VariableDeclaration::Parse( l, scope );
} catch( CompileError &declErr ) {
LOG( "Not a decl, resetting position: %d", pos );
l.setCurrentPos( pos );
try {
statement = Expression::Parse( l, scope );
} catch ( CompileError &exprErr ) {
l.setCurrentPos( pos );
// Neither a declaration nor an expression: surface the deeper,
// located cause instead of a generic "Unexpected token". Fall
// back to a located generic error only if neither attempt
// carries a usable location.
const CompileError &deepest = deeperError( declErr, exprErr );
if ( deepest.getLocation().isSet() )
throw deepest;
COMPILE_ERROR( l, "Unexpected token" );
}
if ( statement == nullptr )
{
l.setCurrentPos( pos );
// The expression attempt did not THROW — it simply produced
// nothing — so the `deeperError` arm above never ran and
// `declErr` was discarded in favour of a generic "Unexpected
// token" at the statement's first column. That hid every
// located cause raised inside VariableDeclaration::Parse:
// `string s = "{missing}";` reported "Unexpected token"
// instead of naming the unresolvable interpolation
// placeholder. Surface the declaration attempt's located cause,
// exactly as the both-threw path already does.
if ( declErr.getLocation().isSet() )
throw declErr;
COMPILE_ERROR( l, "Unexpected token" );
}
}
break;
}
return statement;
}