OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [gcc/] [go/] [gofrontend/] [lex.cc] - Blame information for rev 720

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.