8#include "antlr/CommonAST.hpp"
9#include "antlr/ANTLRException.hpp"
10#include "antlr/IOException.hpp"
11#include "antlr/ASTFactory.hpp"
12#include "antlr/ANTLRUtil.hpp"
19#ifdef ANTLR_CXX_SUPPORTS_NAMESPACE
40: default_factory_descriptor(ANTLR_USE_NAMESPACE(std)make_pair(
CommonAST::TYPE_NAME,&
CommonAST::factory))
42 nodeFactories.resize( Token::MIN_USER_TYPE, &default_factory_descriptor );
50: default_factory_descriptor(ANTLR_USE_NAMESPACE(std)make_pair(factory_node_name, fact))
52 nodeFactories.resize( Token::MIN_USER_TYPE, &default_factory_descriptor );
58 factory_descriptor_list::iterator i = nodeFactories.begin();
60 while( i != nodeFactories.end() )
62 if( *i != &default_factory_descriptor )
72 if( type < Token::MIN_USER_TYPE )
73 throw ANTLRException(
"Internal parser error invalid type passed to RegisterFactory");
75 throw ANTLRException(
"Internal parser error 0 factory passed to RegisterFactory");
79 if( nodeFactories.size() < (
static_cast<unsigned int>(type)+1) )
80 nodeFactories.resize( type+1, &default_factory_descriptor );
83 nodeFactories[type] =
new ANTLR_USE_NAMESPACE(std)pair<const char*, factory_type>( ast_name, factory );
88 if( nodeFactories.size() < (
static_cast<unsigned int>(type)+1) )
89 nodeFactories.resize( type+1, &default_factory_descriptor );
97 RefAST node = nodeFactories[0]->second();
98 node->
setType(Token::INVALID_TYPE);
104 RefAST t = nodeFactories[type]->second();
111 RefAST t = nodeFactories[type]->second();
116#ifdef ANTLR_SUPPORT_XML
119 factory_descriptor_list::iterator fact = nodeFactories.begin();
121 while( fact != nodeFactories.end() )
123 if( type_name == (*fact)->first )
125 RefAST t = (*fact)->second();
132 string error =
"ASTFactory::create: Unknown AST type '" + type_name +
"'";
133 throw ANTLRException(error);
155 RefAST t = nodeFactories[tok->getType()]->second();
165 if (!currentAST.root)
168 currentAST.root = child;
172 if (!currentAST.child)
183 currentAST.child = child;
196 return RefAST(nullASTptr);
234 if ( nodes.size() == 0 )
235 return RefAST(nullASTptr);
244 for(
unsigned int i = 1; i < nodes.size(); i++ )
250 root = tail = nodes[i];
251 else if ( tail == 0 )
291 currentAST.child = currentAST.root;
294 currentAST.root = root;
299 factory_type factory )
301 default_factory_descriptor.first = factory_node_name;
302 default_factory_descriptor.second = factory;
305#ifdef ANTLR_SUPPORT_XML
306bool ASTFactory::checkCloseTag( ANTLR_USE_NAMESPACE(std)istream& in )
334void ASTFactory::loadChildren( ANTLR_USE_NAMESPACE(std)istream& infile,
346 string error =
"Invalid XML file... no '<' found (";
348 throw IOException(error);
358 temp = read_identifier( infile );
360 if( strcmp(temp.c_str(), current->typeName() ) != 0 )
362 string error =
"Invalid XML file... close tag does not match start tag: ";
363 error += current->typeName();
364 error +=
" closed by " + temp;
365 throw IOException(error);
372 string error =
"Invalid XML file... no '>' found (";
374 throw IOException(error);
385 RefAST child = LoadAST(infile);
387 current->addChild( child );
391void ASTFactory::loadSiblings(ANTLR_USE_NAMESPACE(std)istream& infile,
401 if( checkCloseTag(infile) )
404 RefAST sibling = LoadAST(infile);
405 current->setNextSibling(sibling);
409RefAST ASTFactory::LoadAST( ANTLR_USE_NAMESPACE(std)istream& infile )
411 RefAST current = nullAST;
416 if( !infile.get(ch) )
421 string error =
"Invalid XML file... no '<' found (";
423 throw IOException(error);
426 string ast_type = read_identifier(infile);
429 current =
create( ast_type, infile );
430 if( current == nullAST )
432 string error =
"Unsuported AST type: " + ast_type;
433 throw IOException(error);
448 string error =
"Invalid XML file... no '>' found after '/' (";
450 throw IOException(error);
454 loadSiblings( infile, current );
462 string error =
"Invalid XML file... no '>' found (";
464 throw IOException(error);
468 loadChildren( infile, current );
471 loadSiblings( infile, current );
477#ifdef ANTLR_CXX_SUPPORTS_NAMESPACE
ASTArray is a class that allows ANTLR to generate code that can create and initialize an array in one...
void addASTChild(ASTPair ¤tAST, RefAST child)
Add a child to the current AST.
void makeASTRoot(ASTPair ¤tAST, RefAST root)
Make an AST the root of current AST.
void setASTNodeFactory(const char *factory_node_name, factory_type factory)
Set a new default AST type.
virtual RefAST create()
Create new empty AST node. The right default type shou.
ASTFactory()
Make new factory. Per default (Ref)CommonAST instances are generated.
RefAST dupTree(RefAST t)
Duplicate a tree, assuming this is a root node of a tree– duplicate that node and what's below; ignor...
void registerFactory(int type, const char *ast_name, factory_type factory)
Register a node factory for the node type type with name ast_name.
RefAST dupList(RefAST t)
Duplicate tree including siblings of root.
RefAST make(std ::vector< RefAST > &nodes)
Make a tree from a list of nodes.
RefAST dup(RefAST t)
Deep copy a single node.
virtual ~ASTFactory()
Destroy factory.
void setMaxNodeType(int type)
Set the maximum node (AST) type this factory may encounter.
virtual RefAST getFirstChild() const =0
Get the first child of this node; null if no children.
virtual RefAST getNextSibling() const =0
Get the next sibling in line after this one.
virtual int getType() const =0
Get the token type for this node.
virtual void initialize(int t, const std ::string &txt)=0
Various initialization routines.
virtual void setType(int type)=0
Set the token type for this node.
virtual void setNextSibling(RefAST n)=0
Set the next sibling after this one.
virtual void addChild(RefAST c)=0
Add a node to the end of the child list for this node.
virtual void setFirstChild(RefAST c)=0
Set the first child of a node.
virtual RefAST clone(void) const =0
Clone this AST node.
ASTPair: utility class used for manipulating a pair of ASTs representing the current AST root and cur...
void advanceChildToEnd()
Make sure that child is the last sibling.