Skip to content

Commit

Permalink
Merge pull request #370 from koto-lang/369-followups
Browse files Browse the repository at this point in the history
Flexible map syntax followups
  • Loading branch information
irh authored Nov 5, 2024
2 parents 5f23357 + f05da37 commit 2915929
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ The Koto project adheres to
incorrect arguments.
- `await` and `const` have been reserved as keywords for future use.
- `@||` has been renamed to `@call`, and `@[]` has been renamed to `@index`.
- `:` placement following keys in maps is now more flexible.
([#368](https://github.com/koto-lang/koto/issues/368))

#### API

Expand Down
10 changes: 6 additions & 4 deletions crates/parser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ impl<'source> Parser<'source> {
Ok(result)
}

// Attempts to parse an indented block after the current positon
// Attempts to parse an indented block after the current position
//
// e.g.
// my_function = |x, y| # <- Here at entry
Expand Down Expand Up @@ -737,7 +737,9 @@ impl<'source> Parser<'source> {
let string = self.parse_string(context)?.unwrap();
let string_node = self.push_node_with_span(Str(string.string), string.span)?;

if self.peek_token() == Some(Token::Colon) && string.context.allow_map_block {
if string.context.allow_map_block
&& self.peek_next_token_on_same_line() == Some(Token::Colon)
{
self.consume_map_block(string_node, start_span, &string.context)
} else {
self.check_for_chain_after_node(string_node, &string.context)
Expand Down Expand Up @@ -1240,7 +1242,7 @@ impl<'source> Parser<'source> {
let id_node = self.push_node(Node::Id(constant_index, None))?;
let id_span = self.current_span();

if self.peek_token() == Some(Token::Colon) && id_context.allow_map_block {
if id_context.allow_map_block && self.peek_next_token_on_same_line() == Some(Token::Colon) {
// The ID is the start of a map block
self.consume_map_block(id_node, id_span, &id_context)
} else {
Expand Down Expand Up @@ -1863,7 +1865,7 @@ impl<'source> Parser<'source> {

let start_indent = self.current_indent();

if self.consume_token() != Some(Token::Colon) {
if self.consume_next_token_on_same_line() != Some(Token::Colon) {
return self.error(InternalError::ExpectedMapColon);
}

Expand Down
26 changes: 19 additions & 7 deletions crates/parser/tests/parser_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ x = {'foo': 42, bar, baz: 'hello', @+: 99}",
}
x = { 'foo': 42
, bar
, baz: 'hello'
, baz : 'hello'
, @+: 99
}
",
Expand All @@ -620,15 +620,15 @@ x = { 'foo': 42
x =
{ 'foo': 42, bar
, baz: 'hello'
, @+: 99
, @+ : 99
}
",
"
{
}
x =
{ 'foo': 42, bar,
{ 'foo' : 42, bar,
baz: 'hello'
, @+: 99
}
Expand Down Expand Up @@ -665,15 +665,27 @@ x =

#[test]
fn map_block_syntax() {
let source = r#"
let sources = [
r#"
x =
foo: 42
"baz":
foo: 0
@-: -1
x"#;
check_ast(
source,
x
"#,
r#"
x =
foo: 42
"baz" :
foo : 0
@- : -1
x
"#,
];

check_ast_for_equivalent_sources(
&sources,
&[
id(0), // x
id(1), // foo
Expand Down

0 comments on commit 2915929

Please sign in to comment.