summaryrefslogtreecommitdiff
path: root/source/slang/lexer.h
blob: 23eddf04e303b7a32e9685893e4e31cad805cbb3 (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
#ifndef RASTER_RENDERER_LEXER_H
#define RASTER_RENDERER_LEXER_H

#include "../core/basic.h"
#include "diagnostics.h"

namespace Slang
{
    struct NamePool;

    //

    struct TokenList
    {
        Token* begin() const;
        Token* end() const;

        List<Token> mTokens;
    };

    struct TokenSpan
    {
        TokenSpan();
        TokenSpan(
            TokenList const& tokenList)
            : mBegin(tokenList.begin())
            , mEnd  (tokenList.end  ())
        {}

        Token* begin() const { return mBegin; }
        Token* end  () const { return mEnd  ; }

        int GetCount() { return (int)(mEnd - mBegin); }

        Token* mBegin;
        Token* mEnd;
    };

    struct TokenReader
    {
        TokenReader();
        explicit TokenReader(TokenSpan const& tokens)
            : mCursor(tokens.begin())
            , mEnd   (tokens.end  ())
        {}
        explicit TokenReader(TokenList const& tokens)
            : mCursor(tokens.begin())
            , mEnd   (tokens.end  ())
        {}

        bool IsAtEnd() const { return mCursor == mEnd; }
        Token PeekToken() const;
        TokenType PeekTokenType() const;
        SourceLoc PeekLoc() const;

        Token AdvanceToken();

        int GetCount() { return (int)(mEnd - mCursor); }

        Token* mCursor;
        Token* mEnd;
    };

    typedef unsigned int LexerFlags;
    enum
    {
        kLexerFlag_InDirective      = 1 << 0,
        kLexerFlag_ExpectFileName   = 1 << 1,
    };

    struct Lexer
    {
        void initialize(
            SourceFile*     sourceFile,
            DiagnosticSink* sink,
            NamePool*       namePool);

        ~Lexer();

        Token lexToken();

        TokenList lexAllTokens();

        // Begin overriding the reported locations of tokens,
        // based on a `#line` directives
        void startOverridingSourceLocations(SourceLoc loc);

        // Stop overriding source locations, and go back
        // to reporting source locations in the original file
        void stopOverridingSourceLocations();

        SourceFile*     sourceFile;
        DiagnosticSink* sink;
        NamePool*       namePool;

        char const*     cursor;

        char const*     begin;
        char const*     end;

        // The starting source location for the code as written,
        // which cannot be overridden.
        SourceLoc       spellingStartLoc;

        // The nominal starting location for the file, taking
        // any active `#line` directive into account.
        SourceLoc       presumedStartLoc;

        TokenFlags      tokenFlags;
        LexerFlags      lexerFlags;
    };

    // Helper routines for extracting values from tokens
    String getStringLiteralTokenValue(Token const& token);
    String getFileNameTokenValue(Token const& token);

    typedef int64_t IntegerLiteralValue;
    typedef double FloatingPointLiteralValue;

    IntegerLiteralValue getIntegerLiteralValue(Token const& token, String* outSuffix = 0);
    FloatingPointLiteralValue getFloatingPointLiteralValue(Token const& token, String* outSuffix = 0);
}

#endif