Skip to content

Commit

Permalink
* added PANIC MODE error recovery to the parser. This breaks two tests
Browse files Browse the repository at this point in the history
  (ParseError1 and 2) so those are temporarily fixed.


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@2098 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Andreas Remar committed Feb 9, 2006
1 parent a6afc9e commit 26bd418
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 1 deletion.
4 changes: 4 additions & 0 deletions Compiler/absyn_builder/expression_parser.g
Expand Up @@ -49,6 +49,10 @@ header "post_include_hpp" {
#define null 0
#include "MyAST.h"
#include "../../Compiler/runtime/errorext.h"
#include "../../Compiler/runtime/error_reporting.h"
}
options {
Expand Down
40 changes: 40 additions & 0 deletions Compiler/runtime/error_reporting.h
@@ -0,0 +1,40 @@
#ifndef __ERROR_REPORTING_H__
#define __ERROR_REPORTING_H__

#include <sstream>

// x05andre, Andreas Remar, 2006-02-02
// The following are helper macros for panic mode error recovery

// This macro is inserted before synchronizing with the FOLLOW set
#define BEFORE_SYNC if(std::string(e.getMessage()) == std::string("unexpected end of file")) \
throw ANTLR_USE_NAMESPACE(antlr)RecognitionException("unexpected end of file", \
modelicafilename, \
LT(1)->getLine(), \
LT(1)->getColumn()); \
std::list<std::string> tokens; \
int errorLine = LT(1)->getLine(); \
int errorColumn = LT(1)->getColumn(); \
tokens.push_back(std::string(e.getMessage()))

// This macro is inserted after synchronizing with the FOLLOW set
#define AFTER_SYNC tokens.push_back(std::string(LT(1)->getText())); \
std::stringstream ss; \
ss << LT(1)->getLine() << ", column " << LT(1)->getColumn(); \
tokens.push_back(std::string(ss.str())); \
add_source_message(2, \
"SYNTAX", \
"ERROR", \
"error: %s, parsing continued at token '%s' on line %s", \
tokens, \
errorLine, \
errorColumn, \
LT(1)->getLine(), \
LT(1)->getColumn(), \
false, \
(char *)modelicafilename.c_str());

// Access the global string modelicafilename found in Compiler/absyn_builder/parse.cpp
extern std::string modelicafilename;

#endif
131 changes: 130 additions & 1 deletion modelica_parser/src/modelica_parser.g
Expand Up @@ -3,7 +3,12 @@ header "post_include_hpp" {
#define null 0
#include "MyAST.h"

#include "../../Compiler/runtime/errorext.h"

#include "../../Compiler/runtime/error_reporting.h"

typedef ANTLR_USE_NAMESPACE(antlr)ASTRefCount<MyAST> RefMyAST;

}

options {
Expand Down Expand Up @@ -120,6 +125,7 @@ stored_definition :

;


within_clause :
WITHIN^ (name_path)?
;
Expand Down Expand Up @@ -253,6 +259,23 @@ external_function_call :
element_list :
((element | annotation ) SEMICOLON!)*
;
exception
catch [ANTLR_USE_NAMESPACE(antlr)RecognitionException &e]
{
BEFORE_SYNC;

// Sync to {PUBLIC, PROTECTED, EQUATION, ALGORITHM, EXTERNAL, END}
while(LA(1) != PUBLIC && LA(1) != PROTECTED && LA(1) != EQUATION && LA(1) != ALGORITHM && LA(1) != EXTERNAL && LA(1) != END)
{
if(LA(1) == EOF_)
{
throw ANTLR_USE_NAMESPACE(antlr)RecognitionException("unexpected end of file", modelicafilename, LT(1)->getLine(), LT(1)->getColumn());
}
consume();
}

AFTER_SYNC;
}

element :
ic:import_clause
Expand All @@ -277,6 +300,23 @@ element :
}
}
;
exception
catch [ANTLR_USE_NAMESPACE(antlr)RecognitionException &e]
{
BEFORE_SYNC;

// Sync to SEMICOLON
while(LA(1) != SEMICOLON)
{
if(LA(1) == EOF_)
{
throw ANTLR_USE_NAMESPACE(antlr)RecognitionException("unexpected end of file", modelicafilename, LT(1)->getLine(), LT(1)->getColumn());
}
consume();
}

AFTER_SYNC;
}

import_clause :
IMPORT^ (explicit_import_name | implicit_import_name) comment
Expand Down Expand Up @@ -442,13 +482,50 @@ equation_annotation_list :
|
( equation SEMICOLON! | annotation SEMICOLON!) equation_annotation_list
;
exception
catch [ANTLR_USE_NAMESPACE(antlr)RecognitionException &e]
{
BEFORE_SYNC;

// Sync to {END, EQUATION, ALGORITHM, INITIAL, PROTECTED, PUBLIC}
while(LA(1) != END && LA(1) != EQUATION && LA(1) != ALGORITHM && LA(1) != INITIAL
&& LA(1) != PROTECTED && LA(1) != PUBLIC)
{
if(LA(1) == EOF_)
{
throw ANTLR_USE_NAMESPACE(antlr)RecognitionException("unexpected end of file", modelicafilename, LT(1)->getLine(), LT(1)->getColumn());
}
consume();
}

AFTER_SYNC;
}

algorithm_clause :
ALGORITHM^
(algorithm SEMICOLON!
|annotation SEMICOLON!
)*
;
exception
catch [ANTLR_USE_NAMESPACE(antlr)RecognitionException &e]
{
BEFORE_SYNC;

// Sync to {END, EQUATION, ALGORITHM, INITIAL, PROTECTED, PUBLIC}
while(LA(1) != END && LA(1) != EQUATION && LA(1) != ALGORITHM && LA(1) != INITIAL
&& LA(1) != PROTECTED && LA(1) != PUBLIC)
{
if(LA(1) == EOF_)
{
throw ANTLR_USE_NAMESPACE(antlr)RecognitionException("unexpected end of file", modelicafilename, LT(1)->getLine(), LT(1)->getColumn());
}
consume();
}

AFTER_SYNC;
}

initial_algorithm_clause :
{ LA(2)==ALGORITHM}?
INITIAL! ALGORITHM^
Expand All @@ -459,8 +536,26 @@ initial_algorithm_clause :
#initial_algorithm_clause = #([INITIAL_ALGORITHM,"INTIAL_ALGORITHM"], #initial_algorithm_clause);
}
;
exception
catch [ANTLR_USE_NAMESPACE(antlr)RecognitionException &e]
{
BEFORE_SYNC;

// Sync to {END, EQUATION, ALGORITHM, INITIAL, PROTECTED, PUBLIC}
while(LA(1) != END && LA(1) != EQUATION && LA(1) != ALGORITHM && LA(1) != INITIAL
&& LA(1) != PROTECTED && LA(1) != PUBLIC)
{
if(LA(1) == EOF_)
{
throw ANTLR_USE_NAMESPACE(antlr)RecognitionException("unexpected end of file", modelicafilename, LT(1)->getLine(), LT(1)->getColumn());
}
consume();
}

AFTER_SYNC;
}

equation :

( (simple_expression EQUALS) => equality_equation
| conditional_equation_e
| for_clause_e
Expand All @@ -473,6 +568,23 @@ equation :
}
comment
;
exception
catch [ANTLR_USE_NAMESPACE(antlr)RecognitionException &e]
{
BEFORE_SYNC;

// Sync to SEMICOLON
while(LA(1) != SEMICOLON)
{
if(LA(1) == EOF_)
{
throw ANTLR_USE_NAMESPACE(antlr)RecognitionException("unexpected end of file", modelicafilename, LT(1)->getLine(), LT(1)->getColumn());
}
consume();
}

AFTER_SYNC;
}

algorithm :
( assign_clause_a
Expand All @@ -487,6 +599,23 @@ algorithm :
#algorithm = #([ALGORITHM_STATEMENT,"ALGORITHM_STATEMENT"], #algorithm);
}
;
exception
catch [ANTLR_USE_NAMESPACE(antlr)RecognitionException &e]
{
BEFORE_SYNC;

// Sync to SEMICOLON
while(LA(1) != SEMICOLON)
{
if(LA(1) == EOF_)
{
throw ANTLR_USE_NAMESPACE(antlr)RecognitionException("unexpected end of file", modelicafilename, LT(1)->getLine(), LT(1)->getColumn());
}
consume();
}

AFTER_SYNC;
}

assign_clause_a : component_reference ( ASSIGN^ expression | function_call );

Expand Down

0 comments on commit 26bd418

Please sign in to comment.