Skip to content

Commit 2a1ec5c

Browse files
committed
Add support for literals in inspect alternatives
1 parent bef05a9 commit 2a1ec5c

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

source/cppfront.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,8 +1282,11 @@ class cppfront
12821282

12831283
auto id = std::string{};
12841284
printer.emit_to_string(&id);
1285-
assert(alt->id_expression);
1286-
emit(*alt->id_expression);
1285+
assert(alt->id_expression || alt->literal);
1286+
if (alt->id_expression)
1287+
emit(*alt->id_expression);
1288+
else if (alt->literal)
1289+
emit(*alt->literal);
12871290
printer.emit_to_string();
12881291

12891292
assert (*alt->is_as_keyword == "is" || *alt->is_as_keyword == "as");

source/parse.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,7 @@ struct alternative_node
667667
std::unique_ptr<id_expression_node> id_expression;
668668
source_position equal_sign;
669669
std::unique_ptr<statement_node> statement;
670+
token const* literal;
670671

671672
auto position() const -> source_position
672673
{
@@ -776,8 +777,11 @@ auto alternative_node::visit(auto& v, int depth) -> void
776777
}
777778
assert (is_as_keyword);
778779
v.start(*is_as_keyword, depth+1);
779-
assert (id_expression);
780-
id_expression->visit(v, depth+1);
780+
assert (id_expression || literal);
781+
if (id_expression)
782+
id_expression->visit(v, depth+1);
783+
else if (literal)
784+
literal->visit(v, depth+1);
781785
assert (statement);
782786
statement->visit(v, depth+1);
783787
v.end(*this, depth);
@@ -2346,8 +2350,12 @@ class parser
23462350
if (auto id = id_expression()) {
23472351
n->id_expression = std::move(id);
23482352
}
2353+
else if (is_literal(curr().type())) {
2354+
n->literal = &curr();
2355+
next();
2356+
}
23492357
else {
2350-
error("expected id-expression after 'is' in inspect alternative");
2358+
error("expected id-expression or literal after 'is' in inspect alternative");
23512359
return {};
23522360
}
23532361

0 commit comments

Comments
 (0)