Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "rule.h"
- bool RULE::TABLE1(std::string str)
- {
- return str[0] == '{' && str[str.length() - 1] == '}' && TABLE2(str.substr(1, str.length() - 2));
- }
- bool RULE::TABLE2(std::string str)
- {
- return FIELD(str) && TABLE3(str);
- }
- bool RULE::TABLE3(std::string str)
- {
- if (str.length() > 1) //Not EMPTY
- return TABLE2(str.substr(1, str.length() - 1));
- else //EMPTY
- return true;
- }
- bool RULE::FIELD(std::string str)
- {
- int i = str.find(',');
- int semi = str.find(';');
- if(i > semi || i == std::string::npos)
- i = semi;
- int quot = str.find("\"");
- if(i > quot || i == std::string::npos)
- i = quot;
- int brac = str.find("{");
- if(i > brac || i == std::string::npos)
- i = brac;
- if (i == 0)
- {
- return false;
- }
- if(str.length() == 0)
- return true;
- bool ans = false;
- ans = VAL(str);
- if (!ans)
- {
- int i = str.find('=');
- if (i != std::string::npos)
- {
- ans = KEY(str.substr(0, i)) && VAL(str.substr(i + 1, str.length() - 1 - i));
- }
- }
- return ans;
- }
- bool RULE::SEPARATOR(char str)
- {
- return str == ',' || str == ';';
- }
- bool RULE::DEC(std::string str)
- {
- CharClass* zeroTonine = new CharClass("0123456789");
- CharClass* oneToNine = new CharClass("123456789");
- Star* starZeroToNine = new Star(zeroTonine);
- Regex* reg = new Seq({
- oneToNine,
- starZeroToNine
- });
- bool ans = reg->match(str.c_str()) == str.length();
- delete reg;
- return ans;
- }
- bool RULE::HEX(std::string str)
- {
- CharClass* zero = new CharClass("0");
- CharClass* xSign = new CharClass("x");
- CharClass* hexNums = new CharClass("abcdefABCDEF0123456789");
- Star* starHexNums = new Star(hexNums);
- Regex* reg = new Seq({
- zero,
- xSign,
- starHexNums});
- bool ans = reg->match(str.c_str()) == str.length();
- delete reg;
- return ans;
- }
- bool RULE::STR(std::string str)
- {
- CharClass* keyboardChars = new CharClass("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890");
- Star* reg = new Star(keyboardChars);
- bool ans = reg->match(str.c_str()) == str.length();
- delete reg;
- delete keyboardChars;
- return ans;
- }
- bool RULE::KEY(std::string str)
- {
- return IDENTIFIER(str) || KEY2(str);
- }
- bool RULE::KEY2(std::string str)
- {
- if (str[0] == '[' && str[str.length()] == ']')
- {
- return VAL(str.substr(1, str.length() - 2));
- }
- }
- bool RULE::VAL(std::string str)
- {
- return DEC(str) || HEX(str) || STR(str) || TABLE1(str);
- }
- bool RULE::IDENTIFIER(std::string str)
- {
- CharClass* aToZ = new CharClass("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM");
- CharClass* keyboardChars = new CharClass("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890");
- Star* starKeyboardChars = new Star(keyboardChars);
- Regex* reg = new Seq({
- aToZ,
- starKeyboardChars
- });
- bool ans = reg->match(str.c_str()) == str.length();
- delete reg;
- return ans;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement