1 /*************************************************************************** 2 getfmtast.cpp - returns the AST for formatted IO 3 ------------------- 4 begin : July 22 2002 5 copyright : (C) 2002 by Marc Schellens 6 email : m_schellens@users.sf.net 7 ***************************************************************************/ 8 9 /*************************************************************************** 10 * * 11 * This program is free software; you can redistribute it and/or modify * 12 * it under the terms of the GNU General Public License as published by * 13 * the Free Software Foundation; either version 2 of the License, or * 14 * (at your option) any later version. * 15 * * 16 ***************************************************************************/ 17 18 //#define FMT_DEBUG 19 #undef FMT_DEBUG 20 21 #include "includefirst.hpp" 22 23 #include "basegdl.hpp" 24 #include "fmtnode.hpp" 25 #include "print_tree.hpp" 26 27 #include "FMTLexer.hpp" 28 #include "FMTParser.hpp" 29 30 #include <antlr/ASTFactory.hpp> 31 32 using namespace std; 33 34 antlr::ASTFactory FMTNodeFactory("FMTNode",FMTNode::factory); 35 GetFMTAST(DString fmtString)36RefFMTNode GetFMTAST( DString fmtString) 37 { 38 istringstream istr(fmtString); //+"\n"); 39 40 RefFMTNode fmtAST; 41 try { 42 antlr::TokenStreamSelector selector; 43 44 FMTLexer lexer( istr); 45 lexer.SetSelector( selector); 46 47 CFMTLexer cLexer( lexer.getInputState()); 48 cLexer.SetSelector( selector); 49 50 lexer.SetCLexer( cLexer); 51 52 selector.select( &lexer); 53 54 FMTParser parser( selector); 55 56 // because we use the standard (ANTLR generated) constructor here 57 // we cannot do it in the constructor 58 parser.initializeASTFactory( FMTNodeFactory); 59 parser.setASTFactory( &FMTNodeFactory ); 60 61 parser.format( 1); 62 63 fmtAST=parser.getAST(); 64 65 #ifdef FMT_DEBUG 66 antlr::print_tree pt; 67 pt.pr_tree(static_cast<antlr::RefAST>(fmtAST)); 68 cout << endl; 69 #endif 70 } 71 catch( GDLException& ex) 72 { 73 throw GDLException("Format: "+ex.getMessage()); 74 } 75 catch( antlr::ANTLRException& ex) 76 { 77 throw GDLException("Format parser: "+ex.getMessage()); 78 } 79 catch( exception& ex) 80 { 81 throw GDLException("Format exception: "+string(ex.what())); 82 } 83 catch(...) 84 { 85 throw GDLException("Format: general exception."); 86 } 87 88 return fmtAST; 89 } 90