Skip to content

Commit 76bdbdc

Browse files
committed
Add support for literals in inspect alternatives
1 parent ed01f4a commit 76bdbdc

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
@@ -1248,8 +1248,11 @@ class cppfront
12481248

12491249
auto id = std::string{};
12501250
printer.emit_to_string(&id);
1251-
assert(alt->id_expression);
1252-
emit(*alt->id_expression);
1251+
assert(alt->id_expression || alt->literal);
1252+
if (alt->id_expression)
1253+
emit(*alt->id_expression);
1254+
else if (alt->literal)
1255+
emit(*alt->literal);
12531256
printer.emit_to_string();
12541257

12551258
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
@@ -639,6 +639,7 @@ struct alternative_node
639639
std::unique_ptr<id_expression_node> id_expression;
640640
source_position equal_sign;
641641
std::unique_ptr<statement_node> statement;
642+
token const* literal;
642643

643644
auto position() const -> source_position
644645
{
@@ -751,8 +752,11 @@ auto alternative_node::visit(auto& v, int depth) -> void
751752
}
752753
assert (is_as_keyword);
753754
v.start(*is_as_keyword, depth+1);
754-
assert (id_expression);
755-
id_expression->visit(v, depth+1);
755+
assert (id_expression || literal);
756+
if (id_expression)
757+
id_expression->visit(v, depth+1);
758+
else if (literal)
759+
literal->visit(v, depth+1);
756760
assert (statement);
757761
statement->visit(v, depth+1);
758762
v.end(*this, depth);
@@ -2293,8 +2297,12 @@ class parser
22932297
if (auto id = id_expression()) {
22942298
n->id_expression = std::move(id);
22952299
}
2300+
else if (is_literal(curr().type())) {
2301+
n->literal = &curr();
2302+
next();
2303+
}
22962304
else {
2297-
error("expected id-expression after 'is' in inspect alternative");
2305+
error("expected id-expression or literal after 'is' in inspect alternative");
22982306
return {};
22992307
}
23002308

0 commit comments

Comments
 (0)