Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
compiler_parser_utils_test.cc
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
36#include <gtest/gtest.h>
37
39
40using namespace Aleph;
41
43{
44 EXPECT_TRUE(compiler_is_expression_start(Compiler_Token_Kind::Identifier));
45 EXPECT_TRUE(compiler_is_expression_start(Compiler_Token_Kind::Minus));
46 EXPECT_FALSE(compiler_is_expression_start(Compiler_Token_Kind::Kw_Return));
47
48 EXPECT_TRUE(compiler_is_statement_start(Compiler_Token_Kind::Kw_Let));
49 EXPECT_TRUE(compiler_is_statement_start(Compiler_Token_Kind::Identifier));
50 EXPECT_FALSE(compiler_is_statement_start(Compiler_Token_Kind::Comma));
51
52 EXPECT_TRUE(compiler_is_top_level_declaration_start(Compiler_Token_Kind::Kw_Fn));
53 EXPECT_TRUE(compiler_is_top_level_declaration_start(Compiler_Token_Kind::Kw_Import));
54 EXPECT_FALSE(compiler_is_top_level_declaration_start(Compiler_Token_Kind::Kw_Let));
55}
56
58{
59 const Compiler_Token token{Compiler_Token_Kind::String_Literal,
60 "\"math.aw\"",
61 {}};
63
64 const Compiler_Token identifier{Compiler_Token_Kind::Identifier, "math", {}};
66}
67
69{
71 const auto file_id = sm.add_virtual_file("params.aw", "left, right, total)");
73 Compiler_Lexer lexer(sm, file_id, &dx);
75
77 ASSERT_TRUE(stream.parse_separated_list(Compiler_Token_Kind::Comma,
78 Compiler_Token_Kind::RParen,
79 [&]()
80 {
81 names.append(
82 stream.expect_identifier("in parameter list").lexeme);
83 return true;
84 }));
85
86 ASSERT_EQ(names.size(), 3u);
87 EXPECT_EQ(names.access(0), "left");
88 EXPECT_EQ(names.access(1), "right");
89 EXPECT_EQ(names.access(2), "total");
90 EXPECT_EQ(stream.peek().kind, Compiler_Token_Kind::RParen);
92}
93
95{
97 const auto file_id = sm.add_virtual_file("enum.aw", "Red, Green, }");
99 Compiler_Lexer lexer(sm, file_id, &dx);
100 Compiler_Parser_Stream stream(lexer, &dx);
101
103 ASSERT_TRUE(stream.parse_separated_list(Compiler_Token_Kind::Comma,
104 Compiler_Token_Kind::RBrace,
105 [&]()
106 {
107 names.append(
108 stream.expect_identifier("in enum variant list").lexeme);
109 return true;
110 },
111 true));
112
113 ASSERT_EQ(names.size(), 2u);
114 EXPECT_EQ(names.access(0), "Red");
115 EXPECT_EQ(names.access(1), "Green");
116 EXPECT_EQ(stream.peek().kind, Compiler_Token_Kind::RBrace);
118}
119
121{
123 const auto file_id = sm.add_virtual_file("recover.aw", "1 2 let value = 3;");
125 Compiler_Lexer lexer(sm, file_id, &dx);
126 Compiler_Parser_Stream stream(lexer, &dx);
127
128 EXPECT_EQ(stream.next().kind, Compiler_Token_Kind::Integer_Literal);
129 stream.synchronize_statement();
130 EXPECT_EQ(stream.peek().kind, Compiler_Token_Kind::Kw_Let);
131}
Reusable token-stream helpers for recursive-descent frontends.
Token generator for a single source file.
Lightweight token stream facade for recursive-descent parsers.
Diagnostic accumulator and renderer.
bool has_errors() const noexcept
Returns whether any error or fatal diagnostic was emitted.
size_t size() const noexcept
Return the current dimension of array.
T & access(const size_t i) const noexcept
Fast access without checking allocation and bound_min_clock checking.
Stores source files and resolves offsets into human-readable data.
Definition ah-source.H:184
Source_File_Id add_virtual_file(const std::string &name, const std::string &text)
Registers an in-memory source file.
Definition ah-source.H:258
#define TEST(name)
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
bool compiler_is_expression_start(const Compiler_Token_Kind kind) noexcept
Returns whether a token kind can start an expression in the MVP grammar.
bool compiler_is_top_level_declaration_start(const Compiler_Token_Kind kind) noexcept
Returns whether a token kind starts one top-level declaration.
bool compiler_is_statement_start(const Compiler_Token_Kind kind) noexcept
Returns whether a token kind can start a statement in the MVP grammar.
std::string compiler_unquote_string_token(const Compiler_Token &token)
Removes surrounding double quotes from one string-literal token.
Lexical token representation with its value and location.