Skip to content

Commit

Permalink
minor code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
iamdudeman committed Nov 24, 2024
1 parent 09f96bb commit 373ce7b
Showing 1 changed file with 12 additions and 26 deletions.
38 changes: 12 additions & 26 deletions src/main/java/technology/sola/json/parser/SolaJsonParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,50 +86,36 @@ private AstNode ruleArray() {
}

private AstNode rulePair() {
Token nameToken = currentToken;
eat(TokenType.STRING);
Token nameToken = eat(TokenType.STRING);
eat(TokenType.COLON);

return AstNode.pair(nameToken, ruleValue());
}

private AstNode ruleValue() {
Token token = currentToken;

return switch (currentToken.type()) {
case L_BRACKET -> ruleArray();
case L_CURLY -> ruleObject();
case TRUE -> {
eat(TokenType.TRUE);
yield AstNode.value(token);
}
case FALSE -> {
eat(TokenType.FALSE);
yield AstNode.value(token);
}
case NULL -> {
eat(TokenType.NULL);
yield AstNode.value(token);
}
case STRING -> {
eat(TokenType.STRING);
yield AstNode.value(token);
}
case NUMBER -> {
eat(TokenType.NUMBER);
yield AstNode.value(token);
}
case TRUE -> AstNode.value(eat(TokenType.TRUE));
case FALSE -> AstNode.value(eat(TokenType.FALSE));
case NULL -> AstNode.value(eat(TokenType.NULL));
case STRING -> AstNode.value(eat(TokenType.STRING));
case NUMBER -> AstNode.value(eat(TokenType.NUMBER));
default -> throw new InvalidSyntaxException(
token.type(), textIndex,
currentToken.type(), textIndex,
TokenType.L_BRACKET, TokenType.L_CURLY, TokenType.TRUE, TokenType.FALSE, TokenType.NULL, TokenType.STRING, TokenType.NUMBER
);
};
}

private void eat(TokenType tokenType) {
private Token eat(TokenType tokenType) {
var token = currentToken;

if (currentToken.type() == tokenType) {
textIndex = solaJsonTokenizer.getTextIndex();
currentToken = solaJsonTokenizer.getNextToken();

return token;
} else {
throw new InvalidSyntaxException(currentToken.type(), textIndex, tokenType);
}
Expand Down

0 comments on commit 373ce7b

Please sign in to comment.