aboutsummaryrefslogtreecommitdiffstats
path: root/libsolidity/parsing/Scanner.cpp
blob: 6541f6c2ff5bbac19fc093fd676bea5c4d328135 (plain) (blame)
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
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
/*
    This file is part of solidity.

    solidity is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    solidity is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with solidity.  If not, see <http://www.gnu.org/licenses/>.

    This file is derived from the file "scanner.cc", which was part of the
    V8 project. The original copyright header follows:

    Copyright 2006-2012, the V8 project authors. All rights reserved.
    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 Google Inc. 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 Christian <c@ethdev.com>
 * @date 2014
 * Solidity scanner.
 */

#include <algorithm>
#include <tuple>
#include <libsolidity/interface/Exceptions.h>
#include <libsolidity/parsing/Scanner.h>

using namespace std;

namespace dev
{
namespace solidity
{

namespace
{
bool isDecimalDigit(char c)
{
    return '0' <= c && c <= '9';
}
bool isHexDigit(char c)
{
    return isDecimalDigit(c)
           || ('a' <= c && c <= 'f')
           || ('A' <= c && c <= 'F');
}
bool isLineTerminator(char c)
{
    return c == '\n';
}
bool isWhiteSpace(char c)
{
    return c == ' ' || c == '\n' || c == '\t' || c == '\r';
}
bool isIdentifierStart(char c)
{
    return c == '_' || c == '$' || ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z');
}
bool isIdentifierPart(char c)
{
    return isIdentifierStart(c) || isDecimalDigit(c);
}
int hexValue(char c)
{
    if (c >= '0' && c <= '9')
        return c - '0';
    else if (c >= 'a' && c <= 'f')
        return c - 'a' + 10;
    else if (c >= 'A' && c <= 'F')
        return c - 'A' + 10;
    else return -1;
}
} // end anonymous namespace



/// Scoped helper for literal recording. Automatically drops the literal
/// if aborting the scanning before it's complete.
enum LiteralType {
    LITERAL_TYPE_STRING,
    LITERAL_TYPE_NUMBER, // not really different from string type in behaviour
    LITERAL_TYPE_COMMENT
};

class LiteralScope
{
public:
    explicit LiteralScope(Scanner* _self, enum LiteralType _type): m_type(_type)
    , m_scanner(_self)
    , m_complete(false)
    {
        if (_type == LITERAL_TYPE_COMMENT)
            m_scanner->m_nextSkippedComment.literal.clear();
        else
            m_scanner->m_nextToken.literal.clear();
    }
    ~LiteralScope()
    {
        if (!m_complete)
        {
            if (m_type == LITERAL_TYPE_COMMENT)
                m_scanner->m_nextSkippedComment.literal.clear();
            else
                m_scanner->m_nextToken.literal.clear();
        }
    }
    void complete() { m_complete = true; }

private:
    enum LiteralType m_type;
    Scanner* m_scanner;
    bool m_complete;
}; // end of LiteralScope class


void Scanner::reset(CharStream const& _source, string const& _sourceName)
{
    m_source = _source;
    m_sourceName = make_shared<string const>(_sourceName);
    reset();
}

void Scanner::reset()
{
    m_source.reset();
    m_char = m_source.get();
    skipWhitespace();
    scanToken();
    next();
}

bool Scanner::scanHexByte(char& o_scannedByte)
{
    char x = 0;
    for (int i = 0; i < 2; i++)
    {
        int d = hexValue(m_char);
        if (d < 0)
        {
            rollback(i);
            return false;
        }
        x = x * 16 + d;
        advance();
    }
    o_scannedByte = x;
    return true;
}

bool Scanner::scanUnicode(unsigned & o_codepoint)
{
    unsigned x = 0;
    for (int i = 0; i < 4; i++)
    {
        int d = hexValue(m_char);
        if (d < 0)
        {
            rollback(i);
            return false;
        }
        x = x * 16 + d;
        advance();
    }
    o_codepoint = x;
    return true;
}

// This supports codepoints between 0000 and FFFF.
void Scanner::addUnicodeAsUTF8(unsigned codepoint)
{
    if (codepoint <= 0x7f)
        addLiteralChar(codepoint);
    else if (codepoint <= 0x7ff)
    {
        addLiteralChar(0xc0 | (codepoint >> 6));
        addLiteralChar(0x80 | (codepoint & 0x3f));
    }
    else
    {
        addLiteralChar(0xe0 | (codepoint >> 12));
        addLiteralChar(0x80 | ((codepoint >> 6) & 0x3f));
        addLiteralChar(0x80 | (codepoint & 0x3f));
    }
}

// Ensure that tokens can be stored in a byte.
BOOST_STATIC_ASSERT(Token::NUM_TOKENS <= 0x100);

Token::Value Scanner::next()
{
    m_currentToken = m_nextToken;
    m_skippedComment = m_nextSkippedComment;
    scanToken();

    return m_currentToken.token;
}

Token::Value Scanner::selectToken(char _next, Token::Value _then, Token::Value _else)
{
    advance();
    if (m_char == _next)
        return selectToken(_then);
    else
        return _else;
}

bool Scanner::skipWhitespace()
{
    int const startPosition = sourcePos();
    while (isWhiteSpace(m_char))
        advance();
    // Return whether or not we skipped any characters.
    return sourcePos() != startPosition;
}

bool Scanner::skipWhitespaceExceptLF()
{
    int const startPosition = sourcePos();
    while (isWhiteSpace(m_char) && !isLineTerminator(m_char))
        advance();
    // Return whether or not we skipped any characters.
    return sourcePos() != startPosition;
}

Token::Value Scanner::skipSingleLineComment()
{
    // The line terminator at the end of the line is not considered
    // to be part of the single-line comment; it is recognized
    // separately by the lexical grammar and becomes part of the
    // stream of input elements for the syntactic grammar
    while (!isLineTerminator(m_char))
        if (!advance()) break;

    return Token::Whitespace;
}

Token::Value Scanner::scanSingleLineDocComment()
{
    LiteralScope literal(this, LITERAL_TYPE_COMMENT);
    advance(); //consume the last '/' at ///
    skipWhitespaceExceptLF();
    while (!isSourcePastEndOfInput())
    {
        if (isLineTerminator(m_char))
        {
            // check if next line is also a documentation comment
            skipWhitespace();
            if (!m_source.isPastEndOfInput(3) &&
                m_source.get(0) == '/' &&
                m_source.get(1) == '/' &&
                m_source.get(2) == '/')
            {
                addCommentLiteralChar('\n');
                m_char = m_source.advanceAndGet(3);
            }
            else
                break; // next line is not a documentation comment, we are done

        }
        addCommentLiteralChar(m_char);
        advance();
    }
    literal.complete();
    return Token::CommentLiteral;
}

Token::Value Scanner::skipMultiLineComment()
{
    advance();
    while (!isSourcePastEndOfInput())
    {
        char ch = m_char;
        advance();

        // If we have reached the end of the multi-line comment, we
        // consume the '/' and insert a whitespace. This way all
        // multi-line comments are treated as whitespace.
        if (ch == '*' && m_char == '/')
        {
            m_char = ' ';
            return Token::Whitespace;
        }
    }
    // Unterminated multi-line comment.
    return Token::Illegal;
}

Token::Value Scanner::scanMultiLineDocComment()
{
    LiteralScope literal(this, LITERAL_TYPE_COMMENT);
    bool endFound = false;
    bool charsAdded = false;

    while (!isSourcePastEndOfInput())
    {
        //handle newlines in multline comments
        if (isLineTerminator(m_char))
        {
            skipWhitespace();
            if (!m_source.isPastEndOfInput(1) && m_source.get(0) == '*' && m_source.get(1) == '*')
            { // it is unknown if this leads to the end of the comment
                addCommentLiteralChar('*');
                advance();
            }
            else if (!m_source.isPastEndOfInput(1) && m_source.get(0) == '*' && m_source.get(1) != '/')
            { // skip first '*' in subsequent lines
                if (charsAdded)
                    addCommentLiteralChar('\n');
                m_char = m_source.advanceAndGet(2);
            }
            else if (!m_source.isPastEndOfInput(1) && m_source.get(0) == '*' && m_source.get(1) == '/')
            { // if after newline the comment ends, don't insert the newline
                m_char = m_source.advanceAndGet(2);
                endFound = true;
                break;
            }
            else if (charsAdded)
                addCommentLiteralChar('\n');
        }

        if (!m_source.isPastEndOfInput(1) && m_source.get(0) == '*' && m_source.get(1) == '/')
        {
            m_char = m_source.advanceAndGet(2);
            endFound = true;
            break;
        }
        addCommentLiteralChar(m_char);
        charsAdded = true;
        advance();
    }
    literal.complete();
    if (!endFound)
        return Token::Illegal;
    else
        return Token::CommentLiteral;
}

Token::Value Scanner::scanSlash()
{
    int firstSlashPosition = sourcePos();
    advance();
    if (m_char == '/')
    {
        if (!advance()) /* double slash comment directly before EOS */
            return  Token::Whitespace;
        else if (m_char == '/')
        {
            // doxygen style /// comment
            Token::Value comment;
            m_nextSkippedComment.location.start = firstSlashPosition;
            comment = scanSingleLineDocComment();
            m_nextSkippedComment.location.end = sourcePos();
            m_nextSkippedComment.token = comment;
            return Token::Whitespace;
        }
        else
            return skipSingleLineComment();
    }
    else if (m_char == '*')
    {
        // doxygen style /** natspec comment
        if (!advance()) /* slash star comment before EOS */
            return Token::Whitespace;
        else if (m_char == '*')
        {
            advance(); //consume the last '*' at /**
            skipWhitespaceExceptLF();

            // special case of a closed normal multiline comment
            if (!m_source.isPastEndOfInput() && m_source.get(0) == '/')
                advance(); //skip the closing slash
            else // we actually have a multiline documentation comment
            {
                Token::Value comment;
                m_nextSkippedComment.location.start = firstSlashPosition;
                comment = scanMultiLineDocComment();
                m_nextSkippedComment.location.end = sourcePos();
                m_nextSkippedComment.token = comment;
            }
            return Token::Whitespace;
        }
        else
            return skipMultiLineComment();
    }
    else if (m_char == '=')
        return selectToken(Token::AssignDiv);
    else
        return Token::Div;
}

void Scanner::scanToken()
{
    m_nextToken.literal.clear();
    m_nextToken.extendedTokenInfo = make_tuple(0, 0);
    m_nextSkippedComment.literal.clear();
    m_nextSkippedComment.extendedTokenInfo = make_tuple(0, 0);

    Token::Value token;
    // M and N are for the purposes of grabbing different type sizes
    unsigned m;
    unsigned n;
    do
    {
        // Remember the position of the next token
        m_nextToken.location.start = sourcePos();
        switch (m_char)
        {
        case '\n':
        case ' ':
        case '\t':
            token = selectToken(Token::Whitespace);
            break;
        case '"':
        case '\'':
            token = scanString();
            break;
        case '<':
            // < <= << <<=
            advance();
            if (m_char == '=')
                token = selectToken(Token::LessThanOrEqual);
            else if (m_char == '<')
                token = selectToken('=', Token::AssignShl, Token::SHL);
            else
                token = Token::LessThan;
            break;
        case '>':
            // > >= >> >>= >>> >>>=
            advance();
            if (m_char == '=')
                token = selectToken(Token::GreaterThanOrEqual);
            else if (m_char == '>')
            {
                // >> >>= >>> >>>=
                advance();
                if (m_char == '=')
                    token = selectToken(Token::AssignSar);
                else if (m_char == '>')
                    token = selectToken('=', Token::AssignShr, Token::SHR);
                else
                    token = Token::SAR;
            }
            else
                token = Token::GreaterThan;
            break;
        case '=':
            // = == =>
            advance();
            if (m_char == '=')
                token = selectToken(Token::Equal);
            else if (m_char == '>')
                token = selectToken(Token::Arrow);
            else
                token = Token::Assign;
            break;
        case '!':
            // ! !=
            advance();
            if (m_char == '=')
                token = selectToken(Token::NotEqual);
            else
                token = Token::Not;
            break;
        case '+':
            // + ++ +=
            advance();
            if (m_char == '+')
                token = selectToken(Token::Inc);
            else if (m_char == '=')
                token = selectToken(Token::AssignAdd);
            else
                token = Token::Add;
            break;
        case '-':
            // - -- -=
            advance();
            if (m_char == '-')
                token = selectToken(Token::Dec);
            else if (m_char == '=')
                token = selectToken(Token::AssignSub);
            else
                token = Token::Sub;
            break;
        case '*':
            // * ** *=
            advance();
            if (m_char == '*')
                token = selectToken(Token::Exp);
            else if (m_char == '=')
                token = selectToken(Token::AssignMul);
            else
                token = Token::Mul;
            break;
        case '%':
            // % %=
            token = selectToken('=', Token::AssignMod, Token::Mod);
            break;
        case '/':
            // /  // /* /=
            token = scanSlash();
            break;
        case '&':
            // & && &=
            advance();
            if (m_char == '&')
                token = selectToken(Token::And);
            else if (m_char == '=')
                token = selectToken(Token::AssignBitAnd);
            else
                token = Token::BitAnd;
            break;
        case '|':
            // | || |=
            advance();
            if (m_char == '|')
                token = selectToken(Token::Or);
            else if (m_char == '=')
                token = selectToken(Token::AssignBitOr);
            else
                token = Token::BitOr;
            break;
        case '^':
            // ^ ^=
            token = selectToken('=', Token::AssignBitXor, Token::BitXor);
            break;
        case '.':
            // . Number
            advance();
            if (isDecimalDigit(m_char))
                token = scanNumber('.');
            else
                token = Token::Period;
            break;
        case ':':
            token = selectToken(Token::Colon);
            break;
        case ';':
            token = selectToken(Token::Semicolon);
            break;
        case ',':
            token = selectToken(Token::Comma);
            break;
        case '(':
            token = selectToken(Token::LParen);
            break;
        case ')':
            token = selectToken(Token::RParen);
            break;
        case '[':
            token = selectToken(Token::LBrack);
            break;
        case ']':
            token = selectToken(Token::RBrack);
            break;
        case '{':
            token = selectToken(Token::LBrace);
            break;
        case '}':
            token = selectToken(Token::RBrace);
            break;
        case '?':
            token = selectToken(Token::Conditional);
            break;
        case '~':
            token = selectToken(Token::BitNot);
            break;
        default:
            if (isIdentifierStart(m_char))
            {
                tie(token, m, n) = scanIdentifierOrKeyword();

                // Special case for hexademical literals
                if (token == Token::Hex)
                {
                    // reset
                    m = 0;
                    n = 0;

                    // Special quoted hex string must follow
                    if (m_char == '"' || m_char == '\'')
                        token = scanHexString();
                    else
                        token = Token::Illegal;
                }
            }
            else if (isDecimalDigit(m_char))
                token = scanNumber();
            else if (skipWhitespace())
                token = Token::Whitespace;
            else if (isSourcePastEndOfInput())
                token = Token::EOS;
            else
                token = selectToken(Token::Illegal);
            break;
        }
        // Continue scanning for tokens as long as we're just skipping
        // whitespace.
    }
    while (token == Token::Whitespace);
    m_nextToken.location.end = sourcePos();
    m_nextToken.token = token;
    m_nextToken.extendedTokenInfo = make_tuple(m, n);
}

bool Scanner::scanEscape()
{
    char c = m_char;
    advance();
    // Skip escaped newlines.
    if (isLineTerminator(c))
        return true;
    switch (c)
    {
    case '\'':  // fall through
    case '"':  // fall through
    case '\\':
        break;
    case 'b':
        c = '\b';
        break;
    case 'f':
        c = '\f';
        break;
    case 'n':
        c = '\n';
        break;
    case 'r':
        c = '\r';
        break;
    case 't':
        c = '\t';
        break;
    case 'v':
        c = '\v';
        break;
    case 'u':
    {
        unsigned codepoint;
        if (!scanUnicode(codepoint))
            return false;
        addUnicodeAsUTF8(codepoint);
        return true;
    }
    case 'x':
        if (!scanHexByte(c))
            return false;
        break;
    }

    addLiteralChar(c);
    return true;
}

Token::Value Scanner::scanString()
{
    char const quote = m_char;
    advance();  // consume quote
    LiteralScope literal(this, LITERAL_TYPE_STRING);
    while (m_char != quote && !isSourcePastEndOfInput() && !isLineTerminator(m_char))
    {
        char c = m_char;
        advance();
        if (c == '\\')
        {
            if (isSourcePastEndOfInput() || !scanEscape())
                return Token::Illegal;
        }
        else
            addLiteralChar(c);
    }
    if (m_char != quote)
        return Token::Illegal;
    literal.complete();
    advance();  // consume quote
    return Token::StringLiteral;
}

Token::Value Scanner::scanHexString()
{
    char const quote = m_char;
    advance();  // consume quote
    LiteralScope literal(this, LITERAL_TYPE_STRING);
    while (m_char != quote && !isSourcePastEndOfInput() && !isLineTerminator(m_char))
    {
        char c = m_char;
        if (!scanHexByte(c))
            return Token::Illegal;
        addLiteralChar(c);
    }
    if (m_char != quote)
        return Token::Illegal;
    literal.complete();
    advance();  // consume quote
    return Token::StringLiteral;
}

void Scanner::scanDecimalDigits()
{
    while (isDecimalDigit(m_char))
        addLiteralCharAndAdvance();
}

Token::Value Scanner::scanNumber(char _charSeen)
{
    enum { DECIMAL, HEX, BINARY } kind = DECIMAL;
    LiteralScope literal(this, LITERAL_TYPE_NUMBER);
    if (_charSeen == '.')
    {
        // we have already seen a decimal point of the float
        addLiteralChar('.');
        scanDecimalDigits();  // we know we have at least one digit
    }
    else
    {
        solAssert(_charSeen == 0, "");
        // if the first character is '0' we must check for octals and hex
        if (m_char == '0')
        {
            addLiteralCharAndAdvance();
            // either 0, 0exxx, 0Exxx, 0.xxx or a hex number
            if (m_char == 'x' || m_char == 'X')
            {
                // hex number
                kind = HEX;
                addLiteralCharAndAdvance();
                if (!isHexDigit(m_char))
                    return Token::Illegal; // we must have at least one hex digit after 'x'/'X'
                while (isHexDigit(m_char))
                    addLiteralCharAndAdvance();
            }
            else if (isDecimalDigit(m_char))
                // We do not allow octal numbers
                return Token::Illegal;
        }
        // Parse decimal digits and allow trailing fractional part.
        if (kind == DECIMAL)
        {
            scanDecimalDigits();  // optional
            if (m_char == '.')
            {
                addLiteralCharAndAdvance();
                scanDecimalDigits();  // optional
            }
        }
    }
    // scan exponent, if any
    if (m_char == 'e' || m_char == 'E')
    {
        solAssert(kind != HEX, "'e'/'E' must be scanned as part of the hex number");
        if (kind != DECIMAL)
            return Token::Illegal;
        // scan exponent
        addLiteralCharAndAdvance();
        if (m_char == '+' || m_char == '-')
            addLiteralCharAndAdvance();
        if (!isDecimalDigit(m_char))
            return Token::Illegal; // we must have at least one decimal digit after 'e'/'E'
        scanDecimalDigits();
    }
    // The source character immediately following a numeric literal must
    // not be an identifier start or a decimal digit; see ECMA-262
    // section 7.8.3, page 17 (note that we read only one decimal digit
    // if the value is 0).
    if (isDecimalDigit(m_char) || isIdentifierStart(m_char))
        return Token::Illegal;
    literal.complete();
    return Token::Number;
}

tuple<Token::Value, unsigned, unsigned> Scanner::scanIdentifierOrKeyword()
{
    solAssert(isIdentifierStart(m_char), "");
    LiteralScope literal(this, LITERAL_TYPE_STRING);
    addLiteralCharAndAdvance();
    // Scan the rest of the identifier characters.
    while (isIdentifierPart(m_char)) //get full literal
        addLiteralCharAndAdvance();
    literal.complete();
    return Token::fromIdentifierOrKeyword(m_nextToken.literal);
}

char CharStream::advanceAndGet(size_t _chars)
{
    if (isPastEndOfInput())
        return 0;
    m_position += _chars;
    if (isPastEndOfInput())
        return 0;
    return m_source[m_position];
}

char CharStream::rollback(size_t _amount)
{
    solAssert(m_position >= _amount, "");
    m_position -= _amount;
    return get();
}

string CharStream::lineAtPosition(int _position) const
{
    // if _position points to \n, it returns the line before the \n
    using size_type = string::size_type;
    size_type searchStart = min<size_type>(m_source.size(), _position);
    if (searchStart > 0)
        searchStart--;
    size_type lineStart = m_source.rfind('\n', searchStart);
    if (lineStart == string::npos)
        lineStart = 0;
    else
        lineStart++;
    return m_source.substr(lineStart, min(m_source.find('\n', lineStart),
                                          m_source.size()) - lineStart);
}

tuple<int, int> CharStream::translatePositionToLineColumn(int _position) const
{
    using size_type = string::size_type;
    size_type searchPosition = min<size_type>(m_source.size(), _position);
    int lineNumber = count(m_source.begin(), m_source.begin() + searchPosition, '\n');
    size_type lineStart;
    if (searchPosition == 0)
        lineStart = 0;
    else
    {
        lineStart = m_source.rfind('\n', searchPosition - 1);
        lineStart = lineStart == string::npos ? 0 : lineStart + 1;
    }
    return tuple<int, int>(lineNumber, searchPosition - lineStart);
}


}
}