-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileLexer.h
More file actions
209 lines (184 loc) · 5.54 KB
/
Copy pathFileLexer.h
File metadata and controls
209 lines (184 loc) · 5.54 KB
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
#ifndef FILE_LEXER_H_
#define FILE_LEXER_H_
#include <cstdint>
#include <fstream>
#include <string>
#include <vector>
#include "SourceLocation.h"
// Character source for the Lexer. The base class reads from a file; the
// members Lexer uses are virtual so alternative sources (the LSP server's
// StringLexerReader over an in-memory buffer) can substitute without touching
// the Lexer itself.
class LexerReader
{
public:
LexerReader( const std::string &filename );
virtual ~LexerReader() {}
virtual char operator[]( int i );
virtual char popChar();
virtual void popChar( int count );
virtual char peekChar();
virtual bool isEOF();
// Position of the next unconsumed character (1-based).
virtual const std::string &getFileName() const { return mFileName; }
virtual uint32_t getLine() const { return mLine; }
virtual uint32_t getCol() const { return mCol; }
protected:
// Shared by subclasses that track their own position.
LexerReader() {}
std::ifstream mFile;
std::string mFileName;
uint32_t mLine = 1;
uint32_t mCol = 1;
};
class Lexer
{
public:
Lexer( LexerReader *reader );
int peekSymbol();
int getSymbol();
const std::string &getSymbolText();
int getCurrentPos();
void setCurrentPos( int pos );
enum LexerSymbols {
BUILTIN_TYPE,
BOOL,
TYPE_MODIFIER,
KEYWORD_ELSE,
KEYWORD_FOR,
KEYWORD_IF,
KEYWORD_WHILE,
KEYWORD_RETURN,
KEYWORD_FN,
KEYWORD_STRUCT,
KEYWORD_IMPL,
KEYWORD_SELF,
KEYWORD_PROTOCOL,
KEYWORD_MATCH,
KEYWORD_IMPORT,
KEYWORD_PUB,
KEYWORD_BREAK,
KEYWORD_CONTINUE,
KEYWORD_ENUM,
KEYWORD_IN,
VOID,
SYMBOL,
CONSTANT_STRING,
CONSTANT_CHAR,
CONSTANT_NUMBER,
CONSTANT_FLOAT,
CONSTANT_BOOL,
LOR,
LAND,
EQ,
ASSIGN,
SHIFT,
ELLIPSIS,
RANGE,
ARROW,
WILDCARD,
QUESTION_MARK,
// Phase 2 keywords start at 256 to avoid collision with ASCII
// operator characters (e.g., '-' = 45 would collide with enum value 45)
KEYWORD_OWN = 256,
KEYWORD_SHARED,
KEYWORD_SYNC,
KEYWORD_SPAWN,
KEYWORD_CHAN,
KEYWORD_ASYNC,
KEYWORD_AWAIT,
KEYWORD_ON,
KEYWORD_REQUIRES,
KEYWORD_ENSURES,
KEYWORD_TEST,
KEYWORD_ASSERT,
// Phase 3 tokens and keywords
PIPE_ARROW, // |>
AT_SIGN, // @
KEYWORD_TABLE,
KEYWORD_QUERY,
KEYWORD_INSERT,
KEYWORD_UPDATE,
KEYWORD_DELETE,
KEYWORD_WAIT,
KEYWORD_WAIT_ALL,
KEYWORD_CSTRING,
KEYWORD_CARRAY,
KEYWORD_STATIC,
KEYWORD_INIT,
NUM_SYMBOLS
};
// End-of-input from the PARSER's point of view: true only when no token
// remains at the current parse position. This must be cursor-aware, not
// just mReader->isEOF(): after setCurrentPos() rewinds into already-scanned
// tokens (deferred function-body parsing for forward references), the
// underlying reader is at file-EOF while unconsumed buffered tokens remain.
bool isEOF()
{
if ( mLastSym != -1 )
return false; // a peeked token is pending
if ( (size_t)mCurrentPos < mSymbolList.size() )
return mSymbolList[ mCurrentPos ].symbol == -1;
return mReader->isEOF();
}
// If the token at the current parse position is SHIFT spelled ">>", split
// it in place into two '>' tokens (adjacent columns). The type parser calls
// this when a nested generic type-argument list closes with ">>"
// (Array<Array<int>>), which the lexer otherwise tokenizes as right-shift.
// The split rewrites the symbol-replay list, so it persists across
// setCurrentPos backtracking — harmless, because the split only fires while
// parsing a type-argument list, where '>' '>' and '>>' are interchangeable
// spellings of the same close-brackets.
void splitShiftIntoCloseAngles();
// Line/column/location of the token at the current parse position — the
// token the parser is about to consume. Accurate after setCurrentPos
// backtracking because positions are frozen into the symbol list at
// scan time. getLineNumber()/getLinePosition() delegate here so error
// reporting is token-accurate rather than reflecting file read-ahead.
SourceLocation getTokenLocation();
const std::string &getFileName() const { return mReader->getFileName(); }
uint32_t getLineNumber() { return getTokenLocation().line; }
uint32_t getLinePosition() { return getTokenLocation().col; }
// Gate the per-token diagnostic echo (off = quiet). Default preserves
// the historical behavior; qcc disables it for --dump-locations.
void setTraceEnabled( bool enabled ) { mTraceEnabled = enabled; }
protected:
bool match( const char *match_str );
bool matchKeyword( const char *match_str );
void readSymbol();
void readStringConst();
void readCharacterConst();
bool readConst();
void handleComment( bool singleLine );
bool isAlpha( char c );
bool isAlphaNum( char c );
int getSymbolInternal();
int getSymbolFromFile();
private:
LexerReader *mReader;
std::string mMatchString;
int mLastSym;
uint32_t lineno;
uint32_t charPos;
// Quiet by default: the per-token "Symbol …" trace is opt-in via -v
// (driver calls setTraceEnabled). Defaulting to false keeps any Lexer
// constructed without explicit configuration silent (R5 / FR-007).
bool mTraceEnabled = false;
// Position of the first character of the token most recently scanned
// from the file; frozen into SymbolInfo so replay/backtracking returns
// exact positions.
uint32_t mScanLine = 1;
uint32_t mScanCol = 1;
struct SymbolInfo
{
SymbolInfo( int s, std::string &str, uint32_t l, uint32_t c ) :
symbol( s ), symbolText( str ), line( l ), col( c ) {}
int symbol;
std::string symbolText;
uint32_t line;
uint32_t col;
};
std::vector<SymbolInfo> mSymbolList;
int mCurrentPos;
};
#endif // FILE_LEXER_H_