From c1062fa1f22265cb56a7fef16ce2d41aec0ee68a Mon Sep 17 00:00:00 2001 From: Voltrex Date: Wed, 8 Sep 2021 22:06:52 +0430 Subject: [PATCH] fix(code): fix rules (#35) --- .../com/discord/simpleast/code/CodeRules.kt | 4 +- .../com/discord/simpleast/code/JavaScript.kt | 49 +++---------------- .../simpleast/code/JavaScriptRulesTest.kt | 34 +++---------- 3 files changed, 17 insertions(+), 70 deletions(-) diff --git a/simpleast-core/src/main/java/com/discord/simpleast/code/CodeRules.kt b/simpleast-core/src/main/java/com/discord/simpleast/code/CodeRules.kt index 48757f5..ef674cc 100644 --- a/simpleast-core/src/main/java/com/discord/simpleast/code/CodeRules.kt +++ b/simpleast-core/src/main/java/com/discord/simpleast/code/CodeRules.kt @@ -41,7 +41,7 @@ object CodeRules { Pattern.compile("""^```(?:([\w+\-.]+?)?(\s*\n))?([^\n].*?)\n*```""", Pattern.DOTALL) val PATTERN_CODE_INLINE: Pattern = - Pattern.compile("""^`([^`]+)`|``([^`]+)``""", Pattern.DOTALL) + Pattern.compile("""^(``?)([^`]*)\1""", Pattern.DOTALL) private const val CODE_BLOCK_LANGUAGE_GROUP = 1 private const val CODE_BLOCK_WS_PREFIX = 2 @@ -291,7 +291,7 @@ object CodeRules { return object : Rule, S>(PATTERN_CODE_INLINE) { override fun parse(matcher: Matcher, parser: Parser, S>, state: S) : ParseSpec { - val codeBody = matcher.group(1).orEmpty() + val codeBody = matcher.group(2).orEmpty() if (codeBody.isEmpty()) { return ParseSpec.createTerminal(TextNode(matcher.group()), state) } diff --git a/simpleast-core/src/main/java/com/discord/simpleast/code/JavaScript.kt b/simpleast-core/src/main/java/com/discord/simpleast/code/JavaScript.kt index 185c066..d477a1e 100644 --- a/simpleast-core/src/main/java/com/discord/simpleast/code/JavaScript.kt +++ b/simpleast-core/src/main/java/com/discord/simpleast/code/JavaScript.kt @@ -42,12 +42,12 @@ object JavaScript { ) class FunctionNode( - signature: String?, params: String, scope: String, + pre: String, signature: String?, params: String, codeStyleProviders: CodeStyleProviders ) : Node.Parent( + StyleNode.TextStyledNode(pre, codeStyleProviders.keywordStyleProvider), signature?.let { StyleNode.TextStyledNode(signature, codeStyleProviders.identifierStyleProvider) }, - StyleNode.TextStyledNode(params, codeStyleProviders.paramsStyleProvider), - StyleNode.TextStyledNode(scope, codeStyleProviders.defaultStyleProvider) + StyleNode.TextStyledNode(params, codeStyleProviders.paramsStyleProvider) ) { companion object { /** @@ -64,47 +64,15 @@ object JavaScript { * ``` */ private val PATTERN_JAVASCRIPT_FUNC = - """^([a-zA-Z_$][a-zA-Z0-9_$]*)?(\s*\(.*\))(\s*\{)""".toRegex(RegexOption.DOT_MATCHES_ALL).toPattern() + """^(function\*?|static|get|set|async)(\s+[a-zA-Z_$][a-zA-Z0-9_$]*)?(\s*\(.*?\))""".toRegex(RegexOption.DOT_MATCHES_ALL).toPattern() fun createFunctionRule(codeStyleProviders: CodeStyleProviders) = object : Rule, S>(PATTERN_JAVASCRIPT_FUNC) { override fun parse(matcher: Matcher, parser: Parser, S>, state: S): ParseSpec { - val signature = matcher.group(1) - val params = matcher.group(2) - val scope = matcher.group(3) - return ParseSpec.createTerminal(FunctionNode(signature, params!!, scope!!, codeStyleProviders), state) - } - } - } - } - - class ArrowFunctionNode( - params: String, arrow: String, - codeStyleProviders: CodeStyleProviders - ) : Node.Parent( - StyleNode.TextStyledNode(params, codeStyleProviders.paramsStyleProvider), - StyleNode.TextStyledNode(arrow, codeStyleProviders.defaultStyleProvider) - ) { - companion object { - /** - * Matches againt a JavaScript arrow function declaration. - * - * ``` - * file => {} - * file => !file - * (file) => 1 - * () => {} - * ``` - */ - private val PATTERN_JAVASCRIPT_ARROW_FUNC = - """^([a-zA-Z_$][a-zA-Z0-9_$]*|\(.*\))(\s*=>)""".toRegex(RegexOption.DOT_MATCHES_ALL).toPattern() - - fun createArrowFunctionRule(codeStyleProviders: CodeStyleProviders) = - object : Rule, S>(PATTERN_JAVASCRIPT_ARROW_FUNC) { - override fun parse(matcher: Matcher, parser: Parser, S>, state: S): ParseSpec { - val params = matcher.group(1) - val arrow = matcher.group(2) - return ParseSpec.createTerminal(ArrowFunctionNode(params!!, arrow!!, codeStyleProviders), state) + val pre = matcher.group(1) + val signature = matcher.group(2) + val params = matcher.group(3) + return ParseSpec.createTerminal(FunctionNode(pre!!, signature, params!!, codeStyleProviders), state) } } } @@ -234,6 +202,5 @@ object JavaScript { PATTERN_JAVASCRIPT_REGEX.toMatchGroupRule(stylesProvider = codeStyleProviders.literalStyleProvider), FieldNode.createFieldRule(codeStyleProviders), FunctionNode.createFunctionRule(codeStyleProviders), - ArrowFunctionNode.createArrowFunctionRule(codeStyleProviders), ) } diff --git a/simpleast-core/src/test/java/com/discord/simpleast/code/JavaScriptRulesTest.kt b/simpleast-core/src/test/java/com/discord/simpleast/code/JavaScriptRulesTest.kt index d0aef2a..3880e19 100644 --- a/simpleast-core/src/test/java/com/discord/simpleast/code/JavaScriptRulesTest.kt +++ b/simpleast-core/src/test/java/com/discord/simpleast/code/JavaScriptRulesTest.kt @@ -107,33 +107,13 @@ class JavaScriptRulesTest { """.trimIndent(), TestState()) ast.assertNodeContents>( - "test(T) {", - "() {", - "generator() {", - "test() {", - "fetch() {", - "tokens() {", - "constants() {") - } - - @Test - fun arrowFunctions() { - val ast = parser.parse(""" - ```js - test => {} - test => h - (test) => {} - (test) => h - (...args) => {} - ``` - """.trimIndent(), TestState()) - - ast.assertNodeContents>( - "test =>", - "test =>", - "(test) =>", - "(test) =>", - "(...args) =>") + "function test(T)", + "function () {", + "function* generator() {", + "static test() {", + "async fetch() {", + "get tokens() {", + "set constants() {") } @Test