Skip to content
Snippets Groups Projects
Commit a4a90dab authored by Paweł Kędzia's avatar Paweł Kędzia
Browse files

Implemented string operators and command line test for them.

Implemented command line test for testing values expression.
parent d8f89ce7
Branches
No related merge requests found
...@@ -18,67 +18,60 @@ Parser::~Parser() ...@@ -18,67 +18,60 @@ Parser::~Parser()
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
/** /**
* @desc Overloaded parsing operator writed in string. Calls base method * @desc Parse string operator writed in std::string. Converts writed operator
* @arg str operator as string * to istream and calling parseStringOperator with istream
* @return call method @see parserOperator(const std::istream&) * @arg str writed operator
* @retrun boost::shared_ptr<Wccl::StrSet>
*/ */
/* boost::shared_ptr<Wccl::Function<Wccl::StrSet> > Parser::parseStringOperator(
std::string Parser::parseOperator(const std::string& str) const const std::string& str) const
{ {
std::stringstream ss (std::stringstream::in | std::stringstream::out); std::stringstream ss (std::stringstream::in | std::stringstream::out);
ss << str; ss << str;
return this->parseOperator(ss); return this->parseStringOperator(ss);
} }
*/
/** /**
* @desc Base method for parsing operator in stream * @desc Parse string operator. Runs parse_string_operator rule
* @arg istr input stream with operator * in the parser grammar.
* @return Operator * @arg istr input stream with writed operator
* @return boost::shared_ptr<Wccl::Function<Wccl::StrSet> > to created operator
*/ */
/* boost::shared_ptr<Wccl::Function<Wccl::StrSet> > Parser::parseStringOperator(
std::string Parser::parseOperator(std::istream& istr) const std::istream& istr) const
{ {
ANTLRLexer lexer(istr); ANTLRLexer lexer(istr);
ANTLRParser parser(lexer); ANTLRParser parser(lexer);
return "Ala ma kota"; return parser.parse_string_operator();
} }
*/
// ----------------------------------------------------------------------------- // ----------------------------------------------------------------------------
/** /**
* @desc Overloaded parsing expression writed in string. Calls base method * @desc Parse values writed in std::string. Converts writed values
* @arg str variables in string * to istream and calling parseValue with istream
* @arg str writed value(s)
* @retrun boost::shared_ptr<Wccl::Value>
*/ */
void Parser::parseExpression(const std::string& str) const boost::shared_ptr<Wccl::Value> Parser::parseValue(const std::string& str) const
{ {
std::stringstream ss (std::stringstream::in | std::stringstream::out); std::stringstream ss (std::stringstream::in | std::stringstream::out);
ss << str; ss << str;
return this->parseExpression(ss); return this->parseValue(ss);
} }
/** /**
* @desc Base method for parsing expression in stream * @desc Parse values. Runs parse_values rule in the parser grammar.
* @arg istr input stream with variable * @arg istr input stream with writed values
* @return Operator * @return boost::shared_ptr<Wccl::Value> to created value
*/ */
void Parser::parseExpression(std::istream& istr) const boost::shared_ptr<Wccl::Value> Parser::parseValue(std::istream& istr) const
{ {
ANTLRLexer lexer(istr); ANTLRLexer lexer(istr);
ANTLRParser parser(lexer); ANTLRParser parser(lexer);
try { return parser.parse_values();
parser.start_rules();
std::cerr << "Syntax ok!" << std::endl;
}
catch (ParserException e) {
std::cerr << e.info() << std::endl;
}
catch (...) {
std::cerr << "Syntax error!" << std::endl;
}
} }
#ifndef LIBWCCL_PARSER_PARSER_H #ifndef LIBWCCL_PARSER_PARSER_H
#define LIBWCCL_PARSER_PARSER_H #define LIBWCCL_PARSER_PARSER_H
#include "ANTLRLexer.hpp"
#include "ANTLRParser.hpp"
// ----------------------------------------------------------------------------
#include <sstream> #include <sstream>
// ----------------------------------------------------------------------------
#include <libcorpus2/tagset.h> #include <libcorpus2/tagset.h>
#include "ANTLRLexer.hpp" // ----------------------------------------------------------------------------
#include "ANTLRParser.hpp" #include <libwccl/variables.h>
#include <libwccl/values/strset.h>
// exceptions
#include <libwccl/parser/ParserException.h> #include <libwccl/parser/ParserException.h>
// <libwccl> // ----------------------------------------------------------------------------
class Parser { class Parser {
public: public:
Parser(const Corpus2::Tagset&); Parser(const Corpus2::Tagset&);
~Parser(); ~Parser();
// -------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// FIXME // methods for parsing string operator
void parseExpression(const std::string&) const; boost::shared_ptr<Wccl::Function<Wccl::StrSet> >
void parseExpression(std::istream& ) const; parseStringOperator(const std::string&) const;
boost::shared_ptr<Wccl::Function<Wccl::StrSet> >
parseStringOperator(std::istream& ) const;
// methods for parsing values
boost::shared_ptr<Wccl::Value> parseValue(const std::string&) const;
boost::shared_ptr<Wccl::Value> parseValue(std::istream& ) const;
private: private:
const Corpus2::Tagset &tagset; const Corpus2::Tagset &tagset;
......
This diff is collapsed.
PROJECT( parser )
include_directories( ${CMAKE_SOURCE_DIR} )
add_definitions(-DLIBWCCL_WCCLPARSER_DATA_DIR="${PROJECT_SOURCE_DIR}/")
add_executable(parser-strop
strop_main.cpp
)
target_link_libraries (parser-strop wccl ${Boost_LIBRARIES} antlr)
add_executable(parser-val
val_main.cpp
)
target_link_libraries (parser-val wccl ${Boost_LIBRARIES} antlr)
include_directories(${Boost_INCLUDE_DIR})
link_directories(${Boost_LIBRARY_DIRS})
#add_custom_target(test tests)
#add_custom_target(test-verbose ./tests --log_level=message)
#include <cstdlib>
#include <libwccl/values/strset.h>
#include <libwccl/parser/Parser.h>
// ----------------------------------------------------------------------------
/**
* @desc It's simple command line tester for testing string operators
*/
int main()
{
std::string str_in;
Corpus2::Tagset tagset;
Parser parser(tagset);
boost::shared_ptr<Wccl::StrSet> retStr;
boost::shared_ptr<Wccl::Function<Wccl::StrSet> > retOp;
boost::shared_ptr<Corpus2::Sentence> sentence;
Wccl::SentenceContext sc(sentence);
while (1) {
std::cerr << "Enter a string operator expression: ";
getline(std::cin, str_in);
if (str_in == "clear" || str_in == "cls") {
if (system("clear")) {
//
}
}
else if (str_in == "exit" || str_in == "quit") {
break;
}
else {
try {
retOp = parser.parseStringOperator(str_in);
if (retOp.get()) {
if ((retStr = retOp->apply(sc)).get()) {
std::cerr << "Parsed expression: " << retStr->to_raw_string() << std::endl;
}
else {
std::cerr << "Problem while parsing -- haven't StrSet object in boost::shared_ptr!" << std::endl;
}
}
else {
std::cerr << "Problem while parsing -- haven't Function<Wccl::StrSet> object in boost::shared_ptr!" << std::endl;
}
}
catch (antlr::MismatchedTokenException &e) {
std::cerr << "Mismatch token exception!" << std::endl;
}
/*
catch (antlr::TokenStreamRecognitionException &e) {
std::cerr << "[2] Syntax error!" << std::endl;
}
*/
catch (...) {
std::cerr << "[N] Syntax error!" << std::endl;
}
}
}
return 0;
}
#include <cstdlib>
#include <libwccl/values/strset.h>
#include <libwccl/parser/Parser.h>
// ----------------------------------------------------------------------------
/**
* @desc It's simple command line tester for testing string operators
*/
int main()
{
std::string str_in;
Corpus2::Tagset tagset;
Parser parser(tagset);
boost::shared_ptr<Wccl::Value> valRet;
while (1) {
std::cerr << "Enter a string operator expression: ";
getline(std::cin, str_in);
if (str_in == "clear" || str_in == "cls") {
if (system("clear")) {
//
}
}
else if (str_in == "exit" || str_in == "quit") {
break;
}
else {
try {
valRet = parser.parseValue(str_in);
/*
if (retOp.get()) {
if ((retStr = retOp->apply(sc)).get()) {
std::cerr << "Parsed expression: " << retStr->to_raw_string() << std::endl;
}
else {
std::cerr << "Problem while parsing -- haven't StrSet object in boost::shared_ptr!" << std::endl;
}
}
else {
std::cerr << "Problem while parsing -- haven't Function<Wccl::StrSet> object in boost::shared_ptr!" << std::endl;
}
*/
}
catch (antlr::MismatchedTokenException &e) {
std::cerr << "Mismatch token exception!" << std::endl;
}
catch (...) {
std::cerr << "[N] Syntax error!" << std::endl;
}
}
}
return 0;
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment