Skip to content

Commit

Permalink
Language Server - parseAggregateLiteral arrays (#22)
Browse files Browse the repository at this point in the history
(from https://kolmafia.us/threads/ash-language-server-features.26098/post-163764 )
Alters parseAggregateLiteral a little so that we try to read key-value pairs (components of a map literal) even once we identified that the user is supplying an array literal (which doesn't expect keys).
  • Loading branch information
fredg1 committed Sep 27, 2021
1 parent 172f8af commit 78c24b6
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 12 deletions.
46 changes: 34 additions & 12 deletions src/net/sourceforge/kolmafia/textui/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -1298,17 +1298,24 @@ private Value parseAggregateLiteral( final BasicScope scope, final AggregateType
arrayAllowed = false;
}

// If parsing an ArrayLiteral, accumulate only values
if ( isArray )
if ( !delim.equals( ":" ) )
{
// The value must have the correct data type
lhs = this.autoCoerceValue( data, lhs, scope );
if ( !Operator.validCoercion( dataType, lhs.getType(), "assign" ) )
// If parsing an ArrayLiteral, accumulate only values
if ( isArray )
{
throw this.parseException( "Invalid array literal" );
}
// The value must have the correct data type
lhs = this.autoCoerceValue( data, lhs, scope );
if ( !Operator.validCoercion( dataType, lhs.getType(), "assign" ) )
{
throw this.parseException( "Invalid array literal" );
}

values.add( lhs );
values.add( lhs );
}
else
{
throw this.parseException( ":", delim );
}

// Move on to the next value
if ( delim.equals( "," ) )
Expand All @@ -1324,12 +1331,27 @@ else if ( !delim.equals( "}" ) )
}

// We are parsing a MapLiteral
if ( !delim.equals( ":" ) )
this.readToken(); // read :

if ( isArray )
{
throw this.parseException( ":", this.currentToken() );
}
// In order to reach this point without an error, we must have had a correct
// array literal so far, meaning the index type is an integer, and what we saw before
// the colon must have matched the aggregate's data type. Therefore, the next
// question is: is the data type also an integer?

this.readToken(); // read :
if ( data.equals( DataTypes.INT_TYPE ) )
{
// If so, this is an int[int] aggregate. They could have done something like
// {0, 1, 2, 3:3, 4:4, 5:5}
throw this.parseException( "Cannot include keys when making an array literal" );
}
else
{
// If not, we can't tell why there's a colon here.
throw this.parseException( ", or }", delim );
}
}

Value rhs;
if ( this.currentToken().equals( "{" ) && dataType instanceof AggregateType )
Expand Down
12 changes: 12 additions & 0 deletions test/net/sourceforge/kolmafia/textui/ParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,18 @@ public static Collection<Object[]> data()
"Expected :, found ,",
null,
},
{
"Ambiguity between array and map literal 2: index and data are both integers",
"int[5]{ 0, 1, 2, 3:3, 4 }",
"Cannot include keys when making an array literal",
null,
},
{
"Ambiguity between array and map literal 3: that can't be a key",
"string[5]{ '0', '1', '2', '3':'3', '4' }",
"Expected , or }, found :",
null,
},
{
// This... exercises a different code path.
"Parenthesized map literal",
Expand Down

0 comments on commit 78c24b6

Please sign in to comment.