Skip to content

Commit

Permalink
Added line and column info to AST
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@1178 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Kaj Nyström committed Jun 4, 2004
1 parent 6a48dc1 commit cfe1606
Show file tree
Hide file tree
Showing 8 changed files with 334 additions and 179 deletions.
113 changes: 113 additions & 0 deletions modelica_parser/src/MyAST.h
@@ -0,0 +1,113 @@
#ifndef __MY_AST_H__
# define __MY_AST_H__

#include <antlr/CommonAST.hpp>

class MyAST;

typedef ANTLR_USE_NAMESPACE(antlr)ASTRefCount<MyAST> RefMyAST;

/** Custom AST class that adds line numbers to the AST nodes.
* easily extended with columns. Filenames will take more work since
* you'll need a custom token class as well (one that contains the
* filename)
*/
class MyAST : public ANTLR_USE_NAMESPACE(antlr)CommonAST {
public:
// copy constructor
MyAST( const MyAST& other )
: CommonAST(other)
, line(other.line), column(other.column)
{
}
// Default constructor
MyAST( void ) : CommonAST(), line(0), column(0) {}
virtual ~MyAST( void ) {}

// get the line number of the node (or try to derive it from the child node
virtual int getLine( void ) const
{
// most of the time the line number is not set if the node is a
// imaginary one. Usually this means it has a child. Refer to the
// child line number. Of course this could be extended a bit.
if ( line != 0 )
return line;
if( getFirstChild() )
return ( RefMyAST(getFirstChild())->getLine() );
return 0;
}


virtual void setLine( int l )
{
line = l;
}

// get the line number of the node (or try to derive it from the child node
virtual int getColumn( void ) const
{
// most of the time the line number is not set if the node is a
// imaginary one. Usually this means it has a child. Refer to the
// child line number. Of course this could be extended a bit.
if ( column != 0 )
return column;
if( getFirstChild() )
return ( RefMyAST(getFirstChild())->getColumn() );
return 0;
}

virtual void setColumn( int c )
{
column = c;
}

/** the initialize methods are called by the tree building constructs
* depending on which version is called the line number is filled in.
* e.g. a bit depending on how the node is constructed it will have the
* line number filled in or not (imaginary nodes!).
*/
virtual void initialize(int t, const ANTLR_USE_NAMESPACE(std)string& txt)
{
CommonAST::initialize(t,txt);
line = 0;
column = 0;
}

virtual void initialize( ANTLR_USE_NAMESPACE(antlr)RefToken t )
{
CommonAST::initialize(t);
line = t->getLine();
column = t->getColumn();
}

virtual void initialize( RefMyAST ast )
{
CommonAST::initialize(ANTLR_USE_NAMESPACE(antlr)RefAST(ast));
line = ast->getLine();
line = ast->getColumn();
}
// for convenience will also work without
void addChild( RefMyAST c )
{
BaseAST::addChild( ANTLR_USE_NAMESPACE(antlr)RefAST(c) );
}
// for convenience will also work without
void setNextSibling( RefMyAST c )
{
BaseAST::setNextSibling( ANTLR_USE_NAMESPACE(antlr)RefAST(c) );
}
// provide a clone of the node (no sibling/child pointers are copied)
virtual ANTLR_USE_NAMESPACE(antlr)RefAST clone( void )
{
return ANTLR_USE_NAMESPACE(antlr)RefAST(new MyAST(*this));
}
static ANTLR_USE_NAMESPACE(antlr)RefAST factory( void )
{
return ANTLR_USE_NAMESPACE(antlr)RefAST(RefMyAST(new MyAST()));
}
private:
int line;
int column;
};

#endif
4 changes: 2 additions & 2 deletions modelica_parser/src/modelica_lexer.g
Expand Up @@ -126,11 +126,11 @@ ML_COMMENT :
protected
ML_COMMENT_CHAR :
("\r\n" | '\n') { newline(); }
| ~('*'|'\n'|'\r')
| ~('*'|'\n'|'\r')
;

SL_COMMENT :
"//" (~('\n' | '\r'))*
"//" (~('\n' | '\r') { newline(); } )*
{ $setType(antlr::Token::SKIP); }
;

Expand Down
11 changes: 8 additions & 3 deletions modelica_parser/src/modelica_parser.g
@@ -1,8 +1,9 @@

header "post_include_hpp" {

#define null 0
#include "MyAST.h"

typedef ANTLR_USE_NAMESPACE(antlr)ASTRefCount<MyAST> RefMyAST;
}

options {
Expand All @@ -12,10 +13,14 @@ options {
class modelica_parser extends Parser;

options {
codeGenMakeSwitchThreshold = 3;
codeGenBitsetTestThreshold = 4;
importVocab = modelica;
defaultErrorHandler = false;
// defaultErrorHandler = false;
k = 2;
buildAST = true;
ASTLabelType = "RefMyAST";

}

tokens {
Expand Down Expand Up @@ -103,7 +108,7 @@ stored_definition :
|
// End handle split models.
(within_clause SEMICOLON!)?
((f:FINAL)? class_definition SEMICOLON!)*
((FINAL)? class_definition SEMICOLON!)*
EOF!
{
#stored_definition = #([STORED_DEFINITION,"STORED_DEFINITION"],
Expand Down

0 comments on commit cfe1606

Please sign in to comment.