#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Grammar;
class CristallParser{
std::vector <Grammar *> Operations;
std::string &T;
public:
CristallParser(std::string &Text);
void parse();
};
class Grammar{
public:
std::string Label;
virtual bool isRule(std::string& Text) = 0;
virtual void parse(std::string& Text) = 0;
};
class NumbersGrammar: public Grammar{
int Limit = 0;
public:
NumbersGrammar(std::string Label, int Limit);
bool isRule(std::string &Text);
void parse(std::string &Text);
};
class SingleGrammar : public Grammar
{
std::string Text;
public:
SingleGrammar(std::string Label, std::string Char);
bool isRule(std::string &Text);
void parse(std::string &Text);
};
CristallParser::CristallParser(std::string &Text): T(Text){
Operations.push_back(new NumbersGrammar("Number",0));
Operations.push_back(new SingleGrammar("Text","test"));
T = Text;
}
void CristallParser::parse(){
while (T.size()>0){
for (auto El: Operations){
if(El->isRule(T))
El->parse(T);
}
T.erase(0,1);
}
}
NumbersGrammar::NumbersGrammar(std::string Label, int Limit = 0)
{
this->Label = Label;
if (Limit != 0)
this->Limit = 0;
}
bool NumbersGrammar::isRule(std::string& Text){
return (isdigit(Text[0]) == true || Text[0] == '-' );
}
void NumbersGrammar::parse(std::string& Text){
std::cout<<Text<<std::endl;
int Element = 0;
for (char Char:Text){
if (isdigit(Char) || Char == '-')
Element++;
else
break;
}
if (Limit == Element || (Limit == 0)){
std::cout<<Text.substr(0,Element);
Text = Text.erase(0,Element);
}
}
SingleGrammar::SingleGrammar(std::string Label, std::string Char)
{
this->Label = Label;
Text = Char;
}
bool SingleGrammar::isRule(std::string &Text)
{
return (Text.substr(0, this-> Text.length()) == Text );
}
void SingleGrammar::parse(std::string &Text)
{
std::cout<<Text<<std::endl;
std::cout<<"Wykryto teskt";
Text.erase(0, this->Text.length());
}
#define DBG(x) { cout << left << setw(30) << #x << (x) << endl; }
int main()
{
string s = "12345dupatestdupa223";
DBG(s);
CristallParser c(s);
c.parse();
cout << endl << endl;
DBG(s);
}