Got rid of un-needed tokens types.

This commit is contained in:
Jesse Brault 2023-01-23 13:14:45 +01:00
parent 82bb229eb8
commit 98086f89ac
5 changed files with 16 additions and 49 deletions

View File

@ -142,16 +142,16 @@ class ComponentParser {
return this.doubleQuoteStringValue() return this.doubleQuoteStringValue()
} else if (this.peek(SINGLE_QUOTE)) { } else if (this.peek(SINGLE_QUOTE)) {
return this.singleQuoteStringValue() return this.singleQuoteStringValue()
} else if (this.peek(DOLLAR) && this.peekSecond(GROOVY_IDENTIFIER)) { } else if (this.peek(GROOVY_IDENTIFIER)) {
return this.dollarReferenceValue() return this.dollarReferenceValue()
} else if (this.peek(DOLLAR) && this.peekSecond(CURLY_OPEN)) { } else if (this.peek(GROOVY)) {
return this.dollarScriptletValue() return this.dollarScriptletValue()
} else if (this.peek(LT) && this.peekSecond(PERCENT) && this.peekThird(EQUALS)) { } else if (this.peek(LT) && this.peekSecond(PERCENT) && this.peekThird(EQUALS)) {
return this.expressionScriptletValue() return this.expressionScriptletValue()
} else if (this.peek(LT) && this.peekSecond(PERCENT)) { } else if (this.peek(LT) && this.peekSecond(PERCENT)) {
return this.scriptletValue() return this.scriptletValue()
} else { } 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') throw new RuntimeException('should not get here')
} }
@ -176,21 +176,17 @@ class ComponentParser {
} }
} }
@PeekBefore([DOLLAR, GROOVY_IDENTIFIER]) @PeekBefore([GROOVY_IDENTIFIER])
private DollarReferenceValue dollarReferenceValue() { private DollarReferenceValue dollarReferenceValue() {
this.expect(DOLLAR)
def groovyIdentifierToken = this.expect(GROOVY_IDENTIFIER) def groovyIdentifierToken = this.expect(GROOVY_IDENTIFIER)
new DollarReferenceValue().tap { new DollarReferenceValue().tap {
reference = groovyIdentifierToken.text reference = groovyIdentifierToken.text
} }
} }
@PeekBefore([DOLLAR, CURLY_OPEN]) @PeekBefore([GROOVY])
private DollarScriptletValue dollarScriptletValue() { private DollarScriptletValue dollarScriptletValue() {
this.expect(DOLLAR)
this.expect(CURLY_OPEN)
def groovyToken = this.expect(GROOVY) def groovyToken = this.expect(GROOVY)
this.expect(CURLY_CLOSE)
new DollarScriptletValue().tap { new DollarScriptletValue().tap {
scriptlet = groovyToken.text scriptlet = groovyToken.text
} }

View File

@ -14,10 +14,7 @@ class ComponentToken {
DOUBLE_QUOTE, DOUBLE_QUOTE,
SINGLE_QUOTE, SINGLE_QUOTE,
STRING, STRING,
DOLLAR,
CURLY_OPEN,
GROOVY, GROOVY,
CURLY_CLOSE,
GROOVY_IDENTIFIER, GROOVY_IDENTIFIER,
PERCENT, PERCENT,
FORWARD_SLASH FORWARD_SLASH

View File

@ -37,7 +37,6 @@ class ComponentTokenizer {
KEYS_AND_VALUES, KEYS_AND_VALUES,
DOUBLE_QUOTE_STRING, DOUBLE_QUOTE_STRING,
SINGLE_QUOTE_STRING, SINGLE_QUOTE_STRING,
DOLLAR_GROOVY,
EXPRESSION_SCRIPTLET_GROOVY, EXPRESSION_SCRIPTLET_GROOVY,
DONE DONE
} }
@ -88,15 +87,11 @@ class ComponentTokenizer {
tokens << new ComponentToken(Type.SINGLE_QUOTE, it) tokens << new ComponentToken(Type.SINGLE_QUOTE, it)
} }
on dollarReference exec { String s -> on dollarReference exec { String s ->
tokens << new ComponentToken(Type.DOLLAR, s[0]) tokens << new ComponentToken(Type.GROOVY_IDENTIFIER, s.substring(1)) // skip opening $
tokens << new ComponentToken(Type.GROOVY_IDENTIFIER, s.substring(1))
} }
on dollarOpen shiftTo State.DOLLAR_GROOVY exec { String s -> //noinspection GroovyAssignabilityCheck // for some reason IntelliJ is confused by this
tokens << new ComponentToken(Type.DOLLAR, s[0]) on DollarGroovyParser::parse exec { String s ->
tokens << new ComponentToken(Type.CURLY_OPEN, s[1]) tokens << new ComponentToken(Type.GROOVY, s.substring(2, s.length() - 1))
}
on DollarGroovyParser.&parse exec {
} }
on percent shiftTo State.EXPRESSION_SCRIPTLET_GROOVY exec { on percent shiftTo State.EXPRESSION_SCRIPTLET_GROOVY exec {
tokens << new ComponentToken(Type.PERCENT, it) 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) { whileIn(State.EXPRESSION_SCRIPTLET_GROOVY) {
on expressionScriptletGroovy shiftTo State.KEYS_AND_VALUES exec { String s -> on expressionScriptletGroovy shiftTo State.KEYS_AND_VALUES exec { String s ->
tokens << new ComponentToken(Type.GROOVY, s.substring(0, s.length() - 2)) tokens << new ComponentToken(Type.GROOVY, s.substring(0, s.length() - 2))

View File

@ -1,10 +1,6 @@
package com.jessebrault.ssg.template.gspe.component package com.jessebrault.ssg.template.gspe.component
import com.jessebrault.ssg.template.gspe.component.node.ComponentNode import com.jessebrault.ssg.template.gspe.component.node.*
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 groovy.transform.stc.ClosureParams import groovy.transform.stc.ClosureParams
import groovy.transform.stc.FirstParam import groovy.transform.stc.FirstParam
import groovy.transform.stc.SimpleType import groovy.transform.stc.SimpleType
@ -68,7 +64,7 @@ class ComponentParserTests {
def <T extends Node> void expect( def <T extends Node> void expect(
Class<T> childNodeClass, Class<T> childNodeClass,
@DelegatesTo(value = NodeTester, strategy = Closure.DELEGATE_ONLY) @DelegatesTo(value = NodeTester, strategy = Closure.DELEGATE_FIRST)
@ClosureParams(FirstParam.FirstGenericType) @ClosureParams(FirstParam.FirstGenericType)
Closure<Void> furtherTests Closure<Void> furtherTests
) { ) {
@ -84,7 +80,7 @@ class ComponentParserTests {
private final ComponentParser parser = new ComponentParser() private final ComponentParser parser = new ComponentParser()
private void selfClosing( private void selfClosing(
List<ComponentToken> tokens, Queue<ComponentToken> tokens,
@DelegatesTo(value = NodeTester, strategy = Closure.DELEGATE_FIRST) @DelegatesTo(value = NodeTester, strategy = Closure.DELEGATE_FIRST)
@ClosureParams(value = SimpleType, options = ['com.jessebrault.ssg.template.gspe.component.node.ComponentNode']) @ClosureParams(value = SimpleType, options = ['com.jessebrault.ssg.template.gspe.component.node.ComponentNode'])
Closure<Void> tests Closure<Void> tests
@ -99,12 +95,12 @@ class ComponentParserTests {
@Test @Test
void selfClosingNoKeysOrValues() { void selfClosingNoKeysOrValues() {
this.selfClosing([ this.selfClosing(new LinkedList<>([
new ComponentToken(LT), new ComponentToken(LT),
new ComponentToken(IDENTIFIER, 'Test'), new ComponentToken(IDENTIFIER, 'Test'),
new ComponentToken(FORWARD_SLASH), new ComponentToken(FORWARD_SLASH),
new ComponentToken(GT) new ComponentToken(GT)
]) { ])) {
assertEquals('Test', it.identifier) assertEquals('Test', it.identifier)
expect(KeysAndValues) { expect(KeysAndValues) {
assertEquals(0, it.children.size()) assertEquals(0, it.children.size())
@ -114,7 +110,7 @@ class ComponentParserTests {
@Test @Test
void selfClosingWithGStringValue() { void selfClosingWithGStringValue() {
this.selfClosing([ this.selfClosing(new LinkedList<>([
new ComponentToken(LT), new ComponentToken(LT),
new ComponentToken(IDENTIFIER, 'Test'), new ComponentToken(IDENTIFIER, 'Test'),
new ComponentToken(KEY, 'test'), new ComponentToken(KEY, 'test'),
@ -124,7 +120,7 @@ class ComponentParserTests {
new ComponentToken(DOUBLE_QUOTE), new ComponentToken(DOUBLE_QUOTE),
new ComponentToken(FORWARD_SLASH), new ComponentToken(FORWARD_SLASH),
new ComponentToken(GT) new ComponentToken(GT)
]) { ])) {
assertEquals('Test', it.identifier) assertEquals('Test', it.identifier)
expect(KeysAndValues) { expect(KeysAndValues) {
expect(KeyAndValue) { expect(KeyAndValue) {

View File

@ -123,10 +123,7 @@ class ComponentTokenizerTests {
expect IDENTIFIER, 'Test' expect IDENTIFIER, 'Test'
expect KEY, 'key' expect KEY, 'key'
expect EQUALS expect EQUALS
expect DOLLAR
expect CURLY_OPEN
expect GROOVY, ' test ' expect GROOVY, ' test '
expect CURLY_CLOSE
expect FORWARD_SLASH expect FORWARD_SLASH
expect GT expect GT
} }
@ -139,10 +136,7 @@ class ComponentTokenizerTests {
expect IDENTIFIER, 'Test' expect IDENTIFIER, 'Test'
expect KEY, 'key' expect KEY, 'key'
expect EQUALS expect EQUALS
expect DOLLAR
expect CURLY_OPEN
expect GROOVY, ' test.each { it.test() } ' expect GROOVY, ' test.each { it.test() } '
expect CURLY_CLOSE
expect FORWARD_SLASH expect FORWARD_SLASH
expect GT expect GT
} }
@ -155,7 +149,6 @@ class ComponentTokenizerTests {
expect IDENTIFIER, 'Test' expect IDENTIFIER, 'Test'
expect KEY, 'key' expect KEY, 'key'
expect EQUALS expect EQUALS
expect DOLLAR
expect GROOVY_IDENTIFIER, 'test' expect GROOVY_IDENTIFIER, 'test'
expect FORWARD_SLASH expect FORWARD_SLASH
expect GT expect GT