Skip to content

Commit

Permalink
Language Server - parseFunction vararg (#28)
Browse files Browse the repository at this point in the history
( from https://kolmafia.us/threads/ash-language-server-features.26098/post-163765 )
This modifies the logic of parseFunction a bit so that the vararg error checks happen after the variable name was read.
This will allow us to have the full variable + type's location, rather than only the type, once those are implemented.

Additionally, this allows the "Only one vararg parameter is allowed" error message to be reached.
  • Loading branch information
fredg1 committed Sep 27, 2021
1 parent 99f6e79 commit 172f8af
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
35 changes: 20 additions & 15 deletions src/net/sourceforge/kolmafia/textui/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -726,18 +726,10 @@ private Function parseFunction( final Type functionType, final Scope parentScope

if ( this.currentToken().equals( "..." ) )
{
// We can only have a single vararg parameter
if ( vararg )
{
throw this.parseException( "Only one vararg parameter is allowed" );
}
// Make an vararg type out of the previously parsed type.
paramType = new VarArgType( paramType );

this.readToken(); //read ...

// Only one vararg is allowed
vararg = true;
}

Variable param = this.parseVariable( paramType, null );
Expand All @@ -746,19 +738,32 @@ private Function parseFunction( final Type functionType, final Scope parentScope
throw this.parseException( "identifier", this.currentToken() );
}

if ( !paramList.add( param ) )
if ( vararg )
{
if ( paramType instanceof VarArgType )
{
// We can only have a single vararg parameter
throw this.parseException( "Only one vararg parameter is allowed" );
}
else
{
// The single vararg parameter must be the last one
throw this.parseException( "The vararg parameter must be the last one" );
}
}
else if ( !paramList.add( param ) )
{
throw this.parseException( "Parameter " + param.getName() + " is already defined" );
}

if ( !this.currentToken().equals( ")" ) )
if ( paramType instanceof VarArgType )
{
// The single vararg parameter must be the last one
if ( vararg )
{
throw this.parseException( "The vararg parameter must be the last one" );
}
// Only one vararg is allowed
vararg = true;
}

if ( !this.currentToken().equals( ")" ) )
{
if ( this.currentToken().equals( "," ) )
{
this.readToken(); //read comma
Expand Down
7 changes: 2 additions & 5 deletions test/net/sourceforge/kolmafia/textui/ParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -608,15 +608,12 @@ public static Collection<Object[]> data()
"6", ".", "3", ",", "4", ".", "9", ",", "10", ",",
"-", "0", ")" ),
},
/*{
// Is currently trumped by
// "The vararg parameter must be the last one"
{
"Basic function with multiple varargs",
"void f(int ... a, int ... b) {}",
"Only one vararg parameter is allowed",
null,
},*/
},
{
"Basic function with non-terminal vararg",
"void f(int ... a, float b) {}",
Expand Down

0 comments on commit 172f8af

Please sign in to comment.