aboutsummaryrefslogtreecommitdiff
path: root/dep/g3dlite/include/G3D/TextInput.h
blob: 7aefb50ffa800c08d2b581f3e2f04a6b8f8513fe (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
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
/**
 @file TextInput.h

 Simple text lexer/tokenizer.

 @maintainer Morgan McGuire, http://graphics.cs.williams.edu

 @cite Based on a lexer written by Aaron Orenstein. 

 @created 2002-11-27
 @edited  2010-07-03

 Copyright 2000-2010, Morgan McGuire.
 All rights reserved.
 */

#ifndef G3D_TextInput_h
#define G3D_TextInput_h

#include "G3D/platform.h"
#include "G3D/Array.h"
#include "G3D/Set.h"
#include "G3D/ParseError.h"
#include <string>
#include <queue>
#include <ctype.h>
#include <stdio.h>

namespace G3D {

/**
 For use with TextInput.
 */
class Token {
public:

    /**
     More detailed type information than Type.
     */
    enum ExtendedType {
      DOUBLE_QUOTED_TYPE, 
      SINGLE_QUOTED_TYPE, 
      SYMBOL_TYPE,
      FLOATING_POINT_TYPE, 
      INTEGER_TYPE,
      BOOLEAN_TYPE,
      LINE_COMMENT_TYPE,
      BLOCK_COMMENT_TYPE,
      NEWLINE_TYPE,
      END_TYPE
    };

    /**
     Strings are enclosed in quotes, symbols are not.
     */
    enum Type {
      STRING  = DOUBLE_QUOTED_TYPE, 
      SYMBOL  = SYMBOL_TYPE, 
      NUMBER  = FLOATING_POINT_TYPE, 
      BOOLEAN = BOOLEAN_TYPE,
      COMMENT = LINE_COMMENT_TYPE,
      NEWLINE = NEWLINE_TYPE,
      END     = END_TYPE
    };

private:

    friend class TextInput;

    /** 
      Holds the actual value, which might be any type.  If a number, it will be 
      parsed at runtime.
    */
    std::string             _string;

    bool                    _bool;
    int                     _line;
    int                     _character;
    uint64                  _bytePosition;
    Type                    _type;
    ExtendedType            _extendedType;

public:

    Token() :
        _string(""),
        _bool(false),
        _line(0), 
        _character(0), 
        _bytePosition(0),
        _type(END), 
        _extendedType(END_TYPE) {}

    Token(Type t, ExtendedType e, const std::string& s, int L, int c, uint64 byte)
        : _string(s), _bool(false), _line(L), _character(c), _bytePosition(byte), _type(t), _extendedType(e) {}

    Token(Type t, ExtendedType e, const std::string& s, bool b, int L, int c, uint64 byte)
        : _string(s), _bool(b), _line(L), _character(c), _bytePosition(byte), _type(t), _extendedType(e) {}

    Type type() const {
        return _type;
    }

    ExtendedType extendedType() const {
        return _extendedType;
    }

    /**
     The value of a single or double quote string (not including the quotes),
     the name of a symbol, or the exact textual representation of a number as
     parsed from the input. 
     */
    const std::string& string() const {
        return _string;
    }

    bool boolean() const {
        return _bool;
    }

    /**
     Starting line of the input from which this token was parsed.  Starts
     at 1.
     */
    int line() const {
        return _line;
    }

    /**
     Starting character position in the input line from which this token was
     parsed.  Starts at 1.
     */
    int character() const {
        return _character;
    }

    /** Number of bytes from the beginning of the buffer that this token was parsed from. 
      Begins at 0 */
    uint64 bytePosition() const {
        return _bytePosition;
    }

    /** Return the numeric value for a number type, or zero if this is
        not a number type.
    */
    double number() const;
};


/**
 A simple style tokenizer for reading text files.  TextInput handles a
 superset of C++,Java, Matlab, and Bash code text including single
 line comments, block comments, quoted strings with escape sequences,
 and operators.  TextInput recognizes several categories of tokens,
 which are separated by white space, quotation marks, or the end of a
 recognized operator:

 <ul>
  <li><CODE>Token::SINGLE_QUOTED_TYPE</CODE> string of characters surrounded by single quotes, e.g., 'x', '\\0', 'foo'.
  <li><CODE>Token::DOUBLE_QUOTED_TYPE</CODE> string of characters surrounded by double quotes, e.g., "x", "abc\txyz", "b o b".
  <li><CODE>Token::SYMBOL_TYPE</CODE> legal C++ operators, keywords, and identifiers.  e.g., >=, Foo, _X, class, {
  <li><CODE>Token::INTEGER_TYPE</CODE> numbers without decimal places or exponential notation. e.g., 10, 0x17F, 32, 0, -155
  <li><CODE>Token::FLOATING_POINT_TYPE</CODE> numbers with decimal places or exponential notation. e.g., 1e3, -1.2, .4, 0.5
  <li><CODE>Token::BOOLEAN_TYPE</CODE> special symbols like "true" and "false"; the exact details can be configured in TextInput::Settings
  <li><CODE>Token::LINE_COMMENT_TYPE</CODE> (disabled by default); generated for line comments as specified by TextInput::Settings
  <li><CODE>Token::BLOCK_COMMENT_TYPE</CODE> (disabled by default); generated for c-style block comments as specified by TextInput::Settings
  <li><CODE>Token::NEWLINE_TYPE</CODE> (disabled by default); generated for any of "\\r", "\\n" or "\\r\\n"
 </ul>

 <P>The special ".." and "..." tokens are always recognized in
 addition to normal C++ operators. Additional tokens can be made
 available by changing the Settings.

 Negative numbers are handled specially because of the ambiguity between unary minus and negative numbers-- 
 see the note on TextInput::read.

  TextInput does not have helper functions for types with non-obvious
  formatting, or helpers that would be redundant.  Use the serialize
  methods instead for parsing specific types like int, Vector3, and
  Color3.

  Inside quoted strings escape sequences are converted.  Thus the
  string token for ["a\\nb"] is 'a', followed by a newline, followed by
  'b'.  Outside of quoted strings, escape sequences are not converted,
  so the token sequence for [a\\nb] is symbol 'a', symbol '\\', symbol
  'nb' (this matches what a C++ parser would do).  The exception is
  that a specified TextInput::Settings::otherCommentCharacter preceeded
  by a backslash is assumed to be an escaped comment character and is
  returned as a symbol token instead of being parsed as a comment
  (this is what a LaTex or VRML parser would do).

  <B>Examples</B>

  <PRE>
  TextInput ti(TextInput::FROM_STRING, "name = \"Max\", height = 6");

  Token t;

  t = ti.read(); 
  debugAssert(t.type == Token::SYMBOL);
  debugAssert(t.sval == "name");

  ti.read();
  debugAssert(t.type == Token::SYMBOL);
  debugAssert(t.sval == "=");

  std::string name = ti.read().sval;
  ti.read();
  </PRE>

  <PRE>
  TextInput ti(TextInput::FROM_STRING, "name = \"Max\", height = 6");
  ti.readSymbols("name", "=");
  std::string name = ti.readString();
  ti.readSymbols(",", "height", "=");
  double height = ti. readNumber();
  </PRE>

 Assumes that the file is not modified once opened.
 */
class TextInput {
public:
    /** Includes MSVC specials parsing */
    static double parseNumber(const std::string& _string);

    /** toLower(_string) == "true" */
    static bool parseBoolean(const std::string& _string);


    /** Tokenizer configuration options.  */
    class Settings {
    public:
        /** If true, C-style slash-star marks a multi-line comment.

            See generateCommentTokens for rules on how this is applied.

            Default is true.
         */
        bool                cppBlockComments;

        /** If true, // begins a single line comment.

            See generateCommentTokens for rules on how this is applied.

            Default is true.
         */
        bool                cppLineComments;

        /** If true, otherCommentCharacter and otherCommentCharacter2
            are used to begin single line comments in the same way
            cppLineComments is.

            See generateCommentTokens for rules on how this is applied.

            Default is true.
         */
        bool                otherLineComments;

        /** If true, \\r, \\n, \\t, \\0, \\\\ and other escape sequences inside
            strings are converted to the equivalent C++ escaped character.
            If false, backslashes are treated literally.  It is convenient to
            set to false if reading Windows paths, for example, like
            c:\\foo\\bar.

            Default is true.
         */
        bool                escapeSequencesInStrings;

        /** If not '\\0', specifies a character that begins single line
            comments ('#' and '%' are popular choices).  This is independent
            of the cppLineComments flag.  If the character appears in text with
            a backslash in front of it, it is considered escaped and is not
            treated as a comment character.

            Default is '\\0'.
         */
        char                otherCommentCharacter;

        /** Another (optional) 1-comment character.  Useful for files that
            support multiple comment syntaxes.  Default is '\\0'.
         */
        char                otherCommentCharacter2;

        /** If true, comments enabled by cppBlockComments, cppLineComments
            and otherLineComments will generate their respective tokens.
            If false, the same settings will enable parsing and ignoring
            comments

            Default is false.
         */
        bool                generateCommentTokens;

        /** If true, newlines will generate  tokens.
            If false, newlines will be discarded as whitespace when parsed
            outside of other tokens.

            Default is false.
         */
        bool                generateNewlineTokens;

        /** If true, "-1" parses as the number -1 instead of the
            symbol "-" followed by the number 1.  Default is true.*/
        bool                signedNumbers;

        /** If true, strings can be marked with single quotes (e.g.,
            'aaa').  If false, the quote character is parsed as a
            symbol. Default is true.  Backquote (`) is always parsed
            as a symbol. */
        bool                singleQuotedStrings;

        /** The character to use as a single quote.  Defaults to "'" (backquote),
            occasionally useful to set to "`" (forward quote) or to "," (comma) for
            reading CSV files. */
        char                singleQuoteCharacter;
        
        /** If set to a non-empty string, that string will be used in
            place of the real file name (or in place of a pseudonym
            constructed from the buffer if given FROM_STRING) in
            tokens and exceptions.
            
            Default is empty.
        */
        std::string         sourceFileName;

    
        /** Added to the line number reported by peekLineNumber and in
            exceptions.  Useful for concatenating files that are
            parsed separately.  Default is zero. */
        int                 startingLineNumberOffset;

        /** 
          Parse -1.#IND00 as the floating point number returned by
          nan(), -1.#INF00 as -G3D::inf(), and 1.#INF00 as G3D::inf().  
          
          Note that the C99 standard specifies that a variety of formats
          like "nan" are to be used; these are supported by 
          G3D::TextInput::Settings::simpleFloatSpecials.

          An alternative to specifying msvcFloatSpecials is to read numbers as:
          <pre>
            Token x = t.read();
            Token y = t.peek();
            if ((x.string() == "-1.") && 
                (y.string() == "#INF00") && 
                (y.character() == x.character() + 3) &&
                (y.line() == x.line()) {
                t.read();
                return nan();
            }
            // ... similar cases for inf
          </pre>

          If the single-comment character was #, the floating point
          special format overrides the comment and will be parsed
          instead.

          If signedNumbers is false msvcFloatSpecials will not be parsed.

          Default is true. */
        bool                msvcFloatSpecials;

        /** Parses "+inf', "-inf", "inf", "nan" as floats instead of symbols. 
            Defaults to true.*/
        bool                simpleFloatSpecials;

        /**
         Parse the following set of useful proof symbols:
         
           =>
           ::>
           <::
           :>
           <:
           |-
           ::=
           :=
           <-

           Default is false.
        */
        bool                proofSymbols;

        /**
         When parsing booleans and msvcFloatSpecials, is case significant?
         Default is {true}
        */
        bool                caseSensitive;

        /** All symbols that will become the 'true' boolean token.  See also caseSensitive.
            Clear this value to disable parsing of true booleans.

            Default is {true}.
         */
        Set<std::string>    trueSymbols;

        /** See trueSymbols. Default is {false}*/
        Set<std::string>    falseSymbols;

        Settings();
    };
	
private:

    std::deque<Token>       stack;

    /**
     Characters to be tokenized.
     */
    Array<char>             buffer;

    /**
     Offset of current character (the next character to consumed) in
     input buffer.
     */
    int                     currentCharOffset;

    /**
     Line number of next character to be consumed from the input buffer.  (1
     indicates first line of input.)

     Note that this is the line number of the @e next character to be
     consumed from the input, not the line number of the @e last character
     consumed!
     */
    int                     lineNumber;

    /**
     Character number (within the line) of the next character to be consumed
     from the input buffer.  (1 indicates first character of the line).

     Note that this is the character number of the @e next character to be
     consumed from the input, not the character number of the @e last
     character consumed!
     */
    int                     charNumber;

    /** Configuration options.  This includes the file name that will be
        reported in tokens and exceptions.  */
    Settings                options;
  
    void init();

    /**
     Consumes the next character from the input buffer, and returns that
     character.  Updates lineNumber and charNumber to reflect the location of
     the next character in the input buffer.

     Note: you shouldn't be using the return value of this function in most
     cases.  In general, you should peekInputChar() to get the next
     character, determine what to do with it, then consume it with this
     function (or with eatAndPeekInputChar()).  Given that usage, in most
     instances you already know what this function would return!
     */
    int eatInputChar();

    /**
     Returns the next character from the input buffer, without consuming any
     characters.  Can also be used to look deeper into the input buffer.
     Does not modify lineNumber or charNumber.

     @param distance Index of the character in the input buffer to peek at,
     relative to the next character.  Default is 0, for the next character in
     the input buffer.
     */
    int peekInputChar(int distance = 0);

    /**
     Helper function to consume the next character in the input buffer and
     peek at the one following (without consuming it).
     */
    inline int eatAndPeekInputChar() {
        eatInputChar();
        return peekInputChar(0);
    }

    /**
     Read the next token, returning an END token if no more input is
     available.
     */
    Token nextToken();

    /**
       Helper for nextToken.  Appends characters to t._string until the end
       delimiter is reached.
       
       When called, the next character in the input buffer should be first the
       first character after the opening delimiter character.
    */
    void parseQuotedString(unsigned char delimiter, Token& t);

public:

    class TokenException : public ParseError {
    public:
        /** Name of file being parsed when exception occurred. 
            \deprecated  Use filename
         */
        std::string     sourceFile;

        virtual ~TokenException() {}

    protected:
        
        TokenException(
            const std::string&  src,
            int                 ln,
            int                 ch);

    };

    /** While parsing a number of the form 1.\#IN?00, ? was 
        not 'D' or 'F'. */
    class BadMSVCSpecial : public TokenException {
    public:

        BadMSVCSpecial(
            const std::string&  src,
            int                 ln,
            int                 ch);
    };

    /** Thrown by the read methods. */
    class WrongTokenType : public TokenException {
    public:
        Token::Type     expected;
        Token::Type     actual;

        WrongTokenType(
            const std::string&  src,
            int                 ln,
            int                 ch,
            Token::Type         e,
            Token::Type         a);
    };

    class WrongSymbol : public TokenException {
    public:
        std::string             expected;
        std::string             actual;

        WrongSymbol(
            const std::string&  src,
            int                 ln,
            int                 ch,
            const std::string&  e,
            const std::string&  a);
    };


    /** String read from input did not match expected string.  */
    class WrongString : public TokenException {
    public:
        std::string             expected;
        std::string             actual;

        WrongString(
            const std::string&  src,
            int                 ln,
            int                 ch,
            const std::string&  e,
            const std::string&  a);
    };

    TextInput(const std::string& filename, const Settings& settings = Settings());

    enum FS {FROM_STRING};
    /** Creates input directly from a string.  The first argument must be
        TextInput::FROM_STRING.
    */
    TextInput(FS fs, const std::string& str, const Settings& settings = Settings());

    /** Returns true while there are tokens remaining. */
    bool hasMore();

    /** Read the next token (which will be the END token if ! hasMore()).
    
        Signed numbers can be handled in one of two modes.  If the option 
        TextInput::Settings::signedNumbers is true,
        A '+' or '-' immediately before a number is prepended onto that number and
        if there is intervening whitespace, it is read as a separate symbol.

        If TextInput::Settings::signedNumbers is false,
        read() does not distinguish between a plus or minus symbol next
        to a number and a positive/negative number itself.  For example, "x - 1" and "x -1"
        will be parsed the same way by read().  
        
        In both cases, readNumber() will contract a leading "-" or "+" onto
        a number.
    */
    Token read();

    /** Calls read() until the result is not a newline or comment */
    Token readSignificant();

    /** Read one token (or possibly two) as a number or throws
        WrongTokenType, and returns the number.

        If the first token in the input is a number, it is returned directly.

        If TextInput::Settings::signedNumbers is false and the input stream
        contains a '+' or '-' symbol token immediately followed by a number
        token, both tokens will be consumed and a single token will be
        returned by this method.

        WrongTokenType will be thrown if one of the input conditions
        described above is not satisfied.  When an exception is thrown, no
        tokens are consumed.
    */
    double readNumber();

    bool readBoolean();

    /** Reads a string token or throws WrongTokenType, and returns the token.

        Use this method (rather than readString) if you want the token's
        location as well as its value.

        WrongTokenType will be thrown if the next token in the input stream
        is not a string.  When an exception is thrown, no tokens are
        consumed.
    */
    Token readStringToken();

    /** Like readStringToken, but returns the token's string.

        Use this method (rather than readStringToken) if you want the token's
        value but don't really care about its location in the input.  Use of
        readStringToken is encouraged for better error reporting.
    */
    std::string readString();

    /** Reads a specific string token or throws either WrongTokenType or
        WrongString.  If the next token in the input is a string matching @p
        s, it will be consumed.

        Use this method if you want to match a specific string from the
        input.  In that case, typically error reporting related to the token
        is only going to occur because of a mismatch, so no location
        information is needed by the caller.

        WrongTokenType will be thrown if the next token in the input stream
        is not a string.  WrongString will be thrown if the next token in the
        input stream is a string but does not match the @p s parameter.  When
        an exception is thrown, no tokens are consumed.

        \sa readString(), readStringToken(), readUntilNewlineAsString()
      */
    void readString(const std::string& s);

    /** Read from the beginning of the next token until the following newline 
      and return the result as a string, ignoring all parsing in between. The newline 
      is not returned in the string, and the following token read will be a newline or
      end of file token (if they are enabled for parsing).*/
    std::string readUntilNewlineAsString();

    /** Reads a comment token or throws WrongTokenType, and returns the token.

        Use this method (rather than readComment) if you want the token's
        location as well as its value.

        WrongTokenType will be thrown if the next token in the input stream
        is not a comment.  When an exception is thrown, no tokens are
        consumed.
    */
    Token readCommentToken();

    /** Like readCommentToken, but returns the token's string.

        Use this method (rather than readCommentToken) if you want the token's
        value but don't really care about its location in the input.  Use of
        readCommentToken is encouraged for better error reporting.
    */
    std::string readComment();

    /** Reads a specific comment token or throws either WrongTokenType or
        WrongString.  If the next token in the input is a comment matching @p
        s, it will be consumed.

        Use this method if you want to match a specific comment from the
        input.  In that case, typically error reporting related to the token
        is only going to occur because of a mismatch, so no location
        information is needed by the caller.

        WrongTokenType will be thrown if the next token in the input stream
        is not a comment.  WrongString will be thrown if the next token in the
        input stream is a comment but does not match the @p s parameter.  When
        an exception is thrown, no tokens are consumed.
      */
     void readComment(const std::string& s);

    /** Reads a newline token or throws WrongTokenType, and returns the token.

        Use this method (rather than readNewline) if you want the token's
        location as well as its value.

        WrongTokenType will be thrown if the next token in the input stream
        is not a newline.  When an exception is thrown, no tokens are
        consumed.
    */
    Token readNewlineToken();

    /** Like readNewlineToken, but returns the token's string.

        Use this method (rather than readNewlineToken) if you want the token's
        value but don't really care about its location in the input.  Use of
        readNewlineToken is encouraged for better error reporting.
    */
    std::string readNewline();

    /** Reads a specific newline token or throws either WrongTokenType or
        WrongString.  If the next token in the input is a newline matching @p
        s, it will be consumed.

        Use this method if you want to match a specific newline from the
        input.  In that case, typically error reporting related to the token
        is only going to occur because of a mismatch, so no location
        information is needed by the caller.

        WrongTokenType will be thrown if the next token in the input stream
        is not a newline.  WrongString will be thrown if the next token in the
        input stream is a newlin but does not match the @p s parameter.  When
        an exception is thrown, no tokens are consumed.
      */
     void readNewline(const std::string& s);

    /** Reads a symbol token or throws WrongTokenType, and returns the token.

        Use this method (rather than readSymbol) if you want the token's
        location as well as its value.

        WrongTokenType will be thrown if the next token in the input stream
        is not a symbol.  When an exception is thrown, no tokens are
        consumed.
    */
    Token readSymbolToken();

    /** Like readSymbolToken, but returns the token's string.

        Use this method (rather than readSymbolToken) if you want the token's
        value but don't really care about its location in the input.  Use of
        readSymbolToken is encouraged for better error reporting.
    */
    std::string readSymbol();

    /** Reads a specific symbol token or throws either WrongTokenType or
        WrongSymbol.  If the next token in the input is a symbol matching @p
        symbol, it will be consumed.

        Use this method if you want to match a specific symbol from the
        input.  In that case, typically error reporting related to the token
        is only going to occur because of a mismatch, so no location
        information is needed by the caller.

        WrongTokenType will be thrown if the next token in the input stream
        is not a symbol.  WrongSymbol will be thrown if the next token in the
        input stream is a symbol but does not match the @p symbol parameter.
        When an exception is thrown, no tokens are consumed.
    */
    void readSymbol(const std::string& symbol);


    /** Read a series of two specific symbols.  See readSymbol.  */
    void readSymbols(const std::string& s1, const std::string& s2) {
        readSymbol(s1);
        readSymbol(s2);
    }

    /** Read a series of three specific symbols.  See readSymbol.  */
    void readSymbols(
        const std::string& s1, 
        const std::string& s2, 
        const std::string& s3) {
        readSymbol(s1);
        readSymbol(s2);
        readSymbol(s3);
    }

    /** Read a series of four specific symbols.  See readSymbol.  */
    void readSymbols(
        const std::string& s1, 
        const std::string& s2, 
        const std::string& s3,     
        const std::string& s4) {
        readSymbol(s1);
        readSymbol(s2);
        readSymbol(s3);
        readSymbol(s4);
    }

    /** Return a copy of the next token in the input stream, but don't remove
        it from the input stream.
    */
    Token peek();

    /** Returns the line number for the @e next token.  See also peek.  */
    int peekLineNumber();

    /** Returns the character number (relative to the line) for the @e next
        token in the input stream.  See also peek.
    */
    int peekCharacterNumber();

    /** Take a previously read token and push it back at the front of the
        input stream.

        Can be used in the case where more than one token of read-ahead is
        needed (i.e., when peek doesn't suffice).
    */
    void push(const Token& t);

    /** Returns the filename from which this input is drawn, or the first few
        characters of the string if created from a string.
        If settings::filename is non-empty that will replace the
        true filename.*/
    const std::string& filename() const;
};

void deserialize(bool& b, TextInput& ti);
void deserialize(int& b, TextInput& ti);
void deserialize(uint8& b, TextInput& ti);
void deserialize(double& b, TextInput& ti);
void deserialize(float& b, TextInput& ti);
void deserialize(std::string& b, TextInput& ti);

} // namespace

#endif