tree.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /* Copyright (c) 2007 by Ian Piumarta
  2. * All rights reserved.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the 'Software'),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, provided that the above copyright notice(s) and this
  10. * permission notice appear in all copies of the Software. Acknowledgement
  11. * of the use of this Software in supporting documentation would be
  12. * appreciated but is not required.
  13. *
  14. * THE SOFTWARE IS PROVIDED 'AS IS'. USE ENTIRELY AT YOUR OWN RISK.
  15. *
  16. * Last edited: 2007-05-15 10:32:05 by piumarta on emilia
  17. */
  18. #include <stdio.h>
  19. enum { Unknown= 0, Rule, Variable, Name, Dot, Character, String, Class, Action, Predicate, Alternate, Sequence, PeekFor, PeekNot, Query, Star, Plus };
  20. enum {
  21. RuleUsed = 1<<0,
  22. RuleReached = 1<<1,
  23. };
  24. typedef union Node Node;
  25. struct Rule { int type; Node *next; char *name; Node *variables; Node *expression; int id; int flags; };
  26. struct Variable { int type; Node *next; char *name; Node *value; int offset; };
  27. struct Name { int type; Node *next; Node *rule; Node *variable; };
  28. struct Dot { int type; Node *next; };
  29. struct Character { int type; Node *next; char *value; };
  30. struct String { int type; Node *next; char *value; };
  31. struct Class { int type; Node *next; unsigned char *value; };
  32. struct Action { int type; Node *next; char *text; Node *list; char *name; Node *rule; };
  33. struct Predicate { int type; Node *next; char *text; };
  34. struct Alternate { int type; Node *next; Node *first; Node *last; };
  35. struct Sequence { int type; Node *next; Node *first; Node *last; };
  36. struct PeekFor { int type; Node *next; Node *element; };
  37. struct PeekNot { int type; Node *next; Node *element; };
  38. struct Query { int type; Node *next; Node *element; };
  39. struct Star { int type; Node *next; Node *element; };
  40. struct Plus { int type; Node *next; Node *element; };
  41. struct Any { int type; Node *next; };
  42. union Node
  43. {
  44. int type;
  45. struct Rule rule;
  46. struct Variable variable;
  47. struct Name name;
  48. struct Dot dot;
  49. struct Character character;
  50. struct String string;
  51. struct Class cclass;
  52. struct Action action;
  53. struct Predicate predicate;
  54. struct Alternate alternate;
  55. struct Sequence sequence;
  56. struct PeekFor peekFor;
  57. struct PeekNot peekNot;
  58. struct Query query;
  59. struct Star star;
  60. struct Plus plus;
  61. struct Any any;
  62. };
  63. extern Node *actions;
  64. extern Node *rules;
  65. extern Node *start;
  66. extern int ruleCount;
  67. extern FILE *output;
  68. extern Node *makeRule(char *name);
  69. extern Node *findRule(char *name);
  70. extern Node *beginRule(Node *rule);
  71. extern void Rule_setExpression(Node *rule, Node *expression);
  72. extern Node *Rule_beToken(Node *rule);
  73. extern Node *makeVariable(char *name);
  74. extern Node *makeName(Node *rule);
  75. extern Node *makeDot(void);
  76. extern Node *makeCharacter(char *text);
  77. extern Node *makeString(char *text);
  78. extern Node *makeClass(char *text);
  79. extern Node *makeAction(char *text);
  80. extern Node *makePredicate(char *text);
  81. extern Node *makeAlternate(Node *e);
  82. extern Node *Alternate_append(Node *e, Node *f);
  83. extern Node *makeSequence(Node *e);
  84. extern Node *Sequence_append(Node *e, Node *f);
  85. extern Node *makePeekFor(Node *e);
  86. extern Node *makePeekNot(Node *e);
  87. extern Node *makeQuery(Node *e);
  88. extern Node *makeStar(Node *e);
  89. extern Node *makePlus(Node *e);
  90. extern Node *push(Node *node);
  91. extern Node *top(void);
  92. extern Node *pop(void);
  93. extern void Rule_compile_c_header(void);
  94. extern void Rule_compile_c(Node *node);
  95. extern void Node_print(Node *node);
  96. extern void Rule_print(Node *node);