Commit 700c04e7 authored by Sylvain Berfini's avatar Sylvain Berfini 🎩

Improve belr parser configuration

parent b8938404
......@@ -10,6 +10,7 @@
#include <memory>
using namespace::std;
using namespace::belr;
namespace belcard {
class BelCard : public BelCardGeneric {
......@@ -28,6 +29,17 @@ namespace belcard {
return make_shared<BelCard>();
}
static void setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
parser->setHandler("vcard", make_fn(&BelCard::create))
->setCollector("FN", make_sfn(&BelCard::setFN))
->setCollector("N", make_sfn(&BelCard::setN))
->setCollector("BDAY", make_sfn(&BelCard::setBirthday))
->setCollector("ANNIVERSARY", make_sfn(&BelCard::setAnniversary))
->setCollector("GENDER", make_sfn(&BelCard::setGender))
->setCollector("NICKNAME", make_sfn(&BelCard::addNickname))
->setCollector("PHOTO", make_sfn(&BelCard::addPhoto));
}
BelCard() {
}
......
#ifndef belcard_generic_hpp
#define belcard_generic_hpp
#include <belr/parser-impl.cc>
#include <string>
#include <list>
#include <map>
#include <memory>
using namespace::std;
using namespace::belr;
namespace belcard {
class BelCardGeneric {
......@@ -25,6 +28,12 @@ namespace belcard {
return make_shared<BelCardParam>();
}
static void setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
parser->setHandler("any-param", make_fn(&BelCardParam::create))
->setCollector("param-name", make_sfn(&BelCardParam::setName))
->setCollector("param-value", make_sfn(&BelCardParam::setValue));
}
BelCardParam() : BelCardGeneric() {
}
......
#ifndef belcard_identification_hpp
#define belcard_identification_hpp
#include "belcard.hpp"
#include "belcard_generic.hpp"
#include <string>
#include <list>
......@@ -9,6 +9,7 @@
#include <memory>
using namespace::std;
using namespace::belr;
namespace belcard {
class BelCardFN : public BelCardProperty {
......@@ -21,6 +22,13 @@ namespace belcard {
setName("FN");
}
static void setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
parser->setHandler("FN", make_fn(&BelCardFN::create))
->setCollector("group", make_sfn(&BelCardFN::setGroup))
->setCollector("any-param", make_sfn(&BelCardFN::addParam))
->setCollector("FN-value", make_sfn(&BelCardFN::setValue));
}
virtual void addParam(const shared_ptr<BelCardParam> &param) {
BelCardProperty::addParam(param);
}
......@@ -38,6 +46,17 @@ namespace belcard {
return make_shared<BelCardN>();
}
static void setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
parser->setHandler("N", make_fn(&BelCardN::create))
->setCollector("group", make_sfn(&BelCardN::setGroup))
->setCollector("any-param", make_sfn(&BelCardN::addParam))
->setCollector("N-fn", make_sfn(&BelCardN::setFamilyName))
->setCollector("N-gn", make_sfn(&BelCardN::setGivenName))
->setCollector("N-an", make_sfn(&BelCardN::setAdditionalName))
->setCollector("N-prefixes", make_sfn(&BelCardN::setPrefixes))
->setCollector("N-suffixes", make_sfn(&BelCardN::setSuffixes));
}
BelCardN() : BelCardProperty() {
setName("N");
}
......@@ -93,6 +112,13 @@ namespace belcard {
return make_shared<BelCardNickname>();
}
static void setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
parser->setHandler("NICKNAME", make_fn(&BelCardNickname::create))
->setCollector("group", make_sfn(&BelCardNickname::setGroup))
->setCollector("any-param", make_sfn(&BelCardNickname::addParam))
->setCollector("NICKNAME-value", make_sfn(&BelCardNickname::setValue));
}
BelCardNickname() : BelCardProperty() {
setName("NICKNAME");
}
......@@ -108,6 +134,13 @@ namespace belcard {
return make_shared<BelCardBirthday>();
}
static void setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
parser->setHandler("BDAY", make_fn(&BelCardBirthday::create))
->setCollector("group", make_sfn(&BelCardBirthday::setGroup))
->setCollector("any-param", make_sfn(&BelCardBirthday::addParam))
->setCollector("BDAY-value", make_sfn(&BelCardBirthday::setValue));
}
BelCardBirthday() : BelCardProperty() {
setName("BDAY");
}
......@@ -123,6 +156,13 @@ namespace belcard {
return make_shared<BelCardAnniversary>();
}
static void setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
parser->setHandler("ANNIVERSARY", make_fn(&BelCardAnniversary::create))
->setCollector("group", make_sfn(&BelCardAnniversary::setGroup))
->setCollector("any-param", make_sfn(&BelCardAnniversary::addParam))
->setCollector("ANNIVERSARY-value", make_sfn(&BelCardAnniversary::setValue));
}
BelCardAnniversary() : BelCardProperty() {
setName("ANNIVERSARY");
}
......@@ -138,6 +178,13 @@ namespace belcard {
return make_shared<BelCardGender>();
}
static void setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
parser->setHandler("GENDER", make_fn(&BelCardGender::create))
->setCollector("group", make_sfn(&BelCardGender::setGroup))
->setCollector("any-param", make_sfn(&BelCardGender::addParam))
->setCollector("GENDER-value", make_sfn(&BelCardGender::setValue));
}
BelCardGender() : BelCardProperty() {
setName("GENDER");
}
......@@ -153,6 +200,13 @@ namespace belcard {
return make_shared<BelCardPhoto>();
}
static void setHandlerAndCollectors(Parser<shared_ptr<BelCardGeneric>> *parser) {
parser->setHandler("PHOTO", make_fn(&BelCardPhoto::create))
->setCollector("group", make_sfn(&BelCardPhoto::setGroup))
->setCollector("any-param", make_sfn(&BelCardPhoto::addParam))
->setCollector("PHOTO-value", make_sfn(&BelCardPhoto::setValue));
}
BelCardPhoto() : BelCardProperty() {
setName("PHOTO");
}
......
#include "belcard/belcard_parser.hpp"
#include "belcard/belcard.hpp"
#include <belr/parser-impl.cc>
using namespace::std;
using namespace::belr;
using namespace::belcard;
......@@ -19,57 +17,16 @@ shared_ptr<BelCard> BelCardParser::parse(const string &input) {
string vcard = unfold(input);
Parser<shared_ptr<BelCardGeneric>> parser(_grammar);
parser.setHandler("vcard", make_fn(&BelCard::create))
->setCollector("FN", make_sfn(&BelCard::setFN))
->setCollector("N", make_sfn(&BelCard::setN))
->setCollector("BDAY", make_sfn(&BelCard::setBirthday))
->setCollector("ANNIVERSARY", make_sfn(&BelCard::setAnniversary))
->setCollector("GENDER", make_sfn(&BelCard::setGender))
->setCollector("NICKNAME", make_sfn(&BelCard::addNickname))
->setCollector("PHOTO", make_sfn(&BelCard::addPhoto));
parser.setHandler("any-param", make_fn(&BelCardParam::create))
->setCollector("param-name", make_sfn(&BelCardParam::setName))
->setCollector("param-value", make_sfn(&BelCardParam::setValue));
parser.setHandler("FN", make_fn(&BelCardFN::create))
->setCollector("group", make_sfn(&BelCardFN::setGroup))
->setCollector("any-param", make_sfn(&BelCardFN::addParam))
->setCollector("FN-value", make_sfn(&BelCardFN::setValue));
parser.setHandler("N", make_fn(&BelCardN::create))
->setCollector("group", make_sfn(&BelCardN::setGroup))
->setCollector("any-param", make_sfn(&BelCardN::addParam))
->setCollector("N-fn", make_sfn(&BelCardN::setFamilyName))
->setCollector("N-gn", make_sfn(&BelCardN::setGivenName))
->setCollector("N-an", make_sfn(&BelCardN::setAdditionalName))
->setCollector("N-prefixes", make_sfn(&BelCardN::setPrefixes))
->setCollector("N-suffixes", make_sfn(&BelCardN::setSuffixes));
parser.setHandler("BDAY", make_fn(&BelCardBirthday::create))
->setCollector("group", make_sfn(&BelCardBirthday::setGroup))
->setCollector("any-param", make_sfn(&BelCardBirthday::addParam))
->setCollector("BDAY-value", make_sfn(&BelCardBirthday::setValue));
parser.setHandler("ANNIVERSARY", make_fn(&BelCardAnniversary::create))
->setCollector("group", make_sfn(&BelCardAnniversary::setGroup))
->setCollector("any-param", make_sfn(&BelCardAnniversary::addParam))
->setCollector("ANNIVERSARY-value", make_sfn(&BelCardAnniversary::setValue));
parser.setHandler("GENDER", make_fn(&BelCardGender::create))
->setCollector("group", make_sfn(&BelCardGender::setGroup))
->setCollector("any-param", make_sfn(&BelCardGender::addParam))
->setCollector("GENDER-value", make_sfn(&BelCardGender::setValue));
parser.setHandler("NICKNAME", make_fn(&BelCardNickname::create))
->setCollector("group", make_sfn(&BelCardNickname::setGroup))
->setCollector("any-param", make_sfn(&BelCardNickname::addParam))
->setCollector("NICKNAME-value", make_sfn(&BelCardNickname::setValue));
parser.setHandler("PHOTO", make_fn(&BelCardPhoto::create))
->setCollector("group", make_sfn(&BelCardPhoto::setGroup))
->setCollector("any-param", make_sfn(&BelCardPhoto::addParam))
->setCollector("PHOTO-value", make_sfn(&BelCardPhoto::setValue));
BelCard::setHandlerAndCollectors(&parser);
BelCardParam::setHandlerAndCollectors(&parser);
BelCardFN::setHandlerAndCollectors(&parser);
BelCardN::setHandlerAndCollectors(&parser);
BelCardBirthday::setHandlerAndCollectors(&parser);
BelCardAnniversary::setHandlerAndCollectors(&parser);
BelCardGender::setHandlerAndCollectors(&parser);
BelCardNickname::setHandlerAndCollectors(&parser);
BelCardPhoto::setHandlerAndCollectors(&parser);
size_t parsedSize = 0;
shared_ptr<BelCardGeneric> ret = parser.parseInput("vcard", vcard, &parsedSize);
......
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