OSDN Git Service

effa0871699b0bbef690878465dd0754887f26b7
[pf3gnuchains/gcc-fork.git] / gcc / go / gofrontend / lex.cc
1 // lex.cc -- Go frontend lexer.
2
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.
6
7 #include "go-system.h"
8
9 #include "lex.h"
10
11 // Manage mapping from keywords to the Keyword codes.
12
13 class Keywords
14 {
15  public:
16   // The structure which maps keywords to codes.
17   struct Mapping
18   {
19     // Keyword string.
20     const char* keystring;
21     // Keyword code.
22     Keyword keycode;
23   };
24
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;
29
30   // Return the string for a keyword.
31   const char*
32   keyword_to_string(Keyword) const;
33
34  private:
35   static const Mapping mapping_[];
36   static const int count_;
37 };
38
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.
42
43 const Keywords::Mapping
44 Keywords::mapping_[] =
45 {
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 }
73 };
74
75 // Number of entries in the map.
76
77 const int Keywords::count_ =
78   sizeof(Keywords::mapping_) / sizeof(Keywords::mapping_[0]);
79
80 // Comparison function passed to bsearch.
81
82 extern "C"
83 {
84
85 struct Keywords_search_key
86 {
87   const char* str;
88   size_t len;
89 };
90
91 static int
92 keyword_compare(const void* keyv, const void* mapv)
93 {
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;
106 }
107
108 } // End extern "C".
109
110 // Convert a string to a keyword code.  Return KEYWORD_INVALID if the
111 // string is not a keyword.
112
113 Keyword
114 Keywords::keyword_to_code(const char* keyword, size_t len) const
115 {
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;
128 }
129
130 // Convert a keyword code to a string.
131
132 const char*
133 Keywords::keyword_to_string(Keyword code) const
134 {
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;
139 }
140
141 // There is one instance of the Keywords class.
142
143 static Keywords keywords;
144
145 // Class Token.
146
147 // Make a general token.
148
149 Token::Token(Classification classification, Location location)
150   : classification_(classification), location_(location)
151 {
152 }
153
154 // Destroy a token.
155
156 Token::~Token()
157 {
158   this->clear();
159 }
160
161 // Clear a token--release memory.
162
163 void
164 Token::clear()
165 {
166   if (this->classification_ == TOKEN_INTEGER)
167     mpz_clear(this->u_.integer_value);
168   else if (this->classification_ == TOKEN_FLOAT
169            || this->classification_ == TOKEN_IMAGINARY)
170     mpfr_clear(this->u_.float_value);
171 }
172
173 // Construct a token.
174
175 Token::Token(const Token& tok)
176   : classification_(tok.classification_), location_(tok.location_)
177 {
178   switch (this->classification_)
179     {
180     case TOKEN_INVALID:
181     case TOKEN_EOF:
182       break;
183     case TOKEN_KEYWORD:
184       this->u_.keyword = tok.u_.keyword;
185       break;
186     case TOKEN_IDENTIFIER:
187     case TOKEN_STRING:
188       this->u_.string_value = tok.u_.string_value;
189       break;
190     case TOKEN_OPERATOR:
191       this->u_.op = tok.u_.op;
192       break;
193     case TOKEN_INTEGER:
194       mpz_init_set(this->u_.integer_value, tok.u_.integer_value);
195       break;
196     case TOKEN_FLOAT:
197     case TOKEN_IMAGINARY:
198       mpfr_init_set(this->u_.float_value, tok.u_.float_value, GMP_RNDN);
199       break;
200     default:
201       go_unreachable();
202     }
203 }
204
205 // Assign to a token.
206
207 Token&
208 Token::operator=(const Token& tok)
209 {
210   this->clear();
211   this->classification_ = tok.classification_;
212   this->location_ = tok.location_;
213   switch (tok.classification_)
214     {
215     case TOKEN_INVALID:
216     case TOKEN_EOF:
217       break;
218     case TOKEN_KEYWORD:
219       this->u_.keyword = tok.u_.keyword;
220       break;
221     case TOKEN_IDENTIFIER:
222       this->u_.identifier_value.name = tok.u_.identifier_value.name;
223       this->u_.identifier_value.is_exported =
224         tok.u_.identifier_value.is_exported;
225       break;
226     case TOKEN_STRING:
227       this->u_.string_value = tok.u_.string_value;
228       break;
229     case TOKEN_OPERATOR:
230       this->u_.op = tok.u_.op;
231       break;
232     case TOKEN_INTEGER:
233       mpz_init_set(this->u_.integer_value, tok.u_.integer_value);
234       break;
235     case TOKEN_FLOAT:
236     case TOKEN_IMAGINARY:
237       mpfr_init_set(this->u_.float_value, tok.u_.float_value, GMP_RNDN);
238       break;
239     default:
240       go_unreachable();
241     }
242   return *this;
243 }
244
245 // Print the token for debugging.
246
247 void
248 Token::print(FILE* file) const
249 {
250   switch (this->classification_)
251     {
252     case TOKEN_INVALID:
253       fprintf(file, "invalid");
254       break;
255     case TOKEN_EOF:
256       fprintf(file, "EOF");
257       break;
258     case TOKEN_KEYWORD:
259       fprintf(file, "keyword %s", keywords.keyword_to_string(this->u_.keyword));
260       break;
261     case TOKEN_IDENTIFIER:
262       fprintf(file, "identifier \"%s\"", this->u_.string_value->c_str());
263       break;
264     case TOKEN_STRING:
265       fprintf(file, "quoted string \"%s\"", this->u_.string_value->c_str());
266       break;
267     case TOKEN_INTEGER:
268       fprintf(file, "integer ");
269       mpz_out_str(file, 10, this->u_.integer_value);
270       break;
271     case TOKEN_FLOAT:
272       fprintf(file, "float ");
273       mpfr_out_str(file, 10, 0, this->u_.float_value, GMP_RNDN);
274       break;
275     case TOKEN_IMAGINARY:
276       fprintf(file, "imaginary ");
277       mpfr_out_str(file, 10, 0, this->u_.float_value, GMP_RNDN);
278       break;
279     case TOKEN_OPERATOR:
280       fprintf(file, "operator ");
281       switch (this->u_.op)
282         {
283         case OPERATOR_INVALID:
284           fprintf(file, "invalid");
285           break;
286         case OPERATOR_OROR:
287           fprintf(file, "||");
288           break;
289         case OPERATOR_ANDAND:
290           fprintf(file, "&&");
291           break;
292         case OPERATOR_EQEQ:
293           fprintf(file, "==");
294           break;
295         case OPERATOR_NOTEQ:
296           fprintf(file, "!=");
297           break;
298         case OPERATOR_LT:
299           fprintf(file, "<");
300           break;
301         case OPERATOR_LE:
302           fprintf(file, "<=");
303           break;
304         case OPERATOR_GT:
305           fprintf(file, ">");
306           break;
307         case OPERATOR_GE:
308           fprintf(file, ">=");
309           break;
310         case OPERATOR_PLUS:
311           fprintf(file, "+");
312           break;
313         case OPERATOR_MINUS:
314           fprintf(file, "-");
315           break;
316         case OPERATOR_OR:
317           fprintf(file, "|");
318           break;
319         case OPERATOR_XOR:
320           fprintf(file, "^");
321           break;
322         case OPERATOR_MULT:
323           fprintf(file, "*");
324           break;
325         case OPERATOR_DIV:
326           fprintf(file, "/");
327           break;
328         case OPERATOR_MOD:
329           fprintf(file, "%%");
330           break;
331         case OPERATOR_LSHIFT:
332           fprintf(file, "<<");
333           break;
334         case OPERATOR_RSHIFT:
335           fprintf(file, ">>");
336           break;
337         case OPERATOR_AND:
338           fprintf(file, "&");
339           break;
340         case OPERATOR_BITCLEAR:
341           fprintf(file, "&^");
342           break;
343         case OPERATOR_NOT:
344           fprintf(file, "!");
345           break;
346         case OPERATOR_CHANOP:
347           fprintf(file, "<-");
348           break;
349         case OPERATOR_EQ:
350           fprintf(file, "=");
351           break;
352         case OPERATOR_PLUSEQ:
353           fprintf(file, "+=");
354           break;
355         case OPERATOR_MINUSEQ:
356           fprintf(file, "-=");
357           break;
358         case OPERATOR_OREQ:
359           fprintf(file, "|=");
360           break;
361         case OPERATOR_XOREQ:
362           fprintf(file, "^=");
363           break;
364         case OPERATOR_MULTEQ:
365           fprintf(file, "*=");
366           break;
367         case OPERATOR_DIVEQ:
368           fprintf(file, "/=");
369           break;
370         case OPERATOR_MODEQ:
371           fprintf(file, "%%=");
372           break;
373         case OPERATOR_LSHIFTEQ:
374           fprintf(file, "<<=");
375           break;
376         case OPERATOR_RSHIFTEQ:
377           fprintf(file, ">>=");
378           break;
379         case OPERATOR_ANDEQ:
380           fprintf(file, "&=");
381           break;
382         case OPERATOR_BITCLEAREQ:
383           fprintf(file, "&^=");
384           break;
385         case OPERATOR_PLUSPLUS:
386           fprintf(file, "++");
387           break;
388         case OPERATOR_MINUSMINUS:
389           fprintf(file, "--");
390           break;
391         case OPERATOR_COLON:
392           fprintf(file, ":");
393           break;
394         case OPERATOR_COLONEQ:
395           fprintf(file, ":=");
396           break;
397         case OPERATOR_SEMICOLON:
398           fprintf(file, ";");
399           break;
400         case OPERATOR_DOT:
401           fprintf(file, ".");
402           break;
403         case OPERATOR_COMMA:
404           fprintf(file, ",");
405           break;
406         case OPERATOR_LPAREN:
407           fprintf(file, "(");
408           break;
409         case OPERATOR_RPAREN:
410           fprintf(file, ")");
411           break;
412         case OPERATOR_LCURLY:
413           fprintf(file, "{");
414           break;
415         case OPERATOR_RCURLY:
416           fprintf(file, "}");
417           break;
418         case OPERATOR_LSQUARE:
419           fprintf(file, "[");
420           break;
421         case OPERATOR_RSQUARE:
422           fprintf(file, "]");
423           break;
424         default:
425           go_unreachable();
426         }
427       break;
428     default:
429       go_unreachable();
430     }
431 }
432
433 // Class Lex.
434
435 Lex::Lex(const char* input_file_name, FILE* input_file, Linemap* linemap)
436   : input_file_name_(input_file_name), input_file_(input_file),
437     linemap_(linemap), linebuf_(NULL), linebufsize_(120), linesize_(0),
438     lineoff_(0), lineno_(0), add_semi_at_eol_(false)
439 {
440   this->linebuf_ = new char[this->linebufsize_];
441   this->linemap_->start_file(input_file_name, 0);
442 }
443
444 Lex::~Lex()
445 {
446   delete[] this->linebuf_;
447 }
448
449 // Read a new line from the file.
450
451 ssize_t
452 Lex::get_line()
453 {
454   char* buf = this->linebuf_;
455   size_t size = this->linebufsize_;
456
457   FILE* file = this->input_file_;
458   size_t cur = 0;
459   while (true)
460     {
461       int c = getc(file);
462       if (c == EOF)
463         {
464           if (cur == 0)
465             return -1;
466           break;
467         }
468       if (cur + 1 >= size)
469         {
470           size_t ns = 2 * size + 1;
471           if (ns < size || static_cast<ssize_t>(ns) < 0)
472             error_at(this->location(), "out of memory");
473           char* nb = new char[ns];
474           memcpy(nb, buf, cur);
475           delete[] buf;
476           buf = nb;
477           size = ns;
478         }
479       buf[cur] = c;
480       ++cur;
481
482       if (c == '\n')
483         break;
484     }
485
486   buf[cur] = '\0';
487
488   this->linebuf_ = buf;
489   this->linebufsize_ = size;
490
491   return cur;
492 }
493
494 // See if we need to read a new line.  Return true if there is a new
495 // line, false if we are at EOF.
496
497 bool
498 Lex::require_line()
499 {
500   if (this->lineoff_ < this->linesize_)
501     return true;
502
503   ssize_t got = this->get_line();
504   if (got < 0)
505     return false;
506   ++this->lineno_;
507   this->linesize_= got;
508   this->lineoff_ = 0;
509
510   this->linemap_->start_line(this->lineno_, this->linesize_);
511
512   return true;
513 }
514
515 // Get the current location.
516
517 Location
518 Lex::location() const
519 {
520   return this->linemap_->get_location(this->lineoff_ + 1);
521 }
522
523 // Get a location slightly before the current one.  This is used for
524 // slightly more efficient handling of operator tokens.
525
526 Location
527 Lex::earlier_location(int chars) const
528 {
529   return this->linemap_->get_location(this->lineoff_ + 1 - chars);
530 }
531
532 // Get the next token.
533
534 Token
535 Lex::next_token()
536 {
537   while (true)
538     {
539       if (!this->require_line())
540         {
541           bool add_semi_at_eol = this->add_semi_at_eol_;
542           this->add_semi_at_eol_ = false;
543           if (add_semi_at_eol)
544             return this->make_operator(OPERATOR_SEMICOLON, 1);
545           return this->make_eof_token();
546         }
547
548       const char* p = this->linebuf_ + this->lineoff_;
549       const char* pend = this->linebuf_ + this->linesize_;
550
551       while (p < pend)
552         {
553           unsigned char cc = *p;
554           switch (cc)
555             {
556             case ' ': case '\t': case '\r':
557               ++p;
558               // Skip whitespace quickly.
559               while (*p == ' ' || *p == '\t' || *p == '\r')
560                 ++p;
561               break;
562
563             case '\n':
564               {
565                 ++p;
566                 bool add_semi_at_eol = this->add_semi_at_eol_;
567                 this->add_semi_at_eol_ = false;
568                 if (add_semi_at_eol)
569                   {
570                     this->lineoff_ = p - this->linebuf_;
571                     return this->make_operator(OPERATOR_SEMICOLON, 1);
572                   }
573               }
574               break;
575
576             case '/':
577               if (p[1] == '/')
578                 {
579                   this->lineoff_ = p + 2 - this->linebuf_;
580                   this->skip_cpp_comment();
581                   p = pend;
582                   if (p[-1] == '\n' && this->add_semi_at_eol_)
583                     --p;
584                 }
585               else if (p[1] == '*')
586                 {
587                   this->lineoff_ = p - this->linebuf_;
588                   Location location = this->location();
589                   if (!this->skip_c_comment())
590                     return Token::make_invalid_token(location);
591                   p = this->linebuf_ + this->lineoff_;
592                   pend = this->linebuf_ + this->linesize_;
593                 }
594               else if (p[1] == '=')
595                 {
596                   this->add_semi_at_eol_ = false;
597                   this->lineoff_ = p + 2 - this->linebuf_;
598                   return this->make_operator(OPERATOR_DIVEQ, 2);
599                 }
600               else
601                 {
602                   this->add_semi_at_eol_ = false;
603                   this->lineoff_ = p + 1 - this->linebuf_;
604                   return this->make_operator(OPERATOR_DIV, 1);
605                 }
606               break;
607
608             case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
609             case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
610             case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
611             case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
612             case 'Y': case 'Z':
613             case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
614             case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
615             case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
616             case 's': case 't': case 'u': case 'v': case 'w': case 'x':
617             case 'y': case 'z':
618             case '_':
619               this->lineoff_ = p - this->linebuf_;
620               return this->gather_identifier();
621
622             case '0': case '1': case '2': case '3': case '4':
623             case '5': case '6': case '7': case '8': case '9':
624               this->add_semi_at_eol_ = true;
625               this->lineoff_ = p - this->linebuf_;
626               return this->gather_number();
627
628             case '\'':
629               this->add_semi_at_eol_ = true;
630               this->lineoff_ = p - this->linebuf_;
631               return this->gather_character();
632
633             case '"':
634               this->add_semi_at_eol_ = true;
635               this->lineoff_ = p - this->linebuf_;
636               return this->gather_string();
637
638             case '`':
639               this->add_semi_at_eol_ = true;
640               this->lineoff_ = p - this->linebuf_;
641               return this->gather_raw_string();
642
643             case '<':
644             case '>':
645             case '&':
646               if (p + 2 < pend)
647                 {
648                   this->add_semi_at_eol_ = false;
649                   Operator op = this->three_character_operator(cc, p[1], p[2]);
650                   if (op != OPERATOR_INVALID)
651                     {
652                       this->lineoff_ = p + 3 - this->linebuf_;
653                       return this->make_operator(op, 3);
654                     }
655                 }
656               // Fall through.
657             case '|':
658             case '=':
659             case '!':
660             case '+':
661             case '-':
662             case '^':
663             case '*':
664               // '/' handled above.
665             case '%':
666             case ':':
667             case ';':
668             case ',':
669             case '(': case ')':
670             case '{': case '}':
671             case '[': case ']':
672               {
673                 this->add_semi_at_eol_ = false;
674                 Operator op = this->two_character_operator(cc, p[1]);
675                 int chars;
676                 if (op != OPERATOR_INVALID)
677                   {
678                     ++p;
679                     chars = 2;
680                   }
681                 else
682                   {
683                     op = this->one_character_operator(cc);
684                     chars = 1;
685                   }
686                 this->lineoff_ = p + 1 - this->linebuf_;
687                 return this->make_operator(op, chars);
688               }
689
690             case '.':
691               if (p[1] >= '0' && p[1] <= '9')
692                 {
693                   this->add_semi_at_eol_ = true;
694                   this->lineoff_ = p - this->linebuf_;
695                   return this->gather_number();
696                 }
697               if (p[1] == '.' && p[2] == '.')
698                 {
699                   this->add_semi_at_eol_ = false;
700                   this->lineoff_ = p + 3 - this->linebuf_;
701                   return this->make_operator(OPERATOR_ELLIPSIS, 3);
702                 }
703               this->add_semi_at_eol_ = false;
704               this->lineoff_ = p + 1 - this->linebuf_;
705               return this->make_operator(OPERATOR_DOT, 1);
706
707             default:
708               {
709                 unsigned int ci;
710                 bool issued_error;
711                 this->lineoff_ = p - this->linebuf_;
712                 this->advance_one_utf8_char(p, &ci, &issued_error);
713                 if (Lex::is_unicode_letter(ci))
714                   return this->gather_identifier();
715
716                 if (!issued_error)
717                   error_at(this->location(),
718                            "invalid character 0x%x in input file",
719                            ci);
720
721                 p = pend;
722
723                 break;
724               }
725             }
726         }
727
728       this->lineoff_ = p - this->linebuf_;
729     }
730 }
731
732 // Fetch one UTF-8 character from a string.  Set *VALUE to the value.
733 // Return the number of bytes read from the string.  Returns 0 if the
734 // string does not point to a valid UTF-8 character.
735
736 int
737 Lex::fetch_char(const char* p, unsigned int* value)
738 {
739   unsigned char c = *p;
740   if (c <= 0x7f)
741     {
742       *value = c;
743       return 1;
744     }
745   else if ((c & 0xe0) == 0xc0
746            && (p[1] & 0xc0) == 0x80)
747     {
748       *value = (((c & 0x1f) << 6)
749                 + (p[1] & 0x3f));
750       if (*value <= 0x7f)
751         {
752           *value = 0xfffd;
753           return 0;
754         }
755       return 2;
756     }
757   else if ((c & 0xf0) == 0xe0
758            && (p[1] & 0xc0) == 0x80
759            && (p[2] & 0xc0) == 0x80)
760     {
761       *value = (((c & 0xf) << 12)
762                 + ((p[1] & 0x3f) << 6)
763                 + (p[2] & 0x3f));
764       if (*value <= 0x7ff)
765         {
766           *value = 0xfffd;
767           return 0;
768         }
769       return 3;
770     }
771   else if ((c & 0xf8) == 0xf0
772            && (p[1] & 0xc0) == 0x80
773            && (p[2] & 0xc0) == 0x80
774            && (p[3] & 0xc0) == 0x80)
775     {
776       *value = (((c & 0x7) << 18)
777                 + ((p[1] & 0x3f) << 12)
778                 + ((p[2] & 0x3f) << 6)
779                 + (p[3] & 0x3f));
780       if (*value <= 0xffff)
781         {
782           *value = 0xfffd;
783           return 0;
784         }
785       return 4;
786     }
787   else
788     {
789       /* Invalid encoding. Return the Unicode replacement
790          character.  */
791       *value = 0xfffd;
792       return 0;
793     }
794 }
795
796 // Advance one UTF-8 character.  Return the pointer beyond the
797 // character.  Set *VALUE to the value.  Set *ISSUED_ERROR if an error
798 // was issued.
799
800 const char*
801 Lex::advance_one_utf8_char(const char* p, unsigned int* value,
802                            bool* issued_error)
803 {
804   *issued_error = false;
805
806   if (*p == '\0')
807     {
808       error_at(this->location(), "invalid NUL byte");
809       *issued_error = true;
810       *value = 0;
811       return p + 1;
812     }
813
814   int adv = Lex::fetch_char(p, value);
815   if (adv == 0)
816     {
817       error_at(this->location(), "invalid UTF-8 encoding");
818       *issued_error = true;
819       return p + 1;
820     }
821   return p + adv;
822 }
823
824 // Pick up an identifier.
825
826 Token
827 Lex::gather_identifier()
828 {
829   const char* pstart = this->linebuf_ + this->lineoff_;
830   const char* p = pstart;
831   const char* pend = this->linebuf_ + this->linesize_;
832   bool is_first = true;
833   bool is_exported = false;
834   bool has_non_ascii_char = false;
835   std::string buf;
836   while (p < pend)
837     {
838       unsigned char cc = *p;
839       if (cc <= 0x7f)
840         {
841           if ((cc < 'A' || cc > 'Z')
842               && (cc < 'a' || cc > 'z')
843               && cc != '_'
844               && (cc < '0' || cc > '9'))
845             break;
846           ++p;
847           if (is_first)
848             {
849               is_exported = cc >= 'A' && cc <= 'Z';
850               is_first = false;
851             }
852           if (has_non_ascii_char)
853             buf.push_back(cc);
854         }
855       else
856         {
857           unsigned int ci;
858           bool issued_error;
859           this->lineoff_ = p - this->linebuf_;
860           const char* pnext = this->advance_one_utf8_char(p, &ci,
861                                                           &issued_error);
862           if (!Lex::is_unicode_letter(ci) && !Lex::is_unicode_digit(ci))
863             {
864               // There is no valid place for a non-ASCII character
865               // other than an identifier, so we get better error
866               // handling behaviour if we swallow this character after
867               // giving an error.
868               if (!issued_error)
869                 error_at(this->location(),
870                          "invalid character 0x%x in identifier",
871                          ci);
872             }
873           if (is_first)
874             {
875               is_exported = Lex::is_unicode_uppercase(ci);
876               is_first = false;
877             }
878           if (!has_non_ascii_char)
879             {
880               buf.assign(pstart, p - pstart);
881               has_non_ascii_char = true;
882             }
883           p = pnext;
884           char ubuf[50];
885           // This assumes that all assemblers can handle an identifier
886           // with a '$' character.
887           snprintf(ubuf, sizeof ubuf, "$U%x$", ci);
888           buf.append(ubuf);
889         }
890     }
891   Location location = this->location();
892   this->add_semi_at_eol_ = true;
893   this->lineoff_ = p - this->linebuf_;
894   if (has_non_ascii_char)
895     return Token::make_identifier_token(buf, is_exported, location);
896   else
897     {
898       Keyword code = keywords.keyword_to_code(pstart, p - pstart);
899       if (code == KEYWORD_INVALID)
900         return Token::make_identifier_token(std::string(pstart, p - pstart),
901                                             is_exported, location);
902       else
903         {
904           switch (code)
905             {
906             case KEYWORD_BREAK:
907             case KEYWORD_CONTINUE:
908             case KEYWORD_FALLTHROUGH:
909             case KEYWORD_RETURN:
910               break;
911             default:
912               this->add_semi_at_eol_ = false;
913               break;
914             }
915           return Token::make_keyword_token(code, location);
916         }
917     }
918 }
919
920 // Return whether C is a hex digit.
921
922 bool
923 Lex::is_hex_digit(char c)
924 {
925   return ((c >= '0' && c <= '9')
926           || (c >= 'A' && c <= 'F')
927           || (c >= 'a' && c <= 'f'));
928 }
929
930 // Return whether an exponent could start at P.
931
932 bool
933 Lex::could_be_exponent(const char* p, const char* pend)
934 {
935   if (*p != 'e' && *p != 'E')
936     return false;
937   ++p;
938   if (p >= pend)
939     return false;
940   if (*p == '+' || *p == '-')
941     {
942       ++p;
943       if (p >= pend)
944         return false;
945     }
946   return *p >= '0' && *p <= '9';
947 }
948
949 // Pick up a number.
950
951 Token
952 Lex::gather_number()
953 {
954   const char* pstart = this->linebuf_ + this->lineoff_;
955   const char* p = pstart;
956   const char* pend = this->linebuf_ + this->linesize_;
957
958   Location location = this->location();
959
960   bool neg = false;
961   if (*p == '+')
962     ++p;
963   else if (*p == '-')
964     {
965       ++p;
966       neg = true;
967     }
968
969   const char* pnum = p;
970   if (*p == '0')
971     {
972       int base;
973       if ((p[1] == 'x' || p[1] == 'X')
974           && Lex::is_hex_digit(p[2]))
975         {
976           base = 16;
977           p += 2;
978           pnum = p;
979           while (p < pend)
980             {
981               if (!Lex::is_hex_digit(*p))
982                 break;
983               ++p;
984             }
985         }
986       else
987         {
988           base = 8;
989           pnum = p;
990           while (p < pend)
991             {
992               if (*p < '0' || *p > '7')
993                 break;
994               ++p;
995             }
996         }
997
998       if (*p != '.' && *p != 'i' && !Lex::could_be_exponent(p, pend))
999         {
1000           std::string s(pnum, p - pnum);
1001           mpz_t val;
1002           int r = mpz_init_set_str(val, s.c_str(), base);
1003           go_assert(r == 0);
1004
1005           if (neg)
1006             mpz_neg(val, val);
1007
1008           this->lineoff_ = p - this->linebuf_;
1009           Token ret = Token::make_integer_token(val, location);
1010           mpz_clear(val);
1011           return ret;
1012         }
1013     }
1014
1015   while (p < pend)
1016     {
1017       if (*p < '0' || *p > '9')
1018         break;
1019       ++p;
1020     }
1021
1022   if (*p != '.' && *p != 'i' && !Lex::could_be_exponent(p, pend))
1023     {
1024       std::string s(pnum, p - pnum);
1025       mpz_t val;
1026       int r = mpz_init_set_str(val, s.c_str(), 10);
1027       go_assert(r == 0);
1028
1029       if (neg)
1030         mpz_neg(val, val);
1031
1032       this->lineoff_ = p - this->linebuf_;
1033       Token ret = Token::make_integer_token(val, location);
1034       mpz_clear(val);
1035       return ret;
1036     }
1037
1038   if (*p != 'i')
1039     {
1040       bool dot = *p == '.';
1041
1042       ++p;
1043
1044       if (!dot)
1045         {
1046           if (*p == '+' || *p == '-')
1047             ++p;
1048         }
1049
1050       while (p < pend)
1051         {
1052           if (*p < '0' || *p > '9')
1053             break;
1054           ++p;
1055         }
1056
1057       if (dot && Lex::could_be_exponent(p, pend))
1058         {
1059           ++p;
1060           if (*p == '+' || *p == '-')
1061             ++p;
1062           while (p < pend)
1063             {
1064               if (*p < '0' || *p > '9')
1065                 break;
1066               ++p;
1067             }
1068         }
1069     }
1070
1071   std::string s(pnum, p - pnum);
1072   mpfr_t val;
1073   int r = mpfr_init_set_str(val, s.c_str(), 10, GMP_RNDN);
1074   go_assert(r == 0);
1075
1076   if (neg)
1077     mpfr_neg(val, val, GMP_RNDN);
1078
1079   bool is_imaginary = *p == 'i';
1080   if (is_imaginary)
1081     ++p;
1082
1083   this->lineoff_ = p - this->linebuf_;
1084   if (is_imaginary)
1085     {
1086       Token ret = Token::make_imaginary_token(val, location);
1087       mpfr_clear(val);
1088       return ret;
1089     }
1090   else
1091     {
1092       Token ret = Token::make_float_token(val, location);
1093       mpfr_clear(val);
1094       return ret;
1095     }
1096 }
1097
1098 // Advance one character, possibly escaped.  Return the pointer beyond
1099 // the character.  Set *VALUE to the character.  Set *IS_CHARACTER if
1100 // this is a character (e.g., 'a' or '\u1234') rather than a byte
1101 // value (e.g., '\001').
1102
1103 const char*
1104 Lex::advance_one_char(const char* p, bool is_single_quote, unsigned int* value,
1105                       bool* is_character)
1106 {
1107   *value = 0;
1108   *is_character = true;
1109   if (*p != '\\')
1110     {
1111       bool issued_error;
1112       const char* ret = this->advance_one_utf8_char(p, value, &issued_error);
1113       if (is_single_quote
1114           && (*value == '\'' || *value == '\n')
1115           && !issued_error)
1116         error_at(this->location(), "invalid character literal");
1117       return ret;
1118     }
1119   else
1120     {
1121       ++p;
1122       switch (*p)
1123         {
1124         case '0': case '1': case '2': case '3':
1125         case '4': case '5': case '6': case '7':
1126           *is_character = false;
1127           if (p[1] >= '0' && p[1] <= '7'
1128               && p[2] >= '0' && p[2] <= '7')
1129             {
1130               *value = ((Lex::octal_value(p[0]) << 6)
1131                         + (Lex::octal_value(p[1]) << 3)
1132                         + Lex::octal_value(p[2]));
1133               if (*value > 255)
1134                 {
1135                   error_at(this->location(), "invalid octal constant");
1136                   *value = 255;
1137                 }
1138               return p + 3;
1139             }
1140               error_at(this->location(), "invalid octal character");
1141           return (p[1] >= '0' && p[1] <= '7'
1142                   ? p + 2
1143                   : p + 1);
1144
1145         case 'x':
1146         case 'X':
1147           *is_character = false;
1148           if (Lex::is_hex_digit(p[1]) && Lex::is_hex_digit(p[2]))
1149             {
1150               *value = (hex_value(p[1]) << 4) + hex_value(p[2]);
1151               return p + 3;
1152             }
1153           error_at(this->location(), "invalid hex character");
1154           return (Lex::is_hex_digit(p[1])
1155                   ? p + 2
1156                   : p + 1);
1157
1158         case 'a':
1159           *value = '\a';
1160           return p + 1;
1161         case 'b':
1162           *value = '\b';
1163           return p + 1;
1164         case 'f':
1165           *value = '\f';
1166           return p + 1;
1167         case 'n':
1168           *value = '\n';
1169           return p + 1;
1170         case 'r':
1171           *value = '\r';
1172           return p + 1;
1173         case 't':
1174           *value = '\t';
1175           return p + 1;
1176         case 'v':
1177           *value = '\v';
1178           return p + 1;
1179         case '\\':
1180           *value = '\\';
1181           return p + 1;
1182         case '\'':
1183           if (!is_single_quote)
1184             error_at(this->location(), "invalid quoted character");
1185           *value = '\'';
1186           return p + 1;
1187         case '"':
1188           if (is_single_quote)
1189             error_at(this->location(), "invalid quoted character");
1190           *value = '"';
1191           return p + 1;
1192
1193         case 'u':
1194           if (Lex::is_hex_digit(p[1]) && Lex::is_hex_digit(p[2])
1195               && Lex::is_hex_digit(p[3]) && Lex::is_hex_digit(p[4]))
1196             {
1197               *value = ((hex_value(p[1]) << 12)
1198                         + (hex_value(p[2]) << 8)
1199                         + (hex_value(p[3]) << 4)
1200                         + hex_value(p[4]));
1201               if (*value >= 0xd800 && *value < 0xe000)
1202                 {
1203                   error_at(this->location(),
1204                            "invalid unicode code point 0x%x",
1205                            *value);
1206                   // Use the replacement character.
1207                   *value = 0xfffd;
1208                 }
1209               return p + 5;
1210             }
1211           error_at(this->location(), "invalid little unicode code point");
1212           return p + 1;
1213
1214         case 'U':
1215           if (Lex::is_hex_digit(p[1]) && Lex::is_hex_digit(p[2])
1216               && Lex::is_hex_digit(p[3]) && Lex::is_hex_digit(p[4])
1217               && Lex::is_hex_digit(p[5]) && Lex::is_hex_digit(p[6])
1218               && Lex::is_hex_digit(p[7]) && Lex::is_hex_digit(p[8]))
1219             {
1220               *value = ((hex_value(p[1]) << 28)
1221                         + (hex_value(p[2]) << 24)
1222                         + (hex_value(p[3]) << 20)
1223                         + (hex_value(p[4]) << 16)
1224                         + (hex_value(p[5]) << 12)
1225                         + (hex_value(p[6]) << 8)
1226                         + (hex_value(p[7]) << 4)
1227                         + hex_value(p[8]));
1228               if (*value > 0x10ffff
1229                   || (*value >= 0xd800 && *value < 0xe000))
1230                 {
1231                   error_at(this->location(), "invalid unicode code point 0x%x",
1232                            *value);
1233                   // Use the replacement character.
1234                   *value = 0xfffd;
1235                 }
1236               return p + 9;
1237             }
1238           error_at(this->location(), "invalid big unicode code point");
1239           return p + 1;
1240
1241         default:
1242           error_at(this->location(), "invalid character after %<\\%>");
1243           *value = *p;
1244           return p + 1;
1245         }
1246     }
1247 }
1248
1249 // Append V to STR.  IS_CHARACTER is true for a character which should
1250 // be stored in UTF-8, false for a general byte value which should be
1251 // stored directly.
1252
1253 void
1254 Lex::append_char(unsigned int v, bool is_character, std::string* str,
1255                  Location location)
1256 {
1257   char buf[4];
1258   size_t len;
1259   if (v <= 0x7f || !is_character)
1260     {
1261       buf[0] = v;
1262       len = 1;
1263     }
1264   else if (v <= 0x7ff)
1265     {
1266       buf[0] = 0xc0 + (v >> 6);
1267       buf[1] = 0x80 + (v & 0x3f);
1268       len = 2;
1269     }
1270   else
1271     {
1272       if (v > 0x10ffff)
1273         {
1274           warning_at(location, 0,
1275                      "unicode code point 0x%x out of range in string", v);
1276           // Turn it into the "replacement character".
1277           v = 0xfffd;
1278         }
1279       if (v <= 0xffff)
1280         {
1281           buf[0] = 0xe0 + (v >> 12);
1282           buf[1] = 0x80 + ((v >> 6) & 0x3f);
1283           buf[2] = 0x80 + (v & 0x3f);
1284           len = 3;
1285         }
1286       else
1287         {
1288           buf[0] = 0xf0 + (v >> 18);
1289           buf[1] = 0x80 + ((v >> 12) & 0x3f);
1290           buf[2] = 0x80 + ((v >> 6) & 0x3f);
1291           buf[3] = 0x80 + (v & 0x3f);
1292           len = 4;
1293         }
1294     }
1295   str->append(buf, len);
1296 }
1297
1298 // Pick up a character literal.
1299
1300 Token
1301 Lex::gather_character()
1302 {
1303   ++this->lineoff_;
1304   const char* pstart = this->linebuf_ + this->lineoff_;
1305   const char* p = pstart;
1306
1307   unsigned int value;
1308   bool is_character;
1309   p = this->advance_one_char(p, true, &value, &is_character);
1310
1311   if (*p != '\'')
1312     {
1313       error_at(this->location(), "unterminated character constant");
1314       this->lineoff_ = p - this->linebuf_;
1315       return this->make_invalid_token();
1316     }
1317
1318   mpz_t val;
1319   mpz_init_set_ui(val, value);
1320
1321   Location location = this->location();
1322   this->lineoff_ = p + 1 - this->linebuf_;
1323   Token ret = Token::make_integer_token(val, location);
1324   mpz_clear(val);
1325   return ret;
1326 }
1327
1328 // Pick up a quoted string.
1329
1330 Token
1331 Lex::gather_string()
1332 {
1333   const char* pstart = this->linebuf_ + this->lineoff_ + 1;
1334   const char* p = pstart;
1335   const char* pend = this->linebuf_ + this->linesize_;
1336
1337   std::string value;
1338   while (*p != '"')
1339     {
1340       Location loc = this->location();
1341       unsigned int c;
1342       bool is_character;
1343       this->lineoff_ = p - this->linebuf_;
1344       p = this->advance_one_char(p, false, &c, &is_character);
1345       if (p >= pend)
1346         {
1347           error_at(this->location(), "unterminated string");
1348           --p;
1349           break;
1350         }
1351       Lex::append_char(c, is_character, &value, loc);
1352     }
1353
1354   Location location = this->location();
1355   this->lineoff_ = p + 1 - this->linebuf_;
1356   return Token::make_string_token(value, location);
1357 }
1358
1359 // Pick up a raw string.
1360
1361 Token
1362 Lex::gather_raw_string()
1363 {
1364   const char* p = this->linebuf_ + this->lineoff_ + 1;
1365   const char* pend = this->linebuf_ + this->linesize_;
1366   Location location = this->location();
1367
1368   std::string value;
1369   while (true)
1370     {
1371       while (p < pend)
1372         {
1373           if (*p == '`')
1374             {
1375               this->lineoff_ = p + 1 - this->linebuf_;
1376               return Token::make_string_token(value, location);
1377             }
1378           Location loc = this->location();
1379           unsigned int c;
1380           bool issued_error;
1381           this->lineoff_ = p - this->linebuf_;
1382           p = this->advance_one_utf8_char(p, &c, &issued_error);
1383           Lex::append_char(c, true, &value, loc);
1384         }
1385       this->lineoff_ = p - this->linebuf_;
1386       if (!this->require_line())
1387         {
1388           error_at(location, "unterminated raw string");
1389           return Token::make_string_token(value, location);
1390         }
1391       p = this->linebuf_ + this->lineoff_;
1392       pend = this->linebuf_ + this->linesize_;
1393     }
1394 }
1395
1396 // If C1 C2 C3 are a three character operator, return the code.
1397
1398 Operator
1399 Lex::three_character_operator(char c1, char c2, char c3)
1400 {
1401   if (c3 == '=')
1402     {
1403       if (c1 == '<' && c2 == '<')
1404         return OPERATOR_LSHIFTEQ;
1405       else if (c1 == '>' && c2 == '>')
1406         return OPERATOR_RSHIFTEQ;
1407       else if (c1 == '&' && c2 == '^')
1408         return OPERATOR_BITCLEAREQ;
1409     }
1410   return OPERATOR_INVALID;
1411 }
1412
1413 // If C1 C2 are a two character operator, return the code.
1414
1415 Operator
1416 Lex::two_character_operator(char c1, char c2)
1417 {
1418   switch (c1)
1419     {
1420     case '|':
1421       if (c2 == '|')
1422         return OPERATOR_OROR;
1423       else if (c2 == '=')
1424         return OPERATOR_OREQ;
1425       break;
1426     case '&':
1427       if (c2 == '&')
1428         return OPERATOR_ANDAND;
1429       else if (c2 == '^')
1430         return OPERATOR_BITCLEAR;
1431       else if (c2 == '=')
1432         return OPERATOR_ANDEQ;
1433       break;
1434     case '^':
1435       if (c2 == '=')
1436         return OPERATOR_XOREQ;
1437       break;
1438     case '=':
1439       if (c2 == '=')
1440         return OPERATOR_EQEQ;
1441       break;
1442     case '!':
1443       if (c2 == '=')
1444         return OPERATOR_NOTEQ;
1445       break;
1446     case '<':
1447       if (c2 == '=')
1448         return OPERATOR_LE;
1449       else if (c2 == '<')
1450         return OPERATOR_LSHIFT;
1451       else if (c2 == '-')
1452         return OPERATOR_CHANOP;
1453       break;
1454     case '>':
1455       if (c2 == '=')
1456         return OPERATOR_GE;
1457       else if (c2 == '>')
1458         return OPERATOR_RSHIFT;
1459       break;
1460     case '*':
1461       if (c2 == '=')
1462         return OPERATOR_MULTEQ;
1463       break;
1464     case '/':
1465       if (c2 == '=')
1466         return OPERATOR_DIVEQ;
1467       break;
1468     case '%':
1469       if (c2 == '=')
1470         return OPERATOR_MODEQ;
1471       break;
1472     case '+':
1473       if (c2 == '+')
1474         {
1475           this->add_semi_at_eol_ = true;
1476           return OPERATOR_PLUSPLUS;
1477         }
1478       else if (c2 == '=')
1479         return OPERATOR_PLUSEQ;
1480       break;
1481     case '-':
1482       if (c2 == '-')
1483         {
1484           this->add_semi_at_eol_ = true;
1485           return OPERATOR_MINUSMINUS;
1486         }
1487       else if (c2 == '=')
1488         return OPERATOR_MINUSEQ;
1489       break;
1490     case ':':
1491       if (c2 == '=')
1492         return OPERATOR_COLONEQ;
1493       break;
1494     default:
1495       break;
1496     }
1497   return OPERATOR_INVALID;
1498 }
1499
1500 // If character C is an operator, return the code.
1501
1502 Operator
1503 Lex::one_character_operator(char c)
1504 {
1505   switch (c)
1506     {
1507     case '<':
1508       return OPERATOR_LT;
1509     case '>':
1510       return OPERATOR_GT;
1511     case '+':
1512       return OPERATOR_PLUS;
1513     case '-':
1514       return OPERATOR_MINUS;
1515     case '|':
1516       return OPERATOR_OR;
1517     case '^':
1518       return OPERATOR_XOR;
1519     case '*':
1520       return OPERATOR_MULT;
1521     case '/':
1522       return OPERATOR_DIV;
1523     case '%':
1524       return OPERATOR_MOD;
1525     case '&':
1526       return OPERATOR_AND;
1527     case '!':
1528       return OPERATOR_NOT;
1529     case '=':
1530       return OPERATOR_EQ;
1531     case ':':
1532       return OPERATOR_COLON;
1533     case ';':
1534       return OPERATOR_SEMICOLON;
1535     case '.':
1536       return OPERATOR_DOT;
1537     case ',':
1538       return OPERATOR_COMMA;
1539     case '(':
1540       return OPERATOR_LPAREN;
1541     case ')':
1542       this->add_semi_at_eol_ = true;
1543       return OPERATOR_RPAREN;
1544     case '{':
1545       return OPERATOR_LCURLY;
1546     case '}':
1547       this->add_semi_at_eol_ = true;
1548       return OPERATOR_RCURLY;
1549     case '[':
1550       return OPERATOR_LSQUARE;
1551     case ']':
1552       this->add_semi_at_eol_ = true;
1553       return OPERATOR_RSQUARE;
1554     default:
1555       return OPERATOR_INVALID;
1556     }
1557 }
1558
1559 // Skip a C-style comment.
1560
1561 bool
1562 Lex::skip_c_comment()
1563 {
1564   while (true)
1565     {
1566       if (!this->require_line())
1567         {
1568           error_at(this->location(), "unterminated comment");
1569           return false;
1570         }
1571
1572       const char* p = this->linebuf_ + this->lineoff_;
1573       const char* pend = this->linebuf_ + this->linesize_;
1574
1575       while (p < pend)
1576         {
1577           if (p[0] == '*' && p + 1 < pend && p[1] == '/')
1578             {
1579               this->lineoff_ = p + 2 - this->linebuf_;
1580               return true;
1581             }
1582
1583           this->lineoff_ = p - this->linebuf_;
1584           unsigned int c;
1585           bool issued_error;
1586           p = this->advance_one_utf8_char(p, &c, &issued_error);
1587         }
1588
1589       this->lineoff_ = p - this->linebuf_;
1590     }
1591 }
1592
1593 // Skip a C++-style comment.
1594
1595 void
1596 Lex::skip_cpp_comment()
1597 {
1598   const char* p = this->linebuf_ + this->lineoff_;
1599   const char* pend = this->linebuf_ + this->linesize_;
1600
1601   // By convention, a C++ comment at the start of the line of the form
1602   //   //line FILE:LINENO
1603   // is interpreted as setting the file name and line number of the
1604   // next source line.
1605
1606   if (this->lineoff_ == 2
1607       && pend - p > 5
1608       && memcmp(p, "line ", 5) == 0)
1609     {
1610       p += 5;
1611       while (p < pend && *p == ' ')
1612         ++p;
1613       const char* pcolon = static_cast<const char*>(memchr(p, ':', pend - p));
1614       if (pcolon != NULL
1615           && pcolon[1] >= '0'
1616           && pcolon[1] <= '9')
1617         {
1618           char* plend;
1619           long lineno = strtol(pcolon + 1, &plend, 10);
1620           if (plend > pcolon + 1
1621               && (plend == pend
1622                   || *plend < '0'
1623                   || *plend > '9')
1624               && lineno > 0
1625               && lineno < 0x7fffffff)
1626             {
1627               unsigned int filelen = pcolon - p;
1628               char* file = new char[filelen + 1];
1629               memcpy(file, p, filelen);
1630               file[filelen] = '\0';
1631
1632               this->linemap_->start_file(file, lineno);
1633               this->lineno_ = lineno - 1;
1634
1635               p = plend;
1636             }
1637         }
1638     }
1639
1640   while (p < pend)
1641     {
1642       this->lineoff_ = p - this->linebuf_;
1643       unsigned int c;
1644       bool issued_error;
1645       p = this->advance_one_utf8_char(p, &c, &issued_error);
1646     }
1647 }
1648
1649 // The Unicode tables use this struct.
1650
1651 struct Unicode_range
1652 {
1653   // The low end of the range.
1654   unsigned int low;
1655   // The high end of the range.
1656   unsigned int high;
1657   // The stride.  This entries represents low, low + stride, low + 2 *
1658   // stride, etc., up to high.
1659   unsigned int stride;
1660 };
1661
1662 // A table of Unicode digits--Unicode code points classified as
1663 // "Digit".
1664
1665 static const Unicode_range unicode_digits[] =
1666 {
1667   { 0x0030, 0x0039, 1},
1668   { 0x0660, 0x0669, 1},
1669   { 0x06f0, 0x06f9, 1},
1670   { 0x07c0, 0x07c9, 1},
1671   { 0x0966, 0x096f, 1},
1672   { 0x09e6, 0x09ef, 1},
1673   { 0x0a66, 0x0a6f, 1},
1674   { 0x0ae6, 0x0aef, 1},
1675   { 0x0b66, 0x0b6f, 1},
1676   { 0x0be6, 0x0bef, 1},
1677   { 0x0c66, 0x0c6f, 1},
1678   { 0x0ce6, 0x0cef, 1},
1679   { 0x0d66, 0x0d6f, 1},
1680   { 0x0e50, 0x0e59, 1},
1681   { 0x0ed0, 0x0ed9, 1},
1682   { 0x0f20, 0x0f29, 1},
1683   { 0x1040, 0x1049, 1},
1684   { 0x17e0, 0x17e9, 1},
1685   { 0x1810, 0x1819, 1},
1686   { 0x1946, 0x194f, 1},
1687   { 0x19d0, 0x19d9, 1},
1688   { 0x1b50, 0x1b59, 1},
1689   { 0xff10, 0xff19, 1},
1690   { 0x104a0, 0x104a9, 1},
1691   { 0x1d7ce, 0x1d7ff, 1},
1692 };
1693
1694 // A table of Unicode letters--Unicode code points classified as
1695 // "Letter".
1696
1697 static const Unicode_range unicode_letters[] =
1698 {
1699   { 0x0041, 0x005a, 1},
1700   { 0x0061, 0x007a, 1},
1701   { 0x00aa, 0x00b5, 11},
1702   { 0x00ba, 0x00ba, 1},
1703   { 0x00c0, 0x00d6, 1},
1704   { 0x00d8, 0x00f6, 1},
1705   { 0x00f8, 0x02c1, 1},
1706   { 0x02c6, 0x02d1, 1},
1707   { 0x02e0, 0x02e4, 1},
1708   { 0x02ec, 0x02ee, 2},
1709   { 0x0370, 0x0374, 1},
1710   { 0x0376, 0x0377, 1},
1711   { 0x037a, 0x037d, 1},
1712   { 0x0386, 0x0386, 1},
1713   { 0x0388, 0x038a, 1},
1714   { 0x038c, 0x038c, 1},
1715   { 0x038e, 0x03a1, 1},
1716   { 0x03a3, 0x03f5, 1},
1717   { 0x03f7, 0x0481, 1},
1718   { 0x048a, 0x0523, 1},
1719   { 0x0531, 0x0556, 1},
1720   { 0x0559, 0x0559, 1},
1721   { 0x0561, 0x0587, 1},
1722   { 0x05d0, 0x05ea, 1},
1723   { 0x05f0, 0x05f2, 1},
1724   { 0x0621, 0x064a, 1},
1725   { 0x066e, 0x066f, 1},
1726   { 0x0671, 0x06d3, 1},
1727   { 0x06d5, 0x06d5, 1},
1728   { 0x06e5, 0x06e6, 1},
1729   { 0x06ee, 0x06ef, 1},
1730   { 0x06fa, 0x06fc, 1},
1731   { 0x06ff, 0x0710, 17},
1732   { 0x0712, 0x072f, 1},
1733   { 0x074d, 0x07a5, 1},
1734   { 0x07b1, 0x07b1, 1},
1735   { 0x07ca, 0x07ea, 1},
1736   { 0x07f4, 0x07f5, 1},
1737   { 0x07fa, 0x07fa, 1},
1738   { 0x0904, 0x0939, 1},
1739   { 0x093d, 0x0950, 19},
1740   { 0x0958, 0x0961, 1},
1741   { 0x0971, 0x0972, 1},
1742   { 0x097b, 0x097f, 1},
1743   { 0x0985, 0x098c, 1},
1744   { 0x098f, 0x0990, 1},
1745   { 0x0993, 0x09a8, 1},
1746   { 0x09aa, 0x09b0, 1},
1747   { 0x09b2, 0x09b2, 1},
1748   { 0x09b6, 0x09b9, 1},
1749   { 0x09bd, 0x09ce, 17},
1750   { 0x09dc, 0x09dd, 1},
1751   { 0x09df, 0x09e1, 1},
1752   { 0x09f0, 0x09f1, 1},
1753   { 0x0a05, 0x0a0a, 1},
1754   { 0x0a0f, 0x0a10, 1},
1755   { 0x0a13, 0x0a28, 1},
1756   { 0x0a2a, 0x0a30, 1},
1757   { 0x0a32, 0x0a33, 1},
1758   { 0x0a35, 0x0a36, 1},
1759   { 0x0a38, 0x0a39, 1},
1760   { 0x0a59, 0x0a5c, 1},
1761   { 0x0a5e, 0x0a5e, 1},
1762   { 0x0a72, 0x0a74, 1},
1763   { 0x0a85, 0x0a8d, 1},
1764   { 0x0a8f, 0x0a91, 1},
1765   { 0x0a93, 0x0aa8, 1},
1766   { 0x0aaa, 0x0ab0, 1},
1767   { 0x0ab2, 0x0ab3, 1},
1768   { 0x0ab5, 0x0ab9, 1},
1769   { 0x0abd, 0x0ad0, 19},
1770   { 0x0ae0, 0x0ae1, 1},
1771   { 0x0b05, 0x0b0c, 1},
1772   { 0x0b0f, 0x0b10, 1},
1773   { 0x0b13, 0x0b28, 1},
1774   { 0x0b2a, 0x0b30, 1},
1775   { 0x0b32, 0x0b33, 1},
1776   { 0x0b35, 0x0b39, 1},
1777   { 0x0b3d, 0x0b3d, 1},
1778   { 0x0b5c, 0x0b5d, 1},
1779   { 0x0b5f, 0x0b61, 1},
1780   { 0x0b71, 0x0b83, 18},
1781   { 0x0b85, 0x0b8a, 1},
1782   { 0x0b8e, 0x0b90, 1},
1783   { 0x0b92, 0x0b95, 1},
1784   { 0x0b99, 0x0b9a, 1},
1785   { 0x0b9c, 0x0b9c, 1},
1786   { 0x0b9e, 0x0b9f, 1},
1787   { 0x0ba3, 0x0ba4, 1},
1788   { 0x0ba8, 0x0baa, 1},
1789   { 0x0bae, 0x0bb9, 1},
1790   { 0x0bd0, 0x0bd0, 1},
1791   { 0x0c05, 0x0c0c, 1},
1792   { 0x0c0e, 0x0c10, 1},
1793   { 0x0c12, 0x0c28, 1},
1794   { 0x0c2a, 0x0c33, 1},
1795   { 0x0c35, 0x0c39, 1},
1796   { 0x0c3d, 0x0c3d, 1},
1797   { 0x0c58, 0x0c59, 1},
1798   { 0x0c60, 0x0c61, 1},
1799   { 0x0c85, 0x0c8c, 1},
1800   { 0x0c8e, 0x0c90, 1},
1801   { 0x0c92, 0x0ca8, 1},
1802   { 0x0caa, 0x0cb3, 1},
1803   { 0x0cb5, 0x0cb9, 1},
1804   { 0x0cbd, 0x0cde, 33},
1805   { 0x0ce0, 0x0ce1, 1},
1806   { 0x0d05, 0x0d0c, 1},
1807   { 0x0d0e, 0x0d10, 1},
1808   { 0x0d12, 0x0d28, 1},
1809   { 0x0d2a, 0x0d39, 1},
1810   { 0x0d3d, 0x0d3d, 1},
1811   { 0x0d60, 0x0d61, 1},
1812   { 0x0d7a, 0x0d7f, 1},
1813   { 0x0d85, 0x0d96, 1},
1814   { 0x0d9a, 0x0db1, 1},
1815   { 0x0db3, 0x0dbb, 1},
1816   { 0x0dbd, 0x0dbd, 1},
1817   { 0x0dc0, 0x0dc6, 1},
1818   { 0x0e01, 0x0e30, 1},
1819   { 0x0e32, 0x0e33, 1},
1820   { 0x0e40, 0x0e46, 1},
1821   { 0x0e81, 0x0e82, 1},
1822   { 0x0e84, 0x0e84, 1},
1823   { 0x0e87, 0x0e88, 1},
1824   { 0x0e8a, 0x0e8d, 3},
1825   { 0x0e94, 0x0e97, 1},
1826   { 0x0e99, 0x0e9f, 1},
1827   { 0x0ea1, 0x0ea3, 1},
1828   { 0x0ea5, 0x0ea7, 2},
1829   { 0x0eaa, 0x0eab, 1},
1830   { 0x0ead, 0x0eb0, 1},
1831   { 0x0eb2, 0x0eb3, 1},
1832   { 0x0ebd, 0x0ebd, 1},
1833   { 0x0ec0, 0x0ec4, 1},
1834   { 0x0ec6, 0x0ec6, 1},
1835   { 0x0edc, 0x0edd, 1},
1836   { 0x0f00, 0x0f00, 1},
1837   { 0x0f40, 0x0f47, 1},
1838   { 0x0f49, 0x0f6c, 1},
1839   { 0x0f88, 0x0f8b, 1},
1840   { 0x1000, 0x102a, 1},
1841   { 0x103f, 0x103f, 1},
1842   { 0x1050, 0x1055, 1},
1843   { 0x105a, 0x105d, 1},
1844   { 0x1061, 0x1061, 1},
1845   { 0x1065, 0x1066, 1},
1846   { 0x106e, 0x1070, 1},
1847   { 0x1075, 0x1081, 1},
1848   { 0x108e, 0x108e, 1},
1849   { 0x10a0, 0x10c5, 1},
1850   { 0x10d0, 0x10fa, 1},
1851   { 0x10fc, 0x10fc, 1},
1852   { 0x1100, 0x1159, 1},
1853   { 0x115f, 0x11a2, 1},
1854   { 0x11a8, 0x11f9, 1},
1855   { 0x1200, 0x1248, 1},
1856   { 0x124a, 0x124d, 1},
1857   { 0x1250, 0x1256, 1},
1858   { 0x1258, 0x1258, 1},
1859   { 0x125a, 0x125d, 1},
1860   { 0x1260, 0x1288, 1},
1861   { 0x128a, 0x128d, 1},
1862   { 0x1290, 0x12b0, 1},
1863   { 0x12b2, 0x12b5, 1},
1864   { 0x12b8, 0x12be, 1},
1865   { 0x12c0, 0x12c0, 1},
1866   { 0x12c2, 0x12c5, 1},
1867   { 0x12c8, 0x12d6, 1},
1868   { 0x12d8, 0x1310, 1},
1869   { 0x1312, 0x1315, 1},
1870   { 0x1318, 0x135a, 1},
1871   { 0x1380, 0x138f, 1},
1872   { 0x13a0, 0x13f4, 1},
1873   { 0x1401, 0x166c, 1},
1874   { 0x166f, 0x1676, 1},
1875   { 0x1681, 0x169a, 1},
1876   { 0x16a0, 0x16ea, 1},
1877   { 0x1700, 0x170c, 1},
1878   { 0x170e, 0x1711, 1},
1879   { 0x1720, 0x1731, 1},
1880   { 0x1740, 0x1751, 1},
1881   { 0x1760, 0x176c, 1},
1882   { 0x176e, 0x1770, 1},
1883   { 0x1780, 0x17b3, 1},
1884   { 0x17d7, 0x17dc, 5},
1885   { 0x1820, 0x1877, 1},
1886   { 0x1880, 0x18a8, 1},
1887   { 0x18aa, 0x18aa, 1},
1888   { 0x1900, 0x191c, 1},
1889   { 0x1950, 0x196d, 1},
1890   { 0x1970, 0x1974, 1},
1891   { 0x1980, 0x19a9, 1},
1892   { 0x19c1, 0x19c7, 1},
1893   { 0x1a00, 0x1a16, 1},
1894   { 0x1b05, 0x1b33, 1},
1895   { 0x1b45, 0x1b4b, 1},
1896   { 0x1b83, 0x1ba0, 1},
1897   { 0x1bae, 0x1baf, 1},
1898   { 0x1c00, 0x1c23, 1},
1899   { 0x1c4d, 0x1c4f, 1},
1900   { 0x1c5a, 0x1c7d, 1},
1901   { 0x1d00, 0x1dbf, 1},
1902   { 0x1e00, 0x1f15, 1},
1903   { 0x1f18, 0x1f1d, 1},
1904   { 0x1f20, 0x1f45, 1},
1905   { 0x1f48, 0x1f4d, 1},
1906   { 0x1f50, 0x1f57, 1},
1907   { 0x1f59, 0x1f5d, 2},
1908   { 0x1f5f, 0x1f7d, 1},
1909   { 0x1f80, 0x1fb4, 1},
1910   { 0x1fb6, 0x1fbc, 1},
1911   { 0x1fbe, 0x1fbe, 1},
1912   { 0x1fc2, 0x1fc4, 1},
1913   { 0x1fc6, 0x1fcc, 1},
1914   { 0x1fd0, 0x1fd3, 1},
1915   { 0x1fd6, 0x1fdb, 1},
1916   { 0x1fe0, 0x1fec, 1},
1917   { 0x1ff2, 0x1ff4, 1},
1918   { 0x1ff6, 0x1ffc, 1},
1919   { 0x2071, 0x207f, 14},
1920   { 0x2090, 0x2094, 1},
1921   { 0x2102, 0x2107, 5},
1922   { 0x210a, 0x2113, 1},
1923   { 0x2115, 0x2115, 1},
1924   { 0x2119, 0x211d, 1},
1925   { 0x2124, 0x2128, 2},
1926   { 0x212a, 0x212d, 1},
1927   { 0x212f, 0x2139, 1},
1928   { 0x213c, 0x213f, 1},
1929   { 0x2145, 0x2149, 1},
1930   { 0x214e, 0x214e, 1},
1931   { 0x2183, 0x2184, 1},
1932   { 0x2c00, 0x2c2e, 1},
1933   { 0x2c30, 0x2c5e, 1},
1934   { 0x2c60, 0x2c6f, 1},
1935   { 0x2c71, 0x2c7d, 1},
1936   { 0x2c80, 0x2ce4, 1},
1937   { 0x2d00, 0x2d25, 1},
1938   { 0x2d30, 0x2d65, 1},
1939   { 0x2d6f, 0x2d6f, 1},
1940   { 0x2d80, 0x2d96, 1},
1941   { 0x2da0, 0x2da6, 1},
1942   { 0x2da8, 0x2dae, 1},
1943   { 0x2db0, 0x2db6, 1},
1944   { 0x2db8, 0x2dbe, 1},
1945   { 0x2dc0, 0x2dc6, 1},
1946   { 0x2dc8, 0x2dce, 1},
1947   { 0x2dd0, 0x2dd6, 1},
1948   { 0x2dd8, 0x2dde, 1},
1949   { 0x2e2f, 0x2e2f, 1},
1950   { 0x3005, 0x3006, 1},
1951   { 0x3031, 0x3035, 1},
1952   { 0x303b, 0x303c, 1},
1953   { 0x3041, 0x3096, 1},
1954   { 0x309d, 0x309f, 1},
1955   { 0x30a1, 0x30fa, 1},
1956   { 0x30fc, 0x30ff, 1},
1957   { 0x3105, 0x312d, 1},
1958   { 0x3131, 0x318e, 1},
1959   { 0x31a0, 0x31b7, 1},
1960   { 0x31f0, 0x31ff, 1},
1961   { 0x3400, 0x4db5, 1},
1962   { 0x4e00, 0x9fc3, 1},
1963   { 0xa000, 0xa48c, 1},
1964   { 0xa500, 0xa60c, 1},
1965   { 0xa610, 0xa61f, 1},
1966   { 0xa62a, 0xa62b, 1},
1967   { 0xa640, 0xa65f, 1},
1968   { 0xa662, 0xa66e, 1},
1969   { 0xa67f, 0xa697, 1},
1970   { 0xa717, 0xa71f, 1},
1971   { 0xa722, 0xa788, 1},
1972   { 0xa78b, 0xa78c, 1},
1973   { 0xa7fb, 0xa801, 1},
1974   { 0xa803, 0xa805, 1},
1975   { 0xa807, 0xa80a, 1},
1976   { 0xa80c, 0xa822, 1},
1977   { 0xa840, 0xa873, 1},
1978   { 0xa882, 0xa8b3, 1},
1979   { 0xa90a, 0xa925, 1},
1980   { 0xa930, 0xa946, 1},
1981   { 0xaa00, 0xaa28, 1},
1982   { 0xaa40, 0xaa42, 1},
1983   { 0xaa44, 0xaa4b, 1},
1984   { 0xac00, 0xd7a3, 1},
1985   { 0xf900, 0xfa2d, 1},
1986   { 0xfa30, 0xfa6a, 1},
1987   { 0xfa70, 0xfad9, 1},
1988   { 0xfb00, 0xfb06, 1},
1989   { 0xfb13, 0xfb17, 1},
1990   { 0xfb1d, 0xfb1d, 1},
1991   { 0xfb1f, 0xfb28, 1},
1992   { 0xfb2a, 0xfb36, 1},
1993   { 0xfb38, 0xfb3c, 1},
1994   { 0xfb3e, 0xfb3e, 1},
1995   { 0xfb40, 0xfb41, 1},
1996   { 0xfb43, 0xfb44, 1},
1997   { 0xfb46, 0xfbb1, 1},
1998   { 0xfbd3, 0xfd3d, 1},
1999   { 0xfd50, 0xfd8f, 1},
2000   { 0xfd92, 0xfdc7, 1},
2001   { 0xfdf0, 0xfdfb, 1},
2002   { 0xfe70, 0xfe74, 1},
2003   { 0xfe76, 0xfefc, 1},
2004   { 0xff21, 0xff3a, 1},
2005   { 0xff41, 0xff5a, 1},
2006   { 0xff66, 0xffbe, 1},
2007   { 0xffc2, 0xffc7, 1},
2008   { 0xffca, 0xffcf, 1},
2009   { 0xffd2, 0xffd7, 1},
2010   { 0xffda, 0xffdc, 1},
2011   { 0x10000, 0x1000b, 1},
2012   { 0x1000d, 0x10026, 1},
2013   { 0x10028, 0x1003a, 1},
2014   { 0x1003c, 0x1003d, 1},
2015   { 0x1003f, 0x1004d, 1},
2016   { 0x10050, 0x1005d, 1},
2017   { 0x10080, 0x100fa, 1},
2018   { 0x10280, 0x1029c, 1},
2019   { 0x102a0, 0x102d0, 1},
2020   { 0x10300, 0x1031e, 1},
2021   { 0x10330, 0x10340, 1},
2022   { 0x10342, 0x10349, 1},
2023   { 0x10380, 0x1039d, 1},
2024   { 0x103a0, 0x103c3, 1},
2025   { 0x103c8, 0x103cf, 1},
2026   { 0x10400, 0x1049d, 1},
2027   { 0x10800, 0x10805, 1},
2028   { 0x10808, 0x10808, 1},
2029   { 0x1080a, 0x10835, 1},
2030   { 0x10837, 0x10838, 1},
2031   { 0x1083c, 0x1083f, 3},
2032   { 0x10900, 0x10915, 1},
2033   { 0x10920, 0x10939, 1},
2034   { 0x10a00, 0x10a00, 1},
2035   { 0x10a10, 0x10a13, 1},
2036   { 0x10a15, 0x10a17, 1},
2037   { 0x10a19, 0x10a33, 1},
2038   { 0x12000, 0x1236e, 1},
2039   { 0x1d400, 0x1d454, 1},
2040   { 0x1d456, 0x1d49c, 1},
2041   { 0x1d49e, 0x1d49f, 1},
2042   { 0x1d4a2, 0x1d4a2, 1},
2043   { 0x1d4a5, 0x1d4a6, 1},
2044   { 0x1d4a9, 0x1d4ac, 1},
2045   { 0x1d4ae, 0x1d4b9, 1},
2046   { 0x1d4bb, 0x1d4bb, 1},
2047   { 0x1d4bd, 0x1d4c3, 1},
2048   { 0x1d4c5, 0x1d505, 1},
2049   { 0x1d507, 0x1d50a, 1},
2050   { 0x1d50d, 0x1d514, 1},
2051   { 0x1d516, 0x1d51c, 1},
2052   { 0x1d51e, 0x1d539, 1},
2053   { 0x1d53b, 0x1d53e, 1},
2054   { 0x1d540, 0x1d544, 1},
2055   { 0x1d546, 0x1d546, 1},
2056   { 0x1d54a, 0x1d550, 1},
2057   { 0x1d552, 0x1d6a5, 1},
2058   { 0x1d6a8, 0x1d6c0, 1},
2059   { 0x1d6c2, 0x1d6da, 1},
2060   { 0x1d6dc, 0x1d6fa, 1},
2061   { 0x1d6fc, 0x1d714, 1},
2062   { 0x1d716, 0x1d734, 1},
2063   { 0x1d736, 0x1d74e, 1},
2064   { 0x1d750, 0x1d76e, 1},
2065   { 0x1d770, 0x1d788, 1},
2066   { 0x1d78a, 0x1d7a8, 1},
2067   { 0x1d7aa, 0x1d7c2, 1},
2068   { 0x1d7c4, 0x1d7cb, 1},
2069   { 0x20000, 0x2a6d6, 1},
2070   { 0x2f800, 0x2fa1d, 1},
2071 };
2072
2073 // A table of Unicode uppercase letters--Unicode code points
2074 // classified as "Letter, uppercase".
2075
2076 static const Unicode_range unicode_uppercase_letters[] =
2077 {
2078   { 0x0041, 0x005a, 1},
2079   { 0x00c0, 0x00d6, 1},
2080   { 0x00d8, 0x00de, 1},
2081   { 0x0100, 0x0136, 2},
2082   { 0x0139, 0x0147, 2},
2083   { 0x014a, 0x0176, 2},
2084   { 0x0178, 0x0179, 1},
2085   { 0x017b, 0x017d, 2},
2086   { 0x0181, 0x0182, 1},
2087   { 0x0184, 0x0184, 1},
2088   { 0x0186, 0x0187, 1},
2089   { 0x0189, 0x018b, 1},
2090   { 0x018e, 0x0191, 1},
2091   { 0x0193, 0x0194, 1},
2092   { 0x0196, 0x0198, 1},
2093   { 0x019c, 0x019d, 1},
2094   { 0x019f, 0x01a0, 1},
2095   { 0x01a2, 0x01a4, 2},
2096   { 0x01a6, 0x01a7, 1},
2097   { 0x01a9, 0x01ac, 3},
2098   { 0x01ae, 0x01af, 1},
2099   { 0x01b1, 0x01b3, 1},
2100   { 0x01b5, 0x01b5, 1},
2101   { 0x01b7, 0x01b8, 1},
2102   { 0x01bc, 0x01c4, 8},
2103   { 0x01c7, 0x01cd, 3},
2104   { 0x01cf, 0x01db, 2},
2105   { 0x01de, 0x01ee, 2},
2106   { 0x01f1, 0x01f4, 3},
2107   { 0x01f6, 0x01f8, 1},
2108   { 0x01fa, 0x0232, 2},
2109   { 0x023a, 0x023b, 1},
2110   { 0x023d, 0x023e, 1},
2111   { 0x0241, 0x0241, 1},
2112   { 0x0243, 0x0246, 1},
2113   { 0x0248, 0x024e, 2},
2114   { 0x0370, 0x0372, 2},
2115   { 0x0376, 0x0386, 16},
2116   { 0x0388, 0x038a, 1},
2117   { 0x038c, 0x038c, 1},
2118   { 0x038e, 0x038f, 1},
2119   { 0x0391, 0x03a1, 1},
2120   { 0x03a3, 0x03ab, 1},
2121   { 0x03cf, 0x03cf, 1},
2122   { 0x03d2, 0x03d4, 1},
2123   { 0x03d8, 0x03ee, 2},
2124   { 0x03f4, 0x03f7, 3},
2125   { 0x03f9, 0x03fa, 1},
2126   { 0x03fd, 0x042f, 1},
2127   { 0x0460, 0x0480, 2},
2128   { 0x048a, 0x04be, 2},
2129   { 0x04c0, 0x04c1, 1},
2130   { 0x04c3, 0x04cd, 2},
2131   { 0x04d0, 0x0522, 2},
2132   { 0x0531, 0x0556, 1},
2133   { 0x10a0, 0x10c5, 1},
2134   { 0x1e00, 0x1e94, 2},
2135   { 0x1e9e, 0x1efe, 2},
2136   { 0x1f08, 0x1f0f, 1},
2137   { 0x1f18, 0x1f1d, 1},
2138   { 0x1f28, 0x1f2f, 1},
2139   { 0x1f38, 0x1f3f, 1},
2140   { 0x1f48, 0x1f4d, 1},
2141   { 0x1f59, 0x1f5f, 2},
2142   { 0x1f68, 0x1f6f, 1},
2143   { 0x1fb8, 0x1fbb, 1},
2144   { 0x1fc8, 0x1fcb, 1},
2145   { 0x1fd8, 0x1fdb, 1},
2146   { 0x1fe8, 0x1fec, 1},
2147   { 0x1ff8, 0x1ffb, 1},
2148   { 0x2102, 0x2107, 5},
2149   { 0x210b, 0x210d, 1},
2150   { 0x2110, 0x2112, 1},
2151   { 0x2115, 0x2115, 1},
2152   { 0x2119, 0x211d, 1},
2153   { 0x2124, 0x2128, 2},
2154   { 0x212a, 0x212d, 1},
2155   { 0x2130, 0x2133, 1},
2156   { 0x213e, 0x213f, 1},
2157   { 0x2145, 0x2183, 62},
2158   { 0x2c00, 0x2c2e, 1},
2159   { 0x2c60, 0x2c60, 1},
2160   { 0x2c62, 0x2c64, 1},
2161   { 0x2c67, 0x2c6b, 2},
2162   { 0x2c6d, 0x2c6f, 1},
2163   { 0x2c72, 0x2c75, 3},
2164   { 0x2c80, 0x2ce2, 2},
2165   { 0xa640, 0xa65e, 2},
2166   { 0xa662, 0xa66c, 2},
2167   { 0xa680, 0xa696, 2},
2168   { 0xa722, 0xa72e, 2},
2169   { 0xa732, 0xa76e, 2},
2170   { 0xa779, 0xa77b, 2},
2171   { 0xa77d, 0xa77e, 1},
2172   { 0xa780, 0xa786, 2},
2173   { 0xa78b, 0xa78b, 1},
2174   { 0xff21, 0xff3a, 1},
2175   { 0x10400, 0x10427, 1},
2176   { 0x1d400, 0x1d419, 1},
2177   { 0x1d434, 0x1d44d, 1},
2178   { 0x1d468, 0x1d481, 1},
2179   { 0x1d49c, 0x1d49c, 1},
2180   { 0x1d49e, 0x1d49f, 1},
2181   { 0x1d4a2, 0x1d4a2, 1},
2182   { 0x1d4a5, 0x1d4a6, 1},
2183   { 0x1d4a9, 0x1d4ac, 1},
2184   { 0x1d4ae, 0x1d4b5, 1},
2185   { 0x1d4d0, 0x1d4e9, 1},
2186   { 0x1d504, 0x1d505, 1},
2187   { 0x1d507, 0x1d50a, 1},
2188   { 0x1d50d, 0x1d514, 1},
2189   { 0x1d516, 0x1d51c, 1},
2190   { 0x1d538, 0x1d539, 1},
2191   { 0x1d53b, 0x1d53e, 1},
2192   { 0x1d540, 0x1d544, 1},
2193   { 0x1d546, 0x1d546, 1},
2194   { 0x1d54a, 0x1d550, 1},
2195   { 0x1d56c, 0x1d585, 1},
2196   { 0x1d5a0, 0x1d5b9, 1},
2197   { 0x1d5d4, 0x1d5ed, 1},
2198   { 0x1d608, 0x1d621, 1},
2199   { 0x1d63c, 0x1d655, 1},
2200   { 0x1d670, 0x1d689, 1},
2201   { 0x1d6a8, 0x1d6c0, 1},
2202   { 0x1d6e2, 0x1d6fa, 1},
2203   { 0x1d71c, 0x1d734, 1},
2204   { 0x1d756, 0x1d76e, 1},
2205   { 0x1d790, 0x1d7a8, 1},
2206   { 0x1d7ca, 0x1d7ca, 1},
2207 };
2208
2209 // Return true if C is in RANGES.
2210
2211 bool
2212 Lex::is_in_unicode_range(unsigned int c, const Unicode_range* ranges,
2213                          size_t range_size)
2214 {
2215   if (c < 0x100)
2216     {
2217       // The common case is a small value, and we know that it will be
2218       // in the first few entries of the table.  Do a linear scan
2219       // rather than a binary search.
2220       for (size_t i = 0; i < range_size; ++i)
2221         {
2222           const Unicode_range* p = &ranges[i];
2223           if (c <= p->high)
2224             {
2225               if (c < p->low)
2226                 return false;
2227               return (c - p->low) % p->stride == 0;
2228             }
2229         }
2230       return false;
2231     }
2232   else
2233     {
2234       size_t lo = 0;
2235       size_t hi = range_size;
2236       while (lo < hi)
2237         {
2238           size_t mid = lo + (hi - lo) / 2;
2239           const Unicode_range* p = &ranges[mid];
2240           if (c < p->low)
2241             hi = mid;
2242           else if (c > p->high)
2243             lo = mid + 1;
2244           else
2245             return (c - p->low) % p->stride == 0;
2246         }
2247       return false;
2248     }
2249 }
2250
2251 // Return whether C is a Unicode digit--a Unicode code point
2252 // classified as "Digit".
2253
2254 bool
2255 Lex::is_unicode_digit(unsigned int c)
2256 {
2257   return Lex::is_in_unicode_range(c, unicode_digits,
2258                                   ARRAY_SIZE(unicode_digits));
2259 }
2260
2261 // Return whether C is a Unicode letter--a Unicode code point
2262 // classified as "Letter".
2263
2264 bool
2265 Lex::is_unicode_letter(unsigned int c)
2266 {
2267   return Lex::is_in_unicode_range(c, unicode_letters,
2268                                   ARRAY_SIZE(unicode_letters));
2269 }
2270
2271 // Return whether C is a Unicode uppercase letter.  a Unicode code
2272 // point classified as "Letter, uppercase".
2273
2274 bool
2275 Lex::is_unicode_uppercase(unsigned int c)
2276 {
2277   return Lex::is_in_unicode_range(c, unicode_uppercase_letters,
2278                                   ARRAY_SIZE(unicode_uppercase_letters));
2279 }
2280
2281 // Return whether the identifier NAME should be exported.  NAME is a
2282 // mangled name which includes only ASCII characters.
2283
2284 bool
2285 Lex::is_exported_name(const std::string& name)
2286 {
2287   unsigned char c = name[0];
2288   if (c != '$')
2289     return c >= 'A' && c <= 'Z';
2290   else
2291     {
2292       const char* p = name.data();
2293       size_t len = name.length();
2294       if (len < 2 || p[1] != 'U')
2295         return false;
2296       unsigned int ci = 0;
2297       for (size_t i = 2; i < len && p[i] != '$'; ++i)
2298         {
2299           c = p[i];
2300           if (!hex_p(c))
2301             return false;
2302           ci <<= 4;
2303           ci |= hex_value(c);
2304         }
2305       return Lex::is_unicode_uppercase(ci);
2306     }
2307 }