diff --git a/acat/src/acat.cpp b/acat/src/acat.cpp index cafee323694a139eb961c1ff05abe641c151c877..f1ab25b513465116282423a2509d52e3f192e242 100644 --- a/acat/src/acat.cpp +++ b/acat/src/acat.cpp @@ -30,35 +30,67 @@ using namespace alib; using namespace sax; using namespace grammar; +void getAutomaton(list<Token>& tokens, bool complexTypes) { + UnknownAutomaton automaton = AutomatonParser::parse(tokens); + + if(complexTypes) { + Automaton* concreateAutomaton = AutomatonFactory::buildAutomaton(automaton); + concreateAutomaton->toXML(cout); + } else { + automaton.toXML(cout); + } +} + +void getGrammar(list<Token>& tokens) { + UnknownGrammar grammar = GrammarParser::parse(tokens); + grammar.toXML(cout); +} + +void getRegExp(list<Token>& tokens) { + RegExp regexp = RegExpParser::parse(tokens); + regexp.toXML(cout); +} + int main(int argc, char** argv) { - list<Token> tokens; + + int fileParameterIndex = -1; + bool complexTypes = false; + try { if (argc > 1) { - if (string("-h").compare(argv[1]) == 0) { - cout << "Automaton parsing.\nUsage: catPDA [automaton.xml]\n"; - return -1; + for (int i = 1; i < argc; i++) { + if (string("-h").compare(argv[i]) == 0) { + cout << "Automaton parsing.\nUsage: catPDA [automaton.xml]\n"; + return -1; + } else if (string("-c").compare(argv[i]) == 0) { + complexTypes = true; + } else { + if(fileParameterIndex == -1) { + fileParameterIndex = i; + } else { + throw AlibException("Only one file can be passed as parameter - " + string(argv[i]) + " " + string(argv[fileParameterIndex])); + } + } } + } + + list<Token> tokens; - SaxInterface::parseFile(argv[1], tokens); + if(fileParameterIndex != -1) { + SaxInterface::parseFile(argv[fileParameterIndex],tokens); } else { - string input(istreambuf_iterator<char>(cin), - (istreambuf_iterator<char>())); + string input(istreambuf_iterator<char>(cin), (istreambuf_iterator<char>())); SaxInterface::parseMemory(input, tokens); } if (tokens.front().getData() == "automaton") { - UnknownAutomaton automaton = AutomatonParser::parse(tokens); - automaton.toXML(cout); + getAutomaton(tokens, complexTypes); } else if (tokens.front().getData() == "grammar") { - UnknownGrammar grammar = GrammarParser::parse(tokens); - grammar.toXML(cout); + getGrammar(tokens); } else if (tokens.front().getData() == "regexp") { - RegExp regexp = RegExpParser::parse(tokens); - regexp.toXML(cout); + getRegExp(tokens); } else { - throw AlibException( - "Expected root tag automaton or grammar. Read: " - + tokens.front().getData()); + throw AlibException("Expected root tag automaton, grammar or regexp. Read: " + tokens.front().getData()); } } catch (AlibException& e) { diff --git a/alib/src/sax/SaxInterface.cpp b/alib/src/sax/SaxInterface.cpp index 6e8b4b0ba034f806a40e927e09c11617eaa44ba1..fb5b28b5ecd0e398bf55ca21cc6705b84dcf3b4a 100644 --- a/alib/src/sax/SaxInterface.cpp +++ b/alib/src/sax/SaxInterface.cpp @@ -9,8 +9,10 @@ #include <string> #include <cstring> +#include "../AlibException.h" namespace sax { + void SaxInterface::initSAXHandler(xmlSAXHandler& handler) { memset(&handler, 0, sizeof(handler)); handler.initialized = XML_SAX2_MAGIC; @@ -34,9 +36,13 @@ void SaxInterface::parseFile(std::string filename, std::list<Token>& out) { xmlSAXHandler handler; initSAXHandler(handler); - xmlSAXUserParseFile(&handler, (void*) &out, filename.c_str()); - + int result = xmlSAXUserParseFile(&handler, (void*) &out, filename.c_str()); xmlCleanupParser(); + + if(result != 0) { + throw alib::AlibException("Cannot parse the XML file " + filename); + } + } void SaxInterface::characters(void * user_data, const xmlChar * ch, int len) {