Skip to content

Commit 4729ff9

Browse files
committed
Add support for literals in inspect alternatives
1 parent a2977c2 commit 4729ff9

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
@@ -1272,8 +1272,11 @@ class cppfront
12721272

12731273
auto id = std::string{};
12741274
printer.emit_to_string(&id);
1275-
assert(alt->id_expression);
1276-
emit(*alt->id_expression);
1275+
assert(alt->id_expression || alt->literal);
1276+
if (alt->id_expression)
1277+
emit(*alt->id_expression);
1278+
else if (alt->literal)
1279+
emit(*alt->literal);
12771280
printer.emit_to_string();
12781281

12791282
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
@@ -646,6 +646,7 @@ struct alternative_node
646646
std::unique_ptr<id_expression_node> id_expression;
647647
source_position equal_sign;
648648
std::unique_ptr<statement_node> statement;
649+
token const* literal;
649650

650651
auto position() const -> source_position
651652
{
@@ -758,8 +759,11 @@ auto alternative_node::visit(auto& v, int depth) -> void
758759
}
759760
assert (is_as_keyword);
760761
v.start(*is_as_keyword, depth+1);
761-
assert (id_expression);
762-
id_expression->visit(v, depth+1);
762+
assert (id_expression || literal);
763+
if (id_expression)
764+
id_expression->visit(v, depth+1);
765+
else if (literal)
766+
literal->visit(v, depth+1);
763767
assert (statement);
764768
statement->visit(v, depth+1);
765769
v.end(*this, depth);
@@ -2300,8 +2304,12 @@ class parser
23002304
if (auto id = id_expression()) {
23012305
n->id_expression = std::move(id);
23022306
}
2307+
else if (is_literal(curr().type())) {
2308+
n->literal = &curr();
2309+
next();
2310+
}
23032311
else {
2304-
error("expected id-expression after 'is' in inspect alternative");
2312+
error("expected id-expression or literal after 'is' in inspect alternative");
23052313
return {};
23062314
}
23072315

0 commit comments

Comments
 (0)