* gcc.dg/guality/guality.exp: Skip on AIX.
[official-gcc.git] / gcc / go / gofrontend / lex.cc
blob22a1f6e2a0c870658328f5ea53ededd0ede578b9
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.
7 #include "go-system.h"
9 #include "lex.h"
11 // Manage mapping from keywords to the Keyword codes.
13 class Keywords
15 public:
16 // The structure which maps keywords to codes.
17 struct Mapping
19 // Keyword string.
20 const char* keystring;
21 // Keyword code.
22 Keyword keycode;
25 // Return the parsecode corresponding to KEYSTRING, or
26 // KEYWORD_INVALID if it is not a keyword.
27 Keyword
28 keyword_to_code(const char* keyword, size_t len) const;
30 // Return the string for a keyword.
31 const char*
32 keyword_to_string(Keyword) const;
34 private:
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 },
59 { "go", KEYWORD_GO },
60 { "goto", KEYWORD_GOTO },
61 { "if", KEYWORD_IF },
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.
82 extern "C"
85 struct Keywords_search_key
87 const char* str;
88 size_t len;
91 static int
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)
99 return 1;
100 int i = strncmp(key->str, map->keystring, key->len);
101 if (i != 0)
102 return i;
103 if (map->keystring[key->len] != '\0')
104 return -1;
105 return 0;
108 } // End extern "C".
110 // Convert a string to a keyword code. Return KEYWORD_INVALID if the
111 // string is not a keyword.
113 Keyword
114 Keywords::keyword_to_code(const char* keyword, size_t len) const
116 Keywords_search_key key;
117 key.str = keyword;
118 key.len = len;
119 void* mapv = bsearch(&key,
120 this->mapping_,
121 this->count_,
122 sizeof(this->mapping_[0]),
123 keyword_compare);
124 if (mapv == NULL)
125 return KEYWORD_INVALID;
126 Mapping* map = static_cast<Mapping*>(mapv);
127 return map->keycode;
130 // Convert a keyword code to a string.
132 const char*
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;
145 // Class Token.
147 // Make a general token.
149 Token::Token(Classification classification, Location location)
150 : classification_(classification), location_(location)
154 // Destroy a token.
156 Token::~Token()
158 this->clear();
161 // Clear a token--release memory.
163 void
164 Token::clear()
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_)
181 case TOKEN_INVALID:
182 case TOKEN_EOF:
183 break;
184 case TOKEN_KEYWORD:
185 this->u_.keyword = tok.u_.keyword;
186 break;
187 case TOKEN_IDENTIFIER:
188 case TOKEN_STRING:
189 this->u_.string_value = tok.u_.string_value;
190 break;
191 case TOKEN_OPERATOR:
192 this->u_.op = tok.u_.op;
193 break;
194 case TOKEN_CHARACTER:
195 case TOKEN_INTEGER:
196 mpz_init_set(this->u_.integer_value, tok.u_.integer_value);
197 break;
198 case TOKEN_FLOAT:
199 case TOKEN_IMAGINARY:
200 mpfr_init_set(this->u_.float_value, tok.u_.float_value, GMP_RNDN);
201 break;
202 default:
203 go_unreachable();
207 // Assign to a token.
209 Token&
210 Token::operator=(const Token& tok)
212 this->clear();
213 this->classification_ = tok.classification_;
214 this->location_ = tok.location_;
215 switch (tok.classification_)
217 case TOKEN_INVALID:
218 case TOKEN_EOF:
219 break;
220 case TOKEN_KEYWORD:
221 this->u_.keyword = tok.u_.keyword;
222 break;
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;
227 break;
228 case TOKEN_STRING:
229 this->u_.string_value = tok.u_.string_value;
230 break;
231 case TOKEN_OPERATOR:
232 this->u_.op = tok.u_.op;
233 break;
234 case TOKEN_CHARACTER:
235 case TOKEN_INTEGER:
236 mpz_init_set(this->u_.integer_value, tok.u_.integer_value);
237 break;
238 case TOKEN_FLOAT:
239 case TOKEN_IMAGINARY:
240 mpfr_init_set(this->u_.float_value, tok.u_.float_value, GMP_RNDN);
241 break;
242 default:
243 go_unreachable();
245 return *this;
248 // Print the token for debugging.
250 void
251 Token::print(FILE* file) const
253 switch (this->classification_)
255 case TOKEN_INVALID:
256 fprintf(file, "invalid");
257 break;
258 case TOKEN_EOF:
259 fprintf(file, "EOF");
260 break;
261 case TOKEN_KEYWORD:
262 fprintf(file, "keyword %s", keywords.keyword_to_string(this->u_.keyword));
263 break;
264 case TOKEN_IDENTIFIER:
265 fprintf(file, "identifier \"%s\"", this->u_.string_value->c_str());
266 break;
267 case TOKEN_STRING:
268 fprintf(file, "quoted string \"%s\"", this->u_.string_value->c_str());
269 break;
270 case TOKEN_CHARACTER:
271 fprintf(file, "character ");
272 mpz_out_str(file, 10, this->u_.integer_value);
273 break;
274 case TOKEN_INTEGER:
275 fprintf(file, "integer ");
276 mpz_out_str(file, 10, this->u_.integer_value);
277 break;
278 case TOKEN_FLOAT:
279 fprintf(file, "float ");
280 mpfr_out_str(file, 10, 0, this->u_.float_value, GMP_RNDN);
281 break;
282 case TOKEN_IMAGINARY:
283 fprintf(file, "imaginary ");
284 mpfr_out_str(file, 10, 0, this->u_.float_value, GMP_RNDN);
285 break;
286 case TOKEN_OPERATOR:
287 fprintf(file, "operator ");
288 switch (this->u_.op)
290 case OPERATOR_INVALID:
291 fprintf(file, "invalid");
292 break;
293 case OPERATOR_OROR:
294 fprintf(file, "||");
295 break;
296 case OPERATOR_ANDAND:
297 fprintf(file, "&&");
298 break;
299 case OPERATOR_EQEQ:
300 fprintf(file, "==");
301 break;
302 case OPERATOR_NOTEQ:
303 fprintf(file, "!=");
304 break;
305 case OPERATOR_LT:
306 fprintf(file, "<");
307 break;
308 case OPERATOR_LE:
309 fprintf(file, "<=");
310 break;
311 case OPERATOR_GT:
312 fprintf(file, ">");
313 break;
314 case OPERATOR_GE:
315 fprintf(file, ">=");
316 break;
317 case OPERATOR_PLUS:
318 fprintf(file, "+");
319 break;
320 case OPERATOR_MINUS:
321 fprintf(file, "-");
322 break;
323 case OPERATOR_OR:
324 fprintf(file, "|");
325 break;
326 case OPERATOR_XOR:
327 fprintf(file, "^");
328 break;
329 case OPERATOR_MULT:
330 fprintf(file, "*");
331 break;
332 case OPERATOR_DIV:
333 fprintf(file, "/");
334 break;
335 case OPERATOR_MOD:
336 fprintf(file, "%%");
337 break;
338 case OPERATOR_LSHIFT:
339 fprintf(file, "<<");
340 break;
341 case OPERATOR_RSHIFT:
342 fprintf(file, ">>");
343 break;
344 case OPERATOR_AND:
345 fprintf(file, "&");
346 break;
347 case OPERATOR_BITCLEAR:
348 fprintf(file, "&^");
349 break;
350 case OPERATOR_NOT:
351 fprintf(file, "!");
352 break;
353 case OPERATOR_CHANOP:
354 fprintf(file, "<-");
355 break;
356 case OPERATOR_EQ:
357 fprintf(file, "=");
358 break;
359 case OPERATOR_PLUSEQ:
360 fprintf(file, "+=");
361 break;
362 case OPERATOR_MINUSEQ:
363 fprintf(file, "-=");
364 break;
365 case OPERATOR_OREQ:
366 fprintf(file, "|=");
367 break;
368 case OPERATOR_XOREQ:
369 fprintf(file, "^=");
370 break;
371 case OPERATOR_MULTEQ:
372 fprintf(file, "*=");
373 break;
374 case OPERATOR_DIVEQ:
375 fprintf(file, "/=");
376 break;
377 case OPERATOR_MODEQ:
378 fprintf(file, "%%=");
379 break;
380 case OPERATOR_LSHIFTEQ:
381 fprintf(file, "<<=");
382 break;
383 case OPERATOR_RSHIFTEQ:
384 fprintf(file, ">>=");
385 break;
386 case OPERATOR_ANDEQ:
387 fprintf(file, "&=");
388 break;
389 case OPERATOR_BITCLEAREQ:
390 fprintf(file, "&^=");
391 break;
392 case OPERATOR_PLUSPLUS:
393 fprintf(file, "++");
394 break;
395 case OPERATOR_MINUSMINUS:
396 fprintf(file, "--");
397 break;
398 case OPERATOR_COLON:
399 fprintf(file, ":");
400 break;
401 case OPERATOR_COLONEQ:
402 fprintf(file, ":=");
403 break;
404 case OPERATOR_SEMICOLON:
405 fprintf(file, ";");
406 break;
407 case OPERATOR_DOT:
408 fprintf(file, ".");
409 break;
410 case OPERATOR_COMMA:
411 fprintf(file, ",");
412 break;
413 case OPERATOR_LPAREN:
414 fprintf(file, "(");
415 break;
416 case OPERATOR_RPAREN:
417 fprintf(file, ")");
418 break;
419 case OPERATOR_LCURLY:
420 fprintf(file, "{");
421 break;
422 case OPERATOR_RCURLY:
423 fprintf(file, "}");
424 break;
425 case OPERATOR_LSQUARE:
426 fprintf(file, "[");
427 break;
428 case OPERATOR_RSQUARE:
429 fprintf(file, "]");
430 break;
431 default:
432 go_unreachable();
434 break;
435 default:
436 go_unreachable();
440 // Class Lex.
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),
446 extern_()
448 this->linebuf_ = new char[this->linebufsize_];
449 this->linemap_->start_file(input_file_name, 0);
452 Lex::~Lex()
454 delete[] this->linebuf_;
457 // Read a new line from the file.
459 ssize_t
460 Lex::get_line()
462 char* buf = this->linebuf_;
463 size_t size = this->linebufsize_;
465 FILE* file = this->input_file_;
466 size_t cur = 0;
467 while (true)
469 int c = getc(file);
470 if (c == EOF)
472 if (cur == 0)
473 return -1;
474 break;
476 if (cur + 1 >= size)
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);
483 delete[] buf;
484 buf = nb;
485 size = ns;
487 buf[cur] = c;
488 ++cur;
490 if (c == '\n')
491 break;
494 buf[cur] = '\0';
496 this->linebuf_ = buf;
497 this->linebufsize_ = size;
499 return cur;
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.
505 bool
506 Lex::require_line()
508 if (this->lineoff_ < this->linesize_)
509 return true;
511 ssize_t got = this->get_line();
512 if (got < 0)
513 return false;
514 ++this->lineno_;
515 this->linesize_= got;
516 this->lineoff_ = 0;
518 this->linemap_->start_line(this->lineno_, this->linesize_);
520 return true;
523 // Get the current location.
525 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.
534 Location
535 Lex::earlier_location(int chars) const
537 return this->linemap_->get_location(this->lineoff_ + 1 - chars);
540 // Get the next token.
542 Token
543 Lex::next_token()
545 bool saw_cpp_comment = false;
546 while (true)
548 if (!this->require_line())
550 bool add_semi_at_eol = this->add_semi_at_eol_;
551 this->add_semi_at_eol_ = false;
552 if (add_semi_at_eol)
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_;
564 while (p < pend)
566 unsigned char cc = *p;
567 switch (cc)
569 case ' ': case '\t': case '\r':
570 ++p;
571 // Skip whitespace quickly.
572 while (*p == ' ' || *p == '\t' || *p == '\r')
573 ++p;
574 break;
576 case '\n':
578 ++p;
579 bool add_semi_at_eol = this->add_semi_at_eol_;
580 this->add_semi_at_eol_ = false;
581 if (add_semi_at_eol)
583 this->lineoff_ = p - this->linebuf_;
584 return this->make_operator(OPERATOR_SEMICOLON, 1);
587 break;
589 case '/':
590 if (p[1] == '/')
592 this->lineoff_ = p + 2 - this->linebuf_;
593 this->skip_cpp_comment();
594 p = pend;
595 if (p[-1] == '\n' && this->add_semi_at_eol_)
596 --p;
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);
614 else
616 this->add_semi_at_eol_ = false;
617 this->lineoff_ = p + 1 - this->linebuf_;
618 return this->make_operator(OPERATOR_DIV, 1);
620 break;
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':
626 case 'Y': case 'Z':
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':
631 case 'y': case 'z':
632 case '_':
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();
642 case '\'':
643 this->add_semi_at_eol_ = true;
644 this->lineoff_ = p - this->linebuf_;
645 return this->gather_character();
647 case '"':
648 this->add_semi_at_eol_ = true;
649 this->lineoff_ = p - this->linebuf_;
650 return this->gather_string();
652 case '`':
653 this->add_semi_at_eol_ = true;
654 this->lineoff_ = p - this->linebuf_;
655 return this->gather_raw_string();
657 case '<':
658 case '>':
659 case '&':
660 if (p + 2 < pend)
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);
670 // Fall through.
671 case '|':
672 case '=':
673 case '!':
674 case '+':
675 case '-':
676 case '^':
677 case '*':
678 // '/' handled above.
679 case '%':
680 case ':':
681 case ';':
682 case ',':
683 case '(': case ')':
684 case '{': case '}':
685 case '[': case ']':
687 this->add_semi_at_eol_ = false;
688 Operator op = this->two_character_operator(cc, p[1]);
689 int chars;
690 if (op != OPERATOR_INVALID)
692 ++p;
693 chars = 2;
695 else
697 op = this->one_character_operator(cc);
698 chars = 1;
700 this->lineoff_ = p + 1 - this->linebuf_;
701 return this->make_operator(op, chars);
704 case '.':
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);
721 default:
723 unsigned int ci;
724 bool issued_error;
725 this->lineoff_ = p - this->linebuf_;
726 const char *pnext = this->advance_one_utf8_char(p, &ci,
727 &issued_error);
729 // Ignore byte order mark at start of file.
730 if (ci == 0xfeff)
732 p = pnext;
733 break;
736 if (Lex::is_unicode_letter(ci))
737 return this->gather_identifier();
739 if (!issued_error)
740 error_at(this->location(),
741 "invalid character 0x%x in input file",
742 ci);
744 p = pend;
746 break;
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;
763 if (c <= 0x7f)
765 *value = c;
766 return 1;
768 else if ((c & 0xe0) == 0xc0
769 && (p[1] & 0xc0) == 0x80)
771 *value = (((c & 0x1f) << 6)
772 + (p[1] & 0x3f));
773 if (*value <= 0x7f)
775 *value = 0xfffd;
776 return 0;
778 return 2;
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)
786 + (p[2] & 0x3f));
787 if (*value <= 0x7ff)
789 *value = 0xfffd;
790 return 0;
792 return 3;
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)
802 + (p[3] & 0x3f));
803 if (*value <= 0xffff)
805 *value = 0xfffd;
806 return 0;
808 return 4;
810 else
812 /* Invalid encoding. Return the Unicode replacement
813 character. */
814 *value = 0xfffd;
815 return 0;
819 // Advance one UTF-8 character. Return the pointer beyond the
820 // character. Set *VALUE to the value. Set *ISSUED_ERROR if an error
821 // was issued.
823 const char*
824 Lex::advance_one_utf8_char(const char* p, unsigned int* value,
825 bool* issued_error)
827 *issued_error = false;
829 if (*p == '\0')
831 error_at(this->location(), "invalid NUL byte");
832 *issued_error = true;
833 *value = 0;
834 return p + 1;
837 int adv = Lex::fetch_char(p, value);
838 if (adv == 0)
840 error_at(this->location(), "invalid UTF-8 encoding");
841 *issued_error = true;
842 return p + 1;
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;
852 return p + adv;
855 // Pick up an identifier.
857 Token
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;
866 std::string buf;
867 while (p < pend)
869 unsigned char cc = *p;
870 if (cc <= 0x7f)
872 if ((cc < 'A' || cc > 'Z')
873 && (cc < 'a' || cc > 'z')
874 && cc != '_'
875 && (cc < '0' || cc > '9'))
876 break;
877 ++p;
878 if (is_first)
880 is_exported = cc >= 'A' && cc <= 'Z';
881 is_first = false;
883 if (has_non_ascii_char)
884 buf.push_back(cc);
886 else
888 unsigned int ci;
889 bool issued_error;
890 this->lineoff_ = p - this->linebuf_;
891 const char* pnext = this->advance_one_utf8_char(p, &ci,
892 &issued_error);
893 bool is_invalid = false;
894 if (!Lex::is_unicode_letter(ci) && !Lex::is_unicode_digit(ci))
896 // There is no valid place for a non-ASCII character
897 // other than an identifier, so we get better error
898 // handling behaviour if we swallow this character after
899 // giving an error.
900 if (!issued_error)
901 error_at(this->location(),
902 "invalid character 0x%x in identifier",
903 ci);
904 is_invalid = true;
906 if (is_first)
908 is_exported = Lex::is_unicode_uppercase(ci);
909 is_first = false;
911 if (!has_non_ascii_char)
913 buf.assign(pstart, p - pstart);
914 has_non_ascii_char = true;
916 if (is_invalid && !Lex::is_invalid_identifier(buf))
917 buf.append("$INVALID$");
918 buf.append(p, pnext - p);
919 p = pnext;
922 Location location = this->location();
923 this->add_semi_at_eol_ = true;
924 this->lineoff_ = p - this->linebuf_;
925 if (has_non_ascii_char)
926 return Token::make_identifier_token(buf, is_exported, location);
927 else
929 Keyword code = keywords.keyword_to_code(pstart, p - pstart);
930 if (code == KEYWORD_INVALID)
931 return Token::make_identifier_token(std::string(pstart, p - pstart),
932 is_exported, location);
933 else
935 switch (code)
937 case KEYWORD_BREAK:
938 case KEYWORD_CONTINUE:
939 case KEYWORD_FALLTHROUGH:
940 case KEYWORD_RETURN:
941 break;
942 default:
943 this->add_semi_at_eol_ = false;
944 break;
946 return Token::make_keyword_token(code, location);
951 // Return whether C is a hex digit.
953 bool
954 Lex::is_hex_digit(char c)
956 return ((c >= '0' && c <= '9')
957 || (c >= 'A' && c <= 'F')
958 || (c >= 'a' && c <= 'f'));
961 // Return whether an exponent could start at P.
963 bool
964 Lex::could_be_exponent(const char* p, const char* pend)
966 if (*p != 'e' && *p != 'E')
967 return false;
968 ++p;
969 if (p >= pend)
970 return false;
971 if (*p == '+' || *p == '-')
973 ++p;
974 if (p >= pend)
975 return false;
977 return *p >= '0' && *p <= '9';
980 // Pick up a number.
982 Token
983 Lex::gather_number()
985 const char* pstart = this->linebuf_ + this->lineoff_;
986 const char* p = pstart;
987 const char* pend = this->linebuf_ + this->linesize_;
989 Location location = this->location();
991 bool neg = false;
992 if (*p == '+')
993 ++p;
994 else if (*p == '-')
996 ++p;
997 neg = true;
1000 const char* pnum = p;
1001 if (*p == '0')
1003 int base;
1004 if ((p[1] == 'x' || p[1] == 'X')
1005 && Lex::is_hex_digit(p[2]))
1007 base = 16;
1008 p += 2;
1009 pnum = p;
1010 while (p < pend)
1012 if (!Lex::is_hex_digit(*p))
1013 break;
1014 ++p;
1017 else
1019 base = 8;
1020 pnum = p;
1021 while (p < pend)
1023 if (*p < '0' || *p > '7')
1024 break;
1025 ++p;
1029 // A partial token that looks like an octal literal might actually be the
1030 // beginning of a floating-point or imaginary literal.
1031 if (base == 16 || (*p != '.' && *p != 'i' && !Lex::could_be_exponent(p, pend)))
1033 std::string s(pnum, p - pnum);
1034 mpz_t val;
1035 int r = mpz_init_set_str(val, s.c_str(), base);
1036 go_assert(r == 0);
1038 if (neg)
1039 mpz_neg(val, val);
1041 this->lineoff_ = p - this->linebuf_;
1042 Token ret = Token::make_integer_token(val, location);
1043 mpz_clear(val);
1044 return ret;
1048 while (p < pend)
1050 if (*p < '0' || *p > '9')
1051 break;
1052 ++p;
1055 if (*p != '.' && *p != 'i' && !Lex::could_be_exponent(p, pend))
1057 std::string s(pnum, p - pnum);
1058 mpz_t val;
1059 int r = mpz_init_set_str(val, s.c_str(), 10);
1060 go_assert(r == 0);
1062 if (neg)
1063 mpz_neg(val, val);
1065 this->lineoff_ = p - this->linebuf_;
1066 Token ret = Token::make_integer_token(val, location);
1067 mpz_clear(val);
1068 return ret;
1071 if (*p != 'i')
1073 bool dot = *p == '.';
1075 ++p;
1077 if (!dot)
1079 if (*p == '+' || *p == '-')
1080 ++p;
1083 while (p < pend)
1085 if (*p < '0' || *p > '9')
1086 break;
1087 ++p;
1090 if (dot && Lex::could_be_exponent(p, pend))
1092 ++p;
1093 if (*p == '+' || *p == '-')
1094 ++p;
1095 while (p < pend)
1097 if (*p < '0' || *p > '9')
1098 break;
1099 ++p;
1104 std::string s(pnum, p - pnum);
1105 mpfr_t val;
1106 int r = mpfr_init_set_str(val, s.c_str(), 10, GMP_RNDN);
1107 go_assert(r == 0);
1109 if (neg)
1110 mpfr_neg(val, val, GMP_RNDN);
1112 bool is_imaginary = *p == 'i';
1113 if (is_imaginary)
1114 ++p;
1116 this->lineoff_ = p - this->linebuf_;
1117 if (is_imaginary)
1119 Token ret = Token::make_imaginary_token(val, location);
1120 mpfr_clear(val);
1121 return ret;
1123 else
1125 Token ret = Token::make_float_token(val, location);
1126 mpfr_clear(val);
1127 return ret;
1131 // Advance one character, possibly escaped. Return the pointer beyond
1132 // the character. Set *VALUE to the character. Set *IS_CHARACTER if
1133 // this is a character (e.g., 'a' or '\u1234') rather than a byte
1134 // value (e.g., '\001').
1136 const char*
1137 Lex::advance_one_char(const char* p, bool is_single_quote, unsigned int* value,
1138 bool* is_character)
1140 *value = 0;
1141 *is_character = true;
1142 if (*p != '\\')
1144 bool issued_error;
1145 const char* ret = this->advance_one_utf8_char(p, value, &issued_error);
1146 if (is_single_quote
1147 && (*value == '\'' || *value == '\n')
1148 && !issued_error)
1149 error_at(this->location(), "invalid character literal");
1150 return ret;
1152 else
1154 ++p;
1155 switch (*p)
1157 case '0': case '1': case '2': case '3':
1158 case '4': case '5': case '6': case '7':
1159 *is_character = false;
1160 if (p[1] >= '0' && p[1] <= '7'
1161 && p[2] >= '0' && p[2] <= '7')
1163 *value = ((Lex::octal_value(p[0]) << 6)
1164 + (Lex::octal_value(p[1]) << 3)
1165 + Lex::octal_value(p[2]));
1166 if (*value > 255)
1168 error_at(this->location(), "invalid octal constant");
1169 *value = 255;
1171 return p + 3;
1173 error_at(this->location(), "invalid octal character");
1174 return (p[1] >= '0' && p[1] <= '7'
1175 ? p + 2
1176 : p + 1);
1178 case 'x':
1179 case 'X':
1180 *is_character = false;
1181 if (Lex::is_hex_digit(p[1]) && Lex::is_hex_digit(p[2]))
1183 *value = (hex_value(p[1]) << 4) + hex_value(p[2]);
1184 return p + 3;
1186 error_at(this->location(), "invalid hex character");
1187 return (Lex::is_hex_digit(p[1])
1188 ? p + 2
1189 : p + 1);
1191 case 'a':
1192 *value = '\a';
1193 return p + 1;
1194 case 'b':
1195 *value = '\b';
1196 return p + 1;
1197 case 'f':
1198 *value = '\f';
1199 return p + 1;
1200 case 'n':
1201 *value = '\n';
1202 return p + 1;
1203 case 'r':
1204 *value = '\r';
1205 return p + 1;
1206 case 't':
1207 *value = '\t';
1208 return p + 1;
1209 case 'v':
1210 *value = '\v';
1211 return p + 1;
1212 case '\\':
1213 *value = '\\';
1214 return p + 1;
1215 case '\'':
1216 if (!is_single_quote)
1217 error_at(this->location(), "invalid quoted character");
1218 *value = '\'';
1219 return p + 1;
1220 case '"':
1221 if (is_single_quote)
1222 error_at(this->location(), "invalid quoted character");
1223 *value = '"';
1224 return p + 1;
1226 case 'u':
1227 if (Lex::is_hex_digit(p[1]) && Lex::is_hex_digit(p[2])
1228 && Lex::is_hex_digit(p[3]) && Lex::is_hex_digit(p[4]))
1230 *value = ((hex_value(p[1]) << 12)
1231 + (hex_value(p[2]) << 8)
1232 + (hex_value(p[3]) << 4)
1233 + hex_value(p[4]));
1234 if (*value >= 0xd800 && *value < 0xe000)
1236 error_at(this->location(),
1237 "invalid unicode code point 0x%x",
1238 *value);
1239 // Use the replacement character.
1240 *value = 0xfffd;
1242 return p + 5;
1244 error_at(this->location(), "invalid little unicode code point");
1245 return p + 1;
1247 case 'U':
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])
1250 && Lex::is_hex_digit(p[5]) && Lex::is_hex_digit(p[6])
1251 && Lex::is_hex_digit(p[7]) && Lex::is_hex_digit(p[8]))
1253 *value = ((hex_value(p[1]) << 28)
1254 + (hex_value(p[2]) << 24)
1255 + (hex_value(p[3]) << 20)
1256 + (hex_value(p[4]) << 16)
1257 + (hex_value(p[5]) << 12)
1258 + (hex_value(p[6]) << 8)
1259 + (hex_value(p[7]) << 4)
1260 + hex_value(p[8]));
1261 if (*value > 0x10ffff
1262 || (*value >= 0xd800 && *value < 0xe000))
1264 error_at(this->location(), "invalid unicode code point 0x%x",
1265 *value);
1266 // Use the replacement character.
1267 *value = 0xfffd;
1269 return p + 9;
1271 error_at(this->location(), "invalid big unicode code point");
1272 return p + 1;
1274 default:
1275 error_at(this->location(), "invalid character after %<\\%>");
1276 *value = *p;
1277 return p + 1;
1282 // Append V to STR. IS_CHARACTER is true for a character which should
1283 // be stored in UTF-8, false for a general byte value which should be
1284 // stored directly.
1286 void
1287 Lex::append_char(unsigned int v, bool is_character, std::string* str,
1288 Location location)
1290 char buf[4];
1291 size_t len;
1292 if (v <= 0x7f || !is_character)
1294 buf[0] = v;
1295 len = 1;
1297 else if (v <= 0x7ff)
1299 buf[0] = 0xc0 + (v >> 6);
1300 buf[1] = 0x80 + (v & 0x3f);
1301 len = 2;
1303 else
1305 if (v > 0x10ffff)
1307 warning_at(location, 0,
1308 "unicode code point 0x%x out of range in string", v);
1309 // Turn it into the "replacement character".
1310 v = 0xfffd;
1312 if (v >= 0xd800 && v < 0xe000)
1314 warning_at(location, 0,
1315 "unicode code point 0x%x is invalid surrogate pair", v);
1316 v = 0xfffd;
1318 if (v <= 0xffff)
1320 buf[0] = 0xe0 + (v >> 12);
1321 buf[1] = 0x80 + ((v >> 6) & 0x3f);
1322 buf[2] = 0x80 + (v & 0x3f);
1323 len = 3;
1325 else
1327 buf[0] = 0xf0 + (v >> 18);
1328 buf[1] = 0x80 + ((v >> 12) & 0x3f);
1329 buf[2] = 0x80 + ((v >> 6) & 0x3f);
1330 buf[3] = 0x80 + (v & 0x3f);
1331 len = 4;
1334 str->append(buf, len);
1337 // Pick up a character literal.
1339 Token
1340 Lex::gather_character()
1342 ++this->lineoff_;
1343 const char* pstart = this->linebuf_ + this->lineoff_;
1344 const char* p = pstart;
1346 unsigned int value;
1347 bool is_character;
1348 p = this->advance_one_char(p, true, &value, &is_character);
1350 if (*p != '\'')
1352 error_at(this->location(), "unterminated character constant");
1353 this->lineoff_ = p - this->linebuf_;
1354 return this->make_invalid_token();
1357 mpz_t val;
1358 mpz_init_set_ui(val, value);
1360 Location location = this->location();
1361 this->lineoff_ = p + 1 - this->linebuf_;
1362 Token ret = Token::make_character_token(val, location);
1363 mpz_clear(val);
1364 return ret;
1367 // Pick up a quoted string.
1369 Token
1370 Lex::gather_string()
1372 const char* pstart = this->linebuf_ + this->lineoff_ + 1;
1373 const char* p = pstart;
1374 const char* pend = this->linebuf_ + this->linesize_;
1376 std::string value;
1377 while (*p != '"')
1379 Location loc = this->location();
1380 unsigned int c;
1381 bool is_character;
1382 this->lineoff_ = p - this->linebuf_;
1383 p = this->advance_one_char(p, false, &c, &is_character);
1384 if (p >= pend)
1386 error_at(this->location(), "unterminated string");
1387 --p;
1388 break;
1390 Lex::append_char(c, is_character, &value, loc);
1393 Location location = this->location();
1394 this->lineoff_ = p + 1 - this->linebuf_;
1395 return Token::make_string_token(value, location);
1398 // Pick up a raw string.
1400 Token
1401 Lex::gather_raw_string()
1403 const char* p = this->linebuf_ + this->lineoff_ + 1;
1404 const char* pend = this->linebuf_ + this->linesize_;
1405 Location location = this->location();
1407 std::string value;
1408 while (true)
1410 while (p < pend)
1412 if (*p == '`')
1414 this->lineoff_ = p + 1 - this->linebuf_;
1415 return Token::make_string_token(value, location);
1417 Location loc = this->location();
1418 unsigned int c;
1419 bool issued_error;
1420 this->lineoff_ = p - this->linebuf_;
1421 p = this->advance_one_utf8_char(p, &c, &issued_error);
1422 Lex::append_char(c, true, &value, loc);
1424 this->lineoff_ = p - this->linebuf_;
1425 if (!this->require_line())
1427 error_at(location, "unterminated raw string");
1428 return Token::make_string_token(value, location);
1430 p = this->linebuf_ + this->lineoff_;
1431 pend = this->linebuf_ + this->linesize_;
1435 // If C1 C2 C3 are a three character operator, return the code.
1437 Operator
1438 Lex::three_character_operator(char c1, char c2, char c3)
1440 if (c3 == '=')
1442 if (c1 == '<' && c2 == '<')
1443 return OPERATOR_LSHIFTEQ;
1444 else if (c1 == '>' && c2 == '>')
1445 return OPERATOR_RSHIFTEQ;
1446 else if (c1 == '&' && c2 == '^')
1447 return OPERATOR_BITCLEAREQ;
1449 return OPERATOR_INVALID;
1452 // If C1 C2 are a two character operator, return the code.
1454 Operator
1455 Lex::two_character_operator(char c1, char c2)
1457 switch (c1)
1459 case '|':
1460 if (c2 == '|')
1461 return OPERATOR_OROR;
1462 else if (c2 == '=')
1463 return OPERATOR_OREQ;
1464 break;
1465 case '&':
1466 if (c2 == '&')
1467 return OPERATOR_ANDAND;
1468 else if (c2 == '^')
1469 return OPERATOR_BITCLEAR;
1470 else if (c2 == '=')
1471 return OPERATOR_ANDEQ;
1472 break;
1473 case '^':
1474 if (c2 == '=')
1475 return OPERATOR_XOREQ;
1476 break;
1477 case '=':
1478 if (c2 == '=')
1479 return OPERATOR_EQEQ;
1480 break;
1481 case '!':
1482 if (c2 == '=')
1483 return OPERATOR_NOTEQ;
1484 break;
1485 case '<':
1486 if (c2 == '=')
1487 return OPERATOR_LE;
1488 else if (c2 == '<')
1489 return OPERATOR_LSHIFT;
1490 else if (c2 == '-')
1491 return OPERATOR_CHANOP;
1492 break;
1493 case '>':
1494 if (c2 == '=')
1495 return OPERATOR_GE;
1496 else if (c2 == '>')
1497 return OPERATOR_RSHIFT;
1498 break;
1499 case '*':
1500 if (c2 == '=')
1501 return OPERATOR_MULTEQ;
1502 break;
1503 case '/':
1504 if (c2 == '=')
1505 return OPERATOR_DIVEQ;
1506 break;
1507 case '%':
1508 if (c2 == '=')
1509 return OPERATOR_MODEQ;
1510 break;
1511 case '+':
1512 if (c2 == '+')
1514 this->add_semi_at_eol_ = true;
1515 return OPERATOR_PLUSPLUS;
1517 else if (c2 == '=')
1518 return OPERATOR_PLUSEQ;
1519 break;
1520 case '-':
1521 if (c2 == '-')
1523 this->add_semi_at_eol_ = true;
1524 return OPERATOR_MINUSMINUS;
1526 else if (c2 == '=')
1527 return OPERATOR_MINUSEQ;
1528 break;
1529 case ':':
1530 if (c2 == '=')
1531 return OPERATOR_COLONEQ;
1532 break;
1533 default:
1534 break;
1536 return OPERATOR_INVALID;
1539 // If character C is an operator, return the code.
1541 Operator
1542 Lex::one_character_operator(char c)
1544 switch (c)
1546 case '<':
1547 return OPERATOR_LT;
1548 case '>':
1549 return OPERATOR_GT;
1550 case '+':
1551 return OPERATOR_PLUS;
1552 case '-':
1553 return OPERATOR_MINUS;
1554 case '|':
1555 return OPERATOR_OR;
1556 case '^':
1557 return OPERATOR_XOR;
1558 case '*':
1559 return OPERATOR_MULT;
1560 case '/':
1561 return OPERATOR_DIV;
1562 case '%':
1563 return OPERATOR_MOD;
1564 case '&':
1565 return OPERATOR_AND;
1566 case '!':
1567 return OPERATOR_NOT;
1568 case '=':
1569 return OPERATOR_EQ;
1570 case ':':
1571 return OPERATOR_COLON;
1572 case ';':
1573 return OPERATOR_SEMICOLON;
1574 case '.':
1575 return OPERATOR_DOT;
1576 case ',':
1577 return OPERATOR_COMMA;
1578 case '(':
1579 return OPERATOR_LPAREN;
1580 case ')':
1581 this->add_semi_at_eol_ = true;
1582 return OPERATOR_RPAREN;
1583 case '{':
1584 return OPERATOR_LCURLY;
1585 case '}':
1586 this->add_semi_at_eol_ = true;
1587 return OPERATOR_RCURLY;
1588 case '[':
1589 return OPERATOR_LSQUARE;
1590 case ']':
1591 this->add_semi_at_eol_ = true;
1592 return OPERATOR_RSQUARE;
1593 default:
1594 return OPERATOR_INVALID;
1598 // Skip a C-style comment.
1600 bool
1601 Lex::skip_c_comment()
1603 while (true)
1605 if (!this->require_line())
1607 error_at(this->location(), "unterminated comment");
1608 return false;
1611 const char* p = this->linebuf_ + this->lineoff_;
1612 const char* pend = this->linebuf_ + this->linesize_;
1614 while (p < pend)
1616 if (p[0] == '*' && p + 1 < pend && p[1] == '/')
1618 this->lineoff_ = p + 2 - this->linebuf_;
1619 return true;
1622 this->lineoff_ = p - this->linebuf_;
1623 unsigned int c;
1624 bool issued_error;
1625 p = this->advance_one_utf8_char(p, &c, &issued_error);
1628 this->lineoff_ = p - this->linebuf_;
1632 // Skip a C++-style comment.
1634 void
1635 Lex::skip_cpp_comment()
1637 // Ensure that if EXTERN_ is set, it means that we just saw a
1638 // //extern comment.
1639 this->extern_.clear();
1641 const char* p = this->linebuf_ + this->lineoff_;
1642 const char* pend = this->linebuf_ + this->linesize_;
1644 // By convention, a C++ comment at the start of the line of the form
1645 // //line FILE:LINENO
1646 // is interpreted as setting the file name and line number of the
1647 // next source line.
1649 if (this->lineoff_ == 2
1650 && pend - p > 5
1651 && memcmp(p, "line ", 5) == 0)
1653 p += 5;
1654 while (p < pend && *p == ' ')
1655 ++p;
1656 const char* pcolon = static_cast<const char*>(memchr(p, ':', pend - p));
1657 if (pcolon != NULL
1658 && pcolon[1] >= '0'
1659 && pcolon[1] <= '9')
1661 char* plend;
1662 long lineno = strtol(pcolon + 1, &plend, 10);
1663 if (plend > pcolon + 1
1664 && (plend == pend
1665 || *plend < '0'
1666 || *plend > '9')
1667 && lineno > 0
1668 && lineno < 0x7fffffff)
1670 unsigned int filelen = pcolon - p;
1671 char* file = new char[filelen + 1];
1672 memcpy(file, p, filelen);
1673 file[filelen] = '\0';
1675 this->linemap_->start_file(file, lineno);
1676 this->lineno_ = lineno - 1;
1678 p = plend;
1683 // As a special gccgo extension, a C++ comment at the start of the
1684 // line of the form
1685 // //extern NAME
1686 // which immediately precedes a function declaration means that the
1687 // external name of the function declaration is NAME. This is
1688 // normally used to permit Go code to call a C function.
1689 if (this->lineoff_ == 2
1690 && pend - p > 7
1691 && memcmp(p, "extern ", 7) == 0)
1693 p += 7;
1694 while (p < pend && (*p == ' ' || *p == '\t'))
1695 ++p;
1696 const char* plend = pend;
1697 while (plend > p
1698 && (plend[-1] == ' ' || plend[-1] == '\t' || plend[-1] == '\n'))
1699 --plend;
1700 if (plend > p)
1701 this->extern_ = std::string(p, plend - p);
1704 // For field tracking analysis: a //go:nointerface comment means
1705 // that the next interface method should not be stored in the type
1706 // descriptor. This permits it to be discarded if it is not needed.
1707 if (this->lineoff_ == 2 && memcmp(p, "go:nointerface", 14) == 0)
1708 this->saw_nointerface_ = true;
1710 while (p < pend)
1712 this->lineoff_ = p - this->linebuf_;
1713 unsigned int c;
1714 bool issued_error;
1715 p = this->advance_one_utf8_char(p, &c, &issued_error);
1716 if (issued_error)
1717 this->extern_.clear();
1721 // The Unicode tables use this struct.
1723 struct Unicode_range
1725 // The low end of the range.
1726 unsigned int low;
1727 // The high end of the range.
1728 unsigned int high;
1729 // The stride. This entries represents low, low + stride, low + 2 *
1730 // stride, etc., up to high.
1731 unsigned int stride;
1734 // A table of whitespace characters--Unicode code points classified as
1735 // "Space", "C" locale whitespace characters, the "next line" control
1736 // character (0085), the line separator (2028), the paragraph
1737 // separator (2029), and the "zero-width non-break space" (feff).
1739 static const Unicode_range unicode_space[] =
1741 { 0x0009, 0x000d, 1 },
1742 { 0x0020, 0x0020, 1 },
1743 { 0x0085, 0x0085, 1 },
1744 { 0x00a0, 0x00a0, 1 },
1745 { 0x1680, 0x1680, 1 },
1746 { 0x180e, 0x180e, 1 },
1747 { 0x2000, 0x200a, 1 },
1748 { 0x2028, 0x2029, 1 },
1749 { 0x202f, 0x202f, 1 },
1750 { 0x205f, 0x205f, 1 },
1751 { 0x3000, 0x3000, 1 },
1752 { 0xfeff, 0xfeff, 1 },
1755 // A table of Unicode digits--Unicode code points classified as
1756 // "Digit".
1758 static const Unicode_range unicode_digits[] =
1760 { 0x0030, 0x0039, 1},
1761 { 0x0660, 0x0669, 1},
1762 { 0x06f0, 0x06f9, 1},
1763 { 0x07c0, 0x07c9, 1},
1764 { 0x0966, 0x096f, 1},
1765 { 0x09e6, 0x09ef, 1},
1766 { 0x0a66, 0x0a6f, 1},
1767 { 0x0ae6, 0x0aef, 1},
1768 { 0x0b66, 0x0b6f, 1},
1769 { 0x0be6, 0x0bef, 1},
1770 { 0x0c66, 0x0c6f, 1},
1771 { 0x0ce6, 0x0cef, 1},
1772 { 0x0d66, 0x0d6f, 1},
1773 { 0x0e50, 0x0e59, 1},
1774 { 0x0ed0, 0x0ed9, 1},
1775 { 0x0f20, 0x0f29, 1},
1776 { 0x1040, 0x1049, 1},
1777 { 0x17e0, 0x17e9, 1},
1778 { 0x1810, 0x1819, 1},
1779 { 0x1946, 0x194f, 1},
1780 { 0x19d0, 0x19d9, 1},
1781 { 0x1b50, 0x1b59, 1},
1782 { 0xff10, 0xff19, 1},
1783 { 0x104a0, 0x104a9, 1},
1784 { 0x1d7ce, 0x1d7ff, 1},
1787 // A table of Unicode letters--Unicode code points classified as
1788 // "Letter".
1790 static const Unicode_range unicode_letters[] =
1792 { 0x0041, 0x005a, 1},
1793 { 0x0061, 0x007a, 1},
1794 { 0x00aa, 0x00b5, 11},
1795 { 0x00ba, 0x00ba, 1},
1796 { 0x00c0, 0x00d6, 1},
1797 { 0x00d8, 0x00f6, 1},
1798 { 0x00f8, 0x02c1, 1},
1799 { 0x02c6, 0x02d1, 1},
1800 { 0x02e0, 0x02e4, 1},
1801 { 0x02ec, 0x02ee, 2},
1802 { 0x0370, 0x0374, 1},
1803 { 0x0376, 0x0377, 1},
1804 { 0x037a, 0x037d, 1},
1805 { 0x0386, 0x0386, 1},
1806 { 0x0388, 0x038a, 1},
1807 { 0x038c, 0x038c, 1},
1808 { 0x038e, 0x03a1, 1},
1809 { 0x03a3, 0x03f5, 1},
1810 { 0x03f7, 0x0481, 1},
1811 { 0x048a, 0x0523, 1},
1812 { 0x0531, 0x0556, 1},
1813 { 0x0559, 0x0559, 1},
1814 { 0x0561, 0x0587, 1},
1815 { 0x05d0, 0x05ea, 1},
1816 { 0x05f0, 0x05f2, 1},
1817 { 0x0621, 0x064a, 1},
1818 { 0x066e, 0x066f, 1},
1819 { 0x0671, 0x06d3, 1},
1820 { 0x06d5, 0x06d5, 1},
1821 { 0x06e5, 0x06e6, 1},
1822 { 0x06ee, 0x06ef, 1},
1823 { 0x06fa, 0x06fc, 1},
1824 { 0x06ff, 0x0710, 17},
1825 { 0x0712, 0x072f, 1},
1826 { 0x074d, 0x07a5, 1},
1827 { 0x07b1, 0x07b1, 1},
1828 { 0x07ca, 0x07ea, 1},
1829 { 0x07f4, 0x07f5, 1},
1830 { 0x07fa, 0x07fa, 1},
1831 { 0x0904, 0x0939, 1},
1832 { 0x093d, 0x0950, 19},
1833 { 0x0958, 0x0961, 1},
1834 { 0x0971, 0x0972, 1},
1835 { 0x097b, 0x097f, 1},
1836 { 0x0985, 0x098c, 1},
1837 { 0x098f, 0x0990, 1},
1838 { 0x0993, 0x09a8, 1},
1839 { 0x09aa, 0x09b0, 1},
1840 { 0x09b2, 0x09b2, 1},
1841 { 0x09b6, 0x09b9, 1},
1842 { 0x09bd, 0x09ce, 17},
1843 { 0x09dc, 0x09dd, 1},
1844 { 0x09df, 0x09e1, 1},
1845 { 0x09f0, 0x09f1, 1},
1846 { 0x0a05, 0x0a0a, 1},
1847 { 0x0a0f, 0x0a10, 1},
1848 { 0x0a13, 0x0a28, 1},
1849 { 0x0a2a, 0x0a30, 1},
1850 { 0x0a32, 0x0a33, 1},
1851 { 0x0a35, 0x0a36, 1},
1852 { 0x0a38, 0x0a39, 1},
1853 { 0x0a59, 0x0a5c, 1},
1854 { 0x0a5e, 0x0a5e, 1},
1855 { 0x0a72, 0x0a74, 1},
1856 { 0x0a85, 0x0a8d, 1},
1857 { 0x0a8f, 0x0a91, 1},
1858 { 0x0a93, 0x0aa8, 1},
1859 { 0x0aaa, 0x0ab0, 1},
1860 { 0x0ab2, 0x0ab3, 1},
1861 { 0x0ab5, 0x0ab9, 1},
1862 { 0x0abd, 0x0ad0, 19},
1863 { 0x0ae0, 0x0ae1, 1},
1864 { 0x0b05, 0x0b0c, 1},
1865 { 0x0b0f, 0x0b10, 1},
1866 { 0x0b13, 0x0b28, 1},
1867 { 0x0b2a, 0x0b30, 1},
1868 { 0x0b32, 0x0b33, 1},
1869 { 0x0b35, 0x0b39, 1},
1870 { 0x0b3d, 0x0b3d, 1},
1871 { 0x0b5c, 0x0b5d, 1},
1872 { 0x0b5f, 0x0b61, 1},
1873 { 0x0b71, 0x0b83, 18},
1874 { 0x0b85, 0x0b8a, 1},
1875 { 0x0b8e, 0x0b90, 1},
1876 { 0x0b92, 0x0b95, 1},
1877 { 0x0b99, 0x0b9a, 1},
1878 { 0x0b9c, 0x0b9c, 1},
1879 { 0x0b9e, 0x0b9f, 1},
1880 { 0x0ba3, 0x0ba4, 1},
1881 { 0x0ba8, 0x0baa, 1},
1882 { 0x0bae, 0x0bb9, 1},
1883 { 0x0bd0, 0x0bd0, 1},
1884 { 0x0c05, 0x0c0c, 1},
1885 { 0x0c0e, 0x0c10, 1},
1886 { 0x0c12, 0x0c28, 1},
1887 { 0x0c2a, 0x0c33, 1},
1888 { 0x0c35, 0x0c39, 1},
1889 { 0x0c3d, 0x0c3d, 1},
1890 { 0x0c58, 0x0c59, 1},
1891 { 0x0c60, 0x0c61, 1},
1892 { 0x0c85, 0x0c8c, 1},
1893 { 0x0c8e, 0x0c90, 1},
1894 { 0x0c92, 0x0ca8, 1},
1895 { 0x0caa, 0x0cb3, 1},
1896 { 0x0cb5, 0x0cb9, 1},
1897 { 0x0cbd, 0x0cde, 33},
1898 { 0x0ce0, 0x0ce1, 1},
1899 { 0x0d05, 0x0d0c, 1},
1900 { 0x0d0e, 0x0d10, 1},
1901 { 0x0d12, 0x0d28, 1},
1902 { 0x0d2a, 0x0d39, 1},
1903 { 0x0d3d, 0x0d3d, 1},
1904 { 0x0d60, 0x0d61, 1},
1905 { 0x0d7a, 0x0d7f, 1},
1906 { 0x0d85, 0x0d96, 1},
1907 { 0x0d9a, 0x0db1, 1},
1908 { 0x0db3, 0x0dbb, 1},
1909 { 0x0dbd, 0x0dbd, 1},
1910 { 0x0dc0, 0x0dc6, 1},
1911 { 0x0e01, 0x0e30, 1},
1912 { 0x0e32, 0x0e33, 1},
1913 { 0x0e40, 0x0e46, 1},
1914 { 0x0e81, 0x0e82, 1},
1915 { 0x0e84, 0x0e84, 1},
1916 { 0x0e87, 0x0e88, 1},
1917 { 0x0e8a, 0x0e8d, 3},
1918 { 0x0e94, 0x0e97, 1},
1919 { 0x0e99, 0x0e9f, 1},
1920 { 0x0ea1, 0x0ea3, 1},
1921 { 0x0ea5, 0x0ea7, 2},
1922 { 0x0eaa, 0x0eab, 1},
1923 { 0x0ead, 0x0eb0, 1},
1924 { 0x0eb2, 0x0eb3, 1},
1925 { 0x0ebd, 0x0ebd, 1},
1926 { 0x0ec0, 0x0ec4, 1},
1927 { 0x0ec6, 0x0ec6, 1},
1928 { 0x0edc, 0x0edd, 1},
1929 { 0x0f00, 0x0f00, 1},
1930 { 0x0f40, 0x0f47, 1},
1931 { 0x0f49, 0x0f6c, 1},
1932 { 0x0f88, 0x0f8b, 1},
1933 { 0x1000, 0x102a, 1},
1934 { 0x103f, 0x103f, 1},
1935 { 0x1050, 0x1055, 1},
1936 { 0x105a, 0x105d, 1},
1937 { 0x1061, 0x1061, 1},
1938 { 0x1065, 0x1066, 1},
1939 { 0x106e, 0x1070, 1},
1940 { 0x1075, 0x1081, 1},
1941 { 0x108e, 0x108e, 1},
1942 { 0x10a0, 0x10c5, 1},
1943 { 0x10d0, 0x10fa, 1},
1944 { 0x10fc, 0x10fc, 1},
1945 { 0x1100, 0x1159, 1},
1946 { 0x115f, 0x11a2, 1},
1947 { 0x11a8, 0x11f9, 1},
1948 { 0x1200, 0x1248, 1},
1949 { 0x124a, 0x124d, 1},
1950 { 0x1250, 0x1256, 1},
1951 { 0x1258, 0x1258, 1},
1952 { 0x125a, 0x125d, 1},
1953 { 0x1260, 0x1288, 1},
1954 { 0x128a, 0x128d, 1},
1955 { 0x1290, 0x12b0, 1},
1956 { 0x12b2, 0x12b5, 1},
1957 { 0x12b8, 0x12be, 1},
1958 { 0x12c0, 0x12c0, 1},
1959 { 0x12c2, 0x12c5, 1},
1960 { 0x12c8, 0x12d6, 1},
1961 { 0x12d8, 0x1310, 1},
1962 { 0x1312, 0x1315, 1},
1963 { 0x1318, 0x135a, 1},
1964 { 0x1380, 0x138f, 1},
1965 { 0x13a0, 0x13f4, 1},
1966 { 0x1401, 0x166c, 1},
1967 { 0x166f, 0x1676, 1},
1968 { 0x1681, 0x169a, 1},
1969 { 0x16a0, 0x16ea, 1},
1970 { 0x1700, 0x170c, 1},
1971 { 0x170e, 0x1711, 1},
1972 { 0x1720, 0x1731, 1},
1973 { 0x1740, 0x1751, 1},
1974 { 0x1760, 0x176c, 1},
1975 { 0x176e, 0x1770, 1},
1976 { 0x1780, 0x17b3, 1},
1977 { 0x17d7, 0x17dc, 5},
1978 { 0x1820, 0x1877, 1},
1979 { 0x1880, 0x18a8, 1},
1980 { 0x18aa, 0x18aa, 1},
1981 { 0x1900, 0x191c, 1},
1982 { 0x1950, 0x196d, 1},
1983 { 0x1970, 0x1974, 1},
1984 { 0x1980, 0x19a9, 1},
1985 { 0x19c1, 0x19c7, 1},
1986 { 0x1a00, 0x1a16, 1},
1987 { 0x1b05, 0x1b33, 1},
1988 { 0x1b45, 0x1b4b, 1},
1989 { 0x1b83, 0x1ba0, 1},
1990 { 0x1bae, 0x1baf, 1},
1991 { 0x1c00, 0x1c23, 1},
1992 { 0x1c4d, 0x1c4f, 1},
1993 { 0x1c5a, 0x1c7d, 1},
1994 { 0x1d00, 0x1dbf, 1},
1995 { 0x1e00, 0x1f15, 1},
1996 { 0x1f18, 0x1f1d, 1},
1997 { 0x1f20, 0x1f45, 1},
1998 { 0x1f48, 0x1f4d, 1},
1999 { 0x1f50, 0x1f57, 1},
2000 { 0x1f59, 0x1f5d, 2},
2001 { 0x1f5f, 0x1f7d, 1},
2002 { 0x1f80, 0x1fb4, 1},
2003 { 0x1fb6, 0x1fbc, 1},
2004 { 0x1fbe, 0x1fbe, 1},
2005 { 0x1fc2, 0x1fc4, 1},
2006 { 0x1fc6, 0x1fcc, 1},
2007 { 0x1fd0, 0x1fd3, 1},
2008 { 0x1fd6, 0x1fdb, 1},
2009 { 0x1fe0, 0x1fec, 1},
2010 { 0x1ff2, 0x1ff4, 1},
2011 { 0x1ff6, 0x1ffc, 1},
2012 { 0x2071, 0x207f, 14},
2013 { 0x2090, 0x2094, 1},
2014 { 0x2102, 0x2107, 5},
2015 { 0x210a, 0x2113, 1},
2016 { 0x2115, 0x2115, 1},
2017 { 0x2119, 0x211d, 1},
2018 { 0x2124, 0x2128, 2},
2019 { 0x212a, 0x212d, 1},
2020 { 0x212f, 0x2139, 1},
2021 { 0x213c, 0x213f, 1},
2022 { 0x2145, 0x2149, 1},
2023 { 0x214e, 0x214e, 1},
2024 { 0x2183, 0x2184, 1},
2025 { 0x2c00, 0x2c2e, 1},
2026 { 0x2c30, 0x2c5e, 1},
2027 { 0x2c60, 0x2c6f, 1},
2028 { 0x2c71, 0x2c7d, 1},
2029 { 0x2c80, 0x2ce4, 1},
2030 { 0x2d00, 0x2d25, 1},
2031 { 0x2d30, 0x2d65, 1},
2032 { 0x2d6f, 0x2d6f, 1},
2033 { 0x2d80, 0x2d96, 1},
2034 { 0x2da0, 0x2da6, 1},
2035 { 0x2da8, 0x2dae, 1},
2036 { 0x2db0, 0x2db6, 1},
2037 { 0x2db8, 0x2dbe, 1},
2038 { 0x2dc0, 0x2dc6, 1},
2039 { 0x2dc8, 0x2dce, 1},
2040 { 0x2dd0, 0x2dd6, 1},
2041 { 0x2dd8, 0x2dde, 1},
2042 { 0x2e2f, 0x2e2f, 1},
2043 { 0x3005, 0x3006, 1},
2044 { 0x3031, 0x3035, 1},
2045 { 0x303b, 0x303c, 1},
2046 { 0x3041, 0x3096, 1},
2047 { 0x309d, 0x309f, 1},
2048 { 0x30a1, 0x30fa, 1},
2049 { 0x30fc, 0x30ff, 1},
2050 { 0x3105, 0x312d, 1},
2051 { 0x3131, 0x318e, 1},
2052 { 0x31a0, 0x31b7, 1},
2053 { 0x31f0, 0x31ff, 1},
2054 { 0x3400, 0x4db5, 1},
2055 { 0x4e00, 0x9fc3, 1},
2056 { 0xa000, 0xa48c, 1},
2057 { 0xa500, 0xa60c, 1},
2058 { 0xa610, 0xa61f, 1},
2059 { 0xa62a, 0xa62b, 1},
2060 { 0xa640, 0xa65f, 1},
2061 { 0xa662, 0xa66e, 1},
2062 { 0xa67f, 0xa697, 1},
2063 { 0xa717, 0xa71f, 1},
2064 { 0xa722, 0xa788, 1},
2065 { 0xa78b, 0xa78c, 1},
2066 { 0xa7fb, 0xa801, 1},
2067 { 0xa803, 0xa805, 1},
2068 { 0xa807, 0xa80a, 1},
2069 { 0xa80c, 0xa822, 1},
2070 { 0xa840, 0xa873, 1},
2071 { 0xa882, 0xa8b3, 1},
2072 { 0xa90a, 0xa925, 1},
2073 { 0xa930, 0xa946, 1},
2074 { 0xaa00, 0xaa28, 1},
2075 { 0xaa40, 0xaa42, 1},
2076 { 0xaa44, 0xaa4b, 1},
2077 { 0xac00, 0xd7a3, 1},
2078 { 0xf900, 0xfa2d, 1},
2079 { 0xfa30, 0xfa6a, 1},
2080 { 0xfa70, 0xfad9, 1},
2081 { 0xfb00, 0xfb06, 1},
2082 { 0xfb13, 0xfb17, 1},
2083 { 0xfb1d, 0xfb1d, 1},
2084 { 0xfb1f, 0xfb28, 1},
2085 { 0xfb2a, 0xfb36, 1},
2086 { 0xfb38, 0xfb3c, 1},
2087 { 0xfb3e, 0xfb3e, 1},
2088 { 0xfb40, 0xfb41, 1},
2089 { 0xfb43, 0xfb44, 1},
2090 { 0xfb46, 0xfbb1, 1},
2091 { 0xfbd3, 0xfd3d, 1},
2092 { 0xfd50, 0xfd8f, 1},
2093 { 0xfd92, 0xfdc7, 1},
2094 { 0xfdf0, 0xfdfb, 1},
2095 { 0xfe70, 0xfe74, 1},
2096 { 0xfe76, 0xfefc, 1},
2097 { 0xff21, 0xff3a, 1},
2098 { 0xff41, 0xff5a, 1},
2099 { 0xff66, 0xffbe, 1},
2100 { 0xffc2, 0xffc7, 1},
2101 { 0xffca, 0xffcf, 1},
2102 { 0xffd2, 0xffd7, 1},
2103 { 0xffda, 0xffdc, 1},
2104 { 0x10000, 0x1000b, 1},
2105 { 0x1000d, 0x10026, 1},
2106 { 0x10028, 0x1003a, 1},
2107 { 0x1003c, 0x1003d, 1},
2108 { 0x1003f, 0x1004d, 1},
2109 { 0x10050, 0x1005d, 1},
2110 { 0x10080, 0x100fa, 1},
2111 { 0x10280, 0x1029c, 1},
2112 { 0x102a0, 0x102d0, 1},
2113 { 0x10300, 0x1031e, 1},
2114 { 0x10330, 0x10340, 1},
2115 { 0x10342, 0x10349, 1},
2116 { 0x10380, 0x1039d, 1},
2117 { 0x103a0, 0x103c3, 1},
2118 { 0x103c8, 0x103cf, 1},
2119 { 0x10400, 0x1049d, 1},
2120 { 0x10800, 0x10805, 1},
2121 { 0x10808, 0x10808, 1},
2122 { 0x1080a, 0x10835, 1},
2123 { 0x10837, 0x10838, 1},
2124 { 0x1083c, 0x1083f, 3},
2125 { 0x10900, 0x10915, 1},
2126 { 0x10920, 0x10939, 1},
2127 { 0x10a00, 0x10a00, 1},
2128 { 0x10a10, 0x10a13, 1},
2129 { 0x10a15, 0x10a17, 1},
2130 { 0x10a19, 0x10a33, 1},
2131 { 0x12000, 0x1236e, 1},
2132 { 0x1d400, 0x1d454, 1},
2133 { 0x1d456, 0x1d49c, 1},
2134 { 0x1d49e, 0x1d49f, 1},
2135 { 0x1d4a2, 0x1d4a2, 1},
2136 { 0x1d4a5, 0x1d4a6, 1},
2137 { 0x1d4a9, 0x1d4ac, 1},
2138 { 0x1d4ae, 0x1d4b9, 1},
2139 { 0x1d4bb, 0x1d4bb, 1},
2140 { 0x1d4bd, 0x1d4c3, 1},
2141 { 0x1d4c5, 0x1d505, 1},
2142 { 0x1d507, 0x1d50a, 1},
2143 { 0x1d50d, 0x1d514, 1},
2144 { 0x1d516, 0x1d51c, 1},
2145 { 0x1d51e, 0x1d539, 1},
2146 { 0x1d53b, 0x1d53e, 1},
2147 { 0x1d540, 0x1d544, 1},
2148 { 0x1d546, 0x1d546, 1},
2149 { 0x1d54a, 0x1d550, 1},
2150 { 0x1d552, 0x1d6a5, 1},
2151 { 0x1d6a8, 0x1d6c0, 1},
2152 { 0x1d6c2, 0x1d6da, 1},
2153 { 0x1d6dc, 0x1d6fa, 1},
2154 { 0x1d6fc, 0x1d714, 1},
2155 { 0x1d716, 0x1d734, 1},
2156 { 0x1d736, 0x1d74e, 1},
2157 { 0x1d750, 0x1d76e, 1},
2158 { 0x1d770, 0x1d788, 1},
2159 { 0x1d78a, 0x1d7a8, 1},
2160 { 0x1d7aa, 0x1d7c2, 1},
2161 { 0x1d7c4, 0x1d7cb, 1},
2162 { 0x20000, 0x2a6d6, 1},
2163 { 0x2f800, 0x2fa1d, 1},
2166 // A table of Unicode uppercase letters--Unicode code points
2167 // classified as "Letter, uppercase".
2169 static const Unicode_range unicode_uppercase_letters[] =
2171 { 0x0041, 0x005a, 1},
2172 { 0x00c0, 0x00d6, 1},
2173 { 0x00d8, 0x00de, 1},
2174 { 0x0100, 0x0136, 2},
2175 { 0x0139, 0x0147, 2},
2176 { 0x014a, 0x0176, 2},
2177 { 0x0178, 0x0179, 1},
2178 { 0x017b, 0x017d, 2},
2179 { 0x0181, 0x0182, 1},
2180 { 0x0184, 0x0184, 1},
2181 { 0x0186, 0x0187, 1},
2182 { 0x0189, 0x018b, 1},
2183 { 0x018e, 0x0191, 1},
2184 { 0x0193, 0x0194, 1},
2185 { 0x0196, 0x0198, 1},
2186 { 0x019c, 0x019d, 1},
2187 { 0x019f, 0x01a0, 1},
2188 { 0x01a2, 0x01a4, 2},
2189 { 0x01a6, 0x01a7, 1},
2190 { 0x01a9, 0x01ac, 3},
2191 { 0x01ae, 0x01af, 1},
2192 { 0x01b1, 0x01b3, 1},
2193 { 0x01b5, 0x01b5, 1},
2194 { 0x01b7, 0x01b8, 1},
2195 { 0x01bc, 0x01c4, 8},
2196 { 0x01c7, 0x01cd, 3},
2197 { 0x01cf, 0x01db, 2},
2198 { 0x01de, 0x01ee, 2},
2199 { 0x01f1, 0x01f4, 3},
2200 { 0x01f6, 0x01f8, 1},
2201 { 0x01fa, 0x0232, 2},
2202 { 0x023a, 0x023b, 1},
2203 { 0x023d, 0x023e, 1},
2204 { 0x0241, 0x0241, 1},
2205 { 0x0243, 0x0246, 1},
2206 { 0x0248, 0x024e, 2},
2207 { 0x0370, 0x0372, 2},
2208 { 0x0376, 0x0386, 16},
2209 { 0x0388, 0x038a, 1},
2210 { 0x038c, 0x038c, 1},
2211 { 0x038e, 0x038f, 1},
2212 { 0x0391, 0x03a1, 1},
2213 { 0x03a3, 0x03ab, 1},
2214 { 0x03cf, 0x03cf, 1},
2215 { 0x03d2, 0x03d4, 1},
2216 { 0x03d8, 0x03ee, 2},
2217 { 0x03f4, 0x03f7, 3},
2218 { 0x03f9, 0x03fa, 1},
2219 { 0x03fd, 0x042f, 1},
2220 { 0x0460, 0x0480, 2},
2221 { 0x048a, 0x04be, 2},
2222 { 0x04c0, 0x04c1, 1},
2223 { 0x04c3, 0x04cd, 2},
2224 { 0x04d0, 0x0522, 2},
2225 { 0x0531, 0x0556, 1},
2226 { 0x10a0, 0x10c5, 1},
2227 { 0x1e00, 0x1e94, 2},
2228 { 0x1e9e, 0x1efe, 2},
2229 { 0x1f08, 0x1f0f, 1},
2230 { 0x1f18, 0x1f1d, 1},
2231 { 0x1f28, 0x1f2f, 1},
2232 { 0x1f38, 0x1f3f, 1},
2233 { 0x1f48, 0x1f4d, 1},
2234 { 0x1f59, 0x1f5f, 2},
2235 { 0x1f68, 0x1f6f, 1},
2236 { 0x1fb8, 0x1fbb, 1},
2237 { 0x1fc8, 0x1fcb, 1},
2238 { 0x1fd8, 0x1fdb, 1},
2239 { 0x1fe8, 0x1fec, 1},
2240 { 0x1ff8, 0x1ffb, 1},
2241 { 0x2102, 0x2107, 5},
2242 { 0x210b, 0x210d, 1},
2243 { 0x2110, 0x2112, 1},
2244 { 0x2115, 0x2115, 1},
2245 { 0x2119, 0x211d, 1},
2246 { 0x2124, 0x2128, 2},
2247 { 0x212a, 0x212d, 1},
2248 { 0x2130, 0x2133, 1},
2249 { 0x213e, 0x213f, 1},
2250 { 0x2145, 0x2183, 62},
2251 { 0x2c00, 0x2c2e, 1},
2252 { 0x2c60, 0x2c60, 1},
2253 { 0x2c62, 0x2c64, 1},
2254 { 0x2c67, 0x2c6b, 2},
2255 { 0x2c6d, 0x2c6f, 1},
2256 { 0x2c72, 0x2c75, 3},
2257 { 0x2c80, 0x2ce2, 2},
2258 { 0xa640, 0xa65e, 2},
2259 { 0xa662, 0xa66c, 2},
2260 { 0xa680, 0xa696, 2},
2261 { 0xa722, 0xa72e, 2},
2262 { 0xa732, 0xa76e, 2},
2263 { 0xa779, 0xa77b, 2},
2264 { 0xa77d, 0xa77e, 1},
2265 { 0xa780, 0xa786, 2},
2266 { 0xa78b, 0xa78b, 1},
2267 { 0xff21, 0xff3a, 1},
2268 { 0x10400, 0x10427, 1},
2269 { 0x1d400, 0x1d419, 1},
2270 { 0x1d434, 0x1d44d, 1},
2271 { 0x1d468, 0x1d481, 1},
2272 { 0x1d49c, 0x1d49c, 1},
2273 { 0x1d49e, 0x1d49f, 1},
2274 { 0x1d4a2, 0x1d4a2, 1},
2275 { 0x1d4a5, 0x1d4a6, 1},
2276 { 0x1d4a9, 0x1d4ac, 1},
2277 { 0x1d4ae, 0x1d4b5, 1},
2278 { 0x1d4d0, 0x1d4e9, 1},
2279 { 0x1d504, 0x1d505, 1},
2280 { 0x1d507, 0x1d50a, 1},
2281 { 0x1d50d, 0x1d514, 1},
2282 { 0x1d516, 0x1d51c, 1},
2283 { 0x1d538, 0x1d539, 1},
2284 { 0x1d53b, 0x1d53e, 1},
2285 { 0x1d540, 0x1d544, 1},
2286 { 0x1d546, 0x1d546, 1},
2287 { 0x1d54a, 0x1d550, 1},
2288 { 0x1d56c, 0x1d585, 1},
2289 { 0x1d5a0, 0x1d5b9, 1},
2290 { 0x1d5d4, 0x1d5ed, 1},
2291 { 0x1d608, 0x1d621, 1},
2292 { 0x1d63c, 0x1d655, 1},
2293 { 0x1d670, 0x1d689, 1},
2294 { 0x1d6a8, 0x1d6c0, 1},
2295 { 0x1d6e2, 0x1d6fa, 1},
2296 { 0x1d71c, 0x1d734, 1},
2297 { 0x1d756, 0x1d76e, 1},
2298 { 0x1d790, 0x1d7a8, 1},
2299 { 0x1d7ca, 0x1d7ca, 1},
2302 // Return true if C is in RANGES.
2304 bool
2305 Lex::is_in_unicode_range(unsigned int c, const Unicode_range* ranges,
2306 size_t range_size)
2308 if (c < 0x100)
2310 // The common case is a small value, and we know that it will be
2311 // in the first few entries of the table. Do a linear scan
2312 // rather than a binary search.
2313 for (size_t i = 0; i < range_size; ++i)
2315 const Unicode_range* p = &ranges[i];
2316 if (c <= p->high)
2318 if (c < p->low)
2319 return false;
2320 return (c - p->low) % p->stride == 0;
2323 return false;
2325 else
2327 size_t lo = 0;
2328 size_t hi = range_size;
2329 while (lo < hi)
2331 size_t mid = lo + (hi - lo) / 2;
2332 const Unicode_range* p = &ranges[mid];
2333 if (c < p->low)
2334 hi = mid;
2335 else if (c > p->high)
2336 lo = mid + 1;
2337 else
2338 return (c - p->low) % p->stride == 0;
2340 return false;
2344 // Return whether C is a space character.
2346 bool
2347 Lex::is_unicode_space(unsigned int c)
2349 return Lex::is_in_unicode_range(c, unicode_space,
2350 ARRAY_SIZE(unicode_space));
2353 // Return whether C is a Unicode digit--a Unicode code point
2354 // classified as "Digit".
2356 bool
2357 Lex::is_unicode_digit(unsigned int c)
2359 return Lex::is_in_unicode_range(c, unicode_digits,
2360 ARRAY_SIZE(unicode_digits));
2363 // Return whether C is a Unicode letter--a Unicode code point
2364 // classified as "Letter".
2366 bool
2367 Lex::is_unicode_letter(unsigned int c)
2369 return Lex::is_in_unicode_range(c, unicode_letters,
2370 ARRAY_SIZE(unicode_letters));
2373 // Return whether C is a Unicode uppercase letter. a Unicode code
2374 // point classified as "Letter, uppercase".
2376 bool
2377 Lex::is_unicode_uppercase(unsigned int c)
2379 return Lex::is_in_unicode_range(c, unicode_uppercase_letters,
2380 ARRAY_SIZE(unicode_uppercase_letters));
2383 // Return whether the identifier NAME should be exported. NAME is a
2384 // mangled name which includes only ASCII characters.
2386 bool
2387 Lex::is_exported_name(const std::string& name)
2389 unsigned char c = name[0];
2390 if (c != '$')
2391 return c >= 'A' && c <= 'Z';
2392 else
2394 const char* p = name.data();
2395 size_t len = name.length();
2396 if (len < 2 || p[1] != 'U')
2397 return false;
2398 unsigned int ci = 0;
2399 for (size_t i = 2; i < len && p[i] != '$'; ++i)
2401 c = p[i];
2402 if (!hex_p(c))
2403 return false;
2404 ci <<= 4;
2405 ci |= hex_value(c);
2407 return Lex::is_unicode_uppercase(ci);
2411 // Return whether the identifier NAME contains an invalid character.
2412 // This is based on how we handle invalid characters in
2413 // gather_identifier.
2415 bool
2416 Lex::is_invalid_identifier(const std::string& name)
2418 return name.find("$INVALID$") != std::string::npos;