Skip to content

Commit

Permalink
✨Reimplement const colors parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
FelixDes committed Sep 16, 2023
1 parent c7b9fa0 commit 81782be
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 25 deletions.
15 changes: 0 additions & 15 deletions src/main/kotlin/color/ConstantColors.kt

This file was deleted.

18 changes: 18 additions & 0 deletions src/main/kotlin/color/ConstantColorsService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package color

import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json
import utils.ResourcePaths.CONSTANT_COLORS
import java.io.File
import java.util.regex.Pattern
import java.util.regex.Pattern.CASE_INSENSITIVE


object ConstantColorsService {
private val colors: Map<String, CssColor> =
Json.decodeFromString<Map<String, String>>(CONSTANT_COLORS.getResource())
.mapValues { CssColor.fromHEX(it.value) }

fun getNamesRegex() = Pattern.compile(colors.keys.joinToString(separator = "|"), CASE_INSENSITIVE)!!.toRegex()
operator fun get(name: String): CssColor = colors[name]!!
}
2 changes: 1 addition & 1 deletion src/main/kotlin/color/CssColor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ data class CssColor(
}
}

fun fromConstant(constant: String): CssColor = ConstantColors[constant.lowercase()]
fun fromConstant(constant: String): CssColor = ConstantColorsService[constant.lowercase()]
}


Expand Down
10 changes: 2 additions & 8 deletions src/main/kotlin/translator/tokenization/Token.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package translator.tokenization

import color.ConstantColorsService
import translator.tokenization.TokenType.*

enum class TokenType {
Expand Down Expand Up @@ -44,14 +45,7 @@ val CSS_EXPRESSION_TOKENS: List<Token> = listOf(
Token(OPERATOR_MUL, """^\*""".toRegex(), "*"),
Token(OPERATOR_DIV, """^/""".toRegex(), "/"),

Token(
COLOR_CONST,
"""aliceblue|antiquewhite|aqua|aquamarine|azure|beige|bisque|black|blanchedalmond|blue|blueviolet|brown|burlywood|cadetblue|chartreuse|chocolate|coral|cornflowerblue|cornsilk|crimson|cyan|darkblue|darkcyan|darkgoldenrod|darkgray|darkgreen|darkgrey|darkkhaki|darkmagenta|darkolivegreen|darkorange|darkorchid|darkred|darksalmon|darkseagreen|darkslateblue|darkslategray|darkslategrey|darkturquoise|darkviolet|deeppink|deepskyblue|dimgray|dimgrey|dodgerblue|firebrick|floralwhite|forestgreen|fuchsia|gainsboro|ghostwhite|goldenrod|gold|gray|green|greenyellow|grey|honeydew|hotpink|indianred|indigo|ivory|khaki|lavenderblush|lavender|lawngreen|lemonchiffon|lightblue|lightcoral|lightcyan|lightgoldenrodyellow|lightgray|lightgreen|lightgrey|lightpink|lightsalmon|lightseagreen|lightskyblue|lightslategray|lightslategrey|lightsteelblue|lightyellow|lime|limegreen|linen|magenta|maroon|mediumaquamarine|mediumblue|mediumorchid|mediumpurple|mediumseagreen|mediumslateblue|mediumspringgreen|mediumturquoise|mediumvioletred|midnightblue|mintcream|mistyrose|moccasin|navajowhite|navy|oldlace|olive|olivedrab|orange|orangered|orchid|palegoldenrod|palegreen|paleturquoise|palevioletred|papayawhip|peachpuff|peru|pink|plum|powderblue|purple|rebeccapurple|red|rosybrown|royalblue|saddlebrown|salmon|sandybrown|seagreen|seashell|sienna|silver|skyblue|slateblue|slategray|slategrey|snow|springgreen|steelblue|tan|teal|thistle|tomato|turquoise|violet|wheat|white|whitesmoke|yellow|yellowgreen"""
.toRegex(
RegexOption.IGNORE_CASE
),
"constant colors"
),
Token(COLOR_CONST, ConstantColorsService.getNamesRegex(), "constant colors"),

Token(
COLOR_HEX,
Expand Down
8 changes: 7 additions & 1 deletion src/main/kotlin/utils/ResourceUtils.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package utils

import java.io.InputStream


class ResourceException(path: String) : RuntimeException("Cannot find the resource: $path")

private fun InputStream.getStringContent() = this.bufferedReader().use { it.readText() }

enum class ResourcePaths(private val path: String) {
CONSTANT_COLORS("/colors/constant_color_names.json");

fun getResourceUri() = this::class.java.getResource(path)?.toURI() ?: throw ResourceException(path)
fun getResource() =
object {}.javaClass.getResourceAsStream(path)?.getStringContent() ?: throw ResourceException(path)
}

0 comments on commit 81782be

Please sign in to comment.