From 98086f89ac5e5a97152238f385dc8731e279486e Mon Sep 17 00:00:00 2001 From: Jesse Brault Date: Mon, 23 Jan 2023 13:14:45 +0100 Subject: [PATCH] Got rid of un-needed tokens types. --- .../gspe/component/ComponentParser.groovy | 14 ++++------- .../gspe/component/ComponentToken.groovy | 3 --- .../gspe/component/ComponentTokenizer.groovy | 23 ++++--------------- .../component/ComponentParserTests.groovy | 18 ++++++--------- .../component/ComponentTokenizerTests.groovy | 7 ------ 5 files changed, 16 insertions(+), 49 deletions(-) diff --git a/template/src/main/groovy/com/jessebrault/ssg/template/gspe/component/ComponentParser.groovy b/template/src/main/groovy/com/jessebrault/ssg/template/gspe/component/ComponentParser.groovy index 9284935..ab1fae4 100644 --- a/template/src/main/groovy/com/jessebrault/ssg/template/gspe/component/ComponentParser.groovy +++ b/template/src/main/groovy/com/jessebrault/ssg/template/gspe/component/ComponentParser.groovy @@ -142,16 +142,16 @@ class ComponentParser { return this.doubleQuoteStringValue() } else if (this.peek(SINGLE_QUOTE)) { return this.singleQuoteStringValue() - } else if (this.peek(DOLLAR) && this.peekSecond(GROOVY_IDENTIFIER)) { + } else if (this.peek(GROOVY_IDENTIFIER)) { return this.dollarReferenceValue() - } else if (this.peek(DOLLAR) && this.peekSecond(CURLY_OPEN)) { + } else if (this.peek(GROOVY)) { return this.dollarScriptletValue() } else if (this.peek(LT) && this.peekSecond(PERCENT) && this.peekThird(EQUALS)) { return this.expressionScriptletValue() } else if (this.peek(LT) && this.peekSecond(PERCENT)) { return this.scriptletValue() } else { - error([DOUBLE_QUOTE, SINGLE_QUOTE, DOLLAR, LT], this.tokens.poll()) + error([DOUBLE_QUOTE, SINGLE_QUOTE, GROOVY_IDENTIFIER, GROOVY, LT], this.tokens.poll()) } throw new RuntimeException('should not get here') } @@ -176,21 +176,17 @@ class ComponentParser { } } - @PeekBefore([DOLLAR, GROOVY_IDENTIFIER]) + @PeekBefore([GROOVY_IDENTIFIER]) private DollarReferenceValue dollarReferenceValue() { - this.expect(DOLLAR) def groovyIdentifierToken = this.expect(GROOVY_IDENTIFIER) new DollarReferenceValue().tap { reference = groovyIdentifierToken.text } } - @PeekBefore([DOLLAR, CURLY_OPEN]) + @PeekBefore([GROOVY]) private DollarScriptletValue dollarScriptletValue() { - this.expect(DOLLAR) - this.expect(CURLY_OPEN) def groovyToken = this.expect(GROOVY) - this.expect(CURLY_CLOSE) new DollarScriptletValue().tap { scriptlet = groovyToken.text } diff --git a/template/src/main/groovy/com/jessebrault/ssg/template/gspe/component/ComponentToken.groovy b/template/src/main/groovy/com/jessebrault/ssg/template/gspe/component/ComponentToken.groovy index 9caeed7..d0b2642 100644 --- a/template/src/main/groovy/com/jessebrault/ssg/template/gspe/component/ComponentToken.groovy +++ b/template/src/main/groovy/com/jessebrault/ssg/template/gspe/component/ComponentToken.groovy @@ -14,10 +14,7 @@ class ComponentToken { DOUBLE_QUOTE, SINGLE_QUOTE, STRING, - DOLLAR, - CURLY_OPEN, GROOVY, - CURLY_CLOSE, GROOVY_IDENTIFIER, PERCENT, FORWARD_SLASH diff --git a/template/src/main/groovy/com/jessebrault/ssg/template/gspe/component/ComponentTokenizer.groovy b/template/src/main/groovy/com/jessebrault/ssg/template/gspe/component/ComponentTokenizer.groovy index 6e81884..bc1b1d3 100644 --- a/template/src/main/groovy/com/jessebrault/ssg/template/gspe/component/ComponentTokenizer.groovy +++ b/template/src/main/groovy/com/jessebrault/ssg/template/gspe/component/ComponentTokenizer.groovy @@ -37,7 +37,6 @@ class ComponentTokenizer { KEYS_AND_VALUES, DOUBLE_QUOTE_STRING, SINGLE_QUOTE_STRING, - DOLLAR_GROOVY, EXPRESSION_SCRIPTLET_GROOVY, DONE } @@ -88,15 +87,11 @@ class ComponentTokenizer { tokens << new ComponentToken(Type.SINGLE_QUOTE, it) } on dollarReference exec { String s -> - tokens << new ComponentToken(Type.DOLLAR, s[0]) - tokens << new ComponentToken(Type.GROOVY_IDENTIFIER, s.substring(1)) + tokens << new ComponentToken(Type.GROOVY_IDENTIFIER, s.substring(1)) // skip opening $ } - on dollarOpen shiftTo State.DOLLAR_GROOVY exec { String s -> - tokens << new ComponentToken(Type.DOLLAR, s[0]) - tokens << new ComponentToken(Type.CURLY_OPEN, s[1]) - } - on DollarGroovyParser.&parse exec { - + //noinspection GroovyAssignabilityCheck // for some reason IntelliJ is confused by this + on DollarGroovyParser::parse exec { String s -> + tokens << new ComponentToken(Type.GROOVY, s.substring(2, s.length() - 1)) } on percent shiftTo State.EXPRESSION_SCRIPTLET_GROOVY exec { tokens << new ComponentToken(Type.PERCENT, it) @@ -133,16 +128,6 @@ class ComponentTokenizer { } } - whileIn(State.DOLLAR_GROOVY) { - on DollarGroovyParser.&parse shiftTo State.KEYS_AND_VALUES exec { String s -> - tokens << new ComponentToken(Type.GROOVY, s.substring(0, s.length() - 1)) - tokens << new ComponentToken(Type.CURLY_CLOSE, '}') - } - onNoMatch() exec { - throw new IllegalArgumentException() - } - } - whileIn(State.EXPRESSION_SCRIPTLET_GROOVY) { on expressionScriptletGroovy shiftTo State.KEYS_AND_VALUES exec { String s -> tokens << new ComponentToken(Type.GROOVY, s.substring(0, s.length() - 2)) diff --git a/template/src/test/groovy/com/jessebrault/ssg/template/gspe/component/ComponentParserTests.groovy b/template/src/test/groovy/com/jessebrault/ssg/template/gspe/component/ComponentParserTests.groovy index 1fc4658..368dd8a 100644 --- a/template/src/test/groovy/com/jessebrault/ssg/template/gspe/component/ComponentParserTests.groovy +++ b/template/src/test/groovy/com/jessebrault/ssg/template/gspe/component/ComponentParserTests.groovy @@ -1,10 +1,6 @@ package com.jessebrault.ssg.template.gspe.component -import com.jessebrault.ssg.template.gspe.component.node.ComponentNode -import com.jessebrault.ssg.template.gspe.component.node.GStringValue -import com.jessebrault.ssg.template.gspe.component.node.KeyAndValue -import com.jessebrault.ssg.template.gspe.component.node.KeysAndValues -import com.jessebrault.ssg.template.gspe.component.node.Node +import com.jessebrault.ssg.template.gspe.component.node.* import groovy.transform.stc.ClosureParams import groovy.transform.stc.FirstParam import groovy.transform.stc.SimpleType @@ -68,7 +64,7 @@ class ComponentParserTests { def void expect( Class childNodeClass, - @DelegatesTo(value = NodeTester, strategy = Closure.DELEGATE_ONLY) + @DelegatesTo(value = NodeTester, strategy = Closure.DELEGATE_FIRST) @ClosureParams(FirstParam.FirstGenericType) Closure furtherTests ) { @@ -84,7 +80,7 @@ class ComponentParserTests { private final ComponentParser parser = new ComponentParser() private void selfClosing( - List tokens, + Queue tokens, @DelegatesTo(value = NodeTester, strategy = Closure.DELEGATE_FIRST) @ClosureParams(value = SimpleType, options = ['com.jessebrault.ssg.template.gspe.component.node.ComponentNode']) Closure tests @@ -99,12 +95,12 @@ class ComponentParserTests { @Test void selfClosingNoKeysOrValues() { - this.selfClosing([ + this.selfClosing(new LinkedList<>([ new ComponentToken(LT), new ComponentToken(IDENTIFIER, 'Test'), new ComponentToken(FORWARD_SLASH), new ComponentToken(GT) - ]) { + ])) { assertEquals('Test', it.identifier) expect(KeysAndValues) { assertEquals(0, it.children.size()) @@ -114,7 +110,7 @@ class ComponentParserTests { @Test void selfClosingWithGStringValue() { - this.selfClosing([ + this.selfClosing(new LinkedList<>([ new ComponentToken(LT), new ComponentToken(IDENTIFIER, 'Test'), new ComponentToken(KEY, 'test'), @@ -124,7 +120,7 @@ class ComponentParserTests { new ComponentToken(DOUBLE_QUOTE), new ComponentToken(FORWARD_SLASH), new ComponentToken(GT) - ]) { + ])) { assertEquals('Test', it.identifier) expect(KeysAndValues) { expect(KeyAndValue) { diff --git a/template/src/test/groovy/com/jessebrault/ssg/template/gspe/component/ComponentTokenizerTests.groovy b/template/src/test/groovy/com/jessebrault/ssg/template/gspe/component/ComponentTokenizerTests.groovy index f52210c..80c47de 100644 --- a/template/src/test/groovy/com/jessebrault/ssg/template/gspe/component/ComponentTokenizerTests.groovy +++ b/template/src/test/groovy/com/jessebrault/ssg/template/gspe/component/ComponentTokenizerTests.groovy @@ -123,10 +123,7 @@ class ComponentTokenizerTests { expect IDENTIFIER, 'Test' expect KEY, 'key' expect EQUALS - expect DOLLAR - expect CURLY_OPEN expect GROOVY, ' test ' - expect CURLY_CLOSE expect FORWARD_SLASH expect GT } @@ -139,10 +136,7 @@ class ComponentTokenizerTests { expect IDENTIFIER, 'Test' expect KEY, 'key' expect EQUALS - expect DOLLAR - expect CURLY_OPEN expect GROOVY, ' test.each { it.test() } ' - expect CURLY_CLOSE expect FORWARD_SLASH expect GT } @@ -155,7 +149,6 @@ class ComponentTokenizerTests { expect IDENTIFIER, 'Test' expect KEY, 'key' expect EQUALS - expect DOLLAR expect GROOVY_IDENTIFIER, 'test' expect FORWARD_SLASH expect GT