Patasuss

parse stuff

Dec 21st, 2016
353
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.50 KB | None | 0 0
  1. typedef unsigned int uint;
  2.  
  3. #define STORE_INDEX() uint tempIndex=cxt.index
  4. #define LOAD_INDEX() cxt.index=tempIndex
  5.  
  6.  
  7. struct Node {
  8. std::string ident;
  9. std::string content;
  10. Node* parent;
  11. std::vector<Node*> nodes;
  12. };
  13.  
  14. Node* newNode(std::string ident, std::string content, Node* parent) {
  15. Node* node = new Node;
  16. node->content=content;
  17. node->ident = ident;
  18. node->parent = parent;
  19. return node;
  20. }
  21.  
  22. void destroy_node(Node* node) {
  23. for(Node* child : node->nodes) {
  24. destroy_node(child);
  25. }
  26. delete node;
  27. }
  28.  
  29. Node* startNewNode(Context& cxt, std::string ident, std::string content) {
  30. Node* node = newNode(ident, content, cxt.currNode);
  31. cxt.currNode->nodes.push_back(node);
  32. cxt.currNode = node;
  33. return node;
  34. }
  35.  
  36. void destroy_context(Context& cxt) {
  37. Node* curr = cxt.currNode;
  38. if(curr==nullptr) {
  39. return;
  40. }
  41. while(curr->parent!=nullptr) {
  42. curr = curr->parent;
  43. }
  44. destroy_node(curr);
  45. }
  46.  
  47.  
  48. struct Context {
  49. std::string code;
  50. uint index;
  51. uint codeSize;
  52. Node* currNode;
  53. };
  54.  
  55. bool accept(Context& cxt, char c, bool addToNode=true) {
  56. if(cxt.index>=cxt.codeSize) {
  57. return false;
  58. }
  59. if(cxt.code[cxt.index]==c) {
  60. ++cxt.index;
  61. if(cxt.currNode!=nullptr) {
  62. cxt.currNode->content+=c;
  63. }
  64. return true;
  65. }
  66. return false;
  67. }
  68.  
  69. bool acceptLowerChar(Context& cxt, bool addToNode=true) {
  70. if(cxt.index>=cxt.codeSize) {
  71. return false;
  72. }
  73. char c = cxt.code[cxt.index];
  74. if(c>='a' && c<='z') {
  75. ++cxt.index;
  76. if(cxt.currNode!=nullptr) {
  77. cxt.currNode->content+=c;
  78. }
  79. return true;
  80. }
  81. return false;
  82. }
  83.  
  84. bool acceptDigit(Context& cxt, bool nullAllowed, bool addToNode=true) {
  85. if(cxt.index>=cxt.codeSize) {
  86. return false;
  87. }
  88. char c = cxt.code[cxt.index];
  89. if( (c=='0' && nullAllowed) || (c>='1' && c<='9')) {
  90. ++cxt.index;
  91. if(cxt.currNode!=nullptr) {
  92. cxt.currNode->content+=c;
  93. }
  94. return true;
  95. }
  96. return false;
  97. }
  98.  
  99. bool whitespace(Context& cxt) {
  100. if(accept(cxt, ' ') || accept(cxt, '\t')) {
  101. return true;
  102. }
  103. return false;
  104. }
  105.  
  106. bool skipWhitespaces(Context& cxt) {
  107. bool res=false;
  108. while(whitespace(cxt)) {
  109. res=true;
  110. };
  111. return res;
  112. }
  113.  
  114. bool skipLine(Context& cxt) {
  115. STORE_INDEX();
  116. skipWhitespaces(cxt);
  117. if(accept(cxt, '\n')) {
  118. return true;
  119. }
  120. LOAD_INDEX();
  121. return false;
  122. }
  123.  
  124. bool skipLines(Context& cxt) {
  125. bool res=false;
  126. while(skipLine(cxt)) {
  127. res=true;
  128. }
  129. return res;
  130. }
  131.  
  132. bool literal(Context& cxt, std::string lit, bool addNewNode=true) {
  133. STORE_INDEX();
  134. for(int i=0; i<lit.size(); ++i) {
  135. char c=lit[i];
  136. if(!accept(cxt, c)) {
  137. LOAD_INDEX();
  138. return false;
  139. }
  140. }
  141. if(addNewNode) {
  142. startNewNode(cxt, "literal", lit);
  143.  
  144. }
  145. return true;
  146. }
  147.  
  148. bool identifier(Context& cxt) {
  149. bool res=false;
  150. while(acceptLowerChar(cxt)) {
  151. res=true;
  152. }
  153. if(res) {
  154. startNewNode();
  155. }
  156. return res;
  157. }
  158.  
  159. bool number(Context& cxt) {
  160. STORE_INDEX();
  161. bool res=false;
  162. if(acceptDigit(cxt, false)) {
  163. while(acceptDigit(cxt, true));
  164. return true;
  165. } else if(acceptDigit(cxt, true) && !acceptDigit(cxt)) {
  166. return true;
  167. }
  168. LOAD_INDEX();
  169. return false;
  170. }
  171.  
  172. bool valueExpression(Context& cxt) {
  173. return (identifier(cxt) || number(cxt));
  174. }
  175.  
  176. bool definition(Context& cxt) {
  177. STORE_INDEX();
  178. skipWhitespaces(cxt);
  179. if(identifier(cxt)) {
  180. skipWhitespaces(cxt);
  181. if(literal(cxt, ":=")) {
  182. skipWhitespaces(cxt);
  183. if(identifier(cxt) || number(cxt)) {
  184. skipLine(cxt);
  185. return true;
  186. }
  187. }
  188. }
  189. LOAD_INDEX();
  190. return false;
  191. }
  192.  
  193. bool definitions(Context& cxt) {
  194. bool res=false;
  195. skipLines(cxt);
  196. while(definition(cxt)) {
  197. skipLines(cxt);
  198. res=true;
  199. }
  200. return res;
  201. }
  202.  
  203. bool program(Context& cxt) {
  204. STORE_INDEX();
  205. skipLines(cxt);
  206. skipWhitespaces(cxt);
  207. if(literal(cxt, "program") && definitions(cxt) ) {
  208. skipLines(cxt);
  209. skipWhitespaces(cxt);
  210. if(literal(cxt, "endprogram")) {
  211. return true;
  212. }
  213. }
  214. //LOAD_INDEX();
  215. return false;
  216. }
Advertisement
Add Comment
Please, Sign In to add comment