37# include <gtest/gtest.h>
48 std::vector<Compiler_Token_Kind>
51 std::vector<Compiler_Token_Kind> kinds;
54 const auto tok = lexer.
next();
55 kinds.push_back(tok.kind);
56 if (tok.is_eof() or tok.is_invalid())
64TEST(CompilerLexer, LexesKeywordsIdentifiersNumbersAndSpans)
68 "fn add(x, y) { return x + 42; }\n");
72 const auto t0 = lexer.
next();
73 const auto t1 = lexer.
next();
74 const auto t2 = lexer.
next();
75 const auto t3 = lexer.
next();
76 const auto t4 = lexer.
next();
77 const auto t5 = lexer.
next();
78 const auto t6 = lexer.
next();
79 const auto t7 = lexer.
next();
80 const auto t8 = lexer.
next();
81 const auto t9 = lexer.
next();
82 const auto t10 = lexer.
next();
83 const auto t11 = lexer.
next();
84 const auto t12 = lexer.
next();
85 const auto t13 = lexer.
next();
86 const auto t14 = lexer.
next();
88 EXPECT_EQ(t0.kind, Compiler_Token_Kind::Kw_Fn);
89 EXPECT_EQ(t1.kind, Compiler_Token_Kind::Identifier);
90 EXPECT_EQ(t1.lexeme,
"add");
91 EXPECT_EQ(t1.span.begin, 3u);
92 EXPECT_EQ(t1.span.end, 6u);
93 EXPECT_EQ(t2.kind, Compiler_Token_Kind::LParen);
94 EXPECT_EQ(t3.kind, Compiler_Token_Kind::Identifier);
95 EXPECT_EQ(t3.lexeme,
"x");
96 EXPECT_EQ(t4.kind, Compiler_Token_Kind::Comma);
97 EXPECT_EQ(t5.kind, Compiler_Token_Kind::Identifier);
98 EXPECT_EQ(t5.lexeme,
"y");
99 EXPECT_EQ(t6.kind, Compiler_Token_Kind::RParen);
100 EXPECT_EQ(t7.kind, Compiler_Token_Kind::LBrace);
101 EXPECT_EQ(t8.kind, Compiler_Token_Kind::Kw_Return);
102 EXPECT_EQ(t9.kind, Compiler_Token_Kind::Identifier);
103 EXPECT_EQ(t10.kind, Compiler_Token_Kind::Plus);
104 EXPECT_EQ(t11.kind, Compiler_Token_Kind::Integer_Literal);
105 EXPECT_EQ(t11.lexeme,
"42");
106 EXPECT_EQ(t11.span.begin, 26u);
107 EXPECT_EQ(t11.span.end, 28u);
108 EXPECT_EQ(t12.kind, Compiler_Token_Kind::Semicolon);
109 EXPECT_EQ(t13.kind, Compiler_Token_Kind::RBrace);
110 EXPECT_TRUE(t14.is_eof());
116TEST(CompilerLexer, LexesNominalTypeDeclarationKeywords)
119 const auto id = sm.
add_virtual_file(
"types.aw",
"struct Point {} enum Color {} type UserId = Int;");
122 const auto t0 = lexer.
next();
123 const auto t1 = lexer.
next();
124 const auto t2 = lexer.
next();
125 const auto t3 = lexer.
next();
126 const auto t4 = lexer.
next();
127 const auto t5 = lexer.
next();
128 const auto t6 = lexer.
next();
129 const auto t7 = lexer.
next();
130 const auto t8 = lexer.
next();
131 const auto t9 = lexer.
next();
132 const auto t10 = lexer.
next();
133 const auto t11 = lexer.
next();
134 const auto t12 = lexer.
next();
136 EXPECT_EQ(t0.kind, Compiler_Token_Kind::Kw_Struct);
137 EXPECT_EQ(t1.kind, Compiler_Token_Kind::Identifier);
138 EXPECT_EQ(t4.kind, Compiler_Token_Kind::Kw_Enum);
139 EXPECT_EQ(t5.kind, Compiler_Token_Kind::Identifier);
140 EXPECT_EQ(t8.kind, Compiler_Token_Kind::Kw_Type);
141 EXPECT_EQ(t9.kind, Compiler_Token_Kind::Identifier);
142 EXPECT_EQ(t10.kind, Compiler_Token_Kind::Assign);
143 EXPECT_EQ(t11.kind, Compiler_Token_Kind::Identifier);
144 EXPECT_EQ(t12.kind, Compiler_Token_Kind::Semicolon);
148TEST(CompilerLexer, PeekDoesNotConsumeTheNextToken)
154 const auto look1 = lexer.
peek();
155 const auto look2 = lexer.
peek();
157 const auto ident = lexer.
next();
159 EXPECT_EQ(look1.kind, Compiler_Token_Kind::Kw_Let);
160 EXPECT_EQ(look1.lexeme,
"let");
161 EXPECT_EQ(look2.kind, Compiler_Token_Kind::Kw_Let);
162 EXPECT_EQ(
next.kind, Compiler_Token_Kind::Kw_Let);
163 EXPECT_EQ(ident.kind, Compiler_Token_Kind::Identifier);
164 EXPECT_EQ(ident.lexeme,
"value");
168TEST(CompilerLexer, SkipsCommentsByDefault)
172 "let x = 1; // note\n/* block */ return x;");
176 const auto kinds = collect_kinds(lexer);
177 const std::vector<Compiler_Token_Kind>
expected = {
178 Compiler_Token_Kind::Kw_Let,
179 Compiler_Token_Kind::Identifier,
180 Compiler_Token_Kind::Assign,
181 Compiler_Token_Kind::Integer_Literal,
182 Compiler_Token_Kind::Semicolon,
183 Compiler_Token_Kind::Kw_Return,
184 Compiler_Token_Kind::Identifier,
185 Compiler_Token_Kind::Semicolon,
186 Compiler_Token_Kind::End_Of_File
194TEST(CompilerLexer, KeepsCommentTokensWhenRequested)
202 const auto t0 = lexer.
next();
203 const auto t1 = lexer.
next();
204 const auto t2 = lexer.
next();
206 EXPECT_EQ(t0.kind, Compiler_Token_Kind::Line_Comment);
207 EXPECT_EQ(t0.lexeme,
"// note");
208 EXPECT_EQ(t1.kind, Compiler_Token_Kind::Block_Comment);
209 EXPECT_EQ(t1.lexeme,
"/* block */");
210 EXPECT_TRUE(t2.is_eof());
214TEST(CompilerLexer, CanDisableBlockCommentLexing)
222 const auto kinds = collect_kinds(lexer);
223 const std::vector<Compiler_Token_Kind>
expected = {
224 Compiler_Token_Kind::Slash,
225 Compiler_Token_Kind::Star,
226 Compiler_Token_Kind::Identifier,
227 Compiler_Token_Kind::Star,
228 Compiler_Token_Kind::Slash,
229 Compiler_Token_Kind::End_Of_File
236TEST(CompilerLexer, LexesStringsCharsAndCompoundOperators)
239 const auto id = sm.
add_virtual_file(
"main.aw",
"'a' \"hi\\n\" += == && ->");
243 const auto t0 = lexer.
next();
244 const auto t1 = lexer.
next();
245 const auto t2 = lexer.
next();
246 const auto t3 = lexer.
next();
247 const auto t4 = lexer.
next();
248 const auto t5 = lexer.
next();
249 const auto t6 = lexer.
next();
251 EXPECT_EQ(t0.kind, Compiler_Token_Kind::Char_Literal);
252 EXPECT_EQ(t0.lexeme,
"'a'");
253 EXPECT_EQ(t1.kind, Compiler_Token_Kind::String_Literal);
254 EXPECT_EQ(t1.lexeme,
"\"hi\\n\"");
255 EXPECT_EQ(t2.kind, Compiler_Token_Kind::PlusEq);
256 EXPECT_EQ(t3.kind, Compiler_Token_Kind::EqEq);
257 EXPECT_EQ(t4.kind, Compiler_Token_Kind::AndAnd);
258 EXPECT_EQ(t5.kind, Compiler_Token_Kind::Arrow);
259 EXPECT_TRUE(t6.is_eof());
265TEST(CompilerLexer, UnexpectedCharacterProducesInvalidTokenAndDiagnostic)
272 const auto tok = lexer.
next();
274 EXPECT_TRUE(tok.is_invalid());
275 EXPECT_EQ(tok.lexeme,
"@");
276 ASSERT_EQ(dx.
size(), 1u);
277 EXPECT_EQ(dx.
get(0).
code,
"LEX001");
278 EXPECT_EQ(dx.
get(0).
message,
"unexpected character '@'");
282TEST(CompilerLexer, UnterminatedStringProducesInvalidTokenAndDiagnostic)
289 const auto tok = lexer.
next();
291 EXPECT_TRUE(tok.is_invalid());
292 EXPECT_EQ(tok.lexeme,
"\"abc");
293 ASSERT_EQ(dx.
size(), 1u);
294 EXPECT_EQ(dx.
get(0).
code,
"LEX002");
295 EXPECT_EQ(dx.
get(0).
message,
"unterminated string literal");
299TEST(CompilerLexer, UnterminatedBlockCommentIsNotSilentlyDropped)
306 const auto tok = lexer.
next();
308 EXPECT_TRUE(tok.is_invalid());
309 EXPECT_EQ(tok.lexeme,
"/* block");
310 ASSERT_EQ(dx.
size(), 1u);
311 EXPECT_EQ(dx.
get(0).
code,
"LEX004");
312 EXPECT_EQ(dx.
get(0).
message,
"unterminated block comment");
316TEST(CompilerLexer, MalformedCharacterLiteralProducesInvalidToken)
323 const auto tok = lexer.
next();
325 EXPECT_TRUE(tok.is_invalid());
326 ASSERT_EQ(dx.
size(), 1u);
327 EXPECT_EQ(dx.
get(0).
code,
"LEX003");
328 EXPECT_EQ(dx.
get(0).
message,
"malformed character literal");
332TEST(CompilerLexer, EmptyInputReturnsEOFImmediately)
340 EXPECT_TRUE(lexer.
eof());
341 const auto tok = lexer.
next();
342 EXPECT_TRUE(tok.is_eof());
346TEST(CompilerLexer, PublicAPIContract)
359 EXPECT_FALSE(lexer.
eof());
362 const auto tok_fn = lexer.
next();
363 EXPECT_EQ(tok_fn.kind, Compiler_Token_Kind::Kw_Fn);
367 const auto tok_x = lexer.
next();
368 EXPECT_EQ(tok_x.kind, Compiler_Token_Kind::Identifier);
371 EXPECT_TRUE(lexer.
eof());
372 const auto tok_eof = lexer.
next();
373 EXPECT_TRUE(tok_eof.is_eof());
378 EXPECT_FALSE(lexer.
eof());
379 const auto tok_fn2 = lexer.
next();
380 EXPECT_EQ(tok_fn2.kind, Compiler_Token_Kind::Kw_Fn);
384 EXPECT_THROW(lexer.
reset(past_end), std::out_of_range);
Lexical analyzer (Lexer) based on Source_Manager.
Token generator for a single source file.
Source_Offset current_offset() const noexcept
Returns the current byte offset in the file.
Compiler_Token next()
Consumes and returns the next token from the stream.
bool eof()
Indicates whether the end of the file has been reached.
const Compiler_Token & peek()
Peeks at the next token without advancing the cursor.
Source_File_Id source_file_id() const noexcept
Returns the file ID currently being analyzed.
void reset(const Source_Offset offset=0)
Repositions the reading cursor.
Diagnostic accumulator and renderer.
bool has_errors() const noexcept
Returns whether any error or fatal diagnostic was emitted.
const Diagnostic & get(const size_t i) const
Returns diagnostic i.
size_t size() const noexcept
Returns the number of stored diagnostics.
Stores source files and resolves offsets into human-readable data.
const std::string & file_text(const Source_File_Id id) const
Returns the full text of file id.
Source_File_Id add_virtual_file(const std::string &name, const std::string &text)
Registers an in-memory source file.
Minimal std::expected-style result type for C++20.
Main namespace for Aleph-w library functions.
void next()
Advance all underlying iterators (bounds-checked).
Configuration options for lexer behavior.
bool allow_block_comments
If false, /* is treated as / followed by *.
bool keep_comments
If true, comments are returned as tokens.
std::string code
Optional stable code such as E001.
std::string message
Main headline message.