1 /* Language lexer for the GNU compiler for the Java(TM) language.
2 Copyright (C) 1997, 1998, 1999 Free Software Foundation, Inc.
3 Contributed by Alexandre Petit-Bianco (apbianco@cygnus.com)
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.
22 Java and all Java-based marks are trademarks or registered trademarks
23 of Sun Microsystems, Inc. in the United States and other countries.
24 The Free Software Foundation is independent of Sun Microsystems, Inc. */
26 /* It defines java_lex (yylex) that reads a Java ASCII source file
27 possibly containing Unicode escape sequence or utf8 encoded characters
28 and returns a token for everything found but comments, white spaces
29 and line terminators. When necessary, it also fills the java_lval
30 (yylval) union. It's implemented to be called by a re-entrant parser
33 The lexical analysis conforms to the Java grammar described in "The
34 Java(TM) Language Specification. J. Gosling, B. Joy, G. Steele.
35 Addison Wesley 1996" (http://java.sun.com/docs/books/jls/html/3.doc.html) */
52 extern struct obstack *expression_obstack;
55 /* Function declaration */
56 static int java_lineterminator PROTO ((unicode_t));
57 static char *java_sprint_unicode PROTO ((struct java_line *, int));
58 static void java_unicode_2_utf8 PROTO ((unicode_t));
59 static void java_lex_error PROTO ((char *, int));
60 static int java_is_eol PROTO ((FILE *, int));
61 static void java_store_unicode PROTO ((struct java_line *, unicode_t, int));
62 static unicode_t java_parse_escape_sequence PROTO (());
63 static int java_letter_or_digit_p PROTO ((unicode_t));
64 static int java_parse_doc_section PROTO ((unicode_t));
65 static void java_parse_end_comment PROTO (());
66 static unicode_t java_get_unicode PROTO (());
67 static unicode_t java_read_unicode PROTO ((int, int *));
68 static void java_store_unicode PROTO ((struct java_line *, unicode_t, int));
69 static unicode_t java_read_char PROTO (());
70 static void java_allocate_new_line PROTO (());
71 static void java_unget_unicode PROTO (());
72 static unicode_t java_sneak_unicode PROTO (());
77 int java_lang_imported = 0;
81 java_lang_id = get_identifier ("java.lang");
83 if (!java_lang_imported)
85 tree node = build_tree_list
86 (build_expr_wfl (java_lang_id, NULL, 0, 0), NULL_TREE);
87 read_import_dir (TREE_PURPOSE (node));
88 TREE_CHAIN (node) = ctxp->import_demand_list;
89 ctxp->import_demand_list = node;
90 java_lang_imported = 1;
94 wfl_operator = build_expr_wfl (NULL_TREE, ctxp->filename, 0, 0);
96 label_id = get_identifier ("$L");
98 wfl_append = build_expr_wfl (get_identifier ("append"), NULL, 0, 0);
99 if (!wfl_string_buffer)
101 build_expr_wfl (get_identifier ("java.lang.StringBuffer"), NULL, 0, 0);
103 wfl_to_string = build_expr_wfl (get_identifier ("toString"), NULL, 0, 0);
105 ctxp->static_initialized = ctxp->non_static_initialized =
106 ctxp->incomplete_class = NULL_TREE;
108 bzero (ctxp->modifier_ctx, 11*sizeof (ctxp->modifier_ctx[0]));
109 bzero (current_jcf, sizeof (JCF));
110 ctxp->current_parsed_class = NULL;
111 ctxp->package = NULL_TREE;
114 ctxp->filename = input_filename;
115 ctxp->lineno = lineno = 0;
118 ctxp->unget_utf8_value = 0;
119 ctxp->minus_seen = 0;
120 ctxp->java_error_flag = 0;
124 java_sprint_unicode (line, i)
125 struct java_line *line;
128 static char buffer [10];
129 if (line->unicode_escape_p [i] || line->line [i] > 128)
130 sprintf (buffer, "\\u%04x", line->line [i]);
133 buffer [0] = line->line [i];
140 java_sneak_unicode ()
142 return (ctxp->c_line->line [ctxp->c_line->current]);
146 java_unget_unicode ()
148 if (!ctxp->c_line->current)
149 fatal ("can't unget unicode - java_unget_unicode");
150 ctxp->c_line->current--;
151 ctxp->c_line->char_col -= JAVA_COLUMN_DELTA (0);
155 java_allocate_new_line ()
157 unicode_t ahead = (ctxp->c_line ? ctxp->c_line->ahead[0] : '\0');
158 char ahead_escape_p = (ctxp->c_line ?
159 ctxp->c_line->unicode_escape_ahead_p : 0);
161 if (ctxp->c_line && !ctxp->c_line->white_space_only)
165 free (ctxp->p_line->unicode_escape_p);
166 free (ctxp->p_line->line);
169 ctxp->p_line = ctxp->c_line;
170 ctxp->c_line = NULL; /* Reallocated */
175 ctxp->c_line = (struct java_line *)xmalloc (sizeof (struct java_line));
176 ctxp->c_line->max = JAVA_LINE_MAX;
177 ctxp->c_line->line = (unicode_t *)xmalloc
178 (sizeof (unicode_t)*ctxp->c_line->max);
179 ctxp->c_line->unicode_escape_p =
180 (char *)xmalloc (sizeof (char)*ctxp->c_line->max);
181 ctxp->c_line->white_space_only = 0;
184 ctxp->c_line->line [0] = ctxp->c_line->size = 0;
185 ctxp->c_line->char_col = ctxp->c_line->current = 0;
188 ctxp->c_line->line [ctxp->c_line->size] = ahead;
189 ctxp->c_line->unicode_escape_p [ctxp->c_line->size] = ahead_escape_p;
190 ctxp->c_line->size++;
192 ctxp->c_line->ahead [0] = 0;
193 ctxp->c_line->unicode_escape_ahead_p = 0;
194 ctxp->c_line->lineno = ++lineno;
195 ctxp->c_line->white_space_only = 1;
204 if (ctxp->unget_utf8_value)
206 int to_return = ctxp->unget_utf8_value;
207 ctxp->unget_utf8_value = 0;
219 if ((c & 0xe0) == 0xc0)
222 if ((c1 & 0xc0) == 0x80)
223 return (unicode_t)(((c &0x1f) << 6) + (c1 & 0x3f));
225 else if ((c & 0xf0) == 0xe0)
228 if ((c1 & 0xc0) == 0x80)
231 if ((c2 & 0xc0) == 0x80)
232 return (unicode_t)(((c & 0xf) << 12) +
233 (( c1 & 0x3f) << 6) + (c2 & 0x3f));
236 java_lex_error ("Bad utf8 encoding", 0);
242 java_store_unicode (l, c, unicode_escape_p)
245 int unicode_escape_p;
247 if (l->size == l->max)
249 l->max += JAVA_LINE_MAX;
250 l->line = (unicode_t *)realloc (l->line, sizeof (unicode_t)*l->max);
251 l->unicode_escape_p = (char *)realloc (l->unicode_escape_p,
252 sizeof (char)*l->max);
254 l->line [l->size] = c;
255 l->unicode_escape_p [l->size++] = unicode_escape_p;
259 java_read_unicode (term_context, unicode_escape_p)
261 int *unicode_escape_p;
266 c = java_read_char ();
267 *unicode_escape_p = 0;
270 return ((term_context ? c :
271 java_lineterminator (c) ? '\n' : (unicode_t)c));
273 /* Count the number of preceeding '\' */
274 for (base = ftell (finput), i = base-2; c == '\\';)
276 fseek (finput, i--, SEEK_SET);
277 c = java_read_char (); /* Will fail if reading utf8 stream. FIXME */
279 fseek (finput, base, SEEK_SET);
280 if ((base-i-3)%2 == 0) /* If odd number of \ seen */
282 c = java_read_char ();
285 unsigned short unicode = 0;
287 /* Next should be 4 hex digits, otherwise it's an error.
288 The hex value is converted into the unicode, pushed into
289 the Unicode stream. */
290 for (shift = 12; shift >= 0; shift -= 4)
292 if ((c = java_read_char ()) == UEOF)
294 if (c >= '0' && c <= '9')
295 unicode |= (unicode_t)((c-'0') << shift);
296 else if ((c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'))
297 unicode |= (unicode_t)((10+(c | 0x20)-'a') << shift);
300 ("Non hex digit in Unicode escape sequence", 0);
302 *unicode_escape_p = 1;
303 return (term_context ? unicode :
304 (java_lineterminator (c) ? '\n' : unicode));
308 return (unicode_t)'\\';
314 /* It's time to read a line when... */
315 if (!ctxp->c_line || ctxp->c_line->current == ctxp->c_line->size)
318 java_allocate_new_line ();
319 if (ctxp->c_line->line[0] != '\n')
322 int unicode_escape_p;
323 c = java_read_unicode (0, &unicode_escape_p);
324 java_store_unicode (ctxp->c_line, c, unicode_escape_p);
325 if (ctxp->c_line->white_space_only
326 && !JAVA_WHITE_SPACE_P (c) && c!='\n')
327 ctxp->c_line->white_space_only = 0;
328 if ((c == '\n') || (c == UEOF))
332 ctxp->c_line->char_col += JAVA_COLUMN_DELTA (0);
333 JAVA_LEX_CHAR (ctxp->c_line->line [ctxp->c_line->current]);
334 return ctxp->c_line->line [ctxp->c_line->current++];
338 java_lineterminator (c)
341 int unicode_escape_p;
342 if (c == '\n') /* CR */
344 if ((c = java_read_unicode (1, &unicode_escape_p)) != '\r')
346 ctxp->c_line->ahead [0] = c;
347 ctxp->c_line->unicode_escape_ahead_p = unicode_escape_p;
351 else if (c == '\r') /* LF */
353 if ((c = java_read_unicode (1, &unicode_escape_p)) != '\n')
355 ctxp->c_line->ahead [0] = c;
356 ctxp->c_line->unicode_escape_ahead_p = unicode_escape_p;
364 /* Parse the end of a C style comment */
366 java_parse_end_comment ()
370 for (c = java_get_unicode ();; c = java_get_unicode ())
375 java_lex_error ("Comment not terminated at end of input", 0);
377 switch (c = java_get_unicode ())
380 java_lex_error ("Comment not terminated at end of input", 0);
383 case '*': /* reparse only '*' */
384 java_unget_unicode ();
390 /* Parse the documentation section. Keywords must be at the beginning
391 of a documentation comment line (ignoring white space and any `*'
392 character). Parsed keyword(s): @DEPRECATED. */
395 java_parse_doc_section (c)
398 int valid_tag = 0, seen_star;
400 while (JAVA_WHITE_SPACE_P (c) || (c == '*') || c == '\n')
412 c = java_get_unicode();
416 java_lex_error ("Comment not terminated at end of input", 0);
418 if (seen_star && (c == '/'))
419 return 1; /* Goto step1 in caller */
421 /* We're parsing @deprecated */
422 if (valid_tag && (c == '@'))
427 while (tag_index < 10 && c != UEOF && c != ' ' && c != '\n')
429 c = java_get_unicode ();
430 tag [tag_index++] = c;
434 java_lex_error ("Comment not terminated at end of input", 0);
436 java_unget_unicode ();
437 tag [tag_index] = '\0';
439 if (!strcmp (tag, "deprecated"))
440 ctxp->deprecated = 1;
445 /* This function to be used only by JAVA_ID_CHAR_P (), otherwise it
446 will return a wrong result. */
448 java_letter_or_digit_p (c)
451 return _JAVA_LETTER_OR_DIGIT_P (c);
455 java_parse_escape_sequence ()
460 switch (c = java_get_unicode ())
463 return (unicode_t)0x8;
465 return (unicode_t)0x9;
467 return (unicode_t)0xa;
469 return (unicode_t)0xc;
471 return (unicode_t)0xd;
473 return (unicode_t)0x22;
475 return (unicode_t)0x27;
477 return (unicode_t)0x5c;
478 case '0': case '1': case '2': case '3': case '4':
479 case '5': case '6': case '7': case '8': case '9':
482 int octal_escape_index = 0;
484 for (; octal_escape_index < 3 && RANGE (c, '0', '9');
485 c = java_get_unicode ())
486 octal_escape [octal_escape_index++] = c;
488 java_unget_unicode ();
490 if ((octal_escape_index == 3) && (octal_escape [0] > '3'))
492 java_lex_error ("Literal octal escape out of range", 0);
493 return JAVA_CHAR_ERROR;
498 for (char_lit=0, i = 0, shift = 3*(octal_escape_index-1);
499 i < octal_escape_index; i++, shift -= 3)
500 char_lit |= (octal_escape [i] - '0') << shift;
507 return '\n'; /* ULT, caught latter as a specific error */
509 java_lex_error ("Illegal character in escape sequence", 0);
510 return JAVA_CHAR_ERROR;
522 unicode_t c, first_unicode;
523 int ascii_index, all_ascii;
526 /* Translation of the Unicode escape in the raw stream of Unicode
527 characters. Takes care of line terminator. */
529 /* Skip white spaces: SP, TAB and FF or ULT */
530 for (c = java_get_unicode ();
531 c == '\n' || JAVA_WHITE_SPACE_P (c); c = java_get_unicode ())
534 ctxp->elc.line = ctxp->c_line->lineno;
535 ctxp->elc.col = ctxp->c_line->char_col-2;
538 ctxp->elc.col = (ctxp->elc.col < 0 ? 0 : ctxp->elc.col);
540 if (c == 0x1a) /* CTRL-Z */
542 if ((c = java_get_unicode ()) == UEOF)
543 return 0; /* Ok here */
545 java_unget_unicode (); /* Caught latter at the end the function */
547 /* Handle EOF here */
548 if (c == UEOF) /* Should probably do something here... */
551 /* Take care of eventual comments. */
554 switch (c = java_get_unicode ())
557 for (c = java_get_unicode ();;c = java_get_unicode ())
560 java_lex_error ("Comment not terminated at end of input", 0);
561 if (c == '\n') /* ULT */
567 if ((c = java_get_unicode ()) == '*')
569 if ((c = java_get_unicode ()) == '/')
570 goto step1; /* Empy documentation comment */
571 else if (java_parse_doc_section (c))
575 java_unget_unicode ();
577 java_parse_end_comment ();
581 java_unget_unicode ();
587 ctxp->elc.line = ctxp->c_line->lineno;
588 ctxp->elc.prev_col = ctxp->elc.col;
589 ctxp->elc.col = ctxp->c_line->char_col - JAVA_COLUMN_DELTA (-1);
590 if (ctxp->elc.col < 0)
591 fatal ("ctxp->elc.col < 0 - java_lex");
593 /* Numeric literals */
594 if (JAVA_ASCII_DIGIT (c) || (c == '.'))
596 /* This section of code is borrowed from gcc/c-lex.c */
597 #define TOTAL_PARTS ((HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR) * 2 + 2)
598 int parts[TOTAL_PARTS];
599 HOST_WIDE_INT high, low;
600 /* End borrowed section */
601 char literal_token [256];
602 int literal_index = 0, radix = 10, long_suffix = 0, overflow = 0, bytes;
604 int number_beginning = ctxp->c_line->current;
606 /* We might have a . separator instead of a FP like .[0-9]* */
609 unicode_t peep = java_sneak_unicode ();
611 if (!JAVA_ASCII_DIGIT (peep))
614 BUILD_OPERATOR (DOT_TK);
618 for (i = 0; i < TOTAL_PARTS; i++)
623 c = java_get_unicode ();
624 if (c == 'x' || c == 'X')
627 c = java_get_unicode ();
629 else if (JAVA_ASCII_DIGIT (c))
633 /* Push the '.' back and prepare for a FP parsing... */
634 java_unget_unicode ();
639 /* We have a zero literal: 0, 0{f,F}, 0{d,D} */
640 JAVA_LEX_LIT ("0", 10);
644 SET_LVAL_NODE (long_zero_node);
647 SET_LVAL_NODE (float_zero_node);
650 SET_LVAL_NODE (double_zero_node);
653 java_unget_unicode ();
654 SET_LVAL_NODE (integer_zero_node);
659 /* Parse the first part of the literal, until we find something
660 which is not a number. */
661 while ((radix == 10 && JAVA_ASCII_DIGIT (c)) ||
662 (radix == 16 && JAVA_ASCII_HEXDIGIT (c)) ||
663 (radix == 8 && JAVA_ASCII_OCTDIGIT (c)))
665 /* We store in a string (in case it turns out to be a FP) and in
666 PARTS if we have to process a integer literal. */
667 int numeric = (RANGE (c, '0', '9') ? c-'0' : 10 +(c|0x20)-'a');
670 literal_token [literal_index++] = c;
671 /* This section of code if borrowed from gcc/c-lex.c */
672 for (count = 0; count < TOTAL_PARTS; count++)
674 parts[count] *= radix;
677 parts[count] += (parts[count-1] >> HOST_BITS_PER_CHAR);
678 parts[count-1] &= (1 << HOST_BITS_PER_CHAR) - 1;
683 if (parts [TOTAL_PARTS-1] != 0)
685 /* End borrowed section. */
686 c = java_get_unicode ();
689 /* If we have something from the FP char set but not a digit, parse
691 if (JAVA_ASCII_FPCHAR (c) && !JAVA_ASCII_DIGIT (c))
694 int seen_digit = (literal_index ? 1 : 0);
695 int seen_exponent = 0;
696 int fflag = 0; /* 1 for {f,F}, 0 for {d,D}. FP literal are
697 double unless specified. */
699 java_lex_error ("Can't express non-decimal FP literal", 0);
708 literal_token [literal_index++ ] = c;
709 c = java_get_unicode ();
712 java_lex_error ("Invalid character in FP literal", 0);
715 if (c == 'e' || c == 'E')
719 /* {E,e} must have seen at list a digit */
721 java_lex_error ("Invalid FP literal", 0);
725 literal_token [literal_index++] = c;
726 c = java_get_unicode ();
729 java_lex_error ("Invalid character in FP literal", 0);
731 if ( c == 'f' || c == 'F' || c == 'd' || c == 'D')
733 fflag = ((c == 'd') || (c == 'D')) ? 0 : 1;
734 stage = 4; /* So we fall through */
737 if ((c=='-' || c =='+') && stage < 3)
740 literal_token [literal_index++] = c;
741 c = java_get_unicode ();
744 if ((stage == 0 && JAVA_ASCII_FPCHAR (c)) ||
745 (stage == 1 && JAVA_ASCII_FPCHAR (c) && !(c == '.')) ||
746 (stage == 2 && (JAVA_ASCII_DIGIT (c) || JAVA_FP_PM (c))) ||
747 (stage == 3 && JAVA_ASCII_DIGIT (c)))
749 if (JAVA_ASCII_DIGIT (c))
751 literal_token [literal_index++ ] = c;
752 c = java_get_unicode ();
757 REAL_VALUE_TYPE value;
759 tree type = (fflag ? FLOAT_TYPE_NODE : DOUBLE_TYPE_NODE);
762 if (stage != 4) /* Don't push back fF/dD */
763 java_unget_unicode ();
765 /* An exponent (if any) must have seen a digit. */
766 if (seen_exponent && !seen_digit)
767 java_lex_error ("Invalid FP literal", 0);
769 literal_token [literal_index] = '\0';
770 JAVA_LEX_LIT (literal_token, radix);
772 if (setjmp (handler))
774 JAVA_FLOAT_RANGE_ERROR ((fflag ? "float" : "double"));
779 SET_FLOAT_HANDLER (handler);
781 (value, REAL_VALUE_ATOF (literal_token,
784 if (REAL_VALUE_ISINF (value))
785 JAVA_FLOAT_RANGE_ERROR ((fflag ? "float" : "double"));
787 if (REAL_VALUE_ISNAN (value))
788 JAVA_FLOAT_RANGE_ERROR ((fflag ? "float" : "double"));
790 SET_LVAL_NODE_TYPE (build_real (type, value), type);
791 SET_FLOAT_HANDLER (NULL_PTR);
796 } /* JAVA_ASCCI_FPCHAR (c) */
798 /* Here we get back to converting the integral literal. */
799 if (c == 'L' || c == 'l')
801 else if (radix == 16 && JAVA_ASCII_LETTER (c))
802 java_lex_error ("Digit out of range in hexadecimal literal", 0);
803 else if (radix == 8 && JAVA_ASCII_DIGIT (c))
804 java_lex_error ("Digit out of range in octal literal", 0);
805 else if (radix == 16 && !literal_index)
806 java_lex_error ("No digit specified for hexadecimal literal", 0);
808 java_unget_unicode ();
810 #ifdef JAVA_LEX_DEBUG
811 literal_token [literal_index] = '\0'; /* So JAVA_LEX_LIT is safe. */
812 JAVA_LEX_LIT (literal_token, radix);
814 /* This section of code is borrowed from gcc/c-lex.c */
817 bytes = GET_TYPE_PRECISION (long_type_node);
818 for (i = bytes; i < TOTAL_PARTS; i++)
826 for (i = 0; i < HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR; i++)
828 high |= ((HOST_WIDE_INT) parts[i + (HOST_BITS_PER_WIDE_INT
829 / HOST_BITS_PER_CHAR)]
830 << (i * HOST_BITS_PER_CHAR));
831 low |= (HOST_WIDE_INT) parts[i] << (i * HOST_BITS_PER_CHAR);
833 /* End borrowed section. */
838 /* 9223372036854775808L is valid if operand of a '-'. Otherwise
839 9223372036854775807L is the biggest `long' literal that can be
840 expressed using a 10 radix. For other radixes, everything that
841 fits withing 64 bits is OK. */
842 int hb = (high >> 31);
843 if (overflow || (hb && low && radix == 10) ||
844 (hb && high & 0x7fffffff && radix == 10) ||
845 (hb && !(high & 0x7fffffff) && !ctxp->minus_seen && radix == 10))
846 JAVA_INTEGRAL_RANGE_ERROR ("Numeric overflow for `long' literal");
850 /* 2147483648 is valid if operand of a '-'. Otherwise,
851 2147483647 is the biggest `int' literal that can be
852 expressed using a 10 radix. For other radixes, everything
853 that fits within 32 bits is OK. As all literals are
854 signed, we sign extend here. */
855 int hb = (low >> 31) & 0x1;
856 if (overflow || high || (hb && low & 0x7fffffff && radix == 10) ||
857 (hb && !(low & 0x7fffffff) && !ctxp->minus_seen && radix == 10))
858 JAVA_INTEGRAL_RANGE_ERROR ("Numeric overflow for `int' literal");
861 ctxp->minus_seen = 0;
862 SET_LVAL_NODE_TYPE (build_int_2 (low, high),
863 (long_suffix ? long_type_node : int_type_node));
867 ctxp->minus_seen = 0;
868 /* Character literals */
872 if ((c = java_get_unicode ()) == '\\')
873 char_lit = java_parse_escape_sequence ();
877 c = java_get_unicode ();
879 if ((c == '\n') || (c == UEOF))
880 java_lex_error ("Character literal not terminated at end of line", 0);
882 java_lex_error ("Syntax error in character literal", 0);
884 if (c == JAVA_CHAR_ERROR)
885 char_lit = 0; /* We silently convert it to zero */
887 JAVA_LEX_CHAR_LIT (char_lit);
888 SET_LVAL_NODE_TYPE (build_int_2 (char_lit, 0), char_type_node);
892 /* String literals */
898 for (no_error = 1, c = java_get_unicode ();
899 c != '"' && c != '\n'; c = java_get_unicode ())
902 c = java_parse_escape_sequence ();
903 no_error &= (c != JAVA_CHAR_ERROR ? 1 : 0);
905 java_unicode_2_utf8 (c);
907 if (c == '\n' || c == UEOF) /* ULT */
909 lineno--; /* Refer to the line the terminator was seen */
910 java_lex_error ("String not terminated at end of line.", 0);
914 obstack_1grow (&temporary_obstack, '\0');
915 string = obstack_finish (&temporary_obstack);
917 if (!no_error || (c != '"'))
918 java_lval->node = error_mark_node; /* Requires futher testing FIXME */
921 tree s = make_node (STRING_CST);
922 TREE_STRING_LENGTH (s) = strlen (string);
923 TREE_STRING_POINTER (s) =
924 obstack_alloc (expression_obstack, TREE_STRING_LENGTH (s)+1);
925 strcpy (TREE_STRING_POINTER (s), string);
929 return STRING_LIT_TK;
937 BUILD_OPERATOR (OP_TK);
943 if (ctxp->ccb_indent == 1)
944 ctxp->first_ccb_indent1 = lineno;
946 BUILD_OPERATOR (OCB_TK);
950 if (ctxp->ccb_indent == 1)
951 ctxp->last_ccb_indent1 = lineno;
955 BUILD_OPERATOR (OSB_TK);
967 BUILD_OPERATOR (DOT_TK);
975 if ((c = java_get_unicode ()) == '=')
977 BUILD_OPERATOR (EQ_TK);
981 /* Equals is used in two different locations. In the
982 variable_declarator: rule, it has to be seen as '=' as opposed
983 to being seen as an ordinary assignment operator in
984 assignment_operators: rule. */
985 java_unget_unicode ();
986 BUILD_OPERATOR (ASSIGN_TK);
990 switch ((c = java_get_unicode ()))
993 BUILD_OPERATOR (GTE_TK);
995 switch ((c = java_get_unicode ()))
998 if ((c = java_get_unicode ()) == '=')
1000 BUILD_OPERATOR2 (ZRS_ASSIGN_TK);
1004 java_unget_unicode ();
1005 BUILD_OPERATOR (ZRS_TK);
1008 BUILD_OPERATOR2 (SRS_ASSIGN_TK);
1010 java_unget_unicode ();
1011 BUILD_OPERATOR (SRS_TK);
1014 java_unget_unicode ();
1015 BUILD_OPERATOR (GT_TK);
1019 switch ((c = java_get_unicode ()))
1022 BUILD_OPERATOR (LTE_TK);
1024 if ((c = java_get_unicode ()) == '=')
1026 BUILD_OPERATOR2 (LS_ASSIGN_TK);
1030 java_unget_unicode ();
1031 BUILD_OPERATOR (LS_TK);
1034 java_unget_unicode ();
1035 BUILD_OPERATOR (LT_TK);
1039 switch ((c = java_get_unicode ()))
1042 BUILD_OPERATOR (BOOL_AND_TK);
1044 BUILD_OPERATOR2 (AND_ASSIGN_TK);
1046 java_unget_unicode ();
1047 BUILD_OPERATOR (AND_TK);
1051 switch ((c = java_get_unicode ()))
1054 BUILD_OPERATOR (BOOL_OR_TK);
1056 BUILD_OPERATOR2 (OR_ASSIGN_TK);
1058 java_unget_unicode ();
1059 BUILD_OPERATOR (OR_TK);
1063 switch ((c = java_get_unicode ()))
1066 BUILD_OPERATOR (INCR_TK);
1068 BUILD_OPERATOR2 (PLUS_ASSIGN_TK);
1070 java_unget_unicode ();
1071 BUILD_OPERATOR (PLUS_TK);
1075 switch ((c = java_get_unicode ()))
1078 BUILD_OPERATOR (DECR_TK);
1080 BUILD_OPERATOR2 (MINUS_ASSIGN_TK);
1082 java_unget_unicode ();
1083 ctxp->minus_seen = 1;
1084 BUILD_OPERATOR (MINUS_TK);
1088 if ((c = java_get_unicode ()) == '=')
1090 BUILD_OPERATOR2 (MULT_ASSIGN_TK);
1094 java_unget_unicode ();
1095 BUILD_OPERATOR (MULT_TK);
1099 if ((c = java_get_unicode ()) == '=')
1101 BUILD_OPERATOR2 (DIV_ASSIGN_TK);
1105 java_unget_unicode ();
1106 BUILD_OPERATOR (DIV_TK);
1110 if ((c = java_get_unicode ()) == '=')
1112 BUILD_OPERATOR2 (XOR_ASSIGN_TK);
1116 java_unget_unicode ();
1117 BUILD_OPERATOR (XOR_TK);
1121 if ((c = java_get_unicode ()) == '=')
1123 BUILD_OPERATOR2 (REM_ASSIGN_TK);
1127 java_unget_unicode ();
1128 BUILD_OPERATOR (REM_TK);
1132 if ((c = java_get_unicode()) == '=')
1134 BUILD_OPERATOR (NEQ_TK);
1138 java_unget_unicode ();
1139 BUILD_OPERATOR (NEG_TK);
1144 BUILD_OPERATOR (REL_QM_TK);
1147 BUILD_OPERATOR (REL_CL_TK);
1149 BUILD_OPERATOR (NOT_TK);
1152 /* Keyword, boolean literal or null literal */
1153 for (first_unicode = c, all_ascii = 1, ascii_index = 0;
1154 JAVA_ID_CHAR_P (c); c = java_get_unicode ())
1156 java_unicode_2_utf8 (c);
1157 if (all_ascii && c >= 128)
1162 obstack_1grow (&temporary_obstack, '\0');
1163 string = obstack_finish (&temporary_obstack);
1164 java_unget_unicode ();
1166 /* If we have something all ascii, we consider a keyword, a boolean
1167 literal, a null literal or an all ASCII identifier. Otherwise,
1168 this is an identifier (possibly not respecting formation rule). */
1171 struct java_keyword *kw;
1172 if ((kw=java_keyword (string, ascii_index)))
1174 JAVA_LEX_KW (string);
1177 case PUBLIC_TK: case PROTECTED_TK: case STATIC_TK:
1178 case ABSTRACT_TK: case FINAL_TK: case NATIVE_TK:
1179 case SYNCHRONIZED_TK: case TRANSIENT_TK: case VOLATILE_TK:
1181 SET_MODIFIER_CTX (kw->token);
1184 SET_LVAL_NODE (float_type_node);
1187 SET_LVAL_NODE (double_type_node);
1190 SET_LVAL_NODE (boolean_type_node);
1193 SET_LVAL_NODE (byte_type_node);
1196 SET_LVAL_NODE (short_type_node);
1199 SET_LVAL_NODE (int_type_node);
1202 SET_LVAL_NODE (long_type_node);
1205 SET_LVAL_NODE (char_type_node);
1208 /* Keyword based literals */
1211 SET_LVAL_NODE ((kw->token == TRUE_TK ?
1212 boolean_true_node : boolean_false_node));
1215 SET_LVAL_NODE (null_pointer_node);
1218 /* Some keyword we want to retain information on the location
1231 BUILD_OPERATOR (kw->token);
1239 /* We may have and ID here */
1240 if (JAVA_ID_CHAR_P(first_unicode) && !JAVA_DIGIT_P (first_unicode))
1242 JAVA_LEX_ID (string);
1243 java_lval->node = BUILD_ID_WFL (GET_IDENTIFIER (string));
1247 /* Everything else is an invalid character in the input */
1249 char lex_error_buffer [128];
1250 sprintf (lex_error_buffer, "Invalid character '%s' in input",
1251 java_sprint_unicode (ctxp->c_line, ctxp->c_line->current));
1252 java_lex_error (lex_error_buffer, 1);
1258 java_unicode_2_utf8 (unicode)
1261 if (RANGE (unicode, 0x01, 0x7f))
1262 obstack_1grow (&temporary_obstack, (char)unicode);
1263 else if (RANGE (unicode, 0x80, 0x7ff) || unicode == 0)
1265 obstack_1grow (&temporary_obstack,
1266 (unsigned char)(0xc0 | ((0x7c0 & unicode) >> 6)));
1267 obstack_1grow (&temporary_obstack,
1268 (unsigned char)(0x80 | (unicode & 0x3f)));
1270 else /* Range 0x800-0xffff */
1272 obstack_1grow (&temporary_obstack,
1273 (unsigned char)(0xe0 | (unicode & 0xf000) >> 12));
1274 obstack_1grow (&temporary_obstack,
1275 (unsigned char)(0x80 | (unicode & 0x0fc0) >> 6));
1276 obstack_1grow (&temporary_obstack,
1277 (unsigned char)(0x80 | (unicode & 0x003f)));
1283 build_wfl_node (node)
1286 return build_expr_wfl (node, ctxp->filename, ctxp->elc.line, ctxp->elc.col);
1291 java_lex_error (msg, forward)
1296 ctxp->elc.line = ctxp->c_line->lineno;
1297 ctxp->elc.col = ctxp->c_line->char_col-1+forward;
1299 /* Might be caught in the middle of some error report */
1300 ctxp->java_error_flag = 0;
1316 if (next != '\r' && next != EOF)
1327 java_get_line_col (filename, line, col)
1334 /* Dumb implementation. Doesn't try to cache or optimize things. */
1335 /* First line of the file is line 1, first column is 1 */
1337 /* COL == -1 means, at the CR/LF in LINE */
1338 /* COL == -2 means, at the first non space char in LINE */
1341 int c, ccol, cline = 1;
1342 int current_line_col = 0;
1343 int first_non_space = 0;
1346 if (!(fp = fopen (filename, "r")))
1347 fatal ("Can't open file - java_display_line_col");
1349 while (cline != line)
1354 static char msg[] = "<<file too short - unexpected EOF>>";
1355 obstack_grow (&temporary_obstack, msg, sizeof(msg)-1);
1358 if (java_is_eol (fp, c))
1362 /* Gather the chars of the current line in a buffer */
1366 if (c < 0 || java_is_eol (fp, c))
1368 if (!first_non_space && !JAVA_WHITE_SPACE_P (c))
1369 first_non_space = current_line_col;
1370 obstack_1grow (&temporary_obstack, c);
1375 obstack_1grow (&temporary_obstack, '\n');
1379 col = current_line_col;
1380 first_non_space = 0;
1383 col = first_non_space;
1385 first_non_space = 0;
1387 /* Place the '^' a the right position */
1388 base = obstack_base (&temporary_obstack);
1389 for (ccol = 1; ccol <= col; ccol++)
1391 /* Compute \t when reaching first_non_space */
1392 char c = (first_non_space ?
1393 (base [ccol-1] == '\t' ? '\t' : ' ') : ' ');
1394 obstack_1grow (&temporary_obstack, c);
1396 obstack_grow0 (&temporary_obstack, "^", 1);
1399 return obstack_finish (&temporary_obstack);