mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-02-16 10:30:42 +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);
|
||||
}
|
||||
|
||||
|
||||
@@ -5,14 +5,12 @@ import com.dfsek.terra.api.platform.world.Chunk;
|
||||
import com.dfsek.terra.api.structures.parser.FunctionBuilder;
|
||||
import com.dfsek.terra.api.structures.parser.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 com.dfsek.terra.api.structures.parser.lang.Item;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class ParserTest {
|
||||
@@ -27,8 +25,8 @@ public class ParserTest {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Argument<?>> getArguments() {
|
||||
return Arrays.asList(id -> id, Double::parseDouble);
|
||||
public int getArguments() {
|
||||
return 2;
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user