2011-12-14 00:23:17 +03:00
|
|
|
#include "StdInc.h"
|
2011-03-19 19:06:46 +02:00
|
|
|
#include "ERMParser.h"
|
2011-12-14 00:23:17 +03:00
|
|
|
|
2014-11-15 13:10:51 +02:00
|
|
|
|
|
|
|
#include <boost/spirit/include/qi.hpp>
|
|
|
|
#include <boost/spirit/include/phoenix_core.hpp>
|
|
|
|
#include <boost/spirit/include/phoenix_operator.hpp>
|
|
|
|
#include <boost/spirit/include/phoenix_fusion.hpp>
|
|
|
|
#include <boost/spirit/include/phoenix_stl.hpp>
|
|
|
|
#include <boost/spirit/include/phoenix_object.hpp>
|
|
|
|
#include <boost/fusion/include/adapt_struct.hpp>
|
|
|
|
|
|
|
|
|
|
|
|
namespace qi = boost::spirit::qi;
|
|
|
|
namespace ascii = spirit::ascii;
|
|
|
|
namespace phoenix = boost::phoenix;
|
|
|
|
|
|
|
|
|
2011-04-06 23:30:59 +03:00
|
|
|
/*
|
|
|
|
* ERMParser.cpp, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2011-03-23 21:41:29 +02:00
|
|
|
|
|
|
|
//Greenspun's Tenth Rule of Programming:
|
|
|
|
//Any sufficiently complicated C or Fortran program contains an ad hoc, informally-specified,
|
|
|
|
//bug-ridden, slow implementation of half of Common Lisp.
|
|
|
|
//actually these macros help in dealing with boost::variant
|
2011-03-25 00:05:08 +02:00
|
|
|
|
|
|
|
|
2011-04-07 20:54:08 +03:00
|
|
|
CERMPreprocessor::CERMPreprocessor(const std::string &Fname) : fname(Fname), file(Fname.c_str()), lineNo(0), version(INVALID)
|
2011-03-19 19:06:46 +02:00
|
|
|
{
|
|
|
|
if(!file.is_open())
|
|
|
|
{
|
2013-04-11 15:04:44 +03:00
|
|
|
logGlobal->errorStream() << "File " << Fname << " not found or unable to open";
|
2011-03-19 19:06:46 +02:00
|
|
|
return;
|
|
|
|
}
|
2011-04-04 00:38:47 +03:00
|
|
|
|
2011-03-19 19:06:46 +02:00
|
|
|
//check header
|
2011-04-04 00:38:47 +03:00
|
|
|
std::string header;
|
|
|
|
getline(header);
|
|
|
|
|
|
|
|
if(header == "ZVSE")
|
|
|
|
version = ERM;
|
|
|
|
else if(header == "VERM")
|
|
|
|
version = VERM;
|
|
|
|
else
|
2011-03-19 19:06:46 +02:00
|
|
|
{
|
2013-04-11 15:04:44 +03:00
|
|
|
logGlobal->errorStream() << "File " << fname << " has wrong header";
|
2011-03-19 19:06:46 +02:00
|
|
|
return;
|
|
|
|
}
|
2011-04-04 00:38:47 +03:00
|
|
|
}
|
|
|
|
|
2011-04-06 23:30:59 +03:00
|
|
|
class ParseErrorException : public std::exception
|
|
|
|
{
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2011-04-04 00:38:47 +03:00
|
|
|
std::string CERMPreprocessor::retreiveCommandLine()
|
|
|
|
{
|
|
|
|
std::string wholeCommand;
|
|
|
|
|
2011-03-19 19:06:46 +02:00
|
|
|
//parse file
|
2011-04-04 00:38:47 +03:00
|
|
|
bool verm = false;
|
|
|
|
bool openedString = false;
|
|
|
|
int openedBraces = 0;
|
|
|
|
|
|
|
|
|
2011-03-19 19:06:46 +02:00
|
|
|
while(file.good())
|
|
|
|
{
|
2011-04-04 00:38:47 +03:00
|
|
|
|
|
|
|
std::string line ;
|
|
|
|
getline(line); //reading line
|
|
|
|
|
|
|
|
|
2013-06-26 17:25:23 +03:00
|
|
|
size_t dash = line.find_first_of('^');
|
2011-04-04 00:38:47 +03:00
|
|
|
bool inTheMiddle = openedBraces || openedString;
|
|
|
|
|
|
|
|
if(!inTheMiddle)
|
2011-03-19 19:06:46 +02:00
|
|
|
{
|
2011-04-04 00:38:47 +03:00
|
|
|
if(line.size() < 2)
|
|
|
|
continue;
|
|
|
|
if(line[0] != '!' ) //command lines must begin with ! -> otherwise treat as comment
|
|
|
|
continue;
|
|
|
|
verm = line[1] == '[';
|
2011-03-19 19:06:46 +02:00
|
|
|
}
|
|
|
|
|
2011-04-04 00:38:47 +03:00
|
|
|
if(openedString)
|
2011-03-25 22:38:24 +02:00
|
|
|
{
|
2011-04-04 00:38:47 +03:00
|
|
|
wholeCommand += "\\n";
|
|
|
|
if(dash != std::string::npos)
|
2011-03-25 22:38:24 +02:00
|
|
|
{
|
2011-04-04 00:38:47 +03:00
|
|
|
wholeCommand += line.substr(0, dash);
|
|
|
|
line.erase(0,dash);
|
2011-03-25 22:38:24 +02:00
|
|
|
}
|
2011-04-04 00:38:47 +03:00
|
|
|
else //no closing marker -> the whole line is further part of string
|
2011-03-25 22:38:24 +02:00
|
|
|
{
|
2011-04-04 00:38:47 +03:00
|
|
|
wholeCommand += line;
|
|
|
|
continue;
|
2011-03-25 22:38:24 +02:00
|
|
|
}
|
2011-04-04 00:38:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
for(; i < line.length(); i++)
|
|
|
|
{
|
|
|
|
char c = line[i];
|
|
|
|
if(!openedString)
|
2011-03-25 22:38:24 +02:00
|
|
|
{
|
2011-04-04 00:38:47 +03:00
|
|
|
if(c == '[')
|
|
|
|
openedBraces++;
|
|
|
|
else if(c == ']')
|
|
|
|
{
|
|
|
|
openedBraces--;
|
|
|
|
if(!openedBraces) //the last brace has been matched -> stop "parsing", everything else in the line is comment
|
|
|
|
{
|
|
|
|
i++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(c == '^')
|
|
|
|
openedString = true;
|
|
|
|
else if(c == ';') // a ';' that is in command line (and not in string) ends the command -> throw away rest
|
|
|
|
{
|
|
|
|
line.erase(i+!verm, line.length() - i - !verm); //leave ';' at the end only at ERM commands
|
|
|
|
break;
|
|
|
|
}
|
2011-03-25 22:38:24 +02:00
|
|
|
}
|
2011-04-04 00:38:47 +03:00
|
|
|
else if(c == '^')
|
|
|
|
openedString = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(verm && !openedBraces && i < line.length())
|
|
|
|
{
|
|
|
|
line.erase(i, line.length() - i);
|
2011-03-25 22:38:24 +02:00
|
|
|
}
|
2011-03-19 19:06:46 +02:00
|
|
|
|
2011-04-06 23:30:59 +03:00
|
|
|
if(wholeCommand.size()) //separate lines with a space
|
|
|
|
wholeCommand += " ";
|
|
|
|
|
2011-04-04 00:38:47 +03:00
|
|
|
wholeCommand += line;
|
|
|
|
if(!openedBraces && !openedString)
|
|
|
|
return wholeCommand;
|
|
|
|
|
2011-03-19 19:06:46 +02:00
|
|
|
//loop end
|
|
|
|
}
|
2011-04-04 00:38:47 +03:00
|
|
|
|
|
|
|
if(openedBraces || openedString)
|
2013-04-11 15:04:44 +03:00
|
|
|
logGlobal->errorStream() << "Ill-formed file: " << fname;
|
2011-04-04 00:38:47 +03:00
|
|
|
return "";
|
2011-03-19 19:06:46 +02:00
|
|
|
}
|
|
|
|
|
2011-04-04 00:38:47 +03:00
|
|
|
void CERMPreprocessor::getline(std::string &ret)
|
2011-03-19 19:06:46 +02:00
|
|
|
{
|
2011-04-04 00:38:47 +03:00
|
|
|
lineNo++;
|
|
|
|
std::getline(file, ret);
|
|
|
|
boost::trim(ret); //get rid of wspace
|
2011-03-19 19:06:46 +02:00
|
|
|
}
|
|
|
|
|
2011-04-04 00:38:47 +03:00
|
|
|
ERMParser::ERMParser(std::string file)
|
|
|
|
:srcFile(file)
|
|
|
|
{}
|
|
|
|
|
2011-05-11 22:53:55 +03:00
|
|
|
std::vector<LineInfo> ERMParser::parseFile()
|
2011-04-04 00:38:47 +03:00
|
|
|
{
|
|
|
|
CERMPreprocessor preproc(srcFile);
|
2011-05-11 22:53:55 +03:00
|
|
|
std::vector<LineInfo> ret;
|
2011-04-06 23:30:59 +03:00
|
|
|
try
|
2011-03-25 22:38:24 +02:00
|
|
|
{
|
2011-04-06 23:30:59 +03:00
|
|
|
while(1)
|
2011-03-25 22:38:24 +02:00
|
|
|
{
|
2011-04-06 23:30:59 +03:00
|
|
|
std::string command = preproc.retreiveCommandLine();
|
|
|
|
if(command.length() == 0)
|
|
|
|
break;
|
2011-03-25 22:38:24 +02:00
|
|
|
|
2011-04-06 23:30:59 +03:00
|
|
|
repairEncoding(command);
|
2011-05-11 22:53:55 +03:00
|
|
|
LineInfo li;
|
|
|
|
li.realLineNum = preproc.getCurLineNo();
|
|
|
|
li.tl = parseLine(command, li.realLineNum);
|
|
|
|
ret.push_back(li);
|
2011-03-25 00:05:08 +02:00
|
|
|
}
|
2011-03-23 21:41:29 +02:00
|
|
|
}
|
2011-04-06 23:30:59 +03:00
|
|
|
catch (ParseErrorException & e)
|
2011-03-28 22:34:00 +03:00
|
|
|
{
|
2013-04-11 15:04:44 +03:00
|
|
|
logGlobal->errorStream() << "stopped parsing file";
|
2011-04-01 22:09:05 +03:00
|
|
|
}
|
2011-04-06 23:30:59 +03:00
|
|
|
return ret;
|
2011-03-20 20:09:55 +02:00
|
|
|
}
|
|
|
|
|
2011-03-28 22:34:00 +03:00
|
|
|
BOOST_FUSION_ADAPT_STRUCT(
|
|
|
|
ERM::TStringConstant,
|
|
|
|
(std::string, str)
|
|
|
|
)
|
|
|
|
|
|
|
|
BOOST_FUSION_ADAPT_STRUCT(
|
|
|
|
ERM::TMacroUsage,
|
|
|
|
(std::string, macro)
|
|
|
|
)
|
|
|
|
|
2011-05-14 16:20:19 +03:00
|
|
|
// BOOST_FUSION_ADAPT_STRUCT(
|
|
|
|
// ERM::TQMacroUsage,
|
|
|
|
// (std::string, qmacro)
|
|
|
|
// )
|
2011-03-28 22:34:00 +03:00
|
|
|
|
|
|
|
BOOST_FUSION_ADAPT_STRUCT(
|
|
|
|
ERM::TMacroDef,
|
|
|
|
(std::string, macro)
|
|
|
|
)
|
|
|
|
|
2011-03-21 22:34:44 +02:00
|
|
|
BOOST_FUSION_ADAPT_STRUCT(
|
2011-03-27 20:24:30 +03:00
|
|
|
ERM::TVarExpNotMacro,
|
2011-03-28 22:34:00 +03:00
|
|
|
(boost::optional<char>, questionMark)
|
2011-03-27 20:24:30 +03:00
|
|
|
(std::string, varsym)
|
2011-03-28 22:34:00 +03:00
|
|
|
(ERM::TVarExpNotMacro::Tval, val)
|
2011-03-21 22:34:44 +02:00
|
|
|
)
|
|
|
|
|
2011-03-25 22:38:24 +02:00
|
|
|
BOOST_FUSION_ADAPT_STRUCT(
|
|
|
|
ERM::TArithmeticOp,
|
2011-03-28 22:34:00 +03:00
|
|
|
(ERM::TIexp, lhs)
|
2011-03-25 22:38:24 +02:00
|
|
|
(char, opcode)
|
2011-03-28 22:34:00 +03:00
|
|
|
(ERM::TIexp, rhs)
|
|
|
|
)
|
|
|
|
|
|
|
|
BOOST_FUSION_ADAPT_STRUCT(
|
|
|
|
ERM::TVarpExp,
|
2011-05-16 15:11:00 +03:00
|
|
|
(ERM::TVarExp, var)
|
2011-03-25 22:38:24 +02:00
|
|
|
)
|
|
|
|
|
2011-03-27 20:24:30 +03:00
|
|
|
BOOST_FUSION_ADAPT_STRUCT(
|
|
|
|
ERM::TVRLogic,
|
|
|
|
(char, opcode)
|
2011-03-28 22:34:00 +03:00
|
|
|
(ERM::TIexp, var)
|
2011-03-27 20:24:30 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
BOOST_FUSION_ADAPT_STRUCT(
|
|
|
|
ERM::TVRArithmetic,
|
|
|
|
(char, opcode)
|
2011-03-28 22:34:00 +03:00
|
|
|
(ERM::TIexp, rhs)
|
2011-03-27 20:24:30 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
BOOST_FUSION_ADAPT_STRUCT(
|
|
|
|
ERM::TNormalBodyOption,
|
|
|
|
(char, optionCode)
|
|
|
|
(ERM::TNormalBodyOptionList, params)
|
|
|
|
)
|
|
|
|
|
2011-03-20 20:09:55 +02:00
|
|
|
BOOST_FUSION_ADAPT_STRUCT(
|
2011-03-28 22:34:00 +03:00
|
|
|
ERM::Ttrigger,
|
2011-03-27 20:24:30 +03:00
|
|
|
(ERM::TCmdName, name)
|
2011-03-28 22:34:00 +03:00
|
|
|
(boost::optional<ERM::Tidentifier>, identifier)
|
|
|
|
(boost::optional<ERM::Tcondition>, condition)
|
2011-03-20 20:09:55 +02:00
|
|
|
)
|
|
|
|
|
2011-03-25 22:38:24 +02:00
|
|
|
BOOST_FUSION_ADAPT_STRUCT(
|
|
|
|
ERM::TComparison,
|
2011-03-28 22:34:00 +03:00
|
|
|
(ERM::TIexp, lhs)
|
2011-03-25 22:38:24 +02:00
|
|
|
(std::string, compSign)
|
2011-03-28 22:34:00 +03:00
|
|
|
(ERM::TIexp, rhs)
|
2011-03-25 22:38:24 +02:00
|
|
|
)
|
|
|
|
|
2011-03-27 20:24:30 +03:00
|
|
|
BOOST_FUSION_ADAPT_STRUCT(
|
|
|
|
ERM::TSemiCompare,
|
|
|
|
(std::string, compSign)
|
2011-03-28 22:34:00 +03:00
|
|
|
(ERM::TIexp, rhs)
|
2011-03-27 20:24:30 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
BOOST_FUSION_ADAPT_STRUCT(
|
|
|
|
ERM::TCurriedString,
|
2011-03-28 22:34:00 +03:00
|
|
|
(ERM::TIexp, iexp)
|
2011-03-27 20:24:30 +03:00
|
|
|
(ERM::TStringConstant, string)
|
|
|
|
)
|
|
|
|
|
|
|
|
BOOST_FUSION_ADAPT_STRUCT(
|
|
|
|
ERM::TVarConcatString,
|
|
|
|
(ERM::TVarExp, var)
|
|
|
|
(ERM::TStringConstant, string)
|
|
|
|
)
|
|
|
|
|
2011-03-21 22:34:44 +02:00
|
|
|
BOOST_FUSION_ADAPT_STRUCT(
|
2011-03-28 22:34:00 +03:00
|
|
|
ERM::Tcondition,
|
2011-03-25 00:05:08 +02:00
|
|
|
(char, ctype)
|
2011-03-28 22:34:00 +03:00
|
|
|
(ERM::Tcondition::Tcond, cond)
|
|
|
|
(ERM::TconditionNode, rhs)
|
2011-03-21 22:34:44 +02:00
|
|
|
)
|
|
|
|
|
2011-03-20 20:09:55 +02:00
|
|
|
BOOST_FUSION_ADAPT_STRUCT(
|
2011-03-28 22:34:00 +03:00
|
|
|
ERM::Tinstruction,
|
2011-03-27 20:24:30 +03:00
|
|
|
(ERM::TCmdName, name)
|
2011-03-28 22:34:00 +03:00
|
|
|
(boost::optional<ERM::Tidentifier>, identifier)
|
|
|
|
(boost::optional<ERM::Tcondition>, condition)
|
|
|
|
(ERM::Tbody, body)
|
2011-03-20 20:09:55 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
BOOST_FUSION_ADAPT_STRUCT(
|
2011-03-28 22:34:00 +03:00
|
|
|
ERM::Treceiver,
|
2011-03-27 20:24:30 +03:00
|
|
|
(ERM::TCmdName, name)
|
2011-03-28 22:34:00 +03:00
|
|
|
(boost::optional<ERM::Tidentifier>, identifier)
|
|
|
|
(boost::optional<ERM::Tcondition>, condition)
|
|
|
|
(boost::optional<ERM::Tbody>, body)
|
2011-03-20 20:09:55 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
BOOST_FUSION_ADAPT_STRUCT(
|
2011-03-28 22:34:00 +03:00
|
|
|
ERM::TPostTrigger,
|
|
|
|
(ERM::TCmdName, name)
|
|
|
|
(boost::optional<ERM::Tidentifier>, identifier)
|
|
|
|
(boost::optional<ERM::Tcondition>, condition)
|
2011-03-20 20:09:55 +02:00
|
|
|
)
|
|
|
|
|
2011-03-23 21:41:29 +02:00
|
|
|
BOOST_FUSION_ADAPT_STRUCT(
|
2011-03-28 22:34:00 +03:00
|
|
|
ERM::Tcommand,
|
|
|
|
(ERM::Tcommand::Tcmd, cmd)
|
2011-03-23 21:41:29 +02:00
|
|
|
(std::string, comment)
|
|
|
|
)
|
|
|
|
|
2011-04-01 22:09:05 +03:00
|
|
|
BOOST_FUSION_ADAPT_STRUCT(
|
|
|
|
ERM::TVExp,
|
2011-04-03 16:15:07 +03:00
|
|
|
(std::vector<ERM::TVModifier>, modifier)
|
2011-04-01 22:09:05 +03:00
|
|
|
(std::vector<ERM::TVOption>, children)
|
|
|
|
)
|
|
|
|
|
|
|
|
BOOST_FUSION_ADAPT_STRUCT(
|
|
|
|
ERM::TSymbol,
|
2011-04-03 16:15:07 +03:00
|
|
|
(std::vector<ERM::TVModifier>, symModifier)
|
2011-04-01 22:09:05 +03:00
|
|
|
(std::string, sym)
|
|
|
|
)
|
|
|
|
|
2011-03-20 20:09:55 +02:00
|
|
|
namespace ERM
|
|
|
|
{
|
|
|
|
template<typename Iterator>
|
2011-04-01 22:09:05 +03:00
|
|
|
struct ERM_grammar : qi::grammar<Iterator, TLine(), ascii::space_type>
|
2011-03-20 20:09:55 +02:00
|
|
|
{
|
2011-04-01 22:09:05 +03:00
|
|
|
ERM_grammar() : ERM_grammar::base_type(vline, "VERM script line")
|
2011-03-20 20:09:55 +02:00
|
|
|
{
|
2011-03-27 20:24:30 +03:00
|
|
|
//do not build too complicated expressions, e.g. (a >> b) | c, qi has problems with them
|
2011-04-06 23:30:59 +03:00
|
|
|
ERMmacroUsage %= qi::lexeme[qi::lit('$') >> *(qi::char_ - '$') >> qi::lit('$')];
|
|
|
|
ERMmacroDef %= qi::lexeme[qi::lit('@') >> *(qi::char_ - '@') >> qi::lit('@')];
|
2011-03-28 22:34:00 +03:00
|
|
|
varExpNotMacro %= -qi::char_("?") >> (+(qi::char_("a-z") - 'u')) >> -qi::int_;
|
2011-05-14 16:20:19 +03:00
|
|
|
//TODO: mixed var/macro expressions like in !!HE-1&407:Id$cost$; [script 13]
|
|
|
|
/*qERMMacroUsage %= qi::lexeme[qi::lit("?$") >> *(qi::char_ - '$') >> qi::lit('$')];*/
|
2011-04-06 23:30:59 +03:00
|
|
|
varExp %= varExpNotMacro | ERMmacroUsage;
|
2011-03-27 20:24:30 +03:00
|
|
|
iexp %= varExp | qi::int_;
|
2011-05-16 15:11:00 +03:00
|
|
|
varp %= qi::lit("?") >> varExp;
|
2011-03-28 22:34:00 +03:00
|
|
|
comment %= *qi::char_;
|
2011-04-03 16:15:07 +03:00
|
|
|
commentLine %= (~qi::char_("!") >> comment | (qi::char_('!') >> (~qi::char_("?!$#[")) >> comment ));
|
2011-03-27 20:24:30 +03:00
|
|
|
cmdName %= qi::lexeme[qi::repeat(2)[qi::char_]];
|
2011-03-25 22:38:24 +02:00
|
|
|
arithmeticOp %= iexp >> qi::char_ >> iexp;
|
|
|
|
//identifier is usually a vector of i-expressions but VR receiver performs arithmetic operations on it
|
|
|
|
identifier %= (iexp | arithmeticOp) % qi::lit('/');
|
|
|
|
comparison %= iexp >> (*qi::char_("<=>")) >> iexp;
|
|
|
|
condition %= qi::char_("&|X/") >> (comparison | qi::int_) >> -condition;
|
2011-03-21 22:34:44 +02:00
|
|
|
|
|
|
|
trigger %= cmdName >> -identifier >> -condition > qi::lit(";"); /////
|
|
|
|
string %= qi::lexeme['^' >> *(qi::char_ - '^') >> '^'];
|
2011-03-27 20:24:30 +03:00
|
|
|
|
|
|
|
VRLogic %= qi::char_("&|X") >> iexp;
|
|
|
|
VRarithmetic %= qi::char_("+*:/%-") >> iexp;
|
2011-05-14 16:20:19 +03:00
|
|
|
semiCompare %= +qi::char_("<=>") >> iexp;
|
2011-03-27 20:24:30 +03:00
|
|
|
curStr %= iexp >> string;
|
|
|
|
varConcatString %= varExp >> qi::lit("+") >> string;
|
2011-05-16 15:11:00 +03:00
|
|
|
bodyOptionItem %= varConcatString | curStr | string | semiCompare | ERMmacroDef | varp | iexp | qi::eps;
|
2011-03-27 20:24:30 +03:00
|
|
|
exactBodyOptionList %= (bodyOptionItem % qi::lit("/"));
|
2011-03-28 22:34:00 +03:00
|
|
|
normalBodyOption = qi::char_("A-Z+") > exactBodyOptionList;
|
2011-03-27 20:24:30 +03:00
|
|
|
bodyOption %= VRLogic | VRarithmetic | normalBodyOption;
|
|
|
|
body %= qi::lit(":") >> +(bodyOption) > qi::lit(";");
|
|
|
|
|
2011-03-20 20:09:55 +02:00
|
|
|
instruction %= cmdName >> -identifier >> -condition >> body;
|
2011-03-28 22:34:00 +03:00
|
|
|
receiver %= cmdName >> -identifier >> -condition >> -body; //receiver without body exists... change needed
|
|
|
|
postTrigger %= cmdName >> -identifier >> -condition > qi::lit(";");
|
|
|
|
|
2011-03-23 21:41:29 +02:00
|
|
|
command %= (qi::lit("!") >>
|
2011-03-20 20:09:55 +02:00
|
|
|
(
|
2011-03-23 21:41:29 +02:00
|
|
|
(qi::lit("?") >> trigger) |
|
2011-03-27 20:24:30 +03:00
|
|
|
(qi::lit("!") >> receiver) |
|
2011-03-25 22:38:24 +02:00
|
|
|
(qi::lit("#") >> instruction) |
|
2011-03-28 22:34:00 +03:00
|
|
|
(qi::lit("$") >> postTrigger)
|
2011-03-20 20:09:55 +02:00
|
|
|
) >> comment
|
|
|
|
);
|
|
|
|
|
|
|
|
rline %=
|
|
|
|
(
|
2011-03-23 21:41:29 +02:00
|
|
|
command | commentLine | spirit::eps
|
2011-04-01 22:09:05 +03:00
|
|
|
);
|
|
|
|
|
2011-04-06 23:30:59 +03:00
|
|
|
vmod %= qi::string("`") | qi::string(",!") | qi::string(",") | qi::string("#'") | qi::string("'");
|
2011-04-03 16:15:07 +03:00
|
|
|
vsym %= *vmod >> qi::lexeme[+qi::char_("+*/$%&_=<>~a-zA-Z0-9-")];
|
2011-04-01 22:09:05 +03:00
|
|
|
|
2011-04-02 21:06:52 +03:00
|
|
|
qi::real_parser<double, qi::strict_real_policies<double> > strict_double;
|
|
|
|
vopt %= qi::lexeme[(qi::lit("!") >> qi::char_ >> qi::lit("!"))] | qi::lexeme[strict_double] | qi::lexeme[qi::int_] | command | vexp | string | vsym;
|
2011-04-03 16:15:07 +03:00
|
|
|
vexp %= *vmod >> qi::lit("[") >> *(vopt) >> qi::lit("]");
|
2011-04-01 22:09:05 +03:00
|
|
|
|
2011-04-03 16:15:07 +03:00
|
|
|
vline %= (( qi::lit("!") >>vexp) | rline ) > spirit::eoi;
|
2011-03-20 20:09:55 +02:00
|
|
|
|
|
|
|
//error handling
|
|
|
|
|
|
|
|
string.name("string constant");
|
2011-04-06 23:30:59 +03:00
|
|
|
ERMmacroUsage.name("macro usage");
|
2011-05-14 16:20:19 +03:00
|
|
|
/*qERMMacroUsage.name("macro usage with ?");*/
|
2011-04-06 23:30:59 +03:00
|
|
|
ERMmacroDef.name("macro definition");
|
|
|
|
varExpNotMacro.name("variable expression (not macro)");
|
|
|
|
varExp.name("variable expression");
|
2011-03-21 22:34:44 +02:00
|
|
|
iexp.name("i-expression");
|
2011-03-20 20:09:55 +02:00
|
|
|
comment.name("comment");
|
|
|
|
commentLine.name("comment line");
|
|
|
|
cmdName.name("name of a command");
|
|
|
|
identifier.name("identifier");
|
|
|
|
condition.name("condition");
|
|
|
|
trigger.name("trigger");
|
|
|
|
body.name("body");
|
|
|
|
instruction.name("instruction");
|
|
|
|
receiver.name("receiver");
|
2011-03-28 22:34:00 +03:00
|
|
|
postTrigger.name("post trigger");
|
2011-03-20 20:09:55 +02:00
|
|
|
command.name("command");
|
2011-04-01 22:09:05 +03:00
|
|
|
rline.name("ERM script line");
|
|
|
|
vsym.name("V symbol");
|
|
|
|
vopt.name("V option");
|
|
|
|
vexp.name("V expression");
|
|
|
|
vline.name("VERM line");
|
2011-03-20 20:09:55 +02:00
|
|
|
|
|
|
|
qi::on_error<qi::fail>
|
|
|
|
(
|
2011-04-01 22:09:05 +03:00
|
|
|
vline
|
2011-03-20 20:09:55 +02:00
|
|
|
, std::cout //or phoenix::ref(std::count), is there any difference?
|
|
|
|
<< phoenix::val("Error! Expecting ")
|
|
|
|
<< qi::_4 // what failed?
|
|
|
|
<< phoenix::val(" here: \"")
|
|
|
|
<< phoenix::construct<std::string>(qi::_3, qi::_2) // iterators to error-pos, end
|
|
|
|
<< phoenix::val("\"")
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-03-27 20:24:30 +03:00
|
|
|
qi::rule<Iterator, TStringConstant(), ascii::space_type> string;
|
|
|
|
|
2011-04-06 23:30:59 +03:00
|
|
|
qi::rule<Iterator, TMacroUsage(), ascii::space_type> ERMmacroUsage;
|
2011-05-14 16:20:19 +03:00
|
|
|
/*qi::rule<Iterator, TQMacroUsage(), ascii::space_type> qERMMacroUsage;*/
|
2011-04-06 23:30:59 +03:00
|
|
|
qi::rule<Iterator, TMacroDef(), ascii::space_type> ERMmacroDef;
|
2011-03-27 20:24:30 +03:00
|
|
|
qi::rule<Iterator, TVarExpNotMacro(), ascii::space_type> varExpNotMacro;
|
|
|
|
qi::rule<Iterator, TVarExp(), ascii::space_type> varExp;
|
2011-03-28 22:34:00 +03:00
|
|
|
qi::rule<Iterator, TIexp(), ascii::space_type> iexp;
|
2011-03-27 20:24:30 +03:00
|
|
|
qi::rule<Iterator, TVarpExp(), ascii::space_type> varp;
|
|
|
|
qi::rule<Iterator, TArithmeticOp(), ascii::space_type> arithmeticOp;
|
|
|
|
qi::rule<Iterator, std::string(), ascii::space_type> comment;
|
|
|
|
qi::rule<Iterator, std::string(), ascii::space_type> commentLine;
|
|
|
|
qi::rule<Iterator, TCmdName(), ascii::space_type> cmdName;
|
2011-03-28 22:34:00 +03:00
|
|
|
qi::rule<Iterator, Tidentifier(), ascii::space_type> identifier;
|
2011-03-27 20:24:30 +03:00
|
|
|
qi::rule<Iterator, TComparison(), ascii::space_type> comparison;
|
2011-03-28 22:34:00 +03:00
|
|
|
qi::rule<Iterator, Tcondition(), ascii::space_type> condition;
|
2011-03-27 20:24:30 +03:00
|
|
|
qi::rule<Iterator, TVRLogic(), ascii::space_type> VRLogic;
|
|
|
|
qi::rule<Iterator, TVRArithmetic(), ascii::space_type> VRarithmetic;
|
|
|
|
qi::rule<Iterator, TSemiCompare(), ascii::space_type> semiCompare;
|
|
|
|
qi::rule<Iterator, TCurriedString(), ascii::space_type> curStr;
|
|
|
|
qi::rule<Iterator, TVarConcatString(), ascii::space_type> varConcatString;
|
|
|
|
qi::rule<Iterator, TBodyOptionItem(), ascii::space_type> bodyOptionItem;
|
|
|
|
qi::rule<Iterator, TNormalBodyOptionList(), ascii::space_type> exactBodyOptionList;
|
|
|
|
qi::rule<Iterator, TNormalBodyOption(), ascii::space_type> normalBodyOption;
|
|
|
|
qi::rule<Iterator, TBodyOption(), ascii::space_type> bodyOption;
|
2011-03-28 22:34:00 +03:00
|
|
|
qi::rule<Iterator, Ttrigger(), ascii::space_type> trigger;
|
|
|
|
qi::rule<Iterator, Tbody(), ascii::space_type> body;
|
|
|
|
qi::rule<Iterator, Tinstruction(), ascii::space_type> instruction;
|
|
|
|
qi::rule<Iterator, Treceiver(), ascii::space_type> receiver;
|
|
|
|
qi::rule<Iterator, TPostTrigger(), ascii::space_type> postTrigger;
|
|
|
|
qi::rule<Iterator, Tcommand(), ascii::space_type> command;
|
2011-04-01 22:09:05 +03:00
|
|
|
qi::rule<Iterator, TERMline(), ascii::space_type> rline;
|
|
|
|
qi::rule<Iterator, TSymbol(), ascii::space_type> vsym;
|
2011-04-02 21:06:52 +03:00
|
|
|
qi::rule<Iterator, TVModifier(), ascii::space_type> vmod;
|
2011-04-01 22:09:05 +03:00
|
|
|
qi::rule<Iterator, TVOption(), ascii::space_type> vopt;
|
|
|
|
qi::rule<Iterator, TVExp(), ascii::space_type> vexp;
|
|
|
|
qi::rule<Iterator, TLine(), ascii::space_type> vline;
|
2011-03-20 20:09:55 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2011-05-11 22:53:55 +03:00
|
|
|
ERM::TLine ERMParser::parseLine( const std::string & line, int realLineNo )
|
2011-06-24 00:42:30 +03:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return parseLine(line);
|
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
2013-04-11 15:04:44 +03:00
|
|
|
logGlobal->errorStream() << "Parse error occurred in file " << srcFile << " (line " << realLineNo << ") :" << line;
|
2011-06-24 00:42:30 +03:00
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ERM::TLine ERMParser::parseLine(const std::string & line)
|
2011-03-19 19:06:46 +02:00
|
|
|
{
|
2011-03-20 20:09:55 +02:00
|
|
|
std::string::const_iterator beg = line.begin(),
|
2011-03-19 19:06:46 +02:00
|
|
|
end = line.end();
|
2011-03-20 20:09:55 +02:00
|
|
|
|
|
|
|
ERM::ERM_grammar<std::string::const_iterator> ERMgrammar;
|
2011-04-01 22:09:05 +03:00
|
|
|
ERM::TLine AST;
|
2011-03-20 20:09:55 +02:00
|
|
|
|
2011-04-06 23:30:59 +03:00
|
|
|
bool r = qi::phrase_parse(beg, end, ERMgrammar, ascii::space, AST);
|
|
|
|
if(!r || beg != end)
|
|
|
|
{
|
2013-04-11 15:04:44 +03:00
|
|
|
logGlobal->errorStream() << "Parse error: cannot parse: " << std::string(beg, end);
|
2011-04-06 23:30:59 +03:00
|
|
|
throw ParseErrorException();
|
|
|
|
}
|
|
|
|
return AST;
|
2011-03-25 22:38:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int ERMParser::countHatsBeforeSemicolon( const std::string & line ) const
|
|
|
|
{
|
|
|
|
//CHECK: omit macros? or anything else?
|
|
|
|
int numOfHats = 0; //num of '^' before ';'
|
|
|
|
//check for unmatched ^
|
2013-06-29 16:05:48 +03:00
|
|
|
for (char c : line)
|
2011-03-25 22:38:24 +02:00
|
|
|
{
|
|
|
|
if(c == ';')
|
|
|
|
break;
|
|
|
|
if(c == '^')
|
|
|
|
++numOfHats;
|
2011-03-25 00:05:08 +02:00
|
|
|
}
|
2011-03-25 22:38:24 +02:00
|
|
|
return numOfHats;
|
2011-03-19 19:06:46 +02:00
|
|
|
}
|
2011-03-28 22:34:00 +03:00
|
|
|
|
|
|
|
void ERMParser::repairEncoding( std::string & str ) const
|
|
|
|
{
|
|
|
|
for(int g=0; g<str.size(); ++g)
|
|
|
|
if(str[g] & 0x80)
|
|
|
|
str[g] = '|';
|
|
|
|
}
|
|
|
|
|
|
|
|
void ERMParser::repairEncoding( char * str, int len ) const
|
|
|
|
{
|
|
|
|
for(int g=0; g<len; ++g)
|
|
|
|
if(str[g] & 0x80)
|
|
|
|
str[g] = '|';
|
2013-11-03 15:51:25 +03:00
|
|
|
}
|