Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions app/src/main/java/com/discord/simpleast/sample/SampleTexts.kt
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,39 @@ object SampleTexts {
```
"""

private const val CODE_BLOCK_GO = """
Go code block:
```go
package main

import "fmt"

type User struct {
ID uint64
Name string
}

const (
// Create a huge number by shifting a 1 bit left 100 places.
// In other words, the binary number that is 1 followed by 100 zeroes.
Big = 1 << 100
// Shift it right again 99 places, so we end up with 1<<1, or 2.
Small = Big >> 99
)

func needInt(x int) int { return x*10 + 1 }
func needFloat(x float64) float64 {
return x * 0.1
}

func main() {
fmt.Println(needInt(Small))
fmt.Println(needFloat(Small))
fmt.Println(needFloat(Big))
}
```
"""

const val CODE_BLOCKS = """
# Code block samples
inlined:```py language code blocks need newline```
Expand All @@ -266,6 +299,7 @@ object SampleTexts {
$CODE_BLOCK_XML
$CODE_BLOCK_CRYSTAL
$CODE_BLOCK_JAVASCRIPT
$CODE_BLOCK_GO

That should do it....
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,27 @@ object CodeRules {
builtIns = JavaScript.BUILT_INS,
keywords = JavaScript.KEYWORDS)

val goRules = createGenericCodeRules<R, S>(
codeStyleProviders,
additionalRules = listOf(
Pattern.compile("""^(?:(?://.*?(?=\n|$))|(/\*.*?\*/))""", Pattern.DOTALL)
.toMatchGroupRule(stylesProvider = codeStyleProviders.commentStyleProvider),
Pattern.compile("""^(".*?(?<!\\)"|`[\s\S]*?(?<!\\)`)(?=\W|\s|$)""")
.toMatchGroupRule(stylesProvider = codeStyleProviders.literalStyleProvider),
Pattern.compile("""^func""")
.toMatchGroupRule(stylesProvider = codeStyleProviders.keywordStyleProvider),
Pattern.compile("""^[a-z0-9_]+(?=\()""", Pattern.CASE_INSENSITIVE)
.toMatchGroupRule(stylesProvider = codeStyleProviders.identifierStyleProvider),
),
definitions = arrayOf("package", "type", "var|const"),
builtIns = arrayOf(
"nil|iota|true|false|bool",
"byte|complex(?:64|128)|error|float(?:32|64)|rune|string|u?int(?:8|16|32|64)?|uintptr"
),
"break|case|chan|continue|default|defer|else|fallthrough|for|go(?:to)?|if|import|interface|map|range|return|select|switch|struct",
"type|var|const"
)

return mapOf(
"kt" to kotlinRules,
"kotlin" to kotlinRules,
Expand All @@ -228,6 +249,8 @@ object CodeRules {

"js" to javascriptRules,
"javascript" to javascriptRules,

"go" to goRules,
)
}

Expand Down
196 changes: 196 additions & 0 deletions simpleast-core/src/test/java/com/discord/simpleast/code/GoRulesTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
package com.discord.simpleast.code

import com.discord.simpleast.assertNodeContents
import com.discord.simpleast.core.node.*
import com.discord.simpleast.core.parser.Parser
import com.discord.simpleast.core.simple.SimpleMarkdownRules
import com.discord.simpleast.core.utils.TreeMatcher
import org.junit.Before
import org.junit.Test

class GoRulesTest {

private class TestState

private lateinit var parser: Parser<TestRenderContext, Node<TestRenderContext>, TestState>
private lateinit var treeMatcher: TreeMatcher

@Before
fun setup() {
val codeStyleProviders = CodeStyleProviders<TestRenderContext>()
parser = Parser()
parser
.addRule(CodeRules.createCodeRule(
codeStyleProviders.defaultStyleProvider,
CodeRules.createCodeLanguageMap(codeStyleProviders))
)
.addRules(SimpleMarkdownRules.createSimpleMarkdownRules())
treeMatcher = TreeMatcher()
treeMatcher.registerDefaultMatchers()
}

@Test
fun comments() {
val ast = parser.parse("""
```go
/** Multiline
Comment
*/
foo.bar() // Inlined
// Line comment
```
""".trimIndent(), TestState())

ast.assertNodeContents<StyleNode.TextStyledNode<*>>(
"""
/** Multiline
Comment
*/
""".trimIndent(),
"bar",
"// Inlined",
"// Line comment")
}

@Test
fun strings() {
val ast = parser.parse("""
```go
x := "Hello"
println(`world`)
```
""".trimIndent(), TestState())

ast.assertNodeContents<StyleNode.TextStyledNode<*>>(
""""Hello"""",
"println",
"`world`")
}

@Test
fun stringsMultiline() {
val ast = parser.parse("""
```go
text := `
hello
world
`
```
""".trimIndent(), TestState())

ast.assertNodeContents<StyleNode.TextStyledNode<*>>(
"""
`
hello
world
`
""".trimIndent())
}

@Test
fun functions() {
val ast = parser.parse("""
```go
func bar(a int) bool {
return false
}

bar(1)
```
""".trimIndent(), TestState())

ast.assertNodeContents<StyleNode.TextStyledNode<*>>(
"func",
"bar",
"int",
"bool",
"return",
"false",
"bar",
"1")
}

@Test
fun keywords() {
val ast = parser.parse("""
```go
package main

import "fmt"

func main() {
for {
if false {
continue
} else {
break
}
}

switch a {
case 0:
fallthrough
case 1:
fmt.Println("a")
}
}
```
""".trimIndent(), TestState())

ast.assertNodeContents<StyleNode.TextStyledNode<*>>(
"package",
" main",
"import",
""""fmt"""",
"func",
"main",
"for",
"if",
"false",
"continue",
"else",
"break",
"switch",
"case",
"0",
"fallthrough",
"case",
"1",
"Println",
""""a"""")
}

@Test
fun numbers() {
val ast = parser.parse("""
```go
x := 0
x += 12
add(123,456)
```
""".trimIndent(), TestState())
ast.assertNodeContents<StyleNode.TextStyledNode<*>>(
"0", "12", "add", "123", "456"
)
}

@Test
fun structDef() {
val ast = parser.parse("""
```go
type User struct {
ID uint64
Name string
}
```
""".trimIndent(), TestState())

ast.assertNodeContents<CodeNode.DefinitionNode<*>>("type User")
ast.assertNodeContents<StyleNode.TextStyledNode<*>>(
"type",
" User",
"struct",
"uint64",
"string")
}
}