From 373ce7b6a2981d4870c5c815b43012bcffdc7088 Mon Sep 17 00:00:00 2001 From: iamdudeman Date: Sat, 23 Nov 2024 19:36:25 -0600 Subject: [PATCH] minor code cleanup --- .../sola/json/parser/SolaJsonParser.java | 38 ++++++------------- 1 file changed, 12 insertions(+), 26 deletions(-) diff --git a/src/main/java/technology/sola/json/parser/SolaJsonParser.java b/src/main/java/technology/sola/json/parser/SolaJsonParser.java index 4530904..15ff9e2 100644 --- a/src/main/java/technology/sola/json/parser/SolaJsonParser.java +++ b/src/main/java/technology/sola/json/parser/SolaJsonParser.java @@ -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); }