Skip to content

Commit

Permalink
Release 1.4.2
Browse files Browse the repository at this point in the history
  • Loading branch information
naulian-codigo committed May 15, 2024
1 parent a39a46f commit c39d161
Show file tree
Hide file tree
Showing 23 changed files with 443 additions and 430 deletions.
1 change: 1 addition & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,5 @@ dependencies {

implementation(project(":glow"))
implementation(project(":glow-compose"))
implementation(project(":glow-core"))
}
1 change: 1 addition & 0 deletions app/src/main/assets/sample.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

public class CoinFlippingGame {
public static String flipCoin() {
HashMap<String, Integer> map = new HashMap<>();
Random random = new Random();
int result = random.nextInt(2); // 0 for Heads, 1 for Tails
return (result == 0) ? "Heads" : "Tails";
Expand Down
23 changes: 12 additions & 11 deletions app/src/main/java/com/example/glow/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import com.naulian.glow.CodeTheme
import com.naulian.glow.glowSyntax
import com.naulian.glow.setCodeTheme
import com.naulian.glow_compose.Glow
import com.naulian.glow_compose.toComposeColor
import com.naulian.glow_compose.hexToColor

class MainActivity : AppCompatActivity() {
@Suppress("unused")
Expand All @@ -27,24 +27,25 @@ class MainActivity : AppCompatActivity() {
val binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)

val filename = "sample.kt"
val language = "kt"
val theme = CodeTheme.defaultDark
val filename = "sample.java"
val language = "java"
val lightTheme = CodeTheme.defaultLight
val darkTheme = CodeTheme.defaultDark

binding.apply {
textOutput.setCodeTheme(theme.normal)
textOutput.setCodeTheme(lightTheme.normal)
readStringAsset(filename) { result ->
result.onSuccess {
val source = it
result.onSuccess { source ->
textInput.setText(source)
val highlighted = glowSyntax(source, language, theme)
val highlighted = glowSyntax(source, language, lightTheme)
textSource.text = highlighted.raw
textOutput.text = highlighted.spanned

val highLightedCompose = Glow.highlight(source, language, theme)
val highLightedCompose = Glow.highlight(source, language, darkTheme)

composeView.setContent {
LazyRow(modifier = Modifier
.background(theme.background.toComposeColor())
.background(darkTheme.background.hexToColor())
.padding(16.dp)) {
item { Text(text = highLightedCompose.value) }
}
Expand All @@ -62,7 +63,7 @@ class MainActivity : AppCompatActivity() {
/*val strTokens = StrTokens(text).tokenize()
textSource.text = strTokens.toString()*/

val highlighted = glowSyntax(text, language, theme)
val highlighted = glowSyntax(text, language, lightTheme)
textSource.text = highlighted.raw
textOutput.text = highlighted.spanned
}
Expand Down
34 changes: 17 additions & 17 deletions glow-compose/src/main/java/com/naulian/glow_compose/Glow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fun String.toAnnotatedString(): AnnotatedString {
}
}

fun String.toComposeColor(): Color {
fun String.hexToColor(): Color {
return if (startsWith("#")) {
val legacyColor = LegacyColor.parseColor(this)
Color(legacyColor)
Expand Down Expand Up @@ -76,11 +76,11 @@ object Glow {
com.naulian.glow_core.Type.VALUE_FLOAT -> theme.number
com.naulian.glow_core.Type.CHAR -> theme.string
com.naulian.glow_core.Type.STRING -> theme.string
com.naulian.glow_core.Type.ASSIGNMENT -> theme.normal
com.naulian.glow_core.Type.MCOMMENT -> theme.comment
com.naulian.glow_core.Type.SCOMMENT -> theme.comment
com.naulian.glow_core.Type.EQUAL_TO -> theme.normal
com.naulian.glow_core.Type.M_COMMENT -> theme.comment
com.naulian.glow_core.Type.S_COMMENT -> theme.comment
else -> theme.normal
}.toComposeColor()
}.hexToColor()

val hlCode = it.value
withStyle(style = spanStyle(color = hlColor)) {
Expand Down Expand Up @@ -111,11 +111,11 @@ object Glow {
com.naulian.glow_core.Type.VALUE_FLOAT -> theme.number
com.naulian.glow_core.Type.CHAR -> theme.string
com.naulian.glow_core.Type.STRING -> theme.string
com.naulian.glow_core.Type.ASSIGNMENT -> theme.normal
com.naulian.glow_core.Type.MCOMMENT -> theme.comment
com.naulian.glow_core.Type.SCOMMENT -> theme.comment
com.naulian.glow_core.Type.EQUAL_TO -> theme.normal
com.naulian.glow_core.Type.M_COMMENT -> theme.comment
com.naulian.glow_core.Type.S_COMMENT -> theme.comment
else -> theme.normal
}.toComposeColor()
}.hexToColor()

val hlCode = it.value

Expand Down Expand Up @@ -146,10 +146,10 @@ object Glow {
com.naulian.glow_core.Type.VALUE_LONG -> theme.number
com.naulian.glow_core.Type.VALUE_FLOAT -> theme.number
com.naulian.glow_core.Type.STRING -> theme.string
com.naulian.glow_core.Type.ASSIGNMENT -> theme.normal
com.naulian.glow_core.Type.SCOMMENT -> theme.comment
com.naulian.glow_core.Type.EQUAL_TO -> theme.normal
com.naulian.glow_core.Type.S_COMMENT -> theme.comment
else -> theme.normal
}.toComposeColor()
}.hexToColor()

val hlCode = it.value
withStyle(style = spanStyle(color = hlColor)) {
Expand Down Expand Up @@ -184,12 +184,12 @@ object Glow {
com.naulian.glow_core.Type.STRING -> theme.string
com.naulian.glow_core.Type.STRING_BRACE -> theme.keyword
com.naulian.glow_core.Type.INTERPOLATION -> theme.property
com.naulian.glow_core.Type.ASSIGNMENT -> theme.normal
com.naulian.glow_core.Type.ESCAPE -> theme.keyword
com.naulian.glow_core.Type.MCOMMENT -> theme.comment
com.naulian.glow_core.Type.SCOMMENT -> theme.comment
com.naulian.glow_core.Type.EQUAL_TO -> theme.normal
com.naulian.glow_core.Type.B_SCAPE -> theme.keyword
com.naulian.glow_core.Type.M_COMMENT -> theme.comment
com.naulian.glow_core.Type.S_COMMENT -> theme.comment
else -> theme.normal
}.toComposeColor()
}.hexToColor()

val hlCode = it.value
withStyle(style = spanStyle(color = hlColor)) {
Expand Down
32 changes: 16 additions & 16 deletions glow-compose/src/main/java/com/naulian/glow_compose/GlowJob.kt
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ class GlowJob {
Type.VALUE_FLOAT -> theme.number
Type.CHAR -> theme.string
Type.STRING -> theme.string
Type.ASSIGNMENT -> theme.normal
Type.MCOMMENT -> theme.comment
Type.SCOMMENT -> theme.comment
Type.EQUAL_TO -> theme.normal
Type.M_COMMENT -> theme.comment
Type.S_COMMENT -> theme.comment
else -> theme.normal
}.toComposeColor()
}.hexToColor()

val hlCode = it.value
withStyle(style = spanStyle(color = hlColor)) {
Expand Down Expand Up @@ -100,11 +100,11 @@ class GlowJob {
Type.VALUE_FLOAT -> theme.number
Type.CHAR -> theme.string
Type.STRING -> theme.string
Type.ASSIGNMENT -> theme.normal
Type.MCOMMENT -> theme.comment
Type.SCOMMENT -> theme.comment
Type.EQUAL_TO -> theme.normal
Type.M_COMMENT -> theme.comment
Type.S_COMMENT -> theme.comment
else -> theme.normal
}.toComposeColor()
}.hexToColor()

val hlCode = it.value

Expand Down Expand Up @@ -133,10 +133,10 @@ class GlowJob {
Type.VALUE_LONG -> theme.number
Type.VALUE_FLOAT -> theme.number
Type.STRING -> theme.string
Type.ASSIGNMENT -> theme.normal
Type.SCOMMENT -> theme.comment
Type.EQUAL_TO -> theme.normal
Type.S_COMMENT -> theme.comment
else -> theme.normal
}.toComposeColor()
}.hexToColor()

val hlCode = it.value
withStyle(style = spanStyle(color = hlColor)) {
Expand Down Expand Up @@ -169,12 +169,12 @@ class GlowJob {
Type.STRING -> theme.string
Type.STRING_BRACE -> theme.keyword
Type.INTERPOLATION -> theme.property
Type.ASSIGNMENT -> theme.normal
Type.ESCAPE -> theme.keyword
Type.MCOMMENT -> theme.comment
Type.SCOMMENT -> theme.comment
Type.EQUAL_TO -> theme.normal
Type.B_SCAPE -> theme.keyword
Type.M_COMMENT -> theme.comment
Type.S_COMMENT -> theme.comment
else -> theme.normal
}.toComposeColor()
}.hexToColor()

val hlCode = it.value
withStyle(style = spanStyle(color = hlColor)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class KtLexer(private val input: String) {
return when (val char = currentChar()) {
' ' -> whitespaceToken()
'\n' -> createToken(Type.NEWLINE, "\n")
'*' -> createToken(Type.ASTERISK, char)
'*' -> createToken(Type.STAR, char)
'.' -> createToken(Type.DOT, char)
'-' -> createToken(Type.DASH, char)
'@' -> createToken(Type.AT, char)
Expand All @@ -31,28 +31,28 @@ class KtLexer(private val input: String) {
'%' -> createToken(Type.MODULO, char)
'^' -> createToken(Type.POW, char)
'&' -> createToken(Type.AND, char)
'?' -> createToken(Type.QMARK, char)
'?' -> createToken(Type.Q_MARK, char)
'|' -> createToken(Type.OR, char)
'\\' -> createToken(Type.ESCAPE, char)
'\\' -> createToken(Type.B_SCAPE, char)
'!' -> createToken(Type.BANG, char)
'{' -> createToken(Type.LBRACE, char)
'}' -> createToken(Type.RBRACE, char)
'(' -> createToken(Type.LPAREN, char)
')' -> createToken(Type.RPAREN, char)
'{' -> createToken(Type.L_BRACE, char)
'}' -> createToken(Type.R_BRACE, char)
'(' -> createToken(Type.L_PAREN, char)
')' -> createToken(Type.R_PAREN, char)
',' -> createToken(Type.COMMA, char)
':' -> createToken(Type.COLON, char)
'>' -> createToken(Type.GT, char)
'<' -> createToken(Type.LT, char)
';' -> createToken(Type.SEMICOLON, char)
';' -> createToken(Type.S_COLON, char)
'+' -> createToken(Type.PLUS, char)
'=' -> createToken(Type.ASSIGNMENT, char)
'[' -> createToken(Type.LBRACK, char)
']' -> createToken(Type.RBRACKET, char)
'=' -> createToken(Type.EQUAL_TO, char)
'[' -> createToken(Type.L_BRACKET, char)
']' -> createToken(Type.R_BRACKET, char)
'/' -> {
when (input[position + 1]) {
'/' -> lexSingleLineComment()
'*' -> lexMultiLineComment()
else -> createToken(Type.FSLASH, char)
else -> createToken(Type.F_SLASH, char)
}
}

Expand All @@ -72,7 +72,7 @@ class KtLexer(private val input: String) {
} while (currentChar() != '\n' && currentChar() != Char.MIN_VALUE)

val identifier = input.substring(start, position)
return Token(Type.SCOMMENT, identifier)
return Token(Type.S_COMMENT, identifier)
}

private fun lexMultiLineComment(): Token {
Expand All @@ -83,7 +83,7 @@ class KtLexer(private val input: String) {
position++

val identifier = input.substring(start, position)
return Token(Type.MCOMMENT, identifier)
return Token(Type.M_COMMENT, identifier)
}

private fun createToken(type: Type, char: Char) = createToken(type, char.toString())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ fun tokenizeKt(input: String): List<Token> {

//based on previous
val modified = when (prevToken.type) {
Type.ASSIGNMENT -> numberToken(token)
Type.LPAREN -> argumentToken(token)
Type.EQUAL_TO -> numberToken(token)
Type.L_PAREN -> argumentToken(token)
Type.FUNCTION -> token.copy(type = Type.FUNC_NAME)
Type.CLASS -> token.copy(type = Type.CLASS_NAME)
Type.COLON -> token.copy(type = Type.DATA_TYPE)
Expand All @@ -41,7 +41,7 @@ fun tokenizeKt(input: String): List<Token> {
}
}

Type.LPAREN -> {
Type.L_PAREN -> {
tokens.getOrNull(prevIndex - 1)?.let {
if (it.type == Type.DOT) {
tokens[prevIndex] = prevToken.copy(type = Type.FUNC_CALL)
Expand Down

This file was deleted.

98 changes: 98 additions & 0 deletions glow-core/src/main/java/com/naulian/glow_core/Lexer.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package com.naulian.glow_core

private val symbols = listOf(
'*', '.', '-', '@', '#', '$', '%', '^', '&', '?',
'|', '\\', '!', '{', '}', '(', ')', ',', ':', '>',
'<', ';', '+', '=', '[', ']', '/', '\'', '\"',
)

class Lexer(private val source: CharSequence) {
private var cursor: Int = 0
private fun char() = if (cursor < source.length) source[cursor] else Char.MIN_VALUE

fun nextToken(): Token {
return when (val c = char()) {
' ' -> createSpaceToken()
'\n' -> createToken(Type.NEWLINE, c)
'*' -> createToken(Type.STAR, c)
'.' -> createToken(Type.DOT, c)
'-' -> createToken(Type.DASH, c)
'@' -> createToken(Type.AT, c)
'#' -> createToken(Type.HASH, c)
'$' -> createToken(Type.DOLLAR, c)
'%' -> createToken(Type.MODULO, c)
'^' -> createToken(Type.POW, c)
'&' -> createToken(Type.AND, c)
'?' -> createToken(Type.Q_MARK, c)
'|' -> createToken(Type.OR, c)
'\\' -> createToken(Type.B_SLASH, c)
'!' -> createToken(Type.BANG, c)
'{' -> createToken(Type.L_BRACE, c)
'}' -> createToken(Type.R_BRACE, c)
'(' -> createToken(Type.L_PAREN, c)
')' -> createToken(Type.R_PAREN, c)
',' -> createToken(Type.COMMA, c)
':' -> createToken(Type.COLON, c)
'>' -> createToken(Type.GT, c)
'<' -> createToken(Type.LT, c)
';' -> createToken(Type.S_COLON, c)
'+' -> createToken(Type.PLUS, c)
'=' -> createToken(Type.EQUAL_TO, c)
'[' -> createToken(Type.L_BRACKET, c)
']' -> createToken(Type.R_BRACKET, c)
'/' -> createToken(Type.F_SLASH, c)
'\'' -> createToken(Type.S_QUOTE, c)
'\"' -> createToken(Type.D_QUOTE, c)
in '0'..'9' -> createNumberToken()
in 'a'..'z',
in 'A'..'Z',
'_' -> createWordToken()

Char.MIN_VALUE -> createToken(Type.EOF, c)
else -> createToken(Type.ILLEGAL, c)
}
}

private fun peek(offset: Int = 1): Char {
val position = cursor + offset
return if (position < source.length) source[position] else Char.MIN_VALUE
}

private fun createToken(type: Type, value: Char): Token {
cursor++
return Token(type, value.toString())
}

private fun createSpaceToken(): Token {
val start = cursor
while (char() == ' ') {
cursor++
}

val identifier = source.substring(start, cursor)
return Token(Type.SPACE, identifier)
}

private fun createWordToken(): Token {
val start = cursor
while (char().isLetter() || char() == '_' || char().isDigit()) {
cursor++
}

val identifier = source.substring(start, cursor)
return Token(Type.WORD, identifier)
}

private fun createNumberToken(): Token {
val start = cursor

while (
char().isDigit() || char() == '_' ||
char() == 'L' || char() == 'f' ||
char() == '.'
) {
cursor++
}
return Token(Type.NUMBER, source.substring(start, cursor))
}
}
Loading

0 comments on commit c39d161

Please sign in to comment.