1 /* Copyright (C) 2005-2008,2011 G.P. Halkes
2    This program is free software: you can redistribute it and/or modify
3    it under the terms of the GNU General Public License version 3, as
4    published by the Free Software Foundation.
5 
6    This program is distributed in the hope that it will be useful,
7    but WITHOUT ANY WARRANTY; without even the implied warranty of
8    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9    GNU General Public License for more details.
10 
11    You should have received a copy of the GNU General Public License
12    along with this program.  If not, see <http://www.gnu.org/licenses/>.
13 */
14 
15 #ifndef TYPES_H
16 #define TYPES_H
17 
18 #include <stdio.h>
19 #include "list.h"
20 #include "bool.h"
21 #include "scope.h"
22 
23 typedef struct {
24 	char *text;
25 	const char *fileName;
26 	int lineNumber;
27 } Token;
28 
29 /* NOTE: if CCode is changed to something other than a token, several pieces of
30    code need to be changed as well. For example, copyCCode and calls to
31    freeToken. */
32 typedef Token CCode;
33 
34 Token *newToken(void);
35 Token *copyToken(Token *original, const char *file, int line);
36 CCode *newCCode(void);
37 CCode *copyCCode(CCode *original, const char *file, int line);
38 Token *newPlaceHolder(void);
39 void freeToken(Token *token);
40 
41 typedef enum {
42 	START_DIRECTIVE = 1,
43 	TOKEN_DIRECTIVE,
44 	FIRST_DIRECTIVE,
45 	LEXICAL_DIRECTIVE,
46 	PREFIX_DIRECTIVE,
47 	ONERROR_DIRECTIVE,
48 	LABEL_DIRECTIVE,
49 	DATATYPE_DIRECTIVE,
50 	LAST_DIRECTIVE_ENTRY /* Used for directiveToText */
51 } DirectiveSubtype;
52 
53 typedef struct Directive {
54 	DirectiveSubtype subtype;
55 	Token *token[2];			/* For everything with one or two tokens */
56 	union {
57 		struct Directive *next;	/* For checking %start and %first directives */
58 		int number;				/* Terminal number */
59 	} un;
60 #ifdef MEM_DEBUG
61 	bool dontFreeTokens;
62 #endif
63 } Directive;
64 
65 Directive *newDirective(DirectiveSubtype subtype, Token *token1, Token *token2);
66 
67 typedef enum {
68 	CODE = 1,
69 	DIRECTIVE,
70 	NONTERMINAL
71 } DeclarationSubtype;
72 
73 #include "ruleAnalysis.h"
74 
75 typedef struct {
76 	DeclarationSubtype subtype;
77 	bool valid;
78 	union {
79 		CCode *code;
80 		Directive *directive;
81 		NonTerminal *nonTerminal;
82 		void *ptr;	/* To make newDeclaration easier */
83 	} un;
84 } Declaration;
85 
86 Declaration *newDeclaration(DeclarationSubtype subtype, void *ptr);
87 
88 extern int maxTokenNumber;
89 extern Token *literalLabels[256];
90 
91 #define CONDENSED_ISTOKEN (1<<0)
92 #define CONDENSED_REACHABLE (1<<1)
93 
94 typedef struct {
95 	int flags;
96 	union {
97 		Directive *token;
98 		int literal;
99 	} un;
100 } CondensedInfo;
101 
102 extern List *declarations; /* List of all declarations */
103 extern Scope *globalScope;
104 
105 extern Directive *lexicalDirective;
106 extern Directive *prefixDirective;
107 extern const char *prefix;
108 extern Directive *onErrorDirective;
109 extern Directive *dataTypeDirective;
110 extern CCode *top_code;
111 
112 extern int *terminalToCondensed;
113 extern CondensedInfo *condensedToTerminal;
114 extern int condensedNumber;
115 extern int nrOfTerminals;
116 
117 extern bool hasFirst;
118 
119 void setupScope(void);
120 void setupResolve(void);
121 void checkDeclarations(void);
122 void fixCondensedTable(void);
123 void walkDirectives(void (*action)(Directive *));
124 void enumerateMacroSets(Directive *directive);
125 int checkLiteral(Token *literal);
126 
127 #endif
128