aboutsummaryrefslogtreecommitdiff
path: root/src/syntax/sexpr.cxx
blob: f9d768257bb28b0c86e14528475712a9cc9fc80c (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
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
// Copyright (C) 2010-2013, 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 The Numerical Algorithms Group Ltd. 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.

// --% Author: Gabriel Dos Reis.

#include <ctype.h>
#include <string.h>
#include <iostream>
#include <iterator>
#include <open-axiom/sexpr>
#include <open-axiom/FileMapping>
#include <open-axiom/diagnostics>

namespace OpenAxiom {
   namespace Sexpr {
      static void
      invalid_character(Reader::State& s) {
         auto line = std::to_string(s.bytes.lineno);
         auto column = std::to_string(s.cur - s.line);
         auto msg = "invalid character on line " + line +
            " and column " + column;
         if (isprint(*s.cur))
            throw Diagnostics::BasicError(msg + ": " + std::string(1, *s.cur));
         throw Diagnostics::BasicError(msg + " with code " + std::to_string(*s.cur));
      }
      
      static void
      syntax_error(const std::string& s) {
         throw Diagnostics::BasicError(s);
      }

      // Return true if character `c' introduces a blank.
      static bool
      is_blank(char c) {
         return c == ' ' or c == '\t' or c == '\v'
            or c == '\n' or c == '\f' or c == '\r';
      }
      
      // Return true if the character `c' introduces a delimiter.
      static bool
      is_delimiter(char c) {
         return is_blank(c)
            or c == '(' or c == ')' or c == '\''
            or c == '`' or c == '#';
      }

      // Move the cursor past all consecutive blank characters, and
      // return true if there are more input characters to consider.
      static bool
      skip_blank(Reader::State& s) {
         for (bool done = false; s.cur < s.bytes.end and not done; )
            switch (*s.cur) {
            case '\n':
               ++s.bytes.lineno;
               s.line = ++s.cur;
               break;
            case ' ': case '\t': case '\v': case '\r': case '\f':
               ++s.cur;
               break;
            default: done = true; break;
            }
         return s.cur < s.bytes.end;
      }

      // Move `cur' to end-of-line marker.
      static void
      skip_to_eol(Reader::State& s) {
         // FIXME: properly handle CR+LF.
         while (s.cur < s.bytes.end and *s.cur != '\n')
            ++s.cur;
      }

      // Move `cur' one-past a non-esacaped character `c'.
      // Return true if the character was seen.
      static bool
      skip_to_nonescaped_char(Reader::State& s, char c) {
         for (bool saw_escape = false; s.cur < s.bytes.end; ++s.cur)
            if (saw_escape)
               saw_escape = false;
            else if (*s.cur == '\\')
               saw_escape = true;
            else if (*s.cur == c) {
               ++s.cur;
               return true;
            }
         return false;
      }

      // Move the cursor past the closing quote of string literal.
      // Return true if the closing quote was effectively seen.
      static inline bool
      skip_to_quote(Reader::State& s) {
         return skip_to_nonescaped_char(s, '"');
      }

      template<typename Pred>
      static bool
      advance_while(Reader::State& s, Pred p) {
         while (s.cur < s.bytes.end and p(*s.cur))
            ++s.cur;
         return s.cur < s.bytes.end;
      }

      // Return true if the character `c' be part of a non-absolute
      // identifier.
      static bool
      identifier_part(Byte c) {
         switch (c) {
         case '+': case '-': case '*': case '/': case '%': case '^':
         case '~': case '@': case '$': case '&': case '=':
         case '<': case '>': case '?': case '!': case '_':
         case '[': case ']': case '{': case '}':
            return true;
         default:
            return isalnum(c);
         }
      }

      // -- AtomSyntax --
      AtomSyntax::AtomSyntax(const Lexeme& t) : lex(t) { }

      // -- IntegerSyntax --
      IntegerSyntax::IntegerSyntax(const Lexeme& t) : AtomSyntax(t) { }

      void
      IntegerSyntax::accept(Visitor& v) const {
         v.visit(*this);
      }

      // -- CharacterSyntax --
      CharacterSyntax::CharacterSyntax(const Lexeme& t) : AtomSyntax(t) { }

      void
      CharacterSyntax::accept(Visitor& v) const {
         v.visit(*this);
      }

      // -- StringSyntax --
      StringSyntax::StringSyntax(const Lexeme& t) : AtomSyntax(t) { }

      void
      StringSyntax::accept(Visitor& v) const {
         v.visit(*this);
      }

      // -- SymbolSyntax --
      SymbolSyntax::SymbolSyntax(const Lexeme& t, Kind k)
            : AtomSyntax(t), sort(k)
      { }

      void
      SymbolSyntax::accept(Visitor& v) const {
         v.visit(*this);
      }

      // -- AnchorSyntax --
      AnchorSyntax::AnchorSyntax(size_t t, const Syntax* s) : tag(t), val(s) { }

      void
      AnchorSyntax::accept(Visitor& v) const {
         v.visit(*this);
      }

      // -- ReferenceSyntax --
       ReferenceSyntax::ReferenceSyntax(const Lexeme& t, Ordinal n)
             : AtomSyntax(t), pos(n)
      { }

      void
      ReferenceSyntax::accept(Visitor& v) const {
         v.visit(*this);
      }

      // -- QuoteSyntax --
      QuoteSyntax::QuoteSyntax(const Syntax* s)
            : unary_form<QuoteSyntax>(s)
      { }

      // -- AntiquoteSyntax --
      AntiquoteSyntax::AntiquoteSyntax(const Syntax* s)
            : unary_form<AntiquoteSyntax>(s)
      { }

      // -- Expand --
      Expand::Expand(const Syntax* s) : unary_form<Expand>(s) { }

      // -- Eval --
      Eval::Eval(const Syntax* s) : unary_form<Eval>(s) { }

      // -- Splice --
      Splice::Splice(const Syntax* s) : unary_form<Splice>(s) { }

      // -- Function --
      Function::Function(const Syntax* s) : unary_form<Function>(s) { }

      // -- Include --
      Include::Include(const Syntax* c, const Syntax* s)
            : binary_form<Include>(c, s)
      { }

      // -- Exclude --
      Exclude::Exclude(const Syntax* c, const Syntax* s)
            : binary_form<Exclude>(c, s)
      { }

      // -- ListSyntax --
      ListSyntax::ListSyntax() : dot(false) { }

      ListSyntax::ListSyntax(const base& elts, bool d)
            : base(elts), dot(d)
      { }

      ListSyntax::~ListSyntax() { }

      void
      ListSyntax::accept(Visitor& v) const {
         v.visit(*this);
      }

      // -- VectorSyntax --
      VectorSyntax::VectorSyntax() { }

      VectorSyntax::VectorSyntax(const base& elts) : base(elts) { }

      VectorSyntax::~VectorSyntax() { }
      
      void
      VectorSyntax::accept(Visitor& v) const {
         v.visit(*this);
      }

      // ---------------
      // -- Allocator --
      // ---------------
      Allocator::Allocator() { }

      // This destructor is defined here so that it provides
      // a single instantiation point for destructors of all
      // used templates floating around.
      Allocator::~Allocator() { }

      const CharacterSyntax*
      Allocator::make_character(const Lexeme& t) {
         return chars.make(t);
      }

      const IntegerSyntax*
      Allocator::make_integer(const Lexeme& t) {
         return ints.make(t);
      }

      const StringSyntax*
      Allocator::make_string(const Lexeme& t) {
         return strs.make(t);
      }

      const SymbolSyntax*
      Allocator::make_symbol(SymbolSyntax::Kind k, const Lexeme& t) {
         return syms.make(t, k);
      }

      const ReferenceSyntax*
      Allocator::make_reference(size_t i, const Lexeme& t) {
         return refs.make(t, i);
      }

      const AnchorSyntax*
      Allocator::make_anchor(size_t t, const Syntax* s) {
         return ancs.make(t, s);
      }

      const QuoteSyntax*
      Allocator::make_quote(const Syntax* s) {
         return quotes.make(s);
      }

      const AntiquoteSyntax*
      Allocator::make_antiquote(const Syntax* s) {
         return antis.make(s);
      }

      const Expand*
      Allocator::make_expand(const Syntax* s) {
         return exps.make(s);
      }

      const Eval*
      Allocator::make_eval(const Syntax* s) {
         return evls.make(s);
      }

      const Splice*
      Allocator::make_splice(const Syntax* s) {
         return spls.make(s);
      }

      const Function*
      Allocator::make_function(const Syntax* s) {
         return funs.make(s);
      }

      const Include*
      Allocator::make_include(const Syntax* c, const Syntax* s) {
         return incs.make(c, s);
      }

      const Exclude*
      Allocator::make_exclude(const Syntax* c, const Syntax* s) {
         return excs.make(c, s);
      }

      const ListSyntax*
      Allocator::make_list(const std::vector<const Syntax*>& elts, bool dot) {
         if (elts.empty())
            return &empty_list;
         return lists.make(elts, dot);
      }

      const VectorSyntax*
      Allocator::make_vector(const std::vector<const Syntax*>& elts) {
         if (elts.empty())
            return &empty_vector;
         return vectors.make(elts);
      }

      // The sequence of characters in [cur, last) consists
      // entirely of digits.  Return the corresponding natural value.
      static size_t
      natural_value(const Byte* cur, const Byte* last) {
         size_t n = 0;
         for (; cur < last; ++cur)
            // FIXME: check for overflow.
            n = 10 * n + (*cur - '0');
         return n;
      }

      // -- Reader --
      Reader::Reader(const Byte* f, const Byte* l)
            : st{ { f, l, 1 }, f, f }
      { }

      Reader::Reader(const RawInput& ri)
            : st { ri, ri.start, ri.start }
      { }

      static const Syntax* read_sexpr(Reader::State&);

      // Parse a string literal
      static const Syntax*
      read_string(Reader::State& s) {
         auto start = s.cur++;
         if (not skip_to_quote(s))
            syntax_error("missing closing quote sign for string literal");
         Lexeme t = { { start, s.cur }, s.bytes.lineno };
         return s.alloc.make_string(t);
      }

      // Parse an absolute identifier.
      static const Syntax*
      read_absolute_symbol(Reader::State& s) {
         auto start = ++s.cur;
         if (not skip_to_nonescaped_char(s, '|'))
            syntax_error("missing closing bar sign for an absolute symbol");
         Lexeme t = { { start, s.cur - 1 }, s.bytes.lineno };
         return s.alloc.make_symbol(SymbolSyntax::absolute, t);
      }

      // Read an atom starting with digits.
      static const Syntax*
      read_maybe_natural(Reader::State& s) {
         auto start = s.cur;
         advance_while (s, isdigit);
         if (s.cur >= s.bytes.end or is_delimiter(*s.cur)) {
            Lexeme t = { { start, s.cur }, s.bytes.lineno };
            return s.alloc.make_integer(t);
         }
         advance_while(s, identifier_part);
         Lexeme t = { { start, s.cur }, s.bytes.lineno };
         return s.alloc.make_symbol(SymbolSyntax::ordinary, t);
      }

      // Read an identifier.
      static const Syntax*
      read_identifier(Reader::State& s) {
         auto start = s.cur;
         advance_while(s, identifier_part);
         Lexeme t = { { start, s.cur }, s.bytes.lineno };
         return s.alloc.make_symbol(SymbolSyntax::ordinary, t);
      }

      // Read an atom starting with a '+' or '-' sign; this
      // should be identifier, or a signed integer.
      static const Syntax*
      read_maybe_signed_number(Reader::State& s) {
         auto start = s.cur++;
         if (s.cur < s.bytes.end and isdigit(*s.cur)) {
            advance_while(s, isdigit);
            if (s.cur >= s.bytes.end or is_delimiter(*s.cur)) {
               Lexeme t = { { start, s.cur }, s.bytes.lineno };
               return s.alloc.make_integer(t);
            }
         }
         advance_while(s, identifier_part);
         Lexeme t = { { start, s.cur }, s.bytes.lineno };
         return s.alloc.make_symbol(SymbolSyntax::ordinary, t);
      }

      static const Syntax*
      read_keyword(Reader::State& s) {
         auto start = s.cur++;
         advance_while(s, identifier_part);
         Lexeme t = { { start, s.cur }, s.bytes.lineno };
         return s.alloc.make_symbol(SymbolSyntax::keyword, t);
      }

      // Read an atom.
      static const Syntax*
      read_atom(Reader::State& s) {
         switch (*s.cur) {
         case '"': return read_string(s);
         case ':': return read_keyword(s);
         case '-': case '+': return read_maybe_signed_number(s);

         case '0': case '1': case '2': case '3': case '4':
         case '5': case '6': case '7': case '8': case '9':
            return read_maybe_natural(s);

         default:
            if (identifier_part(*s.cur))
               return read_identifier(s);
            invalid_character(s);
            ++s.cur;
            return nullptr;
         }
      }

      // Parse a quote expression.
      static const Syntax*
      read_quote(Reader::State& s) {
         ++s.cur;               // skip the quote character
         auto x = read_sexpr(s);
         if (x == nullptr)
            syntax_error("end of input reached after quote sign");
         return s.alloc.make_quote(x);
      }

      // Parse a backquote expression.
      static const Syntax*
      read_backquote(Reader::State& s) {
         ++s.cur;               // skip the backquote character
         auto x = read_sexpr(s);
         if (x == nullptr)
            syntax_error("end of input reached after backquote sign");
         return s.alloc.make_antiquote(x);
      }

      // We've just seen "#(" indicating the start of a literal
      // vector.  Read the elements and return the corresponding form.
      static const Syntax*
      finish_literal_vector(Reader::State& s) {
         ++s.cur;               // Skip the open paren.
         std::vector<const Syntax*> elts { };
         while (skip_blank(s) and *s.cur != ')') {
            if (auto x = read_sexpr(s))
               elts.push_back(x);
            else
               syntax_error("syntax error while reading vector elements");
         }
         if (s.cur >= s.bytes.end)
            syntax_error("unfinished literal vector");
         else
            ++s.cur;
         return s.alloc.make_vector(elts);
      }

      // We've just seen the sharp sign followed by a digit.  We assume
      // we are about to read an anchor or a back reference.
      static const Syntax*
      finish_anchor_or_reference(Reader::State& s) {
         auto start = s.cur;
         advance_while(s, isdigit);
         if (s.cur >= s.bytes.end)
            syntax_error("end-of-input after sharp-number sign");
         const Byte c = *s.cur;
         if (c != '#' and c != '=')
            syntax_error("syntax error after sharp-number-equal sign");
         Lexeme t = { { start, s.cur }, s.bytes.lineno };
         auto n = natural_value(start, s.cur);
         ++s.cur;
         if (c == '#')
            return s.alloc.make_reference(n, t);
         auto x = read_sexpr(s);
         if (x == nullptr)
            syntax_error("syntax error after sharp-number-equal sign");
         return s.alloc.make_anchor(n, x);
      }

      static const Syntax*
      finish_function(Reader::State& s) {
         ++s.cur;               // skip quote sign.
         auto x = read_sexpr(s);
         if (x == nullptr)
            syntax_error("missing function designator after sharp-quote sign");
         return s.alloc.make_function(x);
      }

      static const Syntax*
      finish_uninterned_symbol(Reader::State& s) {
         ++s.cur;               // skip colon sign.
         auto start = s.cur;
         advance_while(s, identifier_part);
         Lexeme t = { { start, s.cur }, s.bytes.lineno };
         return s.alloc.make_symbol(SymbolSyntax::uninterned, t);
      }

      static const Syntax*
      finish_readtime_eval(Reader::State& s) {
         ++s.cur;               // skip dot sign.
         auto x = read_sexpr(s);
         if (x == nullptr)
            syntax_error("parse error after sharp-dot sign");
         return s.alloc.make_eval(x);
      }

      static const Syntax*
      finish_character(Reader::State& s) {
         ++s.cur;               // skip backslash sign
         auto start = s.cur;
         advance_while(s, identifier_part);
         Lexeme t = { { start, s.cur }, s.bytes.lineno };
         return s.alloc.make_character(t);
      }

      static const Syntax*
      finish_include(Reader::State& s) {
         ++s.cur;
         auto cond = read_sexpr(s);
         auto form = read_sexpr(s);
         return s.alloc.make_include(cond, form);
      }

      static const Syntax*
      finish_exclude(Reader::State& s) {
         ++s.cur;
         auto cond = read_sexpr(s);
         auto form = read_sexpr(s);
         return s.alloc.make_exclude(cond, form);
      }

      static const Syntax*
      read_sharp_et_al(Reader::State& s) {
         if (++s.cur >= s.bytes.end)
            syntax_error("end-of-input reached after sharp sign");
         switch (*s.cur) {
         case '(':  return finish_literal_vector(s);
         case '\'': return finish_function(s);
         case ':': return finish_uninterned_symbol(s);
         case '.': return finish_readtime_eval(s);
         case '\\': return finish_character(s);
         case '+': return finish_include(s);
         case '-': return finish_exclude(s);

         default:
            if (isdigit(*s.cur))
               return finish_anchor_or_reference(s);
            syntax_error("syntax error after sharp-sign");
         }
         return nullptr;
      }

      // We have just seen a dot; read the tail and the closing parenthesis.
      static const Syntax*
      finish_dotted_list(Reader::State& s, std::vector<const Syntax*>& elts) {
         ++s.cur;               // Skip dot sign.
         auto x = read_sexpr(s);
         if (x == nullptr)
            syntax_error("missing expression after dot sign");
         if (not skip_blank(s) or *s.cur != ')')
            syntax_error("missing closing parenthesis");
         ++s.cur;
         elts.push_back(x);
         return s.alloc.make_list(elts, true);
      }

      static const Syntax*
      read_pair(Reader::State& s) {
         ++s.cur;               // skip opening parenthesis
         std::vector<const Syntax*> elts { };
         while (skip_blank(s))
            switch (*s.cur) {
            case '.':
               if (elts.empty())
                  syntax_error("missing expression before dot sign.");
               return finish_dotted_list(s, elts);

            case ')':
               ++s.cur;
               return s.alloc.make_list(elts);

            default:
               if (auto x = read_sexpr(s))
                  elts.push_back(x);
               else
                  syntax_error("unfinished pair expression");
               break;
            }
         syntax_error("end-of-input while looking for closing parenthesis");
         return nullptr;
      }

      static const Syntax*
      read_sexpr(Reader::State& s) {
         while (skip_blank(s))
            switch (*s.cur) {
            case ';': skip_to_eol(s); break;
            case '\'': return read_quote(s);
            case '`': return read_backquote(s);
            case '|': return read_absolute_symbol(s);
            case '#': return read_sharp_et_al(s);
            case '(': return read_pair(s);
            default: return read_atom(s);
            }
         return nullptr;
      }

      const Syntax*
      Reader::read() {
         return read_sexpr(st);
      }

      const Byte*
      Reader::position(Ordinal p) {
         st.cur = st.bytes.start + p;
         st.line = st.cur;
         // while (st.line > st.start and st.line[-1] != '\n')
         //    --st.line;
         return st.cur;
      }

   }
}