1 // lex.cc -- Go frontend lexer.
3 // Copyright 2009 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
11 // Manage mapping from keywords to the Keyword codes.
16 // The structure which maps keywords to codes.
20 const char* keystring
;
25 // Return the parsecode corresponding to KEYSTRING, or
26 // KEYWORD_INVALID if it is not a keyword.
28 keyword_to_code(const char* keyword
, size_t len
) const;
30 // Return the string for a keyword.
32 keyword_to_string(Keyword
) const;
35 static const Mapping mapping_
[];
36 static const int count_
;
39 // Mapping from keyword string to keyword code. This array must be
40 // kept in sorted order, and the order must match the Keyword enum.
41 // Strings are looked up using bsearch.
43 const Keywords::Mapping
44 Keywords::mapping_
[] =
46 { NULL
, KEYWORD_INVALID
},
47 { "__asm__", KEYWORD_ASM
},
48 { "break", KEYWORD_BREAK
},
49 { "case", KEYWORD_CASE
},
50 { "chan", KEYWORD_CHAN
},
51 { "const", KEYWORD_CONST
},
52 { "continue", KEYWORD_CONTINUE
},
53 { "default", KEYWORD_DEFAULT
},
54 { "defer", KEYWORD_DEFER
},
55 { "else", KEYWORD_ELSE
},
56 { "fallthrough", KEYWORD_FALLTHROUGH
},
57 { "for", KEYWORD_FOR
},
58 { "func", KEYWORD_FUNC
},
60 { "goto", KEYWORD_GOTO
},
62 { "import", KEYWORD_IMPORT
},
63 { "interface", KEYWORD_INTERFACE
},
64 { "map", KEYWORD_MAP
},
65 { "package", KEYWORD_PACKAGE
},
66 { "range", KEYWORD_RANGE
},
67 { "return", KEYWORD_RETURN
},
68 { "select", KEYWORD_SELECT
},
69 { "struct", KEYWORD_STRUCT
},
70 { "switch", KEYWORD_SWITCH
},
71 { "type", KEYWORD_TYPE
},
72 { "var", KEYWORD_VAR
}
75 // Number of entries in the map.
77 const int Keywords::count_
=
78 sizeof(Keywords::mapping_
) / sizeof(Keywords::mapping_
[0]);
80 // Comparison function passed to bsearch.
85 struct Keywords_search_key
92 keyword_compare(const void* keyv
, const void* mapv
)
94 const Keywords_search_key
* key
=
95 static_cast<const Keywords_search_key
*>(keyv
);
96 const Keywords::Mapping
* map
=
97 static_cast<const Keywords::Mapping
*>(mapv
);
98 if (map
->keystring
== NULL
)
100 int i
= strncmp(key
->str
, map
->keystring
, key
->len
);
103 if (map
->keystring
[key
->len
] != '\0')
110 // Convert a string to a keyword code. Return KEYWORD_INVALID if the
111 // string is not a keyword.
114 Keywords::keyword_to_code(const char* keyword
, size_t len
) const
116 Keywords_search_key key
;
119 void* mapv
= bsearch(&key
,
122 sizeof(this->mapping_
[0]),
125 return KEYWORD_INVALID
;
126 Mapping
* map
= static_cast<Mapping
*>(mapv
);
130 // Convert a keyword code to a string.
133 Keywords::keyword_to_string(Keyword code
) const
135 go_assert(code
> KEYWORD_INVALID
&& code
< this->count_
);
136 const Mapping
* map
= &this->mapping_
[code
];
137 go_assert(map
->keycode
== code
);
138 return map
->keystring
;
141 // There is one instance of the Keywords class.
143 static Keywords keywords
;
147 // Make a general token.
149 Token::Token(Classification classification
, Location location
)
150 : classification_(classification
), location_(location
)
161 // Clear a token--release memory.
166 if (this->classification_
== TOKEN_INTEGER
167 || this->classification_
== TOKEN_CHARACTER
)
168 mpz_clear(this->u_
.integer_value
);
169 else if (this->classification_
== TOKEN_FLOAT
170 || this->classification_
== TOKEN_IMAGINARY
)
171 mpfr_clear(this->u_
.float_value
);
174 // Construct a token.
176 Token::Token(const Token
& tok
)
177 : classification_(tok
.classification_
), location_(tok
.location_
)
179 switch (this->classification_
)
185 this->u_
.keyword
= tok
.u_
.keyword
;
187 case TOKEN_IDENTIFIER
:
189 this->u_
.string_value
= tok
.u_
.string_value
;
192 this->u_
.op
= tok
.u_
.op
;
194 case TOKEN_CHARACTER
:
196 mpz_init_set(this->u_
.integer_value
, tok
.u_
.integer_value
);
199 case TOKEN_IMAGINARY
:
200 mpfr_init_set(this->u_
.float_value
, tok
.u_
.float_value
, GMP_RNDN
);
207 // Assign to a token.
210 Token::operator=(const Token
& tok
)
213 this->classification_
= tok
.classification_
;
214 this->location_
= tok
.location_
;
215 switch (tok
.classification_
)
221 this->u_
.keyword
= tok
.u_
.keyword
;
223 case TOKEN_IDENTIFIER
:
224 this->u_
.identifier_value
.name
= tok
.u_
.identifier_value
.name
;
225 this->u_
.identifier_value
.is_exported
=
226 tok
.u_
.identifier_value
.is_exported
;
229 this->u_
.string_value
= tok
.u_
.string_value
;
232 this->u_
.op
= tok
.u_
.op
;
234 case TOKEN_CHARACTER
:
236 mpz_init_set(this->u_
.integer_value
, tok
.u_
.integer_value
);
239 case TOKEN_IMAGINARY
:
240 mpfr_init_set(this->u_
.float_value
, tok
.u_
.float_value
, GMP_RNDN
);
248 // Print the token for debugging.
251 Token::print(FILE* file
) const
253 switch (this->classification_
)
256 fprintf(file
, "invalid");
259 fprintf(file
, "EOF");
262 fprintf(file
, "keyword %s", keywords
.keyword_to_string(this->u_
.keyword
));
264 case TOKEN_IDENTIFIER
:
265 fprintf(file
, "identifier \"%s\"", this->u_
.string_value
->c_str());
268 fprintf(file
, "quoted string \"%s\"", this->u_
.string_value
->c_str());
270 case TOKEN_CHARACTER
:
271 fprintf(file
, "character ");
272 mpz_out_str(file
, 10, this->u_
.integer_value
);
275 fprintf(file
, "integer ");
276 mpz_out_str(file
, 10, this->u_
.integer_value
);
279 fprintf(file
, "float ");
280 mpfr_out_str(file
, 10, 0, this->u_
.float_value
, GMP_RNDN
);
282 case TOKEN_IMAGINARY
:
283 fprintf(file
, "imaginary ");
284 mpfr_out_str(file
, 10, 0, this->u_
.float_value
, GMP_RNDN
);
287 fprintf(file
, "operator ");
290 case OPERATOR_INVALID
:
291 fprintf(file
, "invalid");
296 case OPERATOR_ANDAND
:
338 case OPERATOR_LSHIFT
:
341 case OPERATOR_RSHIFT
:
347 case OPERATOR_BITCLEAR
:
353 case OPERATOR_CHANOP
:
359 case OPERATOR_PLUSEQ
:
362 case OPERATOR_MINUSEQ
:
371 case OPERATOR_MULTEQ
:
378 fprintf(file
, "%%=");
380 case OPERATOR_LSHIFTEQ
:
381 fprintf(file
, "<<=");
383 case OPERATOR_RSHIFTEQ
:
384 fprintf(file
, ">>=");
389 case OPERATOR_BITCLEAREQ
:
390 fprintf(file
, "&^=");
392 case OPERATOR_PLUSPLUS
:
395 case OPERATOR_MINUSMINUS
:
401 case OPERATOR_COLONEQ
:
404 case OPERATOR_SEMICOLON
:
413 case OPERATOR_LPAREN
:
416 case OPERATOR_RPAREN
:
419 case OPERATOR_LCURLY
:
422 case OPERATOR_RCURLY
:
425 case OPERATOR_LSQUARE
:
428 case OPERATOR_RSQUARE
:
442 Lex::Lex(const char* input_file_name
, FILE* input_file
, Linemap
* linemap
)
443 : input_file_name_(input_file_name
), input_file_(input_file
),
444 linemap_(linemap
), linebuf_(NULL
), linebufsize_(120), linesize_(0),
445 lineoff_(0), lineno_(0), add_semi_at_eol_(false), saw_nointerface_(false),
448 this->linebuf_
= new char[this->linebufsize_
];
449 this->linemap_
->start_file(input_file_name
, 0);
454 delete[] this->linebuf_
;
457 // Read a new line from the file.
462 char* buf
= this->linebuf_
;
463 size_t size
= this->linebufsize_
;
465 FILE* file
= this->input_file_
;
478 size_t ns
= 2 * size
+ 1;
479 if (ns
< size
|| static_cast<ssize_t
>(ns
) < 0)
480 error_at(this->location(), "out of memory");
481 char* nb
= new char[ns
];
482 memcpy(nb
, buf
, cur
);
496 this->linebuf_
= buf
;
497 this->linebufsize_
= size
;
502 // See if we need to read a new line. Return true if there is a new
503 // line, false if we are at EOF.
508 if (this->lineoff_
< this->linesize_
)
511 ssize_t got
= this->get_line();
515 this->linesize_
= got
;
518 this->linemap_
->start_line(this->lineno_
, this->linesize_
);
523 // Get the current location.
526 Lex::location() const
528 return this->linemap_
->get_location(this->lineoff_
+ 1);
531 // Get a location slightly before the current one. This is used for
532 // slightly more efficient handling of operator tokens.
535 Lex::earlier_location(int chars
) const
537 return this->linemap_
->get_location(this->lineoff_
+ 1 - chars
);
540 // Get the next token.
545 bool saw_cpp_comment
= false;
548 if (!this->require_line())
550 bool add_semi_at_eol
= this->add_semi_at_eol_
;
551 this->add_semi_at_eol_
= false;
553 return this->make_operator(OPERATOR_SEMICOLON
, 1);
554 return this->make_eof_token();
557 if (!saw_cpp_comment
)
558 this->extern_
.clear();
559 saw_cpp_comment
= false;
561 const char* p
= this->linebuf_
+ this->lineoff_
;
562 const char* pend
= this->linebuf_
+ this->linesize_
;
566 unsigned char cc
= *p
;
569 case ' ': case '\t': case '\r':
571 // Skip whitespace quickly.
572 while (*p
== ' ' || *p
== '\t' || *p
== '\r')
579 bool add_semi_at_eol
= this->add_semi_at_eol_
;
580 this->add_semi_at_eol_
= false;
583 this->lineoff_
= p
- this->linebuf_
;
584 return this->make_operator(OPERATOR_SEMICOLON
, 1);
592 this->lineoff_
= p
+ 2 - this->linebuf_
;
593 this->skip_cpp_comment();
595 if (p
[-1] == '\n' && this->add_semi_at_eol_
)
597 saw_cpp_comment
= true;
599 else if (p
[1] == '*')
601 this->lineoff_
= p
- this->linebuf_
;
602 Location location
= this->location();
603 if (!this->skip_c_comment())
604 return Token::make_invalid_token(location
);
605 p
= this->linebuf_
+ this->lineoff_
;
606 pend
= this->linebuf_
+ this->linesize_
;
608 else if (p
[1] == '=')
610 this->add_semi_at_eol_
= false;
611 this->lineoff_
= p
+ 2 - this->linebuf_
;
612 return this->make_operator(OPERATOR_DIVEQ
, 2);
616 this->add_semi_at_eol_
= false;
617 this->lineoff_
= p
+ 1 - this->linebuf_
;
618 return this->make_operator(OPERATOR_DIV
, 1);
622 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
623 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
624 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
625 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
627 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
628 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
629 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
630 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
633 this->lineoff_
= p
- this->linebuf_
;
634 return this->gather_identifier();
636 case '0': case '1': case '2': case '3': case '4':
637 case '5': case '6': case '7': case '8': case '9':
638 this->add_semi_at_eol_
= true;
639 this->lineoff_
= p
- this->linebuf_
;
640 return this->gather_number();
643 this->add_semi_at_eol_
= true;
644 this->lineoff_
= p
- this->linebuf_
;
645 return this->gather_character();
648 this->add_semi_at_eol_
= true;
649 this->lineoff_
= p
- this->linebuf_
;
650 return this->gather_string();
653 this->add_semi_at_eol_
= true;
654 this->lineoff_
= p
- this->linebuf_
;
655 return this->gather_raw_string();
662 this->add_semi_at_eol_
= false;
663 Operator op
= this->three_character_operator(cc
, p
[1], p
[2]);
664 if (op
!= OPERATOR_INVALID
)
666 this->lineoff_
= p
+ 3 - this->linebuf_
;
667 return this->make_operator(op
, 3);
678 // '/' handled above.
687 this->add_semi_at_eol_
= false;
688 Operator op
= this->two_character_operator(cc
, p
[1]);
690 if (op
!= OPERATOR_INVALID
)
697 op
= this->one_character_operator(cc
);
700 this->lineoff_
= p
+ 1 - this->linebuf_
;
701 return this->make_operator(op
, chars
);
705 if (p
[1] >= '0' && p
[1] <= '9')
707 this->add_semi_at_eol_
= true;
708 this->lineoff_
= p
- this->linebuf_
;
709 return this->gather_number();
711 if (p
[1] == '.' && p
[2] == '.')
713 this->add_semi_at_eol_
= false;
714 this->lineoff_
= p
+ 3 - this->linebuf_
;
715 return this->make_operator(OPERATOR_ELLIPSIS
, 3);
717 this->add_semi_at_eol_
= false;
718 this->lineoff_
= p
+ 1 - this->linebuf_
;
719 return this->make_operator(OPERATOR_DOT
, 1);
725 this->lineoff_
= p
- this->linebuf_
;
726 const char *pnext
= this->advance_one_utf8_char(p
, &ci
,
729 // Ignore byte order mark at start of file.
736 if (Lex::is_unicode_letter(ci
))
737 return this->gather_identifier();
740 error_at(this->location(),
741 "invalid character 0x%x in input file",
751 this->lineoff_
= p
- this->linebuf_
;
755 // Fetch one UTF-8 character from a string. Set *VALUE to the value.
756 // Return the number of bytes read from the string. Returns 0 if the
757 // string does not point to a valid UTF-8 character.
760 Lex::fetch_char(const char* p
, unsigned int* value
)
762 unsigned char c
= *p
;
768 else if ((c
& 0xe0) == 0xc0
769 && (p
[1] & 0xc0) == 0x80)
771 *value
= (((c
& 0x1f) << 6)
780 else if ((c
& 0xf0) == 0xe0
781 && (p
[1] & 0xc0) == 0x80
782 && (p
[2] & 0xc0) == 0x80)
784 *value
= (((c
& 0xf) << 12)
785 + ((p
[1] & 0x3f) << 6)
794 else if ((c
& 0xf8) == 0xf0
795 && (p
[1] & 0xc0) == 0x80
796 && (p
[2] & 0xc0) == 0x80
797 && (p
[3] & 0xc0) == 0x80)
799 *value
= (((c
& 0x7) << 18)
800 + ((p
[1] & 0x3f) << 12)
801 + ((p
[2] & 0x3f) << 6)
803 if (*value
<= 0xffff)
812 /* Invalid encoding. Return the Unicode replacement
819 // Advance one UTF-8 character. Return the pointer beyond the
820 // character. Set *VALUE to the value. Set *ISSUED_ERROR if an error
824 Lex::advance_one_utf8_char(const char* p
, unsigned int* value
,
827 *issued_error
= false;
831 error_at(this->location(), "invalid NUL byte");
832 *issued_error
= true;
837 int adv
= Lex::fetch_char(p
, value
);
840 error_at(this->location(), "invalid UTF-8 encoding");
841 *issued_error
= true;
845 // Warn about byte order mark, except at start of file.
846 if (*value
== 0xfeff && (this->lineno_
!= 1 || this->lineoff_
!= 0))
848 error_at(this->location(), "Unicode (UTF-8) BOM in middle of file");
849 *issued_error
= true;
855 // Pick up an identifier.
858 Lex::gather_identifier()
860 const char* pstart
= this->linebuf_
+ this->lineoff_
;
861 const char* p
= pstart
;
862 const char* pend
= this->linebuf_
+ this->linesize_
;
863 bool is_first
= true;
864 bool is_exported
= false;
865 bool has_non_ascii_char
= false;
869 unsigned char cc
= *p
;
872 if ((cc
< 'A' || cc
> 'Z')
873 && (cc
< 'a' || cc
> 'z')
875 && (cc
< '0' || cc
> '9'))
877 // Check for an invalid character here, as we get better
878 // error behaviour if we swallow them as part of the
879 // identifier we are building.
880 if ((cc
>= ' ' && cc
< 0x7f)
886 this->lineoff_
= p
- this->linebuf_
;
887 error_at(this->location(),
888 "invalid character 0x%x in identifier",
890 if (!has_non_ascii_char
)
892 buf
.assign(pstart
, p
- pstart
);
893 has_non_ascii_char
= true;
895 if (!Lex::is_invalid_identifier(buf
))
896 buf
.append("$INVALID$");
901 is_exported
= cc
>= 'A' && cc
<= 'Z';
904 if (has_non_ascii_char
)
911 this->lineoff_
= p
- this->linebuf_
;
912 const char* pnext
= this->advance_one_utf8_char(p
, &ci
,
914 bool is_invalid
= false;
915 if (!Lex::is_unicode_letter(ci
) && !Lex::is_unicode_digit(ci
))
917 // There is no valid place for a non-ASCII character
918 // other than an identifier, so we get better error
919 // handling behaviour if we swallow this character after
922 error_at(this->location(),
923 "invalid character 0x%x in identifier",
929 is_exported
= Lex::is_unicode_uppercase(ci
);
932 if (!has_non_ascii_char
)
934 buf
.assign(pstart
, p
- pstart
);
935 has_non_ascii_char
= true;
937 if (is_invalid
&& !Lex::is_invalid_identifier(buf
))
938 buf
.append("$INVALID$");
939 buf
.append(p
, pnext
- p
);
943 Location location
= this->location();
944 this->add_semi_at_eol_
= true;
945 this->lineoff_
= p
- this->linebuf_
;
946 if (has_non_ascii_char
)
947 return Token::make_identifier_token(buf
, is_exported
, location
);
950 Keyword code
= keywords
.keyword_to_code(pstart
, p
- pstart
);
951 if (code
== KEYWORD_INVALID
)
952 return Token::make_identifier_token(std::string(pstart
, p
- pstart
),
953 is_exported
, location
);
959 case KEYWORD_CONTINUE
:
960 case KEYWORD_FALLTHROUGH
:
964 this->add_semi_at_eol_
= false;
967 return Token::make_keyword_token(code
, location
);
972 // Return whether C is a hex digit.
975 Lex::is_hex_digit(char c
)
977 return ((c
>= '0' && c
<= '9')
978 || (c
>= 'A' && c
<= 'F')
979 || (c
>= 'a' && c
<= 'f'));
982 // Return whether an exponent could start at P.
985 Lex::could_be_exponent(const char* p
, const char* pend
)
987 if (*p
!= 'e' && *p
!= 'E')
992 if (*p
== '+' || *p
== '-')
998 return *p
>= '0' && *p
<= '9';
1001 // Pick up a number.
1004 Lex::gather_number()
1006 const char* pstart
= this->linebuf_
+ this->lineoff_
;
1007 const char* p
= pstart
;
1008 const char* pend
= this->linebuf_
+ this->linesize_
;
1010 Location location
= this->location();
1021 const char* pnum
= p
;
1025 if ((p
[1] == 'x' || p
[1] == 'X')
1026 && Lex::is_hex_digit(p
[2]))
1033 if (!Lex::is_hex_digit(*p
))
1044 if (*p
< '0' || *p
> '7')
1050 // A partial token that looks like an octal literal might actually be the
1051 // beginning of a floating-point or imaginary literal.
1052 if (base
== 16 || (*p
!= '.' && *p
!= 'i' && !Lex::could_be_exponent(p
, pend
)))
1054 std::string
s(pnum
, p
- pnum
);
1056 int r
= mpz_init_set_str(val
, s
.c_str(), base
);
1062 this->lineoff_
= p
- this->linebuf_
;
1063 Token ret
= Token::make_integer_token(val
, location
);
1071 if (*p
< '0' || *p
> '9')
1076 if (*p
!= '.' && *p
!= 'i' && !Lex::could_be_exponent(p
, pend
))
1078 std::string
s(pnum
, p
- pnum
);
1080 int r
= mpz_init_set_str(val
, s
.c_str(), 10);
1086 this->lineoff_
= p
- this->linebuf_
;
1087 Token ret
= Token::make_integer_token(val
, location
);
1094 bool dot
= *p
== '.';
1100 if (*p
== '+' || *p
== '-')
1106 if (*p
< '0' || *p
> '9')
1111 if (dot
&& Lex::could_be_exponent(p
, pend
))
1114 if (*p
== '+' || *p
== '-')
1118 if (*p
< '0' || *p
> '9')
1125 std::string
s(pnum
, p
- pnum
);
1127 int r
= mpfr_init_set_str(val
, s
.c_str(), 10, GMP_RNDN
);
1131 mpfr_neg(val
, val
, GMP_RNDN
);
1133 bool is_imaginary
= *p
== 'i';
1137 this->lineoff_
= p
- this->linebuf_
;
1140 Token ret
= Token::make_imaginary_token(val
, location
);
1146 Token ret
= Token::make_float_token(val
, location
);
1152 // Advance one character, possibly escaped. Return the pointer beyond
1153 // the character. Set *VALUE to the character. Set *IS_CHARACTER if
1154 // this is a character (e.g., 'a' or '\u1234') rather than a byte
1155 // value (e.g., '\001').
1158 Lex::advance_one_char(const char* p
, bool is_single_quote
, unsigned int* value
,
1162 *is_character
= true;
1166 const char* ret
= this->advance_one_utf8_char(p
, value
, &issued_error
);
1168 && (*value
== '\'' || *value
== '\n')
1170 error_at(this->location(), "invalid character literal");
1178 case '0': case '1': case '2': case '3':
1179 case '4': case '5': case '6': case '7':
1180 *is_character
= false;
1181 if (p
[1] >= '0' && p
[1] <= '7'
1182 && p
[2] >= '0' && p
[2] <= '7')
1184 *value
= ((Lex::octal_value(p
[0]) << 6)
1185 + (Lex::octal_value(p
[1]) << 3)
1186 + Lex::octal_value(p
[2]));
1189 error_at(this->location(), "invalid octal constant");
1194 error_at(this->location(), "invalid octal character");
1195 return (p
[1] >= '0' && p
[1] <= '7'
1201 *is_character
= false;
1202 if (Lex::is_hex_digit(p
[1]) && Lex::is_hex_digit(p
[2]))
1204 *value
= (hex_value(p
[1]) << 4) + hex_value(p
[2]);
1207 error_at(this->location(), "invalid hex character");
1208 return (Lex::is_hex_digit(p
[1])
1237 if (!is_single_quote
)
1238 error_at(this->location(), "invalid quoted character");
1242 if (is_single_quote
)
1243 error_at(this->location(), "invalid quoted character");
1248 if (Lex::is_hex_digit(p
[1]) && Lex::is_hex_digit(p
[2])
1249 && Lex::is_hex_digit(p
[3]) && Lex::is_hex_digit(p
[4]))
1251 *value
= ((hex_value(p
[1]) << 12)
1252 + (hex_value(p
[2]) << 8)
1253 + (hex_value(p
[3]) << 4)
1255 if (*value
>= 0xd800 && *value
< 0xe000)
1257 error_at(this->location(),
1258 "invalid unicode code point 0x%x",
1260 // Use the replacement character.
1265 error_at(this->location(), "invalid little unicode code point");
1269 if (Lex::is_hex_digit(p
[1]) && Lex::is_hex_digit(p
[2])
1270 && Lex::is_hex_digit(p
[3]) && Lex::is_hex_digit(p
[4])
1271 && Lex::is_hex_digit(p
[5]) && Lex::is_hex_digit(p
[6])
1272 && Lex::is_hex_digit(p
[7]) && Lex::is_hex_digit(p
[8]))
1274 *value
= ((hex_value(p
[1]) << 28)
1275 + (hex_value(p
[2]) << 24)
1276 + (hex_value(p
[3]) << 20)
1277 + (hex_value(p
[4]) << 16)
1278 + (hex_value(p
[5]) << 12)
1279 + (hex_value(p
[6]) << 8)
1280 + (hex_value(p
[7]) << 4)
1282 if (*value
> 0x10ffff
1283 || (*value
>= 0xd800 && *value
< 0xe000))
1285 error_at(this->location(), "invalid unicode code point 0x%x",
1287 // Use the replacement character.
1292 error_at(this->location(), "invalid big unicode code point");
1296 error_at(this->location(), "invalid character after %<\\%>");
1303 // Append V to STR. IS_CHARACTER is true for a character which should
1304 // be stored in UTF-8, false for a general byte value which should be
1308 Lex::append_char(unsigned int v
, bool is_character
, std::string
* str
,
1313 if (v
<= 0x7f || !is_character
)
1318 else if (v
<= 0x7ff)
1320 buf
[0] = 0xc0 + (v
>> 6);
1321 buf
[1] = 0x80 + (v
& 0x3f);
1328 warning_at(location
, 0,
1329 "unicode code point 0x%x out of range in string", v
);
1330 // Turn it into the "replacement character".
1333 if (v
>= 0xd800 && v
< 0xe000)
1335 warning_at(location
, 0,
1336 "unicode code point 0x%x is invalid surrogate pair", v
);
1341 buf
[0] = 0xe0 + (v
>> 12);
1342 buf
[1] = 0x80 + ((v
>> 6) & 0x3f);
1343 buf
[2] = 0x80 + (v
& 0x3f);
1348 buf
[0] = 0xf0 + (v
>> 18);
1349 buf
[1] = 0x80 + ((v
>> 12) & 0x3f);
1350 buf
[2] = 0x80 + ((v
>> 6) & 0x3f);
1351 buf
[3] = 0x80 + (v
& 0x3f);
1355 str
->append(buf
, len
);
1358 // Pick up a character literal.
1361 Lex::gather_character()
1364 const char* pstart
= this->linebuf_
+ this->lineoff_
;
1365 const char* p
= pstart
;
1369 p
= this->advance_one_char(p
, true, &value
, &is_character
);
1373 error_at(this->location(), "unterminated character constant");
1374 this->lineoff_
= p
- this->linebuf_
;
1375 return this->make_invalid_token();
1379 mpz_init_set_ui(val
, value
);
1381 Location location
= this->location();
1382 this->lineoff_
= p
+ 1 - this->linebuf_
;
1383 Token ret
= Token::make_character_token(val
, location
);
1388 // Pick up a quoted string.
1391 Lex::gather_string()
1393 const char* pstart
= this->linebuf_
+ this->lineoff_
+ 1;
1394 const char* p
= pstart
;
1395 const char* pend
= this->linebuf_
+ this->linesize_
;
1400 Location loc
= this->location();
1403 this->lineoff_
= p
- this->linebuf_
;
1404 p
= this->advance_one_char(p
, false, &c
, &is_character
);
1407 error_at(this->location(), "unterminated string");
1411 Lex::append_char(c
, is_character
, &value
, loc
);
1414 Location location
= this->location();
1415 this->lineoff_
= p
+ 1 - this->linebuf_
;
1416 return Token::make_string_token(value
, location
);
1419 // Pick up a raw string.
1422 Lex::gather_raw_string()
1424 const char* p
= this->linebuf_
+ this->lineoff_
+ 1;
1425 const char* pend
= this->linebuf_
+ this->linesize_
;
1426 Location location
= this->location();
1435 this->lineoff_
= p
+ 1 - this->linebuf_
;
1436 return Token::make_string_token(value
, location
);
1438 Location loc
= this->location();
1441 this->lineoff_
= p
- this->linebuf_
;
1442 p
= this->advance_one_utf8_char(p
, &c
, &issued_error
);
1443 Lex::append_char(c
, true, &value
, loc
);
1445 this->lineoff_
= p
- this->linebuf_
;
1446 if (!this->require_line())
1448 error_at(location
, "unterminated raw string");
1449 return Token::make_string_token(value
, location
);
1451 p
= this->linebuf_
+ this->lineoff_
;
1452 pend
= this->linebuf_
+ this->linesize_
;
1456 // If C1 C2 C3 are a three character operator, return the code.
1459 Lex::three_character_operator(char c1
, char c2
, char c3
)
1463 if (c1
== '<' && c2
== '<')
1464 return OPERATOR_LSHIFTEQ
;
1465 else if (c1
== '>' && c2
== '>')
1466 return OPERATOR_RSHIFTEQ
;
1467 else if (c1
== '&' && c2
== '^')
1468 return OPERATOR_BITCLEAREQ
;
1470 return OPERATOR_INVALID
;
1473 // If C1 C2 are a two character operator, return the code.
1476 Lex::two_character_operator(char c1
, char c2
)
1482 return OPERATOR_OROR
;
1484 return OPERATOR_OREQ
;
1488 return OPERATOR_ANDAND
;
1490 return OPERATOR_BITCLEAR
;
1492 return OPERATOR_ANDEQ
;
1496 return OPERATOR_XOREQ
;
1500 return OPERATOR_EQEQ
;
1504 return OPERATOR_NOTEQ
;
1510 return OPERATOR_LSHIFT
;
1512 return OPERATOR_CHANOP
;
1518 return OPERATOR_RSHIFT
;
1522 return OPERATOR_MULTEQ
;
1526 return OPERATOR_DIVEQ
;
1530 return OPERATOR_MODEQ
;
1535 this->add_semi_at_eol_
= true;
1536 return OPERATOR_PLUSPLUS
;
1539 return OPERATOR_PLUSEQ
;
1544 this->add_semi_at_eol_
= true;
1545 return OPERATOR_MINUSMINUS
;
1548 return OPERATOR_MINUSEQ
;
1552 return OPERATOR_COLONEQ
;
1557 return OPERATOR_INVALID
;
1560 // If character C is an operator, return the code.
1563 Lex::one_character_operator(char c
)
1572 return OPERATOR_PLUS
;
1574 return OPERATOR_MINUS
;
1578 return OPERATOR_XOR
;
1580 return OPERATOR_MULT
;
1582 return OPERATOR_DIV
;
1584 return OPERATOR_MOD
;
1586 return OPERATOR_AND
;
1588 return OPERATOR_NOT
;
1592 return OPERATOR_COLON
;
1594 return OPERATOR_SEMICOLON
;
1596 return OPERATOR_DOT
;
1598 return OPERATOR_COMMA
;
1600 return OPERATOR_LPAREN
;
1602 this->add_semi_at_eol_
= true;
1603 return OPERATOR_RPAREN
;
1605 return OPERATOR_LCURLY
;
1607 this->add_semi_at_eol_
= true;
1608 return OPERATOR_RCURLY
;
1610 return OPERATOR_LSQUARE
;
1612 this->add_semi_at_eol_
= true;
1613 return OPERATOR_RSQUARE
;
1615 return OPERATOR_INVALID
;
1619 // Skip a C-style comment.
1622 Lex::skip_c_comment()
1626 if (!this->require_line())
1628 error_at(this->location(), "unterminated comment");
1632 const char* p
= this->linebuf_
+ this->lineoff_
;
1633 const char* pend
= this->linebuf_
+ this->linesize_
;
1637 if (p
[0] == '*' && p
+ 1 < pend
&& p
[1] == '/')
1639 this->lineoff_
= p
+ 2 - this->linebuf_
;
1643 this->lineoff_
= p
- this->linebuf_
;
1646 p
= this->advance_one_utf8_char(p
, &c
, &issued_error
);
1649 this->lineoff_
= p
- this->linebuf_
;
1653 // Skip a C++-style comment.
1656 Lex::skip_cpp_comment()
1658 // Ensure that if EXTERN_ is set, it means that we just saw a
1659 // //extern comment.
1660 this->extern_
.clear();
1662 const char* p
= this->linebuf_
+ this->lineoff_
;
1663 const char* pend
= this->linebuf_
+ this->linesize_
;
1665 // By convention, a C++ comment at the start of the line of the form
1666 // //line FILE:LINENO
1667 // is interpreted as setting the file name and line number of the
1668 // next source line.
1670 if (this->lineoff_
== 2
1672 && memcmp(p
, "line ", 5) == 0)
1675 while (p
< pend
&& *p
== ' ')
1677 const char* pcolon
= static_cast<const char*>(memchr(p
, ':', pend
- p
));
1680 && pcolon
[1] <= '9')
1683 long lineno
= strtol(pcolon
+ 1, &plend
, 10);
1684 if (plend
> pcolon
+ 1
1689 && lineno
< 0x7fffffff)
1691 unsigned int filelen
= pcolon
- p
;
1692 char* file
= new char[filelen
+ 1];
1693 memcpy(file
, p
, filelen
);
1694 file
[filelen
] = '\0';
1696 this->linemap_
->start_file(file
, lineno
);
1697 this->lineno_
= lineno
- 1;
1704 // As a special gccgo extension, a C++ comment at the start of the
1707 // which immediately precedes a function declaration means that the
1708 // external name of the function declaration is NAME. This is
1709 // normally used to permit Go code to call a C function.
1710 if (this->lineoff_
== 2
1712 && memcmp(p
, "extern ", 7) == 0)
1715 while (p
< pend
&& (*p
== ' ' || *p
== '\t'))
1717 const char* plend
= pend
;
1719 && (plend
[-1] == ' ' || plend
[-1] == '\t' || plend
[-1] == '\n'))
1722 this->extern_
= std::string(p
, plend
- p
);
1725 // For field tracking analysis: a //go:nointerface comment means
1726 // that the next interface method should not be stored in the type
1727 // descriptor. This permits it to be discarded if it is not needed.
1728 if (this->lineoff_
== 2 && memcmp(p
, "go:nointerface", 14) == 0)
1729 this->saw_nointerface_
= true;
1733 this->lineoff_
= p
- this->linebuf_
;
1736 p
= this->advance_one_utf8_char(p
, &c
, &issued_error
);
1738 this->extern_
.clear();
1742 // The Unicode tables use this struct.
1744 struct Unicode_range
1746 // The low end of the range.
1748 // The high end of the range.
1750 // The stride. This entries represents low, low + stride, low + 2 *
1751 // stride, etc., up to high.
1752 unsigned int stride
;
1755 // A table of whitespace characters--Unicode code points classified as
1756 // "Space", "C" locale whitespace characters, the "next line" control
1757 // character (0085), the line separator (2028), the paragraph
1758 // separator (2029), and the "zero-width non-break space" (feff).
1760 static const Unicode_range unicode_space
[] =
1762 { 0x0009, 0x000d, 1 },
1763 { 0x0020, 0x0020, 1 },
1764 { 0x0085, 0x0085, 1 },
1765 { 0x00a0, 0x00a0, 1 },
1766 { 0x1680, 0x1680, 1 },
1767 { 0x180e, 0x180e, 1 },
1768 { 0x2000, 0x200a, 1 },
1769 { 0x2028, 0x2029, 1 },
1770 { 0x202f, 0x202f, 1 },
1771 { 0x205f, 0x205f, 1 },
1772 { 0x3000, 0x3000, 1 },
1773 { 0xfeff, 0xfeff, 1 },
1776 // A table of Unicode digits--Unicode code points classified as
1779 static const Unicode_range unicode_digits
[] =
1781 { 0x0030, 0x0039, 1},
1782 { 0x0660, 0x0669, 1},
1783 { 0x06f0, 0x06f9, 1},
1784 { 0x07c0, 0x07c9, 1},
1785 { 0x0966, 0x096f, 1},
1786 { 0x09e6, 0x09ef, 1},
1787 { 0x0a66, 0x0a6f, 1},
1788 { 0x0ae6, 0x0aef, 1},
1789 { 0x0b66, 0x0b6f, 1},
1790 { 0x0be6, 0x0bef, 1},
1791 { 0x0c66, 0x0c6f, 1},
1792 { 0x0ce6, 0x0cef, 1},
1793 { 0x0d66, 0x0d6f, 1},
1794 { 0x0e50, 0x0e59, 1},
1795 { 0x0ed0, 0x0ed9, 1},
1796 { 0x0f20, 0x0f29, 1},
1797 { 0x1040, 0x1049, 1},
1798 { 0x17e0, 0x17e9, 1},
1799 { 0x1810, 0x1819, 1},
1800 { 0x1946, 0x194f, 1},
1801 { 0x19d0, 0x19d9, 1},
1802 { 0x1b50, 0x1b59, 1},
1803 { 0xff10, 0xff19, 1},
1804 { 0x104a0, 0x104a9, 1},
1805 { 0x1d7ce, 0x1d7ff, 1},
1808 // A table of Unicode letters--Unicode code points classified as
1811 static const Unicode_range unicode_letters
[] =
1813 { 0x0041, 0x005a, 1},
1814 { 0x0061, 0x007a, 1},
1815 { 0x00aa, 0x00b5, 11},
1816 { 0x00ba, 0x00ba, 1},
1817 { 0x00c0, 0x00d6, 1},
1818 { 0x00d8, 0x00f6, 1},
1819 { 0x00f8, 0x02c1, 1},
1820 { 0x02c6, 0x02d1, 1},
1821 { 0x02e0, 0x02e4, 1},
1822 { 0x02ec, 0x02ee, 2},
1823 { 0x0370, 0x0374, 1},
1824 { 0x0376, 0x0377, 1},
1825 { 0x037a, 0x037d, 1},
1826 { 0x0386, 0x0386, 1},
1827 { 0x0388, 0x038a, 1},
1828 { 0x038c, 0x038c, 1},
1829 { 0x038e, 0x03a1, 1},
1830 { 0x03a3, 0x03f5, 1},
1831 { 0x03f7, 0x0481, 1},
1832 { 0x048a, 0x0523, 1},
1833 { 0x0531, 0x0556, 1},
1834 { 0x0559, 0x0559, 1},
1835 { 0x0561, 0x0587, 1},
1836 { 0x05d0, 0x05ea, 1},
1837 { 0x05f0, 0x05f2, 1},
1838 { 0x0621, 0x064a, 1},
1839 { 0x066e, 0x066f, 1},
1840 { 0x0671, 0x06d3, 1},
1841 { 0x06d5, 0x06d5, 1},
1842 { 0x06e5, 0x06e6, 1},
1843 { 0x06ee, 0x06ef, 1},
1844 { 0x06fa, 0x06fc, 1},
1845 { 0x06ff, 0x0710, 17},
1846 { 0x0712, 0x072f, 1},
1847 { 0x074d, 0x07a5, 1},
1848 { 0x07b1, 0x07b1, 1},
1849 { 0x07ca, 0x07ea, 1},
1850 { 0x07f4, 0x07f5, 1},
1851 { 0x07fa, 0x07fa, 1},
1852 { 0x0904, 0x0939, 1},
1853 { 0x093d, 0x0950, 19},
1854 { 0x0958, 0x0961, 1},
1855 { 0x0971, 0x0972, 1},
1856 { 0x097b, 0x097f, 1},
1857 { 0x0985, 0x098c, 1},
1858 { 0x098f, 0x0990, 1},
1859 { 0x0993, 0x09a8, 1},
1860 { 0x09aa, 0x09b0, 1},
1861 { 0x09b2, 0x09b2, 1},
1862 { 0x09b6, 0x09b9, 1},
1863 { 0x09bd, 0x09ce, 17},
1864 { 0x09dc, 0x09dd, 1},
1865 { 0x09df, 0x09e1, 1},
1866 { 0x09f0, 0x09f1, 1},
1867 { 0x0a05, 0x0a0a, 1},
1868 { 0x0a0f, 0x0a10, 1},
1869 { 0x0a13, 0x0a28, 1},
1870 { 0x0a2a, 0x0a30, 1},
1871 { 0x0a32, 0x0a33, 1},
1872 { 0x0a35, 0x0a36, 1},
1873 { 0x0a38, 0x0a39, 1},
1874 { 0x0a59, 0x0a5c, 1},
1875 { 0x0a5e, 0x0a5e, 1},
1876 { 0x0a72, 0x0a74, 1},
1877 { 0x0a85, 0x0a8d, 1},
1878 { 0x0a8f, 0x0a91, 1},
1879 { 0x0a93, 0x0aa8, 1},
1880 { 0x0aaa, 0x0ab0, 1},
1881 { 0x0ab2, 0x0ab3, 1},
1882 { 0x0ab5, 0x0ab9, 1},
1883 { 0x0abd, 0x0ad0, 19},
1884 { 0x0ae0, 0x0ae1, 1},
1885 { 0x0b05, 0x0b0c, 1},
1886 { 0x0b0f, 0x0b10, 1},
1887 { 0x0b13, 0x0b28, 1},
1888 { 0x0b2a, 0x0b30, 1},
1889 { 0x0b32, 0x0b33, 1},
1890 { 0x0b35, 0x0b39, 1},
1891 { 0x0b3d, 0x0b3d, 1},
1892 { 0x0b5c, 0x0b5d, 1},
1893 { 0x0b5f, 0x0b61, 1},
1894 { 0x0b71, 0x0b83, 18},
1895 { 0x0b85, 0x0b8a, 1},
1896 { 0x0b8e, 0x0b90, 1},
1897 { 0x0b92, 0x0b95, 1},
1898 { 0x0b99, 0x0b9a, 1},
1899 { 0x0b9c, 0x0b9c, 1},
1900 { 0x0b9e, 0x0b9f, 1},
1901 { 0x0ba3, 0x0ba4, 1},
1902 { 0x0ba8, 0x0baa, 1},
1903 { 0x0bae, 0x0bb9, 1},
1904 { 0x0bd0, 0x0bd0, 1},
1905 { 0x0c05, 0x0c0c, 1},
1906 { 0x0c0e, 0x0c10, 1},
1907 { 0x0c12, 0x0c28, 1},
1908 { 0x0c2a, 0x0c33, 1},
1909 { 0x0c35, 0x0c39, 1},
1910 { 0x0c3d, 0x0c3d, 1},
1911 { 0x0c58, 0x0c59, 1},
1912 { 0x0c60, 0x0c61, 1},
1913 { 0x0c85, 0x0c8c, 1},
1914 { 0x0c8e, 0x0c90, 1},
1915 { 0x0c92, 0x0ca8, 1},
1916 { 0x0caa, 0x0cb3, 1},
1917 { 0x0cb5, 0x0cb9, 1},
1918 { 0x0cbd, 0x0cde, 33},
1919 { 0x0ce0, 0x0ce1, 1},
1920 { 0x0d05, 0x0d0c, 1},
1921 { 0x0d0e, 0x0d10, 1},
1922 { 0x0d12, 0x0d28, 1},
1923 { 0x0d2a, 0x0d39, 1},
1924 { 0x0d3d, 0x0d3d, 1},
1925 { 0x0d60, 0x0d61, 1},
1926 { 0x0d7a, 0x0d7f, 1},
1927 { 0x0d85, 0x0d96, 1},
1928 { 0x0d9a, 0x0db1, 1},
1929 { 0x0db3, 0x0dbb, 1},
1930 { 0x0dbd, 0x0dbd, 1},
1931 { 0x0dc0, 0x0dc6, 1},
1932 { 0x0e01, 0x0e30, 1},
1933 { 0x0e32, 0x0e33, 1},
1934 { 0x0e40, 0x0e46, 1},
1935 { 0x0e81, 0x0e82, 1},
1936 { 0x0e84, 0x0e84, 1},
1937 { 0x0e87, 0x0e88, 1},
1938 { 0x0e8a, 0x0e8d, 3},
1939 { 0x0e94, 0x0e97, 1},
1940 { 0x0e99, 0x0e9f, 1},
1941 { 0x0ea1, 0x0ea3, 1},
1942 { 0x0ea5, 0x0ea7, 2},
1943 { 0x0eaa, 0x0eab, 1},
1944 { 0x0ead, 0x0eb0, 1},
1945 { 0x0eb2, 0x0eb3, 1},
1946 { 0x0ebd, 0x0ebd, 1},
1947 { 0x0ec0, 0x0ec4, 1},
1948 { 0x0ec6, 0x0ec6, 1},
1949 { 0x0edc, 0x0edd, 1},
1950 { 0x0f00, 0x0f00, 1},
1951 { 0x0f40, 0x0f47, 1},
1952 { 0x0f49, 0x0f6c, 1},
1953 { 0x0f88, 0x0f8b, 1},
1954 { 0x1000, 0x102a, 1},
1955 { 0x103f, 0x103f, 1},
1956 { 0x1050, 0x1055, 1},
1957 { 0x105a, 0x105d, 1},
1958 { 0x1061, 0x1061, 1},
1959 { 0x1065, 0x1066, 1},
1960 { 0x106e, 0x1070, 1},
1961 { 0x1075, 0x1081, 1},
1962 { 0x108e, 0x108e, 1},
1963 { 0x10a0, 0x10c5, 1},
1964 { 0x10d0, 0x10fa, 1},
1965 { 0x10fc, 0x10fc, 1},
1966 { 0x1100, 0x1159, 1},
1967 { 0x115f, 0x11a2, 1},
1968 { 0x11a8, 0x11f9, 1},
1969 { 0x1200, 0x1248, 1},
1970 { 0x124a, 0x124d, 1},
1971 { 0x1250, 0x1256, 1},
1972 { 0x1258, 0x1258, 1},
1973 { 0x125a, 0x125d, 1},
1974 { 0x1260, 0x1288, 1},
1975 { 0x128a, 0x128d, 1},
1976 { 0x1290, 0x12b0, 1},
1977 { 0x12b2, 0x12b5, 1},
1978 { 0x12b8, 0x12be, 1},
1979 { 0x12c0, 0x12c0, 1},
1980 { 0x12c2, 0x12c5, 1},
1981 { 0x12c8, 0x12d6, 1},
1982 { 0x12d8, 0x1310, 1},
1983 { 0x1312, 0x1315, 1},
1984 { 0x1318, 0x135a, 1},
1985 { 0x1380, 0x138f, 1},
1986 { 0x13a0, 0x13f4, 1},
1987 { 0x1401, 0x166c, 1},
1988 { 0x166f, 0x1676, 1},
1989 { 0x1681, 0x169a, 1},
1990 { 0x16a0, 0x16ea, 1},
1991 { 0x1700, 0x170c, 1},
1992 { 0x170e, 0x1711, 1},
1993 { 0x1720, 0x1731, 1},
1994 { 0x1740, 0x1751, 1},
1995 { 0x1760, 0x176c, 1},
1996 { 0x176e, 0x1770, 1},
1997 { 0x1780, 0x17b3, 1},
1998 { 0x17d7, 0x17dc, 5},
1999 { 0x1820, 0x1877, 1},
2000 { 0x1880, 0x18a8, 1},
2001 { 0x18aa, 0x18aa, 1},
2002 { 0x1900, 0x191c, 1},
2003 { 0x1950, 0x196d, 1},
2004 { 0x1970, 0x1974, 1},
2005 { 0x1980, 0x19a9, 1},
2006 { 0x19c1, 0x19c7, 1},
2007 { 0x1a00, 0x1a16, 1},
2008 { 0x1b05, 0x1b33, 1},
2009 { 0x1b45, 0x1b4b, 1},
2010 { 0x1b83, 0x1ba0, 1},
2011 { 0x1bae, 0x1baf, 1},
2012 { 0x1c00, 0x1c23, 1},
2013 { 0x1c4d, 0x1c4f, 1},
2014 { 0x1c5a, 0x1c7d, 1},
2015 { 0x1d00, 0x1dbf, 1},
2016 { 0x1e00, 0x1f15, 1},
2017 { 0x1f18, 0x1f1d, 1},
2018 { 0x1f20, 0x1f45, 1},
2019 { 0x1f48, 0x1f4d, 1},
2020 { 0x1f50, 0x1f57, 1},
2021 { 0x1f59, 0x1f5d, 2},
2022 { 0x1f5f, 0x1f7d, 1},
2023 { 0x1f80, 0x1fb4, 1},
2024 { 0x1fb6, 0x1fbc, 1},
2025 { 0x1fbe, 0x1fbe, 1},
2026 { 0x1fc2, 0x1fc4, 1},
2027 { 0x1fc6, 0x1fcc, 1},
2028 { 0x1fd0, 0x1fd3, 1},
2029 { 0x1fd6, 0x1fdb, 1},
2030 { 0x1fe0, 0x1fec, 1},
2031 { 0x1ff2, 0x1ff4, 1},
2032 { 0x1ff6, 0x1ffc, 1},
2033 { 0x2071, 0x207f, 14},
2034 { 0x2090, 0x2094, 1},
2035 { 0x2102, 0x2107, 5},
2036 { 0x210a, 0x2113, 1},
2037 { 0x2115, 0x2115, 1},
2038 { 0x2119, 0x211d, 1},
2039 { 0x2124, 0x2128, 2},
2040 { 0x212a, 0x212d, 1},
2041 { 0x212f, 0x2139, 1},
2042 { 0x213c, 0x213f, 1},
2043 { 0x2145, 0x2149, 1},
2044 { 0x214e, 0x214e, 1},
2045 { 0x2183, 0x2184, 1},
2046 { 0x2c00, 0x2c2e, 1},
2047 { 0x2c30, 0x2c5e, 1},
2048 { 0x2c60, 0x2c6f, 1},
2049 { 0x2c71, 0x2c7d, 1},
2050 { 0x2c80, 0x2ce4, 1},
2051 { 0x2d00, 0x2d25, 1},
2052 { 0x2d30, 0x2d65, 1},
2053 { 0x2d6f, 0x2d6f, 1},
2054 { 0x2d80, 0x2d96, 1},
2055 { 0x2da0, 0x2da6, 1},
2056 { 0x2da8, 0x2dae, 1},
2057 { 0x2db0, 0x2db6, 1},
2058 { 0x2db8, 0x2dbe, 1},
2059 { 0x2dc0, 0x2dc6, 1},
2060 { 0x2dc8, 0x2dce, 1},
2061 { 0x2dd0, 0x2dd6, 1},
2062 { 0x2dd8, 0x2dde, 1},
2063 { 0x2e2f, 0x2e2f, 1},
2064 { 0x3005, 0x3006, 1},
2065 { 0x3031, 0x3035, 1},
2066 { 0x303b, 0x303c, 1},
2067 { 0x3041, 0x3096, 1},
2068 { 0x309d, 0x309f, 1},
2069 { 0x30a1, 0x30fa, 1},
2070 { 0x30fc, 0x30ff, 1},
2071 { 0x3105, 0x312d, 1},
2072 { 0x3131, 0x318e, 1},
2073 { 0x31a0, 0x31b7, 1},
2074 { 0x31f0, 0x31ff, 1},
2075 { 0x3400, 0x4db5, 1},
2076 { 0x4e00, 0x9fc3, 1},
2077 { 0xa000, 0xa48c, 1},
2078 { 0xa500, 0xa60c, 1},
2079 { 0xa610, 0xa61f, 1},
2080 { 0xa62a, 0xa62b, 1},
2081 { 0xa640, 0xa65f, 1},
2082 { 0xa662, 0xa66e, 1},
2083 { 0xa67f, 0xa697, 1},
2084 { 0xa717, 0xa71f, 1},
2085 { 0xa722, 0xa788, 1},
2086 { 0xa78b, 0xa78c, 1},
2087 { 0xa7fb, 0xa801, 1},
2088 { 0xa803, 0xa805, 1},
2089 { 0xa807, 0xa80a, 1},
2090 { 0xa80c, 0xa822, 1},
2091 { 0xa840, 0xa873, 1},
2092 { 0xa882, 0xa8b3, 1},
2093 { 0xa90a, 0xa925, 1},
2094 { 0xa930, 0xa946, 1},
2095 { 0xaa00, 0xaa28, 1},
2096 { 0xaa40, 0xaa42, 1},
2097 { 0xaa44, 0xaa4b, 1},
2098 { 0xac00, 0xd7a3, 1},
2099 { 0xf900, 0xfa2d, 1},
2100 { 0xfa30, 0xfa6a, 1},
2101 { 0xfa70, 0xfad9, 1},
2102 { 0xfb00, 0xfb06, 1},
2103 { 0xfb13, 0xfb17, 1},
2104 { 0xfb1d, 0xfb1d, 1},
2105 { 0xfb1f, 0xfb28, 1},
2106 { 0xfb2a, 0xfb36, 1},
2107 { 0xfb38, 0xfb3c, 1},
2108 { 0xfb3e, 0xfb3e, 1},
2109 { 0xfb40, 0xfb41, 1},
2110 { 0xfb43, 0xfb44, 1},
2111 { 0xfb46, 0xfbb1, 1},
2112 { 0xfbd3, 0xfd3d, 1},
2113 { 0xfd50, 0xfd8f, 1},
2114 { 0xfd92, 0xfdc7, 1},
2115 { 0xfdf0, 0xfdfb, 1},
2116 { 0xfe70, 0xfe74, 1},
2117 { 0xfe76, 0xfefc, 1},
2118 { 0xff21, 0xff3a, 1},
2119 { 0xff41, 0xff5a, 1},
2120 { 0xff66, 0xffbe, 1},
2121 { 0xffc2, 0xffc7, 1},
2122 { 0xffca, 0xffcf, 1},
2123 { 0xffd2, 0xffd7, 1},
2124 { 0xffda, 0xffdc, 1},
2125 { 0x10000, 0x1000b, 1},
2126 { 0x1000d, 0x10026, 1},
2127 { 0x10028, 0x1003a, 1},
2128 { 0x1003c, 0x1003d, 1},
2129 { 0x1003f, 0x1004d, 1},
2130 { 0x10050, 0x1005d, 1},
2131 { 0x10080, 0x100fa, 1},
2132 { 0x10280, 0x1029c, 1},
2133 { 0x102a0, 0x102d0, 1},
2134 { 0x10300, 0x1031e, 1},
2135 { 0x10330, 0x10340, 1},
2136 { 0x10342, 0x10349, 1},
2137 { 0x10380, 0x1039d, 1},
2138 { 0x103a0, 0x103c3, 1},
2139 { 0x103c8, 0x103cf, 1},
2140 { 0x10400, 0x1049d, 1},
2141 { 0x10800, 0x10805, 1},
2142 { 0x10808, 0x10808, 1},
2143 { 0x1080a, 0x10835, 1},
2144 { 0x10837, 0x10838, 1},
2145 { 0x1083c, 0x1083f, 3},
2146 { 0x10900, 0x10915, 1},
2147 { 0x10920, 0x10939, 1},
2148 { 0x10a00, 0x10a00, 1},
2149 { 0x10a10, 0x10a13, 1},
2150 { 0x10a15, 0x10a17, 1},
2151 { 0x10a19, 0x10a33, 1},
2152 { 0x12000, 0x1236e, 1},
2153 { 0x1d400, 0x1d454, 1},
2154 { 0x1d456, 0x1d49c, 1},
2155 { 0x1d49e, 0x1d49f, 1},
2156 { 0x1d4a2, 0x1d4a2, 1},
2157 { 0x1d4a5, 0x1d4a6, 1},
2158 { 0x1d4a9, 0x1d4ac, 1},
2159 { 0x1d4ae, 0x1d4b9, 1},
2160 { 0x1d4bb, 0x1d4bb, 1},
2161 { 0x1d4bd, 0x1d4c3, 1},
2162 { 0x1d4c5, 0x1d505, 1},
2163 { 0x1d507, 0x1d50a, 1},
2164 { 0x1d50d, 0x1d514, 1},
2165 { 0x1d516, 0x1d51c, 1},
2166 { 0x1d51e, 0x1d539, 1},
2167 { 0x1d53b, 0x1d53e, 1},
2168 { 0x1d540, 0x1d544, 1},
2169 { 0x1d546, 0x1d546, 1},
2170 { 0x1d54a, 0x1d550, 1},
2171 { 0x1d552, 0x1d6a5, 1},
2172 { 0x1d6a8, 0x1d6c0, 1},
2173 { 0x1d6c2, 0x1d6da, 1},
2174 { 0x1d6dc, 0x1d6fa, 1},
2175 { 0x1d6fc, 0x1d714, 1},
2176 { 0x1d716, 0x1d734, 1},
2177 { 0x1d736, 0x1d74e, 1},
2178 { 0x1d750, 0x1d76e, 1},
2179 { 0x1d770, 0x1d788, 1},
2180 { 0x1d78a, 0x1d7a8, 1},
2181 { 0x1d7aa, 0x1d7c2, 1},
2182 { 0x1d7c4, 0x1d7cb, 1},
2183 { 0x20000, 0x2a6d6, 1},
2184 { 0x2f800, 0x2fa1d, 1},
2187 // A table of Unicode uppercase letters--Unicode code points
2188 // classified as "Letter, uppercase".
2190 static const Unicode_range unicode_uppercase_letters
[] =
2192 { 0x0041, 0x005a, 1},
2193 { 0x00c0, 0x00d6, 1},
2194 { 0x00d8, 0x00de, 1},
2195 { 0x0100, 0x0136, 2},
2196 { 0x0139, 0x0147, 2},
2197 { 0x014a, 0x0176, 2},
2198 { 0x0178, 0x0179, 1},
2199 { 0x017b, 0x017d, 2},
2200 { 0x0181, 0x0182, 1},
2201 { 0x0184, 0x0184, 1},
2202 { 0x0186, 0x0187, 1},
2203 { 0x0189, 0x018b, 1},
2204 { 0x018e, 0x0191, 1},
2205 { 0x0193, 0x0194, 1},
2206 { 0x0196, 0x0198, 1},
2207 { 0x019c, 0x019d, 1},
2208 { 0x019f, 0x01a0, 1},
2209 { 0x01a2, 0x01a4, 2},
2210 { 0x01a6, 0x01a7, 1},
2211 { 0x01a9, 0x01ac, 3},
2212 { 0x01ae, 0x01af, 1},
2213 { 0x01b1, 0x01b3, 1},
2214 { 0x01b5, 0x01b5, 1},
2215 { 0x01b7, 0x01b8, 1},
2216 { 0x01bc, 0x01c4, 8},
2217 { 0x01c7, 0x01cd, 3},
2218 { 0x01cf, 0x01db, 2},
2219 { 0x01de, 0x01ee, 2},
2220 { 0x01f1, 0x01f4, 3},
2221 { 0x01f6, 0x01f8, 1},
2222 { 0x01fa, 0x0232, 2},
2223 { 0x023a, 0x023b, 1},
2224 { 0x023d, 0x023e, 1},
2225 { 0x0241, 0x0241, 1},
2226 { 0x0243, 0x0246, 1},
2227 { 0x0248, 0x024e, 2},
2228 { 0x0370, 0x0372, 2},
2229 { 0x0376, 0x0386, 16},
2230 { 0x0388, 0x038a, 1},
2231 { 0x038c, 0x038c, 1},
2232 { 0x038e, 0x038f, 1},
2233 { 0x0391, 0x03a1, 1},
2234 { 0x03a3, 0x03ab, 1},
2235 { 0x03cf, 0x03cf, 1},
2236 { 0x03d2, 0x03d4, 1},
2237 { 0x03d8, 0x03ee, 2},
2238 { 0x03f4, 0x03f7, 3},
2239 { 0x03f9, 0x03fa, 1},
2240 { 0x03fd, 0x042f, 1},
2241 { 0x0460, 0x0480, 2},
2242 { 0x048a, 0x04be, 2},
2243 { 0x04c0, 0x04c1, 1},
2244 { 0x04c3, 0x04cd, 2},
2245 { 0x04d0, 0x0522, 2},
2246 { 0x0531, 0x0556, 1},
2247 { 0x10a0, 0x10c5, 1},
2248 { 0x1e00, 0x1e94, 2},
2249 { 0x1e9e, 0x1efe, 2},
2250 { 0x1f08, 0x1f0f, 1},
2251 { 0x1f18, 0x1f1d, 1},
2252 { 0x1f28, 0x1f2f, 1},
2253 { 0x1f38, 0x1f3f, 1},
2254 { 0x1f48, 0x1f4d, 1},
2255 { 0x1f59, 0x1f5f, 2},
2256 { 0x1f68, 0x1f6f, 1},
2257 { 0x1fb8, 0x1fbb, 1},
2258 { 0x1fc8, 0x1fcb, 1},
2259 { 0x1fd8, 0x1fdb, 1},
2260 { 0x1fe8, 0x1fec, 1},
2261 { 0x1ff8, 0x1ffb, 1},
2262 { 0x2102, 0x2107, 5},
2263 { 0x210b, 0x210d, 1},
2264 { 0x2110, 0x2112, 1},
2265 { 0x2115, 0x2115, 1},
2266 { 0x2119, 0x211d, 1},
2267 { 0x2124, 0x2128, 2},
2268 { 0x212a, 0x212d, 1},
2269 { 0x2130, 0x2133, 1},
2270 { 0x213e, 0x213f, 1},
2271 { 0x2145, 0x2183, 62},
2272 { 0x2c00, 0x2c2e, 1},
2273 { 0x2c60, 0x2c60, 1},
2274 { 0x2c62, 0x2c64, 1},
2275 { 0x2c67, 0x2c6b, 2},
2276 { 0x2c6d, 0x2c6f, 1},
2277 { 0x2c72, 0x2c75, 3},
2278 { 0x2c80, 0x2ce2, 2},
2279 { 0xa640, 0xa65e, 2},
2280 { 0xa662, 0xa66c, 2},
2281 { 0xa680, 0xa696, 2},
2282 { 0xa722, 0xa72e, 2},
2283 { 0xa732, 0xa76e, 2},
2284 { 0xa779, 0xa77b, 2},
2285 { 0xa77d, 0xa77e, 1},
2286 { 0xa780, 0xa786, 2},
2287 { 0xa78b, 0xa78b, 1},
2288 { 0xff21, 0xff3a, 1},
2289 { 0x10400, 0x10427, 1},
2290 { 0x1d400, 0x1d419, 1},
2291 { 0x1d434, 0x1d44d, 1},
2292 { 0x1d468, 0x1d481, 1},
2293 { 0x1d49c, 0x1d49c, 1},
2294 { 0x1d49e, 0x1d49f, 1},
2295 { 0x1d4a2, 0x1d4a2, 1},
2296 { 0x1d4a5, 0x1d4a6, 1},
2297 { 0x1d4a9, 0x1d4ac, 1},
2298 { 0x1d4ae, 0x1d4b5, 1},
2299 { 0x1d4d0, 0x1d4e9, 1},
2300 { 0x1d504, 0x1d505, 1},
2301 { 0x1d507, 0x1d50a, 1},
2302 { 0x1d50d, 0x1d514, 1},
2303 { 0x1d516, 0x1d51c, 1},
2304 { 0x1d538, 0x1d539, 1},
2305 { 0x1d53b, 0x1d53e, 1},
2306 { 0x1d540, 0x1d544, 1},
2307 { 0x1d546, 0x1d546, 1},
2308 { 0x1d54a, 0x1d550, 1},
2309 { 0x1d56c, 0x1d585, 1},
2310 { 0x1d5a0, 0x1d5b9, 1},
2311 { 0x1d5d4, 0x1d5ed, 1},
2312 { 0x1d608, 0x1d621, 1},
2313 { 0x1d63c, 0x1d655, 1},
2314 { 0x1d670, 0x1d689, 1},
2315 { 0x1d6a8, 0x1d6c0, 1},
2316 { 0x1d6e2, 0x1d6fa, 1},
2317 { 0x1d71c, 0x1d734, 1},
2318 { 0x1d756, 0x1d76e, 1},
2319 { 0x1d790, 0x1d7a8, 1},
2320 { 0x1d7ca, 0x1d7ca, 1},
2323 // Return true if C is in RANGES.
2326 Lex::is_in_unicode_range(unsigned int c
, const Unicode_range
* ranges
,
2331 // The common case is a small value, and we know that it will be
2332 // in the first few entries of the table. Do a linear scan
2333 // rather than a binary search.
2334 for (size_t i
= 0; i
< range_size
; ++i
)
2336 const Unicode_range
* p
= &ranges
[i
];
2341 return (c
- p
->low
) % p
->stride
== 0;
2349 size_t hi
= range_size
;
2352 size_t mid
= lo
+ (hi
- lo
) / 2;
2353 const Unicode_range
* p
= &ranges
[mid
];
2356 else if (c
> p
->high
)
2359 return (c
- p
->low
) % p
->stride
== 0;
2365 // Return whether C is a space character.
2368 Lex::is_unicode_space(unsigned int c
)
2370 return Lex::is_in_unicode_range(c
, unicode_space
,
2371 ARRAY_SIZE(unicode_space
));
2374 // Return whether C is a Unicode digit--a Unicode code point
2375 // classified as "Digit".
2378 Lex::is_unicode_digit(unsigned int c
)
2380 return Lex::is_in_unicode_range(c
, unicode_digits
,
2381 ARRAY_SIZE(unicode_digits
));
2384 // Return whether C is a Unicode letter--a Unicode code point
2385 // classified as "Letter".
2388 Lex::is_unicode_letter(unsigned int c
)
2390 return Lex::is_in_unicode_range(c
, unicode_letters
,
2391 ARRAY_SIZE(unicode_letters
));
2394 // Return whether C is a Unicode uppercase letter. a Unicode code
2395 // point classified as "Letter, uppercase".
2398 Lex::is_unicode_uppercase(unsigned int c
)
2400 return Lex::is_in_unicode_range(c
, unicode_uppercase_letters
,
2401 ARRAY_SIZE(unicode_uppercase_letters
));
2404 // Return whether the identifier NAME should be exported. NAME is a
2405 // mangled name which includes only ASCII characters.
2408 Lex::is_exported_name(const std::string
& name
)
2410 unsigned char c
= name
[0];
2412 return c
>= 'A' && c
<= 'Z';
2415 const char* p
= name
.data();
2416 size_t len
= name
.length();
2417 if (len
< 2 || p
[1] != 'U')
2419 unsigned int ci
= 0;
2420 for (size_t i
= 2; i
< len
&& p
[i
] != '$'; ++i
)
2428 return Lex::is_unicode_uppercase(ci
);
2432 // Return whether the identifier NAME contains an invalid character.
2433 // This is based on how we handle invalid characters in
2434 // gather_identifier.
2437 Lex::is_invalid_identifier(const std::string
& name
)
2439 return name
.find("$INVALID$") != std::string::npos
;