2003-03-15 Glen Nakamura <glen@imodulo.com>
[official-gcc.git] / gcc / java / lex.h
blobaa9a2beea34ae1357ba4664cd43ac58023a343d1
1 /* Language lexer definitions for the GNU compiler for the Java(TM) language.
2 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003
3 Free Software Foundation, Inc.
4 Contributed by Alexandre Petit-Bianco (apbianco@cygnus.com)
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA.
23 Java and all Java-based marks are trademarks or registered trademarks
24 of Sun Microsystems, Inc. in the United States and other countries.
25 The Free Software Foundation is independent of Sun Microsystems, Inc. */
27 #ifndef GCC_JAVA_LEX_H
28 #define GCC_JAVA_LEX_H
30 /* Extern global variables declarations */
31 extern FILE *finput;
32 extern int lineno;
34 /* A Unicode character, as read from the input file */
35 typedef unsigned short unicode_t;
37 #ifdef HAVE_ICONV
38 #include <iconv.h>
39 #endif /* HAVE_ICONV */
41 /* Default encoding to use if no encoding is specified. */
42 #define DEFAULT_ENCODING "UTF-8"
44 /* Debug macro to print-out what we match */
45 #ifdef JAVA_LEX_DEBUG
46 #ifdef JAVA_LEX_DEBUG_CHAR
47 #define JAVA_LEX_CHAR(c) printf ("java_lex:%d: char '%c'.%d\n", \
48 lineno, (c < 128 ? c : '.'), c);
49 #else
50 #define JAVA_LEX_CHAR(c)
51 #endif
52 #define JAVA_LEX_KW(c) printf ("java_lex:%d: keyword: '%s'\n", lineno,c)
53 #define JAVA_LEX_ID(s) printf ("java_lex:%d: ID: '%s'\n", \
54 lineno, \
55 (all_ascii ? s : "<U>"))
56 #define JAVA_LEX_LIT(s, r) printf ("java_lex:%d: literal '%s'_%d\n", \
57 lineno, s, r)
58 #define JAVA_LEX_CHAR_LIT(s) printf ("java_lex:%d: literal '%d'\n", lineno, s)
59 #define JAVA_LEX_STR_LIT(s) { \
60 int i; \
61 printf ("java_lex:%d: literal '%s'\n", \
62 lineno, s); \
64 #define JAVA_LEX_SEP(c) printf ("java_lex:%d: separator '%c'\n",lineno,c)
65 #define JAVA_LEX_OP(c) printf ("java_lex:%d: operator '%s'\n", lineno,c)
66 #else
67 #define JAVA_LEX_CHAR(c)
68 #define JAVA_LEX_KW(c)
69 #define JAVA_LEX_ID(s)
70 #define JAVA_LEX_LIT(s,r)
71 #define JAVA_LEX_CHAR_LIT(s)
72 #define JAVA_LEX_STR_LIT(s)
73 #define JAVA_LEX_SEP(c)
74 #define JAVA_LEX_OP(s)
75 #endif
77 /* Line information containers */
78 struct java_line {
79 unicode_t *line; /* The line's unicode */
80 char *unicode_escape_p; /* The matching char was a unicode escape */
81 unicode_t ahead[1]; /* Character ahead */
82 char unicode_escape_ahead_p; /* Character ahead is a unicode escape */
83 int max; /* buffer's max size */
84 int size; /* number of unicodes */
85 int current; /* Current position, unicode based */
86 int char_col; /* Current position, input char based */
87 int lineno; /* Its line number */
88 int white_space_only; /* If it contains only white spaces */
90 #define JAVA_COLUMN_DELTA(p) \
91 (ctxp->c_line->unicode_escape_p [ctxp->c_line->current+(p)] ? 6 : \
92 (ctxp->c_line->line [ctxp->c_line->current+(p)] == '\t' ? 8 : 1))
94 struct java_error {
95 struct java_line *line;
96 int error;
99 typedef struct java_lc_s GTY(()) {
100 int line;
101 int prev_col;
102 int col;
103 } java_lc;
105 struct java_lexer
107 /* The file from which we're reading. */
108 FILE *finput;
110 /* Number of consecutive backslashes we've read. */
111 int bs_count;
113 /* If nonzero, a value that was pushed back. */
114 unicode_t unget_value;
116 /* If nonzero, we've hit EOF. Used only by java_get_unicode(). */
117 int hit_eof : 1;
119 #ifdef HAVE_ICONV
120 /* Nonzero if we've read any bytes. We only recognize the
121 byte-order-marker (BOM) as the first word. */
122 int read_anything : 1;
124 /* Nonzero if we have to byte swap. */
125 int byte_swap : 1;
127 /* Nonzero if we're using the fallback decoder. */
128 int use_fallback : 1;
130 /* The handle for the iconv converter we're using. */
131 iconv_t handle;
133 /* Bytes we've read from the file but have not sent to iconv. */
134 char buffer[1024];
136 /* Index of first valid character in buffer, -1 if no valid
137 characters. */
138 int first;
140 /* Index of last valid character in buffer, plus one. -1 if no
141 valid characters in buffer. */
142 int last;
144 /* This is a buffer of characters already converted by iconv. We
145 use `char' here because we're assuming that iconv() converts to
146 UCS-2, and then we convert it ourselves. */
147 unsigned char out_buffer[1024];
149 /* Index of first valid output character. -1 if no valid
150 characters. */
151 int out_first;
153 /* Index of last valid output character, plus one. -1 if no valid
154 characters. */
155 int out_last;
157 #endif /* HAVE_ICONV */
159 typedef struct java_lexer java_lexer;
161 /* Destroy a lexer object. */
162 extern void java_destroy_lexer (java_lexer *);
164 #define JAVA_LINE_MAX 80
166 /* Build a location compound integer */
167 #define BUILD_LOCATION() ((ctxp->elc.line << 12) | (ctxp->elc.col & 0xfff))
169 /* Those macros are defined differently if we compile jc1-lite
170 (JC1_LITE defined) or jc1. */
171 #ifdef JC1_LITE
173 #define DCONST0 0
174 #define REAL_VALUE_TYPE int
175 #define GET_IDENTIFIER(S) xstrdup ((S))
176 #define REAL_VALUE_ATOF(LIT,MODE) 0
177 #define REAL_VALUE_ISINF(VALUE) 0
178 #define REAL_VALUE_ISNAN(VALUE) 0
179 #define SET_REAL_VALUE_ATOF(TARGET,SOURCE)
180 #define FLOAT_TYPE_NODE 0
181 #define DOUBLE_TYPE_NODE 0
182 #define SET_MODIFIER_CTX(TOKEN) java_lval->value = (TOKEN)
183 #define GET_TYPE_PRECISION(NODE) 4
184 #define BUILD_OPERATOR(TOKEN) return TOKEN
185 #define BUILD_OPERATOR2(TOKEN) return ASSIGN_ANY_TK
186 #define SET_LVAL_NODE(NODE)
187 #define SET_LVAL_NODE_TYPE(NODE, TYPE)
188 #define BUILD_ID_WFL(EXP) (EXP)
189 #define JAVA_FLOAT_RANGE_ERROR(S) {}
190 #define JAVA_INTEGRAL_RANGE_ERROR(S) do { } while (0)
192 #else
194 #define DCONST0 dconst0
195 #define GET_IDENTIFIER(S) get_identifier ((S))
196 #define SET_REAL_VALUE_ATOF(TARGET,SOURCE) (TARGET) = (SOURCE)
197 #define FLOAT_TYPE_NODE float_type_node
198 #define DOUBLE_TYPE_NODE double_type_node
199 /* Set modifier_ctx according to TOKEN */
200 #define SET_MODIFIER_CTX(TOKEN) \
202 ctxp->modifier_ctx [(TOKEN)-PUBLIC_TK] = build_wfl_node (NULL_TREE); \
203 java_lval->value = (TOKEN)-PUBLIC_TK; \
205 /* Type precision for long */
206 #define GET_TYPE_PRECISION(NODE) TYPE_PRECISION (long_type_node) / 8;
207 /* Build an operator tree node and return TOKEN */
208 #define BUILD_OPERATOR(TOKEN) \
210 java_lval->operator.token = (TOKEN); \
211 java_lval->operator.location = BUILD_LOCATION(); \
212 return (TOKEN); \
215 /* Build an operator tree node but return ASSIGN_ANY_TK */
216 #define BUILD_OPERATOR2(TOKEN) \
218 java_lval->operator.token = (TOKEN); \
219 java_lval->operator.location = BUILD_LOCATION(); \
220 return ASSIGN_ANY_TK; \
222 /* Set java_lval->node and TREE_TYPE(java_lval->node) in macros */
223 #define SET_LVAL_NODE(NODE) java_lval->node = (NODE)
224 #define SET_LVAL_NODE_TYPE(NODE,TYPE) \
226 java_lval->node = (NODE); \
227 TREE_TYPE (java_lval->node) = (TYPE); \
229 /* Wrap identifier around a wfl */
230 #define BUILD_ID_WFL(EXP) build_wfl_node ((EXP))
231 /* Special ways to report error on numeric literals */
232 #define JAVA_FLOAT_RANGE_ERROR(m) \
234 char msg [1024]; \
235 int i = ctxp->c_line->current; \
236 ctxp->c_line->current = number_beginning; \
237 sprintf (msg, "Floating point literal exceeds range of `%s'", (m)); \
238 java_lex_error (msg, 0); \
239 ctxp->c_line->current = i; \
241 #define JAVA_INTEGRAL_RANGE_ERROR(m) \
242 do { \
243 int i = ctxp->c_line->current; \
244 ctxp->c_line->current = number_beginning; \
245 java_lex_error (m, 0); \
246 ctxp->c_line->current = i; \
247 } while (0)
249 #endif /* Definitions for jc1 compilation only */
251 /* Macros to decode character ranges */
252 #define RANGE(c, l, h) (((c) >= l && (c) <= h))
253 #define JAVA_WHITE_SPACE_P(c) (c == ' ' || c == '\t' || c == '\f')
254 #define JAVA_START_CHAR_P(c) ((c < 128 \
255 && (ISIDST (c) || c == '$')) \
256 || (c >= 128 && java_start_char_p (c)))
257 #define JAVA_PART_CHAR_P(c) ((c < 128 \
258 && (ISIDNUM (c) \
259 || c == '$' \
260 || c == 0x0000 \
261 || RANGE (c, 0x01, 0x08) \
262 || RANGE (c, 0x0e, 0x1b) \
263 || c == 0x7f)) \
264 || (c >= 128 && java_part_char_p (c)))
265 #define JAVA_ASCII_DIGIT(c) ISDIGIT (c)
266 #define JAVA_ASCII_OCTDIGIT(c) RANGE (c, '0', '7')
267 #define JAVA_ASCII_HEXDIGIT(c) ISXDIGIT (c)
268 #define JAVA_ASCII_FPCHAR(c) (RANGE (c, 'd', 'f') || RANGE (c, 'D', 'F') || \
269 c == '.' || JAVA_ASCII_DIGIT (c))
270 #define JAVA_FP_SUFFIX(c) (c == 'D' || c == 'd' || c == 'f' || c == 'F')
271 #define JAVA_FP_EXP(c) (c == 'E' || c == 'F')
272 #define JAVA_FP_PM(c) (c == '-' || c == '+')
273 #define JAVA_ASCII_LETTER(c) ISALPHA (c)
275 /* Constants */
276 #define JAVA_READ_BUFFER 256
277 #define JAVA_CHAR_ERROR -2
278 #define UEOF -1
280 #endif /* ! GCC_JAVA_LEX_H */