Advertisement
Guest User

Untitled

a guest
May 3rd, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. #include "rule.h"
  2.  
  3. bool RULE::TABLE1(std::string str)
  4. {
  5. return str[0] == '{' && str[str.length() - 1] == '}' && TABLE2(str.substr(1, str.length() - 2));
  6. }
  7.  
  8. bool RULE::TABLE2(std::string str)
  9. {
  10. return FIELD(str) && TABLE3(str);
  11. }
  12. bool RULE::TABLE3(std::string str)
  13. {
  14. if (str.length() > 1) //Not EMPTY
  15. return TABLE2(str.substr(1, str.length() - 1));
  16. else //EMPTY
  17. return true;
  18. }
  19.  
  20. bool RULE::FIELD(std::string str)
  21. {
  22.  
  23. int i = str.find(',');
  24. int semi = str.find(';');
  25. if(i > semi || i == std::string::npos)
  26. i = semi;
  27. int quot = str.find("\"");
  28. if(i > quot || i == std::string::npos)
  29. i = quot;
  30. int brac = str.find("{");
  31. if(i > brac || i == std::string::npos)
  32. i = brac;
  33.  
  34. if (i == 0)
  35. {
  36. return false;
  37. }
  38. if(str.length() == 0)
  39. return true;
  40. bool ans = false;
  41. ans = VAL(str);
  42. if (!ans)
  43. {
  44. int i = str.find('=');
  45. if (i != std::string::npos)
  46. {
  47. ans = KEY(str.substr(0, i)) && VAL(str.substr(i + 1, str.length() - 1 - i));
  48. }
  49. }
  50. return ans;
  51. }
  52.  
  53. bool RULE::SEPARATOR(char str)
  54. {
  55. return str == ',' || str == ';';
  56. }
  57. bool RULE::DEC(std::string str)
  58. {
  59. CharClass* zeroTonine = new CharClass("0123456789");
  60. CharClass* oneToNine = new CharClass("123456789");
  61. Star* starZeroToNine = new Star(zeroTonine);
  62. Regex* reg = new Seq({
  63. oneToNine,
  64. starZeroToNine
  65. });
  66.  
  67. bool ans = reg->match(str.c_str()) == str.length();
  68.  
  69. delete reg;
  70.  
  71. return ans;
  72. }
  73. bool RULE::HEX(std::string str)
  74. {
  75. CharClass* zero = new CharClass("0");
  76. CharClass* xSign = new CharClass("x");
  77. CharClass* hexNums = new CharClass("abcdefABCDEF0123456789");
  78. Star* starHexNums = new Star(hexNums);
  79. Regex* reg = new Seq({
  80. zero,
  81. xSign,
  82. starHexNums});
  83.  
  84. bool ans = reg->match(str.c_str()) == str.length();
  85.  
  86. delete reg;
  87.  
  88. return ans;
  89. }
  90. bool RULE::STR(std::string str)
  91. {
  92. CharClass* keyboardChars = new CharClass("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890");
  93.  
  94. Star* reg = new Star(keyboardChars);
  95.  
  96. bool ans = reg->match(str.c_str()) == str.length();
  97.  
  98. delete reg;
  99. delete keyboardChars;
  100.  
  101. return ans;
  102. }
  103. bool RULE::KEY(std::string str)
  104. {
  105. return IDENTIFIER(str) || KEY2(str);
  106. }
  107. bool RULE::KEY2(std::string str)
  108. {
  109. if (str[0] == '[' && str[str.length()] == ']')
  110. {
  111. return VAL(str.substr(1, str.length() - 2));
  112. }
  113. }
  114. bool RULE::VAL(std::string str)
  115. {
  116. return DEC(str) || HEX(str) || STR(str) || TABLE1(str);
  117. }
  118. bool RULE::IDENTIFIER(std::string str)
  119. {
  120. CharClass* aToZ = new CharClass("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM");
  121. CharClass* keyboardChars = new CharClass("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890");
  122.  
  123. Star* starKeyboardChars = new Star(keyboardChars);
  124.  
  125. Regex* reg = new Seq({
  126. aToZ,
  127. starKeyboardChars
  128. });
  129.  
  130. bool ans = reg->match(str.c_str()) == str.length();
  131.  
  132. delete reg;
  133.  
  134. return ans;
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement