From e56e815f0698cfd926d6a4323a3dac2f0a4fd3b0 Mon Sep 17 00:00:00 2001 From: Violet White Date: Thu, 14 Sep 2023 21:25:59 -0700 Subject: [PATCH] Add AnyOf to the tokenizer --- parser/src/main/kotlin/gay/pizza/pork/parser/Parser.kt | 2 +- .../src/main/kotlin/gay/pizza/pork/parser/TokenType.kt | 7 ++++--- .../kotlin/gay/pizza/pork/parser/TokenTypeProperty.kt | 9 +++++++++ 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/parser/src/main/kotlin/gay/pizza/pork/parser/Parser.kt b/parser/src/main/kotlin/gay/pizza/pork/parser/Parser.kt index f707eca..bc0d1c6 100644 --- a/parser/src/main/kotlin/gay/pizza/pork/parser/Parser.kt +++ b/parser/src/main/kotlin/gay/pizza/pork/parser/Parser.kt @@ -230,7 +230,7 @@ class Parser(source: TokenSource, attribution: NodeAttribution) : } override fun parseImportDeclaration(): ImportDeclaration = guarded { - expect(TokenType.Import, TokenType.Impork, TokenType.PorkLoad) + expect(TokenType.Import) val form = parseSymbol() val components = oneAndContinuedBy(TokenType.Dot) { parseSymbol() diff --git a/parser/src/main/kotlin/gay/pizza/pork/parser/TokenType.kt b/parser/src/main/kotlin/gay/pizza/pork/parser/TokenType.kt index 223cbb3..fd4fa52 100644 --- a/parser/src/main/kotlin/gay/pizza/pork/parser/TokenType.kt +++ b/parser/src/main/kotlin/gay/pizza/pork/parser/TokenType.kt @@ -55,9 +55,7 @@ enum class TokenType(vararg properties: TokenTypeProperty) { In(ManyChars("in"), KeywordFamily), Continue(ManyChars("continue"), KeywordFamily), Break(ManyChars("break"), KeywordFamily), - Import(ManyChars("import"), KeywordFamily), - Impork(ManyChars("impork"), KeywordFamily), - PorkLoad(ManyChars("porkload"), KeywordFamily), + Import(AnyOf("import", "impork", "porkload"), KeywordFamily), Export(ManyChars("export"), KeywordFamily), Func(ManyChars("func"), KeywordFamily), Native(ManyChars("native"), KeywordFamily), @@ -72,6 +70,8 @@ enum class TokenType(vararg properties: TokenTypeProperty) { properties.filterIsInstance() val manyChars: ManyChars? = properties.filterIsInstance().singleOrNull() + val anyOf: AnyOf? = + properties.filterIsInstance().singleOrNull() val singleChar: SingleChar? = properties.filterIsInstance().singleOrNull() val family: TokenFamily = @@ -83,6 +83,7 @@ enum class TokenType(vararg properties: TokenTypeProperty) { properties.filterIsInstance().singleOrNull() companion object { + val AnyOf = entries.filter { item -> item.anyOf != null } val ManyChars = entries.filter { item -> item.manyChars != null } val SingleChars = entries.filter { item -> item.singleChar != null } val CharConsumers = entries.filter { item -> diff --git a/parser/src/main/kotlin/gay/pizza/pork/parser/TokenTypeProperty.kt b/parser/src/main/kotlin/gay/pizza/pork/parser/TokenTypeProperty.kt index 3ab799c..3bd3228 100644 --- a/parser/src/main/kotlin/gay/pizza/pork/parser/TokenTypeProperty.kt +++ b/parser/src/main/kotlin/gay/pizza/pork/parser/TokenTypeProperty.kt @@ -4,6 +4,7 @@ interface TokenTypeProperty { class SingleChar(val char: Char) : TokenTypeProperty class Promotion(val nextChar: Char, val type: TokenType) : TokenTypeProperty class ManyChars(val text: String) : TokenTypeProperty + class AnyOf(vararg val strings: String): TokenTypeProperty class CharConsumer(val isValid: (Char) -> Boolean) : TokenTypeProperty class CharIndexConsumer(val isValid: (Char, Int) -> Boolean) : TokenTypeProperty open class TokenUpgrader(val maybeUpgrade: (Token) -> Token?) : TokenTypeProperty @@ -16,6 +17,14 @@ interface TokenTypeProperty { break } } + if (upgraded == null) { + for(item in TokenType.AnyOf) { + if(item.anyOf != null && item.anyOf.strings.contains(token.text)) { + upgraded = Token(item, token.start, token.text) + break + } + } + } upgraded }) }