diff --git a/libwccl/parser/Parser.cpp b/libwccl/parser/Parser.cpp index 0e9643ce7ad232cffe64dc53c44419a5c186f1ff..7a5703c8d0e2e8c58a173bd9c184e0a9113cb00f 100644 --- a/libwccl/parser/Parser.cpp +++ b/libwccl/parser/Parser.cpp @@ -554,4 +554,58 @@ boost::shared_ptr<MatchRule> Parser::parseMatchRule(std::istream& istr) const return res; } +// ---------------------------------------------------------------------------- + +/** + * @desc Parse an entire WCCL file contained in a std::string. + * Converts the string to a stream and calls parseWcclFile with it + * @arg istr input stream with writed rule + * @return the parsed file via a shared pointer + */ +boost::shared_ptr<WcclFile> Parser::parseWcclFile( + const std::string& str) const +{ + std::stringstream ss (std::stringstream::in | std::stringstream::out); + ss << str; + + try { + return this->parseWcclFile(ss); + } + catch (ParserException&) { + throw; + } +} + +/** + * @desc Parse an entire WCCL file. Runs parse_wccl_file rule from the parser grammar. + * @arg istr input stream with writed rule + * @return the parsed file via a shared pointer + */ +boost::shared_ptr<WcclFile> Parser::parseWcclFile(std::istream& istr) const +{ + ANTLRLexer lexer(istr); + ANTLRParser parser(lexer); + boost::shared_ptr<WcclFile> res; + + try { + res = parser.parse_wccl_file(tagset_); + } catch (antlr::MismatchedTokenException &e) { + throw ParserException( + e.getFileLineColumnString() + " " + e.getMessage() + ); + } catch(antlr::NoViableAltException &e) { + throw ParserException( + e.getFileLineColumnString() + " " + e.getMessage() + ); + } catch(antlr::TokenStreamRecognitionException &e) { + throw ParserException( + e.getLine() + ":" + e.getColumn() + std::string(" ") + e.getMessage() + ); + } catch (antlr::ANTLRException& e) { + throw ParserException(e.getMessage()); + } + + return res; +} + } // end Wccl ns diff --git a/libwccl/parser/Parser.h b/libwccl/parser/Parser.h index cc362727e4b08a2727817753532896555edd4756..6873365caf4a787bb1ca46000f7c9256821411ba 100644 --- a/libwccl/parser/Parser.h +++ b/libwccl/parser/Parser.h @@ -20,6 +20,9 @@ // match actions #include <libwccl/ops/matchrule.h> +// match actions +#include <libwccl/wcclfile.h> + // exceptions #include <libwccl/parser/ParserException.h> @@ -88,6 +91,13 @@ public: boost::shared_ptr<MatchRule> parseMatchRule(std::istream& is) const; + // --------------------------------------------------------------------------- + // WCCL file parsing + boost::shared_ptr<WcclFile> + parseWcclFile(const std::string& file_contents_string) const; + boost::shared_ptr<WcclFile> + parseWcclFile(std::istream& is) const; + // --------------------------------------------------------------------------- const Corpus2::Tagset& tagset() const { return tagset_;