1 /*
2 ** parsecontext.h
3 ** Base class for Lemon-based parsers
4 **
5 **---------------------------------------------------------------------------
6 ** Copyright 2008 Christoph Oelckers
7 ** All rights reserved.
8 **
9 ** Redistribution and use in source and binary forms, with or without
10 ** modification, are permitted provided that the following conditions
11 ** are met:
12 **
13 ** 1. Redistributions of source code must retain the above copyright
14 **    notice, this list of conditions and the following disclaimer.
15 ** 2. Redistributions in binary form must reproduce the above copyright
16 **    notice, this list of conditions and the following disclaimer in the
17 **    documentation and/or other materials provided with the distribution.
18 ** 3. The name of the author may not be used to endorse or promote products
19 **    derived from this software without specific prior written permission.
20 **
21 ** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 ** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 ** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 ** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 ** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 ** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 ** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 **---------------------------------------------------------------------------
32 **
33 */
34 
35 #ifndef __PARSECONTEST__H_
36 #define __PARSECONTEST__H_
37 
38 #include "tarray.h"
39 
40 // The basic tokens the parser requires the grammar to understand
41 enum
42 {
43 	 OR       =1,
44 	 XOR      ,
45 	 AND      ,
46 	 MINUS    ,
47 	 PLUS     ,
48 	 MULTIPLY ,
49 	 DIVIDE   ,
50 	 MODULUS  ,
51 	 NUM      ,
52 	 FLOATVAL ,
53 	 LPAREN   ,
54 	 RPAREN   ,
55 	 SYM      ,
56 	 RBRACE   ,
57 	 LBRACE   ,
58 	 COMMA    ,
59 	 EQUALS   ,
60 	 LBRACKET ,
61 	 RBRACKET ,
62 	 OR_EQUAL ,
63 	 COLON    ,
64 	 SEMICOLON,
65 	 LSHASSIGN,
66 	 RSHASSIGN,
67 	 STRING   ,
68 	 INCLUDE  ,
69 	 DEFINE   ,
70 };
71 
72 #define DEFINE_TOKEN_TRANS(prefix) \
73 	static int TokenTrans[] = { \
74 	0, \
75 	prefix##OR, \
76 	prefix##XOR, \
77 	prefix##AND, \
78 	prefix##MINUS, \
79 	prefix##PLUS, \
80 	prefix##MULTIPLY, \
81 	prefix##DIVIDE, \
82 	prefix##MODULUS, \
83 	prefix##NUM, \
84 	prefix##FLOATVAL, \
85 	prefix##LPAREN, \
86 	prefix##RPAREN, \
87 	prefix##SYM, \
88 	prefix##RBRACE, \
89 	prefix##LBRACE, \
90 	prefix##COMMA, \
91 	prefix##EQUALS, \
92 	prefix##LBRACKET, \
93 	prefix##RBRACKET, \
94 	prefix##OR_EQUAL, \
95 	prefix##COLON, \
96 	prefix##SEMICOLON, \
97 	prefix##LSHASSIGN, \
98 	prefix##RSHASSIGN, \
99 	prefix##STRING, \
100 	prefix##INCLUDE, \
101 	prefix##DEFINE, \
102 	 };
103 
104 
105 struct FParseSymbol
106 {
107 	int Value;
108 	char Sym[80];
109 };
110 
111 union FParseToken
112 {
113 	int val;
114 	double fval;
115 	char sym[80];
116 	char string[80];
117 	FParseSymbol *symval;
118 };
119 
120 
121 struct FParseContext;
122 
123 typedef void (*ParseFunc)(void *pParser, int tokentype, FParseToken token, FParseContext *context);
124 
125 struct FParseContext
126 {
127 	TArray<FParseSymbol> symbols;
128 	int SourceLine;
129 	const char *SourceFile;
130 	int EnumVal;
131 	int *TokenTrans;
132 	void *pParser;
133 	ParseFunc Parse;
134 
FParseContextFParseContext135 	FParseContext(void *parser, ParseFunc parse, int *tt)
136 	{
137 		SourceLine = 0;
138 		SourceFile = NULL;
139 		pParser = parser;
140 		Parse = parse;
141 		TokenTrans = tt;
142 	}
143 
~FParseContextFParseContext144 	virtual ~FParseContext() {}
145 
146 	void AddSym (char *sym, int val);
147 	bool FindSym (char *sym, FParseSymbol **val);
148 	virtual bool FindToken (char *tok, int *type) = 0;
149 	int GetToken (char *&sourcep, FParseToken *yylval);
150 	int PrintError (const char *s);
151 	void ParseLump(const char *lumpname);
152 };
153 
154 
155 #endif
156