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 
19 #include <stdio.h>
20 
21 enum { Unknown= 0, Rule, Variable, Name, Dot, Character, String, Class, Action, Predicate, Alternate, Sequence, PeekFor, PeekNot, Query, Star, Plus };
22 
23 enum {
24   RuleUsed	= 1<<0,
25   RuleReached	= 1<<1,
26 };
27 
28 typedef union Node Node;
29 
30 struct Rule	 { int type;  Node *next;   char *name;	 Node *variables;  Node *expression;  int id;  int flags;	};
31 struct Variable	 { int type;  Node *next;   char *name;  Node *value;  int offset;					};
32 struct Name	 { int type;  Node *next;   Node *rule;  Node *variable;						};
33 struct Dot	 { int type;  Node *next;										};
34 struct Character { int type;  Node *next;   char *value;								};
35 struct String	 { int type;  Node *next;   char *value;								};
36 struct Class	 { int type;  Node *next;   unsigned char *value;							};
37 struct Action	 { int type;  Node *next;   char *text;	  Node *list;  char *name;  Node *rule;				};
38 struct Predicate { int type;  Node *next;   char *text;									};
39 struct Alternate { int type;  Node *next;   Node *first;  Node *last;							};
40 struct Sequence	 { int type;  Node *next;   Node *first;  Node *last;							};
41 struct PeekFor	 { int type;  Node *next;   Node *element;								};
42 struct PeekNot	 { int type;  Node *next;   Node *element;								};
43 struct Query	 { int type;  Node *next;   Node *element;								};
44 struct Star	 { int type;  Node *next;   Node *element;								};
45 struct Plus	 { int type;  Node *next;   Node *element;								};
46 struct Any	 { int type;  Node *next;										};
47 
48 union Node
49 {
50   int			type;
51   struct Rule		rule;
52   struct Variable	variable;
53   struct Name		name;
54   struct Dot		dot;
55   struct Character	character;
56   struct String		string;
57   struct Class		cclass;
58   struct Action		action;
59   struct Predicate	predicate;
60   struct Alternate	alternate;
61   struct Sequence	sequence;
62   struct PeekFor	peekFor;
63   struct PeekNot	peekNot;
64   struct Query		query;
65   struct Star		star;
66   struct Plus		plus;
67   struct Any		any;
68 };
69 
70 extern Node *actions;
71 extern Node *rules;
72 extern Node *start;
73 
74 extern int   ruleCount;
75 
76 extern FILE *output;
77 
78 extern Node *makeRule(char *name);
79 extern Node *findRule(char *name);
80 extern Node *beginRule(Node *rule);
81 extern void  Rule_setExpression(Node *rule, Node *expression);
82 extern Node *Rule_beToken(Node *rule);
83 extern Node *makeVariable(char *name);
84 extern Node *makeName(Node *rule);
85 extern Node *makeDot(void);
86 extern Node *makeCharacter(char *text);
87 extern Node *makeString(char *text);
88 extern Node *makeClass(char *text);
89 extern Node *makeAction(char *text);
90 extern Node *makePredicate(char *text);
91 extern Node *makeAlternate(Node *e);
92 extern Node *Alternate_append(Node *e, Node *f);
93 extern Node *makeSequence(Node *e);
94 extern Node *Sequence_append(Node *e, Node *f);
95 extern Node *makePeekFor(Node *e);
96 extern Node *makePeekNot(Node *e);
97 extern Node *makeQuery(Node *e);
98 extern Node *makeStar(Node *e);
99 extern Node *makePlus(Node *e);
100 extern Node *push(Node *node);
101 extern Node *top(void);
102 extern Node *pop(void);
103 
104 extern void  Rule_compile_c_header(void);
105 extern void  Rule_compile_c(Node *node);
106 
107 extern void  Node_print(Node *node);
108 extern void  Rule_print(Node *node);
109