1 | /*
|
---|
2 | * Copyright (c) 2011 Jiri Svoboda
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | #ifndef LEX_T_H_
|
---|
30 | #define LEX_T_H_
|
---|
31 |
|
---|
32 | #include "bigint_t.h"
|
---|
33 |
|
---|
34 | /** Lexical element class */
|
---|
35 | typedef enum {
|
---|
36 | lc_invalid,
|
---|
37 | lc_eof,
|
---|
38 |
|
---|
39 | lc_ident,
|
---|
40 | lc_lit_char,
|
---|
41 | lc_lit_int,
|
---|
42 | lc_lit_string,
|
---|
43 |
|
---|
44 | /* Keywords */
|
---|
45 | lc_and,
|
---|
46 | lc_as,
|
---|
47 | lc_break,
|
---|
48 | lc_bool,
|
---|
49 | lc_builtin,
|
---|
50 | lc_char,
|
---|
51 | lc_class,
|
---|
52 | lc_deleg,
|
---|
53 | lc_do,
|
---|
54 | lc_elif,
|
---|
55 | lc_else,
|
---|
56 | lc_end,
|
---|
57 | lc_enum,
|
---|
58 | lc_except,
|
---|
59 | lc_false,
|
---|
60 | lc_finally,
|
---|
61 | lc_for,
|
---|
62 | lc_fun,
|
---|
63 | lc_new,
|
---|
64 | lc_get,
|
---|
65 | lc_if,
|
---|
66 | lc_in,
|
---|
67 | lc_int,
|
---|
68 | lc_interface,
|
---|
69 | lc_is,
|
---|
70 | lc_nil,
|
---|
71 | lc_not,
|
---|
72 | lc_or,
|
---|
73 | lc_override,
|
---|
74 | lc_packed,
|
---|
75 | lc_private,
|
---|
76 | lc_prop,
|
---|
77 | lc_protected,
|
---|
78 | lc_public,
|
---|
79 | lc_raise,
|
---|
80 | lc_resource,
|
---|
81 | lc_return,
|
---|
82 | lc_self,
|
---|
83 | lc_set,
|
---|
84 | lc_static,
|
---|
85 | lc_string,
|
---|
86 | lc_struct,
|
---|
87 | lc_switch,
|
---|
88 | lc_then,
|
---|
89 | lc_this,
|
---|
90 | lc_true,
|
---|
91 | lc_var,
|
---|
92 | lc_with,
|
---|
93 | lc_when,
|
---|
94 | lc_while,
|
---|
95 | lc_yield,
|
---|
96 |
|
---|
97 | /* Operators */
|
---|
98 | lc_period,
|
---|
99 | lc_slash,
|
---|
100 | lc_lparen,
|
---|
101 | lc_rparen,
|
---|
102 | lc_lsbr,
|
---|
103 | lc_rsbr,
|
---|
104 | lc_equal,
|
---|
105 | lc_notequal,
|
---|
106 | lc_lt,
|
---|
107 | lc_gt,
|
---|
108 | lc_lt_equal,
|
---|
109 | lc_gt_equal,
|
---|
110 | lc_assign,
|
---|
111 | lc_plus,
|
---|
112 | lc_minus,
|
---|
113 | lc_mult,
|
---|
114 | lc_increase,
|
---|
115 |
|
---|
116 | /* Punctuators */
|
---|
117 | lc_comma,
|
---|
118 | lc_colon,
|
---|
119 | lc_scolon,
|
---|
120 |
|
---|
121 | lc__limit
|
---|
122 | } lclass_t;
|
---|
123 |
|
---|
124 | typedef struct {
|
---|
125 | /* String ID */
|
---|
126 | int sid;
|
---|
127 | } lem_ident_t;
|
---|
128 |
|
---|
129 | typedef struct {
|
---|
130 | /* Character value */
|
---|
131 | bigint_t value;
|
---|
132 | } lem_lit_char_t;
|
---|
133 |
|
---|
134 | typedef struct {
|
---|
135 | /* Integer value */
|
---|
136 | bigint_t value;
|
---|
137 | } lem_lit_int_t;
|
---|
138 |
|
---|
139 | typedef struct {
|
---|
140 | /* String value */
|
---|
141 | char *value;
|
---|
142 | } lem_lit_string_t;
|
---|
143 |
|
---|
144 | /** Lexical element */
|
---|
145 | typedef struct {
|
---|
146 | /* Lexical element class */
|
---|
147 | lclass_t lclass;
|
---|
148 |
|
---|
149 | union {
|
---|
150 | lem_ident_t ident;
|
---|
151 | lem_lit_char_t lit_char;
|
---|
152 | lem_lit_int_t lit_int;
|
---|
153 | lem_lit_string_t lit_string;
|
---|
154 | } u;
|
---|
155 |
|
---|
156 | /** Coordinates of this lexical element */
|
---|
157 | struct cspan *cspan;
|
---|
158 | } lem_t;
|
---|
159 |
|
---|
160 | /** Lexer state object */
|
---|
161 | typedef struct lex {
|
---|
162 | /** Input object */
|
---|
163 | struct input *input;
|
---|
164 |
|
---|
165 | /** Lexing buffer */
|
---|
166 | char *inbuf;
|
---|
167 |
|
---|
168 | /** Pointer to current position in lexing buffer */
|
---|
169 | char *ibp;
|
---|
170 |
|
---|
171 | /** Number of the line currently in inbuf */
|
---|
172 | int ib_line;
|
---|
173 |
|
---|
174 | /** Column number adjustment (due to tabs) */
|
---|
175 | int col_adj;
|
---|
176 |
|
---|
177 | /** @c b_true if we have the previous lem in @c prev */
|
---|
178 | bool_t prev_valid;
|
---|
179 |
|
---|
180 | /** Previous lem (only valid if @c current_valid is true) */
|
---|
181 | lem_t prev;
|
---|
182 |
|
---|
183 | /** @c b_true if we have the current lem in @c current */
|
---|
184 | bool_t current_valid;
|
---|
185 |
|
---|
186 | /** Curent lem (only valid if @c current_valid is true) */
|
---|
187 | lem_t current;
|
---|
188 | } lex_t;
|
---|
189 |
|
---|
190 | #endif
|
---|