Skip to content

Commit

Permalink
Change Pattern.toFullMatchRule -> toMatchGroupRule to allow selec…
Browse files Browse the repository at this point in the history
…ting a single group
  • Loading branch information
lytefast committed Oct 27, 2020
1 parent 076c6b1 commit 63234ee
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,15 @@ object CodeRules {
internal fun createWordPattern(vararg words: String) =
Pattern.compile("""^\b(?:${words.joinToString("|")})\b""")

fun <R, S> Pattern.toFullMatchRule(
fun <R, S> Pattern.toMatchGroupRule(
group: Int = 0,
stylesProvider: StyleNode.SpanProvider<R>? = null
) =
object : Rule<R, Node<R>, S>(this) {
override fun parse(
matcher: Matcher, parser: Parser<R, in Node<R>, S>, state: S
): ParseSpec<R, Node<R>, S> {
val content = matcher.group()
val content = matcher.group(group).orEmpty()
val node = stylesProvider
?.let { StyleNode.Text(content, it) }
?: TextNode(content)
Expand Down Expand Up @@ -101,13 +102,13 @@ object CodeRules {
codeStyleProviders,
additionalRules = listOf(
createSingleLineCommentPattern("#")
.toFullMatchRule(codeStyleProviders.commentStyleProvider),
.toMatchGroupRule(stylesProvider = codeStyleProviders.commentStyleProvider),
Pattern.compile("""^"[\s\S]*?(?<!\\)"(?=\W|\s|$)""")
.toFullMatchRule(codeStyleProviders.literalStyleProvider),
.toMatchGroupRule(stylesProvider = codeStyleProviders.literalStyleProvider),
Pattern.compile("""^'[\s\S]*?(?<!\\)'(?=\W|\s|$)""")
.toFullMatchRule(codeStyleProviders.literalStyleProvider),
.toMatchGroupRule(stylesProvider = codeStyleProviders.literalStyleProvider),
Pattern.compile("""^@(\w+)""")
.toFullMatchRule(codeStyleProviders.genericsStyleProvider)),
.toMatchGroupRule(stylesProvider = codeStyleProviders.genericsStyleProvider)),
definitions = arrayOf("class", "def", "lambda"),
builtIns = arrayOf("True|False|None"),
"from|import|global|nonlocal",
Expand All @@ -122,11 +123,11 @@ object CodeRules {
codeStyleProviders,
additionalRules = listOf(
createSingleLineCommentPattern("//")
.toFullMatchRule(codeStyleProviders.commentStyleProvider),
.toMatchGroupRule(stylesProvider = codeStyleProviders.commentStyleProvider),
Pattern.compile("""^"[\s\S]*?(?<!\\)"(?=\W|\s|$)""")
.toFullMatchRule(codeStyleProviders.literalStyleProvider),
.toMatchGroupRule(stylesProvider = codeStyleProviders.literalStyleProvider),
Pattern.compile("""^#!?\[.*?\]\n""")
.toFullMatchRule(codeStyleProviders.genericsStyleProvider)),
.toMatchGroupRule(stylesProvider = codeStyleProviders.genericsStyleProvider)),
definitions = arrayOf("struct", "trait", "mod"),
builtIns = arrayOf(
"Self|Result|Ok|Err|Option|None|Some",
Expand All @@ -143,10 +144,10 @@ object CodeRules {

val xmlRules = listOf<Rule<R, Node<R>, S>>(
Xml.PATTERN_XML_COMMENT
.toFullMatchRule(codeStyleProviders.commentStyleProvider),
.toMatchGroupRule(stylesProvider = codeStyleProviders.commentStyleProvider),
Xml.createTagRule(codeStyleProviders),
PATTERN_LEADING_WS_CONSUMER.toFullMatchRule(),
PATTERN_TEXT.toFullMatchRule(),
PATTERN_LEADING_WS_CONSUMER.toMatchGroupRule(),
PATTERN_TEXT.toMatchGroupRule(),
)

return mapOf(
Expand Down Expand Up @@ -175,11 +176,11 @@ object CodeRules {
additionalRules +
listOf(
createDefinitionRule(codeStyleProviders, *definitions),
createWordPattern(*builtIns).toFullMatchRule(codeStyleProviders.genericsStyleProvider),
createWordPattern(*keywords).toFullMatchRule(codeStyleProviders.keywordStyleProvider),
PATTERN_NUMBERS.toFullMatchRule(codeStyleProviders.literalStyleProvider),
PATTERN_LEADING_WS_CONSUMER.toFullMatchRule(),
PATTERN_TEXT.toFullMatchRule(),
createWordPattern(*builtIns).toMatchGroupRule(stylesProvider = codeStyleProviders.genericsStyleProvider),
createWordPattern(*keywords).toMatchGroupRule(stylesProvider = codeStyleProviders.keywordStyleProvider),
PATTERN_NUMBERS.toMatchGroupRule(stylesProvider = codeStyleProviders.literalStyleProvider),
PATTERN_LEADING_WS_CONSUMER.toMatchGroupRule(),
PATTERN_TEXT.toMatchGroupRule(),
)

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.discord.simpleast.code

import com.discord.simpleast.code.CodeRules.toFullMatchRule
import com.discord.simpleast.code.CodeRules.toMatchGroupRule
import com.discord.simpleast.core.node.Node
import com.discord.simpleast.core.node.StyleNode
import com.discord.simpleast.core.parser.ParseSpec
Expand Down Expand Up @@ -134,9 +134,9 @@ object Kotlin {
codeStyleProviders: CodeStyleProviders<RC>
): List<Rule<RC, Node<RC>, S>> =
listOf(
PATTERN_KOTLIN_COMMENTS.toFullMatchRule(codeStyleProviders.commentStyleProvider),
PATTERN_KOTLIN_STRINGS.toFullMatchRule(codeStyleProviders.literalStyleProvider),
PATTERN_KOTLIN_ANNOTATION.toFullMatchRule(codeStyleProviders.genericsStyleProvider),
PATTERN_KOTLIN_COMMENTS.toMatchGroupRule(stylesProvider = codeStyleProviders.commentStyleProvider),
PATTERN_KOTLIN_STRINGS.toMatchGroupRule(stylesProvider = codeStyleProviders.literalStyleProvider),
PATTERN_KOTLIN_ANNOTATION.toMatchGroupRule(stylesProvider = codeStyleProviders.genericsStyleProvider),
FieldNode.createFieldRule(codeStyleProviders),
FunctionNode.createFunctionRule(codeStyleProviders),
)
Expand Down

0 comments on commit 63234ee

Please sign in to comment.