Commit 90bf956c authored by Roberto Raggi's avatar Roberto Raggi
Browse files

Simple error recovery.

Well, it's not very advanced but it is probably good enough for now.
parent 055aa15f
......@@ -313,7 +313,11 @@ private:
TypeAST *&type(int n) { return _symStack[_tos + n - 1].type; }
FunctionDeclarationAST *&function(int n) { return _symStack[_tos + n - 1].function_declaration; }
inline int consumeToken() { return _index++; }
inline int consumeToken() {
if (_index < int(_tokens.size()))
return _index++;
return _tokens.size() - 1;
}
inline const Token &tokenAt(int index) const { return _tokens.at(index); }
inline int tokenKind(int index) const { return _tokens.at(index).kind; }
void reduce(int ruleno);
......@@ -380,6 +384,9 @@ private:
int _tos;
int _index;
int yyloc;
int yytoken;
int yyrecovering;
bool _recovered;
std::vector<int> _stateStack;
std::vector<int> _locationStack;
std::vector<Value> _symStack;
......@@ -424,11 +431,12 @@ private:
#include <iostream>
#include <cstdio>
#include <cassert>
#include <QtCore/QDebug>
using namespace GLSL;
Parser::Parser(Engine *engine, const char *source, unsigned size, int variant)
: _engine(engine), _tos(-1), _index(0), yyloc(-1)
: _engine(engine), _tos(-1), _index(0), yyloc(-1), yytoken(-1), yyrecovering(0), _recovered(false)
{
_tokens.reserve(1024);
......@@ -494,16 +502,28 @@ Parser::~Parser()
TranslationUnitAST *Parser::parse()
{
int action = 0;
int yytoken = -1;
yytoken = -1;
yyloc = -1;
void *yyval = 0; // value of the current token.
_recovered = false;
_tos = -1;
do {
again:
if (unsigned(++_tos) == _stateStack.size()) {
_stateStack.resize(_tos * 2);
_locationStack.resize(_tos * 2);
_symStack.resize(_tos * 2);
}
_stateStack[_tos] = action;
if (yytoken == -1 && -TERMINAL_COUNT != action_index[action]) {
yyloc = consumeToken();
yytoken = tokenKind(yyloc);
if (yyrecovering)
--yyrecovering;
if (yytoken == T_IDENTIFIER && t_action(action, T_TYPE_NAME) != 0) {
const Token &la = tokenAt(_index);
......@@ -517,13 +537,6 @@ TranslationUnitAST *Parser::parse()
yyval = _tokens.at(yyloc).ptr;
}
if (unsigned(++_tos) == _stateStack.size()) {
_stateStack.resize(_tos * 2);
_locationStack.resize(_tos * 2);
_symStack.resize(_tos * 2);
}
_stateStack[_tos] = action;
action = t_action(action, yytoken);
if (action > 0) {
if (action == ACCEPT_STATE) {
......@@ -539,20 +552,58 @@ TranslationUnitAST *Parser::parse()
_tos -= N;
reduce(ruleno);
action = nt_action(_stateStack[_tos], lhs[ruleno] - TERMINAL_COUNT);
}
} while (action);
} else if (action == 0) {
const int line = _tokens[yyloc].line + 1;
QString message = QLatin1String("Syntax error");
if (yytoken != -1) {
const QLatin1String s(spell[yytoken]);
message = QString("Unexpected token `%1'").arg(s);
}
const int line = _tokens[yyloc].line + 1;
QString message = QLatin1String("Syntax error");
if (yytoken != -1) {
const QLatin1String s(yytoken != -1 ? spell[yytoken] : "");
message = QString("Unexpected token `%1'").arg(s);
}
for (; _tos; --_tos) {
const int state = _stateStack[_tos];
static int tks[] = {
T_RIGHT_BRACE, T_RIGHT_PAREN, T_RIGHT_BRACKET,
T_SEMICOLON, T_COMMA, T_COLON,
T_NUMBER, T_TYPE_NAME, T_IDENTIFIER,
T_LEFT_BRACE, T_LEFT_PAREN, T_LEFT_BRACKET,
0
};
for (int *tptr = tks; *tptr; ++tptr) {
const int next = t_action(state, *tptr);
if (next > 0) {
if (! yyrecovering && ! _recovered) {
_recovered = true;
error(line, QString("Expected `%1'").arg(QLatin1String(spell[*tptr])));
}
yyrecovering = 3;
if (*tptr == T_IDENTIFIER)
yyval = (void *) _engine->identifier(QLatin1String("$identifier"));
else if (*tptr == T_NUMBER || *tptr == T_TYPE_NAME)
yyval = (void *) _engine->identifier(QLatin1String("$0"));
else
yyval = 0;
_symStack[_tos].ptr = yyval;
_locationStack[_tos] = yyloc;
yytoken = -1;
action = next;
goto again;
}
}
}
error(line, message);
if (! _recovered) {
_recovered = true;
error(line, message);
}
}
// fprintf(stderr, "unexpected token `%s' at line %d\n", yytoken != -1 ? spell[yytoken] : "",
// _tokens[yyloc].line + 1);
} while (action);
return 0;
}
......
This diff is collapsed.
#line 214 "./glsl.g"
#line 214 "glsl.g"
/**************************************************************************
**
......@@ -102,7 +102,11 @@ private:
TypeAST *&type(int n) { return _symStack[_tos + n - 1].type; }
FunctionDeclarationAST *&function(int n) { return _symStack[_tos + n - 1].function_declaration; }
inline int consumeToken() { return _index++; }
inline int consumeToken() {
if (_index < int(_tokens.size()))
return _index++;
return _tokens.size() - 1;
}
inline const Token &tokenAt(int index) const { return _tokens.at(index); }
inline int tokenKind(int index) const { return _tokens.at(index).kind; }
void reduce(int ruleno);
......@@ -169,6 +173,9 @@ private:
int _tos;
int _index;
int yyloc;
int yytoken;
int yyrecovering;
bool _recovered;
std::vector<int> _stateStack;
std::vector<int> _locationStack;
std::vector<Value> _symStack;
......
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtCore module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
// This file was generated by qlalr - DO NOT EDIT!
#include "glslparsertable_p.h"
QT_BEGIN_NAMESPACE
const char *const GLSLParserTable::spell [] = {
"end of file", "+=", "&", "&=", "&&", "attribute", "!", "bool", "break", "bvec2",
"bvec3", "bvec4", "^", "case", "centroid", ":", ",", "const", "continue", "-",
......@@ -62,7 +19,19 @@ const char *const GLSLParserTable::spell [] = {
"~", "type_name", "uint", "uniform", "usampler1D", "usampler1DArray", "usampler2D", "usampler2DArray", "usampler2DMS", "usampler2DMSarray",
"usampler2DRect", "usampler3D", "usamplerBuffer", "usamplerCube", "usamplerCubeArray", "uvec2", "uvec3", "uvec4", "varying", "vec2",
"vec3", "vec4", "|", "void", "while", "^=", "^", "true", "false", "preprocessor directive",
"comment", "error", "reserved word"};
"comment", "error", "reserved word",
#ifndef QLALR_NO_GLSLPARSERTABLE_DEBUG_INFO
"translation_unit", "variable_identifier", "primary_expression", "expression", "postfix_expression", "integer_expression", "function_call",
"function_call_or_method", "function_call_generic", "function_call_header_with_parameters", "function_call_header_no_parameters", "function_call_header", "assignment_expression", "function_identifier", "type_specifier", "unary_expression", "unary_operator",
"multiplicative_expression", "additive_expression", "shift_expression", "relational_expression", "equality_expression", "and_expression", "exclusive_or_expression", "inclusive_or_expression", "logical_and_expression", "logical_xor_expression",
"logical_or_expression", "conditional_expression", "assignment_operator", "constant_expression", "declaration", "function_prototype", "init_declarator_list", "precision_qualifier", "type_specifier_no_prec", "type_qualifier",
"struct_declaration_list", "function_declarator", "function_header", "function_header_with_parameters", "parameter_declaration", "fully_specified_type", "parameter_declarator", "parameter_type_qualifier", "parameter_qualifier", "parameter_type_specifier",
"empty", "single_declaration", "initializer", "invariant_qualifier", "interpolation_qualifier", "layout_qualifier", "layout_qualifier_id_list", "layout_qualifier_id", "storage_qualifier", "type_specifier_nonarray",
"struct_specifier", "struct_declaration", "struct_declarator_list", "struct_declarator", "declaration_statement", "statement", "compound_statement", "simple_statement", "expression_statement", "selection_statement",
"switch_statement", "case_label", "iteration_statement", "jump_statement", "statement_list", "statement_no_new_scope", "compound_statement_no_new_scope", "selection_rest_statement", "condition", "switch_statement_list",
"for_init_statement", "for_rest_statement", "conditionopt", "external_declaration_list", "external_declaration", "function_definition", "$accept"
#endif // QLALR_NO_GLSLPARSERTABLE_DEBUG_INFO
};
const short GLSLParserTable::lhs [] = {
174, 175, 175, 175, 175, 175, 177, 177, 177, 177,
......@@ -132,6 +101,359 @@ const short GLSLParserTable::rhs [] = {
2, 2, 2, 3, 2, 1, 1, 2, 1, 1,
1, 2, 0, 2};
#ifndef QLALR_NO_GLSLPARSERTABLE_DEBUG_INFO
const int GLSLParserTable::rule_info [] = {
174, 50
, 175, 97
, 175, 167
, 175, 168
, 175, 174
, 175, 77, 176, 112
, 177, 175
, 177, 177, 75, 178, 110
, 177, 179
, 177, 177, 37, 50
, 177, 177, 53
, 177, 177, 20
, 178, 176
, 179, 180
, 180, 181
, 180, 177, 37, 181
, 181, 182, 112
, 181, 183, 112
, 183, 184, 163
, 183, 184
, 182, 184, 185
, 182, 182, 16, 185
, 184, 186, 77
, 186, 187
, 186, 50
, 188, 177
, 188, 53, 188
, 188, 20, 188
, 188, 189, 188
, 189, 103
, 189, 19
, 189, 6
, 189, 140
, 190, 188
, 190, 190, 135, 188
, 190, 190, 133, 188
, 190, 190, 102, 188
, 191, 190
, 191, 191, 103, 190
, 191, 191, 19, 190
, 192, 191
, 192, 192, 76, 191
, 192, 192, 111, 191
, 193, 192
, 193, 193, 72, 192
, 193, 193, 107, 192
, 193, 193, 78, 192
, 193, 193, 48, 192
, 194, 193
, 194, 194, 44, 193
, 194, 194, 95, 193
, 195, 194
, 195, 195, 2, 194
, 196, 195
, 196, 196, 12, 195
, 197, 196
, 197, 197, 162, 196
, 198, 197
, 198, 198, 4, 197
, 199, 198
, 199, 199, 166, 198
, 200, 199
, 200, 200, 99, 199
, 201, 200
, 201, 200, 105, 176, 15, 185
, 185, 201
, 185, 188, 202, 185
, 202, 43
, 202, 94
, 202, 23
, 202, 93
, 202, 1
, 202, 138
, 202, 73
, 202, 108
, 202, 3
, 202, 165
, 202, 98
, 176, 185
, 176, 176, 16, 185
, 203, 201
, 204, 205, 132
, 204, 206, 132
, 204, 104, 207, 208, 132
, 204, 209, 50, 74, 210, 109, 132
, 204, 209, 50, 74, 210, 109, 50, 132
, 204, 209, 50, 74, 210, 109, 50, 75, 110, 132
, 204, 209, 50, 74, 210, 109, 50, 75, 203, 110, 132
, 204, 209, 132
, 205, 211, 112
, 211, 212
, 211, 213
, 213, 212, 214
, 213, 213, 16, 214
, 212, 215, 50, 77
, 216, 187, 50
, 216, 187, 50, 75, 203, 110
, 214, 217, 218, 216
, 214, 218, 216
, 214, 217, 218, 219
, 214, 218, 219
, 218, 220
, 218, 52
, 218, 100
, 218, 54
, 219, 187
, 206, 221
, 206, 206, 16, 50
, 206, 206, 16, 50, 75, 110
, 206, 206, 16, 50, 75, 203, 110
, 206, 206, 16, 50, 75, 110, 43, 222
, 206, 206, 16, 50, 75, 203, 110, 43, 222
, 206, 206, 16, 50, 43, 222
, 221, 215
, 221, 215, 50
, 221, 215, 50, 75, 110
, 221, 215, 50, 75, 203, 110
, 221, 215, 50, 75, 110, 43, 222
, 221, 215, 50, 75, 203, 110, 43, 222
, 221, 215, 50, 43, 222
, 221, 56, 50
, 215, 187
, 215, 209, 187
, 223, 56
, 224, 134
, 224, 45
, 224, 96
, 225, 71, 77, 226, 112
, 226, 227
, 226, 226, 16, 227
, 227, 50
, 227, 50, 43, 97
, 217, 17
, 209, 228
, 209, 225
, 209, 225, 228
, 209, 224, 228
, 209, 224
, 209, 223, 228
, 209, 223, 224, 228
, 209, 56
, 228, 17
, 228, 5
, 228, 158
, 228, 14, 158
, 228, 52
, 228, 100
, 228, 14, 52
, 228, 14, 100
, 228, 101, 52
, 228, 101, 100
, 228, 113, 52
, 228, 113, 100
, 228, 143
, 187, 208
, 187, 207, 208
, 208, 229
, 208, 229, 75, 110
, 208, 229, 75, 203, 110
, 229, 163
, 229, 46
, 229, 38
, 229, 55
, 229, 142
, 229, 7
, 229, 159
, 229, 160
, 229, 161
, 229, 39
, 229, 40
, 229, 41
, 229, 9
, 229, 10
, 229, 11
, 229, 68
, 229, 69
, 229, 70
, 229, 155
, 229, 156
, 229, 157
, 229, 80
, 229, 84
, 229, 88
, 229, 81
, 229, 82
, 229, 83
, 229, 85
, 229, 86
, 229, 87
, 229, 89
, 229, 90
, 229, 91
, 229, 24
, 229, 28
, 229, 32
, 229, 25
, 229, 26
, 229, 27
, 229, 29
, 229, 30
, 229, 31
, 229, 33
, 229, 34
, 229, 35
, 229, 114
, 229, 118
, 229, 126
, 229, 128
, 229, 117
, 229, 125
, 229, 131
, 229, 115
, 229, 119
, 229, 116
, 229, 120
, 229, 129
, 229, 130
, 229, 57
, 229, 59
, 229, 64
, 229, 66
, 229, 58
, 229, 60
, 229, 67
, 229, 144
, 229, 146
, 229, 151
, 229, 153
, 229, 145
, 229, 147
, 229, 154
, 229, 123
, 229, 124
, 229, 63
, 229, 150
, 229, 127
, 229, 65
, 229, 152
, 229, 121
, 229, 61
, 229, 148
, 229, 122
, 229, 62
, 229, 149
, 229, 230
, 229, 141
, 207, 49
, 207, 92
, 207, 79
, 230, 136, 50, 74, 210, 109
, 230, 136, 74, 210, 109
, 210, 231
, 210, 210, 231
, 231, 187, 232, 132
, 231, 209, 187, 232, 132
, 232, 233
, 232, 232, 16, 233
, 233, 50
, 233, 50, 75, 110
, 233, 50, 75, 203, 110
, 222, 185
, 234, 204
, 235, 236
, 235, 237
, 237, 234
, 237, 238
, 237, 239
, 237, 240
, 237, 241
, 237, 242
, 237, 243
, 236, 74, 109
, 236, 74, 244, 109
, 245, 246
, 245, 237
, 246, 74, 109
, 246, 74, 244, 109
, 244, 235
, 244, 244, 235
, 238, 132
, 238, 176, 132
, 239, 51, 77, 176, 112, 247
, 247, 235, 42, 235
, 247, 235
, 248, 176
, 248, 215, 50, 43, 222
, 240, 139, 77, 176, 112, 74, 249, 109
, 249, 220
, 249, 244
, 241, 13, 176, 15
, 241, 21, 15
, 242, 164, 77, 248, 112, 245
, 242, 36, 235, 164, 77, 176, 112, 132
, 242, 47, 77, 250, 251, 112, 245
, 250, 238
, 250, 234
, 252, 220
, 252, 248
, 251, 252, 132
, 251, 252, 132, 176
, 243, 18, 132
, 243, 8, 132
, 243, 106, 132
, 243, 106, 176, 132
, 243, 22, 132
, 173, 253
, 253, 254
, 253, 253, 254
, 254, 255
, 254, 204
, 254, 132
, 255, 205, 246
, 220
, 256, 173, 0};
const int GLSLParserTable::rule_index [] = {
0, 2, 4, 6, 8, 10, 14, 16, 21, 23,
27, 30, 33, 35, 37, 39, 43, 46, 49, 52,
54, 57, 61, 64, 66, 68, 70, 73, 76, 79,
81, 83, 85, 87, 89, 93, 97, 101, 103, 107,
111, 113, 117, 121, 123, 127, 131, 135, 139, 141,
145, 149, 151, 155, 157, 161, 163, 167, 169, 173,
175, 179, 181, 185, 187, 193, 195, 199, 201, 203,
205, 207, 209, 211, 213, 215, 217, 219, 221, 223,
227, 229, 232, 235, 240, 247, 255, 265, 276, 279,
282, 284, 286, 289, 293, 297, 300, 306, 310, 313,
317, 320, 322, 324, 326, 328, 330, 332, 336, 342,
349, 357, 366, 372, 374, 377, 382, 388, 395, 403,
408, 411, 413, 416, 418, 420, 422, 424, 429, 431,
435, 437, 441, 443, 445, 447, 450, 453, 455, 458,
462, 464, 466, 468, 470, 473, 475, 477, 480, 483,
486, 489, 492, 495, 497, 499, 502, 504, 508, 513,
515, 517, 519, 521, 523, 525, 527, 529, 531, 533,
535, 537, 539, 541, 543, 545, 547, 549, 551, 553,
555, 557, 559, 561, 563, 565, 567, 569, 571, 573,
575, 577, 579, 581, 583, 585, 587, 589, 591, 593,
595, 597, 599, 601, 603, 605, 607, 609, 611, 613,
615, 617, 619, 621, 623, 625, 627, 629, 631, 633,
635, 637, 639, 641, 643, 645, 647, 649, 651, 653,
655, 657, 659, 661, 663, 665, 667, 669, 671, 673,
675, 677, 679, 681, 683, 685, 687, 689, 691, 693,
699, 704, 706, 709, 713, 718, 720, 724, 726, 730,
735, 737, 739, 741, 743, 745, 747, 749, 751, 753,
755, 757, 760, 764, 766, 768, 771, 775, 777, 780,
782, 785, 791, 795, 797, 799, 804, 812, 814, 816,
820, 823, 829, 837, 844, 846, 848, 850, 852, 855,
859, 862, 865, 868, 872, 875, 877, 879, 882, 884,
886, 888, 891, 892};
#endif // QLALR_NO_GLSLPARSERTABLE_DEBUG_INFO