pvRequest parsing

This commit is contained in:
Michael Davidsaver
2020-02-28 15:33:26 -08:00
parent 62ba8ceafe
commit 4d7ca87f8a
6 changed files with 420 additions and 6 deletions
+191 -2
View File
@@ -7,6 +7,7 @@
#include <stdexcept>
#include <map>
#include <string>
#include <regex>
#include <pvxs/version.h>
#include <pvxs/client.h>
@@ -80,6 +81,8 @@ void CommonBase::_field(const std::string& s)
if(idx==cur->children.size()) {
cur->addChild(Member(TypeCode::Struct, child));
}
cur = &cur->children[idx];
}
}
@@ -109,12 +112,198 @@ void CommonBase::_record(const std::string& key, const void* value, StoreType vt
req->options[key] = std::move(v);
}
struct PVRParser
{
/* Grammar
*
* PVR :
* | ENT
* | ENT PVR
* ENT : FIELD | RECORD | name
* FIELD : "field" '(' FIELD_LIST ')'
* RECORD : "record" '[' OPTIONS '}'
* FIELD_LIST :
* | name
* | FIELD_LIST ',' name
* OPTIONS ->
* | name '=' name
* | OPTIONS ',' name '=' name
*/
enum token_t {
// terminals
comma = ',',
lp = '(',
rp = ')',
lb = '[',
rb = ']',
eq = '=',
field = 'f',
record = 'r',
name = 'n',
tEOF = -1,
};
std::regex lexer;
token_t lextok = tEOF;
std::string lexval;
const char* input;
CommonBase& target;
PVRParser(CommonBase& target, const char* input)
:lexer(R"re((?:([\[\],\(\)=])|([a-zA-Z0-9_.]+))(.*))re")
// (?: literal | name ) remaining
// \1 \2 \3
,input(input)
,target(target)
{}
void lex()
{
lexval.clear();
if(!*input) {
lextok = tEOF;
return;
}
// skip leading whitespace
while(' '==*input)
input++;
std::cmatch M;
std::regex_match(input, M, lexer);
if(M.empty())
throw std::runtime_error("invalid charactor near: "+std::string(input));
if(M[1].matched) {
lextok = token_t(input[M.position(1)]);
} else if(M[2].matched) {
lexval = M[2].str();
if(lexval=="field") {
lextok = field;
} else if(lexval=="record") {
lextok = record;
} else {
lextok = name;
}
} else {
throw std::logic_error("pvRequest lexer logic error invalid state");
}
if(!M[3].matched)
throw std::logic_error("pvRequest lexer logic error no continuation");
input += M.position(3);
}
void parse()
{
while(input) {
auto start = input;
lex();
if(lextok==tEOF) {
break;
} else if(lextok==field) {
lex();
if(lextok!=lp)
throw std::runtime_error(SB()<<"Expected field( at "<<start);
parse_fields();
if(lextok!=rp)
throw std::runtime_error(SB()<<"Expected field(...) at "<<start);
} else if(lextok==record) {
lex();
if(lextok!=lb)
throw std::runtime_error(SB()<<"Expected record[ at "<<start);
parse_options();
if(lextok!=rb)
throw std::runtime_error(SB()<<"Expected record[...] at "<<start);
} else if(lextok==name) {
// short-hand for field(name)
if(lexval=="field" || lexval=="record")
std::logic_error("pvReq regex alternative order logic error");
target._field(lexval);
} else {
throw std::runtime_error(SB()<<"Expected field|record|name|EOF at "<<start<<" not token="<<lextok<<"("<<lexval<<")");
}
}
}
void parse_fields()
{
do {
lex();
if(lextok==name) {
target._field(lexval);
continue;
} else if(lextok==comma) {
continue;
} else {
break; // caller signals error
}
} while(true);
}
void parse_options()
{
lex();
do {
auto start = input;
std::string key, val;
if(lextok==rb) {
break;
} else if(lextok==name) {
key = lexval;
bool ok = true;
lex();
ok &= lextok==eq;
lex();
ok &= lextok==name;
val = lexval;
if(!ok) {
throw std::runtime_error(SB()<<"Expected K=V or K=V,... at "<<start);
}
target._record(key, &val, StoreType::String);
lex();
if(lextok==comma) {
lex();
continue;
} else {
break; // caller signals error
}
} else {
break; // caller signals error
}
} while(true);
}
};
void CommonBase::_parse(const std::string& req)
{
throw std::logic_error("Not implemented");
PVRParser(*this, req.c_str()).parse();
}
Value CommonBase::_build()
Value CommonBase::_build() const
{
if(!req) {
using namespace pvxs::members;