mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-04-09 01:06:21 +00:00
account for dangling opening/closing brace
This commit is contained in:
@@ -1,13 +1,12 @@
|
||||
package com.dfsek.terra.api.structures.parser;
|
||||
|
||||
import com.dfsek.terra.api.structures.parser.exceptions.ParseException;
|
||||
import com.dfsek.terra.api.structures.parser.lang.Argument;
|
||||
import com.dfsek.terra.api.structures.parser.lang.Function;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface FunctionBuilder<T extends Function> {
|
||||
public interface FunctionBuilder<T extends Function<?>> {
|
||||
T build(List<String> argumentList) throws ParseException;
|
||||
|
||||
List<Argument<?>> getArguments();
|
||||
int getArguments();
|
||||
}
|
||||
|
||||
@@ -45,6 +45,14 @@ public class Parser {
|
||||
} catch(TokenizerException e) {
|
||||
throw new ParseException("Failed to tokenize input", e);
|
||||
}
|
||||
// Check for dangling brackets
|
||||
int blockLevel = 0;
|
||||
for(Token t : tokens) {
|
||||
if(t.getType().equals(Token.Type.BLOCK_BEGIN)) blockLevel++;
|
||||
else if(t.getType().equals(Token.Type.BLOCK_END)) blockLevel--;
|
||||
if(blockLevel < 0) throw new ParseException("Dangling closing brace: " + t.getStart());
|
||||
}
|
||||
if(blockLevel != 0) throw new ParseException("Dangling opening brace");
|
||||
|
||||
return parseBlock(tokens);
|
||||
}
|
||||
@@ -121,8 +129,8 @@ public class Parser {
|
||||
List<String> arg = args.stream().map(Token::getContent).collect(Collectors.toList());
|
||||
|
||||
FunctionBuilder<?> builder = functions.get(identifier.getContent());
|
||||
if(arg.size() != builder.getArguments().size())
|
||||
throw new ParseException("Expected " + builder.getArguments().size() + " arguments, found " + arg.size() + ": " + identifier.getStart());
|
||||
if(arg.size() != builder.getArguments() && builder.getArguments() != -1)
|
||||
throw new ParseException("Expected " + builder.getArguments() + " arguments, found " + arg.size() + ": " + identifier.getStart());
|
||||
return functions.get(identifier.getContent()).build(arg);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user