aboutsummaryrefslogtreecommitdiff
path: root/src/include/open-axiom/token
blob: 1d17f5dec67f222cad6bfd334f315fa224feac2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
// -*- C++ -*-
// Copyright (C) 2013-2017, Gabriel Dos Reis.
// All rights reserved.
// Written by Gabriel Dos Reis.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     - Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//
//     - Redistributions in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in
//       the documentation and/or other materials provided with the
//       distribution.
//
//     - Neither the name of OpenAxiom. nor the names of its contributors
//       may be used to endorse or promote products derived from this
//       software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
// OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#ifndef OPENAXIOM_TOKEN_included
#define OPENAXIOM_TOKEN_included

#include <stdint.h>
#include <stack>
#include <iosfwd>
#include <open-axiom/dialect>
#include <open-axiom/InputFragment>

namespace OpenAxiom {
   // Categorization of Boot and Spad tokens.
   enum class TokenCategory : uint8_t {
      Unclassified,             // token of unknown class
      Whitespace,               // sequence of white-space characters
      Comment,                  // a description of an ignorable comment
      Punctuator,               // a punctuator character
      Operator,                 // an operator both symbolic and alphabetic
      Integer,                  // an integer literal
      FloatingPoint,            // a floating-point literal
      String,                   // a string literal
      Keyword,                  // a reserved word both symbolic and alphabetic
      Identifier,               // an identifier
      Formatting,               // a layout formatting token
      Junk,                     // invalid/malformed token
      EOS                       // end-of-token-stream indicator
   };

   std::ostream& operator<<(std::ostream&, TokenCategory);

   // The abstract value associated with a token.
   enum class TokenValue : uint8_t {
#undef OPENAXIOM_DEFINE_TOKEN
#define OPENAXIOM_DEFINE_TOKEN(T, ...)  T,
#include <open-axiom/token-value>
#undef OPENAXIOM_DEFINE_TOKEN

      EndOfStream               // end of token stream
   };

   std::ostream& operator<<(std::ostream&, TokenValue);


   enum class TokenIndex : uint32_t { };

   constexpr TokenValue value(TokenIndex t) {
      return TokenValue((uint32_t(t) & 0xFF000000) >> 24);
   }

   constexpr uint32_t index(TokenIndex t) {
      return uint32_t(t) & 0x00FFFFFF;
   }

   // Program text region, with a fragment.
   struct Region {
      FragmentCursor start;
      FragmentCursor end;
   };

   // Given a symbolic or alphabetic token, retrieve its category
   // and associated abstract value.
   struct TokenClassification {
      TokenCategory category;
      TokenValue value;

      explicit operator bool() const {
         return category != TokenCategory::EOS;
      }
   };

   TokenClassification classify(const std::string&);

   // Token data structure: a region of text with a classification.
   struct Token : Region, TokenClassification { };

   // -- Exception types
   struct EndOfStringUnseen {
      LineNumber line;
      ColumnIndex column;
   };

   struct MissingExponent {
      LineNumber line;
      ColumnIndex column;
   };

   // Object of this datatype decompose a program fragment into a
   // token stream.  The tokens are of type indicated by Tok.
   template<typename Tok>
   struct Tokenizer {
      Tokenizer(Fragment& f)
            : frag(f),
              pos{ 0, frag.front().indent }
      {
         indents.push(pos);
      }

      bool eos() const {
         return pos.line >= frag.size();
      }

      Tok get(Language = Language::Spad);
   private:
      Fragment& frag;
      FragmentCursor pos;
      std::stack<FragmentCursor> indents;

      std::size_t line_length() const { return frag(pos).size(); }

      bool line_continuation() const {
         return pos.column == line_length() - 1 and frag(pos).back() == '_';
      }

      Tok finish(Tok&, Language);
   };

   bool separator_or_punctuator(uint8_t);

   template<typename T>
   inline void comment_token(T& t, TokenValue v) {
      t.category = TokenCategory::Comment;
      t.value = v;
   }

   template<typename T>
   inline T& formatting_token(T& t, TokenValue v) {
      t.category = TokenCategory::Formatting;
      t.value = v;
      return t;
   }

   template<typename T>
   inline void operator_token(T& t, TokenValue v) {
      t.category = TokenCategory::Operator;
      t.value = v;
   }

   template<typename T>
   inline void punctuator_token(T& t, TokenValue v) {
      t.category = TokenCategory::Punctuator;
      t.value = v;
   }

   template<typename T>
   inline T& eos_token(T& t) {
      t.category = TokenCategory::EOS;
      t.value = TokenValue::EndOfStream;
      t.end = t.start;
      return t;
   }

   template<typename T>
   inline T& ws_token(T& t, FragmentCursor pos) {
      t.category = TokenCategory::Whitespace;
      t.end = pos;
      return t;
   }

   template<typename L, typename T>
   void junk(L& line, ColumnIndex& idx, T& t) {
      while (idx < line.size() and not separator_or_punctuator(line[idx]))
         ++idx;
      t.category = TokenCategory::Junk;
   }

   template<typename L>
   inline void
   skip_whitespace(L& line, ColumnIndex& idx) {
      while (idx < line.size() and isblank(line[idx]))
         ++idx;
   }

   template<typename Tok>
   void string_literal(Fragment& frag, FragmentCursor& pos, Tok& t) {
      bool done = false;
      bool escape = false;
      while (frag.covering(pos) && not done) {
         switch (frag(pos)[pos.column++]) {
         case '"': done = !escape;
            // fallthrough
         default: escape = false; break;
         case '_':
            if (pos.column == frag(pos).size()
                and pos.line < frag.size() - 1) {
               ++pos.line;
               pos.column = 0;
            }
            else
               escape = !escape;
            break;
         }
      }
      if (not done)
         throw EndOfStringUnseen{ frag(pos).number, pos.column };
      t.category = TokenCategory::String;
   }

   template<typename L>
   void skip_to_end_of_integer(L& line, ColumnIndex& idx) {
      while (idx < line.size() and isdigit(line[idx]))
         ++idx;
   }
   
   static bool next_line(Fragment& frag, FragmentCursor& pos) {
      if (++pos.line < frag.size()) {
         pos.column = frag(pos).indent;
         return true;
      }
      pos.column = 0;
      return false;
   }

   template<typename L, typename T>
   void integer(L& line, ColumnIndex& idx, T& t) {
      skip_to_end_of_integer(line, idx);
      t.category = TokenCategory::Integer;
   }

   template<typename L, typename T>
   T& number(L& line, ColumnIndex& idx, T& t) {
      integer(line, idx, t);
      if (idx >= line.size() or line[idx] != '.')
         return t;
      if (++idx >= line.size() or not isdigit(line[idx])) {
         --idx;
         return t;
      }

      t.category = TokenCategory::FloatingPoint;
      skip_to_end_of_integer(line, idx);
      if (idx >= line.size() or (line[idx] != 'e' and line[idx] != 'E'))
         return t;
      if (++idx < line.size() and (line[idx] == '+' or line[idx] == '-'))
         ++idx;
      if (idx >= line.size() or not isdigit(line[idx]))
         throw MissingExponent{ line.number, idx };
      skip_to_end_of_integer(line, idx);
      return t;
   }

   inline bool
   identifier_head(uint8_t c) {
      return isalpha(c) or c == '%' or c == '_';
   }

   inline bool
   identifier_part(uint8_t c) {
      return identifier_head(c) or isdigit(c);
   }

   inline bool
   identifier_suffix(uint8_t c) {
      return c == '!' or c == '?';
   }

   inline bool internal_prefix(uint8_t c) {
      return c == '%' or c == '$';
   }

   template<typename L>
   inline void
   skip_prefix(L& line, ColumnIndex& idx, uint8_t c) {
      while (idx < line.size() and line[idx] == c)
         ++idx;
   }

   template<typename L, typename T>
   T& identifier(L& line, ColumnIndex& idx, T& t, Language dialect) {
      t.category = TokenCategory::Identifier;

      ColumnIndex start = --idx; // idx was ahead by 1.
      if (dialect == Language::Boot and internal_prefix(line[idx]))
         skip_prefix(line, idx, line[idx]);
      bool saw_escape = false;
      while (idx < line.size()) {
         if (not identifier_part(line[idx]) and line[idx - 1] != '_')
            break;
         else if (line[idx] == '_')
            saw_escape = true;
         ++idx;
      }
      while (idx < line.size() and identifier_suffix(line[idx]))
         ++idx;

      if (!saw_escape) {
         auto info = classify(line.sub_string(start, idx));
         if (info.category != TokenCategory::Identifier) {
            t.category = info.category;
            t.value = info.value;
         }
      }
      return t;
   }

   template<typename Tok>
   void left_paren_et_al(Fragment& frag, FragmentCursor& pos, Tok& t) {
      punctuator_token(t, TokenValue::OpenParen);
      if (frag.covering(pos) and frag[pos] == '|') {
         ++pos;
         t.value = TokenValue::OpenMetaParen;
      }
   }

   template<typename Tok>
   void left_brace_et_al(Fragment& frag, FragmentCursor& pos, Tok& t) {
      punctuator_token(t, TokenValue::OpenBrace);
      if (frag.covering(pos) and frag[pos] == '|') {
         ++pos;
         t.value = TokenValue::OpenMetaBrace;
      }
   }

   template<typename Tok>
   void left_bracket_et_al(Fragment& frag, FragmentCursor& pos, Tok& t) {
      punctuator_token(t, TokenValue::OpenBracket);
      if (frag.covering(pos) and frag[pos] == '|') {
         ++pos;
         t.value = TokenValue::OpenMetaBracket;
      }
   }

   template<typename Tok>
   void colon_et_al(Fragment& frag, FragmentCursor& pos, Tok& t) {
      operator_token(t, TokenValue::Colon);
      if (frag.covering(pos))
         switch (frag[pos]) {
         case ':': t.value = TokenValue::ColonColon; ++pos; break;
         case '=': t.value = TokenValue::ColonEq; ++pos; break;
         case '-': t.value = TokenValue::ColonDash; ++pos; break;
         default: break;
         }
   }

   template<typename Tok>
   void star_et_al(Fragment& frag, FragmentCursor& pos, Tok& t) {
      operator_token(t, TokenValue::Star);
      if (frag.covering(pos) and frag[pos] == '*') {
         t.value = TokenValue::StarStar;
         ++pos;
      }
   }

   template<typename Tok>
   void slash_et_al(Fragment& frag, FragmentCursor& pos, Tok& t) {
      operator_token(t, TokenValue::Slash);
      if (frag.covering(pos))
         switch (frag[pos]) {
         case '/': t.value = TokenValue::SlashSlash; ++pos; break;
         case '\\': t.value = TokenValue::SlashBackslash; ++pos; break;
         default: break;
         }
   }

   template<typename Tok>
   void backslash_et_al(Fragment& frag, FragmentCursor& pos, Tok& t) {
      operator_token(t, TokenValue::Backslash);
      if (frag.covering(pos))
         switch (frag[pos]) {
         case '\\': t.value = TokenValue::BackslashBackslash; ++pos; break;
         case '/': t.value = TokenValue::BackslashSlash; ++pos; break;
         default: break;
         }
   }

   template<typename Tok>
   void less_et_al(Fragment& frag, FragmentCursor& pos, Tok& t) {
      operator_token(t, TokenValue::Less);
      if (frag.covering(pos))
         switch (frag[pos]) {
         case '-': t.value = TokenValue::LeftArrow; ++pos; break;
         case '<': t.value = TokenValue::OpenChevron; ++pos; break;
         case '=':
            t.value = TokenValue::LessEq;
            if (frag.covering(++pos) and frag[pos] == '>') {
               t.value = TokenValue::Equiv;
               ++pos;
            }
            break;
         default: break;
         }
   }

   template<typename Tok>
   void equal_et_al(Fragment& frag, FragmentCursor& pos, Tok& t) {
      operator_token(t, TokenValue::Eq);
      if (frag.covering(pos))
         switch (frag[pos]) {
         case '>': t.value = TokenValue::Implies; ++pos; break;
         case '=':
            t.value = TokenValue::EqEq;
            if (frag.covering(++pos) and frag[pos] == '>') {
               t.value = TokenValue::FatArrow;
               ++pos;
            }
            break;
         default: break;
         }
   }

   template<typename Tok>
   void tilde_et_al(Fragment& frag, FragmentCursor& pos, Tok& t) {
      operator_token(t, TokenValue::Tilde);
      if (frag.covering(pos) and frag[pos] == '=') {
         t.value = TokenValue::TildeEq;
         ++pos;
      }
   }

   template<typename Tok>
   void greater_et_al(Fragment& frag, FragmentCursor& pos, Tok& t) {
      operator_token(t, TokenValue::Greater);
      if (frag.covering(pos))
         switch (frag[pos]) {
         case '=': t.value = TokenValue::GreaterEq; ++pos; break;
         case '>': t.value = TokenValue::CloseChevron; ++pos; break;
         }
   }

   template<typename Tok>
   void bar_et_al(Fragment& frag, FragmentCursor& pos, Tok& t) {
      punctuator_token(t, TokenValue::Bar);
      if (frag.covering(pos))
         switch (frag[pos]) {
         case ']': t.value = TokenValue::CloseMetaBracket; ++pos; break;
         case '}': t.value = TokenValue::CloseMetaBrace; ++pos; break;
         case ')': t.value = TokenValue::CloseMetaParen; ++pos; break;
         default: break;
         }
   }

   template<typename Tok>
   void minus_et_al(Fragment& frag, FragmentCursor& pos, Tok& t) {
      operator_token(t, TokenValue::Minus); 
      if (frag.covering(pos))
         switch (frag[pos]) {
         case '>': t.value = TokenValue::RightArrow; ++pos; break;
         case '-':
            comment_token(t, TokenValue::Wisecrack);
            pos.column = frag(pos).length();
            break;
         }
   }


   template<typename Tok>
   void plus_et_al(Fragment& frag, FragmentCursor& pos, Tok& t) {
      operator_token(t, TokenValue::Plus);
      if (frag.covering(pos))
         switch (frag[pos]) {
         case '+':
            comment_token(t, TokenValue::Commentary);
            pos.column = frag(pos).length();
            break;
         case '-':
            if (pos.column + 1 < frag(pos).size()
                and frag(pos)[pos.column + 1] == '>') {
               t.value = TokenValue::MapsTo;
               pos.column += 2;
            }
            break;
         default: break;
         }
   }

   template<typename Tok>
   void dot_et_al(Fragment& frag, FragmentCursor& pos, Tok& t) {
      operator_token(t, TokenValue::Dot);
      if (frag.covering(pos) and frag[pos] == '.') {
         t.value = TokenValue::DotDot;
         ++pos;
      }
   }

   template<typename Tok>
   void
   dollar_et_al(Fragment& frag, FragmentCursor& pos, Tok& t, Language dialect) {
      if (dialect != Language::Boot or not frag.covering(pos)
          or separator_or_punctuator(frag[pos]))
         operator_token(t, TokenValue::Dollar);
      else
         identifier(frag(pos), pos.column, t, dialect);
   }

   template<typename Tok>
   void
   sharp_et_al(Fragment& frag, FragmentCursor& pos, Tok& t, Language dialect) {
      if (dialect != Language::Lisp)
         operator_token(t, TokenValue::Sharp);
      else if (frag.covering(pos))
         switch (frag[pos++]) {
         case '(': punctuator_token(t, TokenValue::SharpLeftParen); break;
         case '\'': operator_token(t, TokenValue::SharpApostrophe); break;
         case ':': operator_token(t, TokenValue::SharpColon); break;
         case '+': punctuator_token(t, TokenValue::SharpPlus); break;
         case '-': punctuator_token(t, TokenValue::SharpMinus); break;
         case '.': operator_token(t, TokenValue::SharpDot); break;
         default: --pos; break;
         }
   }

   template<typename Tok>
   Tok Tokenizer<Tok>::finish(Tok& t, Language dialect) {
      switch (auto c = frag.advance(pos)) {
      case '#': sharp_et_al(frag, pos, t, dialect); break;
      case '@': operator_token(t, TokenValue::At); break;
      case '^': operator_token(t, TokenValue::Caret); break;
      case '&': punctuator_token(t, TokenValue::Ampersand); break;         
      case '!': punctuator_token(t, TokenValue::Exclamation); break;
      case '\'': punctuator_token(t, TokenValue::Apostrophe); break;
      case ',': punctuator_token(t, TokenValue::Comma); break;
      case ';': punctuator_token(t, TokenValue::Semicolon); break;
      case '`': punctuator_token(t, TokenValue::Backquote); break;
      case '(': left_paren_et_al(frag, pos, t); break;
      case ')': punctuator_token(t, TokenValue::CloseParen); break;
      case '{': left_brace_et_al(frag, pos, t); break;
      case '}': punctuator_token(t, TokenValue::CloseBrace); break;
      case '[': left_bracket_et_al(frag, pos, t); break;
      case ']': punctuator_token(t, TokenValue::CloseBracket); break;
      case ':': colon_et_al(frag, pos, t); break;
      case '*': star_et_al(frag, pos, t); break;
      case '/': slash_et_al(frag, pos, t); break;
      case '\\': backslash_et_al(frag, pos, t); break;
      case '<': less_et_al(frag, pos, t); break;
      case '=': equal_et_al(frag, pos, t); break;
      case '~': tilde_et_al(frag, pos, t); break;
      case '>': greater_et_al(frag, pos, t); break;
      case '|': bar_et_al(frag, pos, t); break;
      case '-': minus_et_al(frag, pos, t); break;
      case '+': plus_et_al(frag, pos, t); break;
      case '.': dot_et_al(frag, pos, t); break;
      case '"': string_literal(frag, pos, t); break;
      case '$': dollar_et_al(frag, pos, t, dialect); break;

      default:
         if (isdigit(c))
            number(frag(pos), pos.column, t);
         else if (identifier_head(c))
            identifier(frag(pos), pos.column, t, dialect);
         else
            junk(frag(pos), pos.column, t);
         break;
      }

      t.end = pos;
      return t;
   }

   template<typename Tok>
   Tok Tokenizer<Tok>::get(Language dialect) {
      Tok t { };
      t.start = pos;
      
      if (eos())
         return eos_token(t);
      else if (isblank(frag[pos])) {
         skip_whitespace(frag(pos), pos.column);
         return ws_token(t, pos);
      }
      else if (line_continuation()) {
         if (next_line(frag, pos))
            return finish(t, dialect);
         return eos_token(t);
      }
      else if (pos.column >= line_length()) {
         if (not next_line(frag, pos))
            return eos_token(t);
         t.start = t.end = pos;
         auto indent = indents.top();
         if (indent.column < pos.column) {
            indents.push(pos);
            return formatting_token(t, TokenValue::Indent);
         }
         else if (indent.column > pos.column) {
            indents.pop();
            return formatting_token(t, TokenValue::Unindent);
         }

         return formatting_token(t, TokenValue::Justify);
      }

      return finish(t, dialect);
   }

   // -- Token streams.
   template<typename T>
   struct TokenStream : std::vector<T> {
      explicit TokenStream(Fragment& f, Language dialect = Language::Spad) {
         Tokenizer<T> lex { f };
         while (auto t = lex.get(dialect))
            this->push_back(t);
      }
   };
}

#endif  // OPENAXIOM_TOKEN_included