1 /* ====================================================================
2  * The Kannel Software License, Version 1.0
3  *
4  * Copyright (c) 2001-2014 Kannel Group
5  * Copyright (c) 1998-2001 WapIT Ltd.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  *    if any, must include the following acknowledgment:
22  *       "This product includes software developed by the
23  *        Kannel Group (http://www.kannel.org/)."
24  *    Alternately, this acknowledgment may appear in the software itself,
25  *    if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Kannel" and "Kannel Group" must not be used to
28  *    endorse or promote products derived from this software without
29  *    prior written permission. For written permission, please
30  *    contact org@kannel.org.
31  *
32  * 5. Products derived from this software may not be called "Kannel",
33  *    nor may "Kannel" appear in their name, without prior written
34  *    permission of the Kannel Group.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED.  IN NO EVENT SHALL THE KANNEL GROUP OR ITS CONTRIBUTORS
40  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
41  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
42  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
43  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
44  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
45  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
46  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of the Kannel Group.  For more information on
51  * the Kannel Group, please see <http://www.kannel.org/>.
52  *
53  * Portions of this software are based upon software originally written at
54  * WapIT Ltd., Helsinki, Finland for the Kannel project.
55  */
56 
57 /*
58  *
59  * wsint.h
60  *
61  * Author: Markku Rossi <mtr@iki.fi>
62  *
63  * Copyright (c) 1999-2000 WAPIT OY LTD.
64  *		 All rights reserved.
65  *
66  * Operating system specific environment and general helper utilities
67  * for the WMLScript tools.
68  *
69  */
70 
71 #ifndef WSINT_H
72 #define WSINT_H
73 
74 #include "gw-config.h"
75 
76 #include <stdio.h>
77 #include <stdlib.h>
78 #include <string.h>
79 #include <stdarg.h>
80 #include <errno.h>
81 #include <math.h>
82 
83 #if HAVE_UNISTD_H
84 #include <unistd.h>
85 #endif /* HAVE_UNISTD_H */
86 
87 #include "gwlib/gwassert.h"
88 
89 /********************* Types and definitions ****************************/
90 
91 /* Platform dependent line terminator.  This is used in diagnostic and
92    error messages to terminate output lines. */
93 
94 #ifdef WIN32
95 #define WS_LINE_TERMINATOR "\r\n"
96 #else /* not WIN32 */
97 #define WS_LINE_TERMINATOR "\n"
98 #endif /* not WIN32 */
99 
100 /* Data types. */
101 
102 #define WS_INT8_MIN	-128
103 #define WS_INT8_MAX	 127
104 
105 #define WS_INT16_MIN	-32768
106 #define WS_INT16_MAX	 32767
107 
108 #define WS_INT32_MIN	-2147483648
109 #define WS_INT32_MAX	 2147483647
110 
111 /* Integer types. */
112 
113 typedef unsigned char WsByte;
114 
115 typedef signed char WsInt8;
116 typedef unsigned char WsUInt8;
117 
118 typedef signed short WsInt16;
119 typedef unsigned short WsUInt16;
120 
121 typedef signed long WsInt32;
122 typedef unsigned long WsUInt32;
123 
124 /* Internally we use as good floating point numbers as possible.  This
125    way we avoid losing data in constant folding, etc. */
126 typedef double WsFloat;
127 
128 typedef enum
129 {
130     WS_FALSE,
131     WS_TRUE
132 } WsBool;
133 
134 
135 /* Error flags. */
136 
137 /* Out of memory. */
138 #define WS_ERROR_B_MEMORY	0x01
139 
140 /* The input program was syntactically incorrect. */
141 #define WS_ERROR_B_SYNTAX	0x02
142 
143 /* The input program was semantically incorrect.  We managed to parse
144    it, but it contained some semantical errors.  For example, a local
145    variable was defined twice. */
146 #define WS_ERROR_B_SEMANTIC	0x04
147 
148 /********************* Include sub-module headers ***********************/
149 
150 #include "ws.h"
151 #include "wserror.h"
152 #include "wsutf8.h"
153 #include "wsieee754.h"
154 #include "wsbuffer.h"
155 #include "wsencode.h"
156 #include "wsalloc.h"
157 #include "wsfalloc.h"
158 #include "wsstream.h"
159 #include "wshash.h"
160 #include "wsbc.h"
161 
162 #include "wsstree.h"
163 #include "wsasm.h"
164 #include "wsopt.h"
165 #include "wsstdlib.h"
166 
167 /********************* The compiler handle ******************************/
168 
169 #if WS_DEBUG
170 /* The currently active compiler.  Just for debugging purposes. */
171 extern WsCompilerPtr global_compiler;
172 #endif /* WS_DEBUG */
173 
174 /* A structure to register the currently active `continue-break'
175    labels.  These are allocated from the syntax-tree pool. */
176 struct WsContBreakRec
177 {
178     struct WsContBreakRec *next;
179     WsAsmIns *l_cont;
180     WsAsmIns *l_break;
181 };
182 
183 typedef struct WsContBreakRec WsContBreak;
184 
185 #define COMPILER_MAGIC (0xfefe0101)
186 struct WsCompilerRec
187 {
188     /* A magic number of assure that a correct compiler handle is passed
189        to the parser and lexer functions. */
190     WsUInt32 magic;
191 
192     /* User-specifiable parameters. */
193     WsCompilerParams params;
194 
195     /* Current input stream. */
196     WsStream *input;
197 
198     /* The source file name and line number of the current input
199        stream. */
200     const char *input_name;
201     WsUInt32 linenum;
202 
203     /* Fast-malloc pool for the syntax tree items. */
204     WsFastMalloc *pool_stree;
205 
206     /* Fast-malloc pool for the symbolic assembler instructions. */
207     WsFastMalloc *pool_asm;
208 
209     /* List of active memory blocks, allocated by the lexer.  When lexer
210        allocates string or symbol tokens, their dynamically allocated
211        data is registered to this list.  The parser removes the items
212        when needed, but if the parsing fails, the items can be freed
213        from this list during the cleanup. */
214     void **lexer_active_list;
215     size_t lexer_active_list_size;
216 
217     /* The byte-code object. */
218     WsBc *bc;
219 
220     /* The next label for the assembler generation. */
221     WsUInt32 next_label;
222 
223     /* The assembler code, currently begin constructed on this compiler. */
224     WsAsmIns *asm_head;
225     WsAsmIns *asm_tail;
226 
227     /* Buffer holding the linearized byte-code for the current symbolic
228        assembler. */
229     WsBuffer byte_code;
230 
231     /* The syntax tree items, found from the source stream. */
232 
233     /* External compilation unit pragmas. */
234     WsHashPtr pragma_use_hash;
235 
236     /* Functions. */
237     WsUInt32 num_functions;
238     WsFunction *functions;
239 
240     /* A mapping from function names to their declarations in
241        `functions'. */
242     WsHashPtr functions_hash;
243 
244     /* A namespace for function arguments and local variables. */
245     WsUInt32 next_vindex;
246     WsHashPtr variables_hash;
247 
248     /* Registry for the currently active `continue-break' labels. */
249     WsContBreak *cont_break;
250 
251     /* Statistics about the compilation. */
252 
253     WsUInt32 num_errors;
254     WsUInt32 num_warnings;
255 
256     WsUInt32 num_extern_functions;
257     WsUInt32 num_local_functions;
258 
259     /* Bitmask to record occurred errors.  This is used in error
260        generation and reporting to make sane error messages. */
261     WsUInt32 errors;
262 
263     /* The latest line where a syntax error occurred.  The compiler do
264        not print multiple syntax errors from the same line. */
265     WsUInt32 last_syntax_error_line;
266 };
267 
268 typedef struct WsCompilerRec WsCompiler;
269 
270 /********************* Lexer and parser *********************************/
271 
272 #define YYPARSE_PARAM   pctx
273 #define YYLEX_PARAM     pctx
274 
275 /* The lexer. */
276 extern int yylex();
277 
278 /* Register the lexer allocated block `ptr' to the compiler's list of
279    active blocks. */
280 WsBool ws_lexer_register_block(WsCompiler *compiler, void *ptr);
281 
282 /* Register the lexer allocated UTF-8 string `string' to the
283    compiler's list of active blocks. */
284 WsBool ws_lexer_register_utf8(WsCompiler *compiler, WsUtf8String *string);
285 
286 /* Unregister the block `ptr' from the compiler's list of active
287    blocks and free it.  It is a fatal error if the block `ptr' does
288    not exist on the list. */
289 void ws_lexer_free_block(WsCompiler *compiler, void *ptr);
290 
291 /* Unregister an UTF-8 string `string' from the compiler's list of
292    active blocks and free it. */
293 void ws_lexer_free_utf8(WsCompiler *compiler, WsUtf8String *string);
294 
295 /* The parser. */
296 int ws_yy_parse(void *context);
297 
298 #endif /* not WSINT_H */
299