Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions starlark/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ class Parser {

std::optional<std::vector<Argument>> parse_argument_list() {
std::vector<Argument> args;
bool seen_kw_arg = false;

while (true) {
auto maybe_token = next_token();
Expand Down Expand Up @@ -391,6 +392,7 @@ class Parser {
}

if (std::holds_alternative<token::Equals>(*next)) {
seen_kw_arg = true;
name = Identifier{.name = std::move(ident->name)};

auto value_token = next_token();
Expand All @@ -414,6 +416,11 @@ class Parser {
reconsume(std::move(*next));
}

if (seen_kw_arg) {
std::cerr << "Positional argument may not follow keyword argument.\n";
return std::nullopt;
}

auto value_expr = parse_expression(token);
if (!value_expr) {
std::cerr << "Failed to parse expression for argument value.\n";
Expand Down
24 changes: 13 additions & 11 deletions starlark/parser_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ int main() {
},
},
{
R"(foo(bar = "baz", "qux"))",
R"(foo("qux", bar = "baz"))",
starlark::Program{
.statements{
starlark::ExpressionStmt{
Expand All @@ -74,14 +74,14 @@ int main() {
.target = std::make_shared<starlark::Expression>(
starlark::Identifier{"foo"}),
.args{
{
starlark::Identifier{"bar"},
starlark::StringLiteral{"baz"},
},
{
std::nullopt,
starlark::StringLiteral{"qux"},
},
{
starlark::Identifier{"bar"},
starlark::StringLiteral{"baz"},
},
},
},
},
Expand All @@ -90,7 +90,7 @@ int main() {
},
},
{
R"(foo(bar = baz, qux()))",
R"(foo(qux(), bar = baz))",
starlark::Program{
.statements{
starlark::ExpressionStmt{
Expand All @@ -99,10 +99,6 @@ int main() {
.target = std::make_shared<starlark::Expression>(
starlark::Identifier{"foo"}),
.args{
{
starlark::Identifier{"bar"},
starlark::Identifier{"baz"},
},
{
std::nullopt,
starlark::CallExpr{
Expand All @@ -111,6 +107,10 @@ int main() {
.args{},
},
},
{
starlark::Identifier{"bar"},
starlark::Identifier{"baz"},
},
},
},
},
Expand Down Expand Up @@ -331,7 +331,9 @@ int main() {
static constexpr auto kExpectedParseFailures = std::to_array<std::string_view>({
// CallExpr
// Missing closing parenthesis.
R"(foo(bar = "baz", "qux")",
R"(foo("qux")",
// Positional argument after kw argument.
R"(foo(bar = baz, qux()))",

// ListExpr
// Tokenization error.
Expand Down