Commit d6746cb5 authored by Simon Morlat's avatar Simon Morlat

work in progress

parent 869290ac
......@@ -136,6 +136,9 @@ ABNFGrammar::ABNFGrammar(): Grammar("ABNF"){
c_nl();
c_wsp();
rulename();
repeat_count();
repeat_max();
repeat_min();
repeat();
defined_as();
rulelist();
......@@ -193,21 +196,35 @@ void ABNFGrammar::rulename(){
);
}
void ABNFGrammar::repeat_count(){
addRule("repeat-count", Foundation::loop()->setRecognizer(getRule("digit"),1));
}
void ABNFGrammar::repeat_max(){
addRule("repeat-max", Foundation::loop()->setRecognizer(getRule("digit")));
}
void ABNFGrammar::repeat_min(){
addRule("repeat-min", Foundation::loop()->setRecognizer(getRule("digit")));
}
/* 1*DIGIT / (*DIGIT "*" *DIGIT) */
void ABNFGrammar::repeat(){
addRule("repeat", Foundation::selector()
->addRecognizer(
Foundation::loop()->setRecognizer(getRule("digit"),1)
getRule("repeat-count")
)
->addRecognizer(Foundation::sequence()
->addRecognizer(
Foundation::loop()->setRecognizer(getRule("digit"))
getRule("repeat-min")
)
->addRecognizer(
Foundation::charRecognizer('*')
)
->addRecognizer(
Foundation::loop()->setRecognizer(getRule("digit"))
getRule("repeat-max")
)
)
);
......
......@@ -40,6 +40,9 @@ private:
void c_wsp();
void rulename();
void repeat();
void repeat_min();
void repeat_max();
void repeat_count();
void defined_as();
void rulelist();
void rule();
......
......@@ -16,14 +16,109 @@ namespace belr{
ABNFBuilder::~ABNFBuilder(){
}
shared_ptr< ABNFGroup > ABNFGroup::create(){
return make_shared<ABNFGroup>();
}
shared_ptr< Recognizer > ABNFGroup::buildRecognizer(const shared_ptr< Grammar >& grammar){
return mAlternation->buildRecognizer(grammar);
}
void ABNFGroup::setAlternation(const shared_ptr< ABNFAlternation >& a){
mAlternation=a;
}
shared_ptr< Recognizer > ABNFElement::buildRecognizer(const shared_ptr< Grammar >& grammar){
if (mRulename.empty())
return mElement->buildRecognizer(grammar);
else
return grammar->getRule(mRulename);
}
shared_ptr< ABNFElement > ABNFElement::create(){
return make_shared<ABNFElement>();
}
void ABNFElement::setElement(const shared_ptr< ABNFBuilder >& e){
mElement=e;
}
void ABNFElement::setRulename(const string& rulename){
mRulename=rulename;
}
shared_ptr< ABNFRepetition > ABNFRepetition::create(){
return make_shared<ABNFRepetition>();
}
void ABNFRepetition::setCount(int count){
mCount=count;
}
void ABNFRepetition::setMin(int min){
mMin=min;
}
void ABNFRepetition::setMax(int max){
mMax=max;
}
void ABNFRepetition::setRepeat(const string& r){
mRepeat=r;
}
shared_ptr< Recognizer > ABNFRepetition::buildRecognizer(const shared_ptr< Grammar >& grammar){
if (mRepeat.empty()) return mElement->buildRecognizer(grammar);
cout<<"Building repetition recognizer with count="<<mCount<<" min="<<mMin<<" max="<<mMax<<endl;
if (mCount!=-1){
return Foundation::loop()->setRecognizer(mElement->buildRecognizer(grammar), mCount, mCount);
}else{
return Foundation::loop()->setRecognizer(mElement->buildRecognizer(grammar), mMin, mMax);
}
}
ABNFRepetition::ABNFRepetition(){
mMin=0;
mMax=-1;
mCount=-1;
}
void ABNFRepetition::setElement(const shared_ptr< ABNFElement >& e){
mElement=e;
}
shared_ptr<ABNFConcatenation> ABNFConcatenation::create(){
return make_shared<ABNFConcatenation>();
}
shared_ptr<Recognizer> ABNFConcatenation::buildRecognizer(const shared_ptr<Grammar> &grammar){
if (mRepetitions.size()==0){
cerr<<"No repetitions set !"<<endl;
abort();
}
if (mRepetitions.size()==1){
return mRepetitions.front()->buildRecognizer(grammar);
}else{
auto seq=Foundation::sequence();
for (auto it=mRepetitions.begin(); it!=mRepetitions.end(); ++it){
seq->addRecognizer((*it)->buildRecognizer(grammar));
}
return seq;
}
return NULL;
}
void ABNFConcatenation::addRepetition(const shared_ptr< ABNFRepetition >& r){
mRepetitions.push_back(r);
}
shared_ptr<ABNFAlternation> ABNFAlternation::create(){
return make_shared<ABNFAlternation>();
}
......@@ -109,6 +204,23 @@ ABNFGrammarBuilder::ABNFGrammarBuilder()
->setCollector("alternation",make_sfn(&ABNFRule::setAlternation));
mParser.setHandler("alternation", make_fn(&ABNFAlternation::create))
->setCollector("concatenation",make_sfn(&ABNFAlternation::addConcatenation));
mParser.setHandler("concatenation", make_fn(&ABNFConcatenation::create))
->setCollector("repetition", make_sfn(&ABNFConcatenation::addRepetition));
mParser.setHandler("repetition", make_fn(&ABNFRepetition::create))
->setCollector("repeat", make_sfn(&ABNFRepetition::setRepeat))
->setCollector("repeat-min", make_sfn(&ABNFRepetition::setMin))
->setCollector("repeat-max", make_sfn(&ABNFRepetition::setMax))
->setCollector("repeat-count", make_sfn(&ABNFRepetition::setCount))
->setCollector("element", make_sfn(&ABNFRepetition::setElement));
mParser.setHandler("element", make_fn(&ABNFElement::create))
->setCollector("rulename", make_sfn(&ABNFElement::setRulename))
->setCollector("group", make_sfn(&ABNFElement::setElement))
->setCollector("option", make_sfn(&ABNFElement::setElement))
->setCollector("char-val", make_sfn(&ABNFElement::setElement))
->setCollector("num-val", make_sfn(&ABNFElement::setElement))
->setCollector("prose-val", make_sfn(&ABNFElement::setElement));
mParser.setHandler("group", make_fn(&ABNFGroup::create))
->setCollector("alternation", make_sfn(&ABNFGroup::setAlternation));
}
shared_ptr<Grammar> ABNFGrammarBuilder::createFromAbnf(const string &path){
......
......@@ -40,10 +40,49 @@ private:
list<shared_ptr<ABNFRule>> mRules;
};
class ABNFElement : public ABNFBuilder{
public:
static shared_ptr<ABNFElement> create();
shared_ptr<Recognizer> buildRecognizer(const shared_ptr<Grammar> &grammar);
void setElement(const shared_ptr<ABNFBuilder> &e);
void setRulename(const string &rulename);
private:
shared_ptr<ABNFBuilder> mElement;
string mRulename;
};
class ABNFGroup : public ABNFBuilder{
public:
static shared_ptr<ABNFGroup> create();
shared_ptr<Recognizer> buildRecognizer(const shared_ptr<Grammar> &grammar);
void setAlternation(const shared_ptr<ABNFAlternation> &a);
private:
shared_ptr<ABNFAlternation> mAlternation;
};
class ABNFRepetition : public ABNFBuilder{
public:
ABNFRepetition();
static shared_ptr<ABNFRepetition> create();
void setRepeat(const string &r);
void setMin(int min);
void setMax(int max);
void setCount(int count);
void setElement(const shared_ptr<ABNFElement> &e);
shared_ptr<Recognizer> buildRecognizer(const shared_ptr<Grammar> &grammar);
private:
int mMin, mMax, mCount;
string mRepeat;
shared_ptr<ABNFElement> mElement;
};
class ABNFConcatenation : public ABNFBuilder{
public:
shared_ptr<ABNFConcatenation> create();
static shared_ptr<ABNFConcatenation> create();
shared_ptr<Recognizer> buildRecognizer(const shared_ptr<Grammar> &grammar);
void addRepetition(const shared_ptr<ABNFRepetition> &r);
private:
list<shared_ptr<ABNFRepetition>> mRepetitions;
};
class ABNFAlternation : public ABNFBuilder{
......
......@@ -65,6 +65,10 @@ public:
this->mCollectors[child_rule_name]=make_shared<ParserCollector<_derivedParserElementT,_parserElementT,const string&>>(fn);
return static_pointer_cast<ParserHandler<_derivedParserElementT,_parserElementT>>(this->shared_from_this());
}
shared_ptr<ParserHandler<_derivedParserElementT,_parserElementT>> setCollector(const string &child_rule_name, function<void (_derivedParserElementT , int )> fn){
this->mCollectors[child_rule_name]=make_shared<ParserCollector<_derivedParserElementT,_parserElementT,int>>(fn);
return static_pointer_cast<ParserHandler<_derivedParserElementT,_parserElementT>>(this->shared_from_this());
}
template <typename _valueT>
shared_ptr<ParserHandler<_derivedParserElementT,_parserElementT>> setCollector(const string &child_rule_name, function<void (_derivedParserElementT , _valueT)> fn){
this->mCollectors[child_rule_name]=make_shared<ParserChildCollector<_derivedParserElementT,_parserElementT,_valueT>>(fn);
......
Markdown is supported
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