diff --git a/app/src/main/java/com/discord/simpleast/sample/SampleTexts.kt b/app/src/main/java/com/discord/simpleast/sample/SampleTexts.kt index 74460a6..2906305 100644 --- a/app/src/main/java/com/discord/simpleast/sample/SampleTexts.kt +++ b/app/src/main/java/com/discord/simpleast/sample/SampleTexts.kt @@ -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``` @@ -266,6 +299,7 @@ object SampleTexts { $CODE_BLOCK_XML $CODE_BLOCK_CRYSTAL $CODE_BLOCK_JAVASCRIPT + $CODE_BLOCK_GO That should do it.... """ 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..8af9fdb 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 @@ -203,6 +203,27 @@ object CodeRules { builtIns = JavaScript.BUILT_INS, keywords = JavaScript.KEYWORDS) + val goRules = createGenericCodeRules( + codeStyleProviders, + additionalRules = listOf( + Pattern.compile("""^(?:(?://.*?(?=\n|$))|(/\*.*?\*/))""", Pattern.DOTALL) + .toMatchGroupRule(stylesProvider = codeStyleProviders.commentStyleProvider), + Pattern.compile("""^(".*?(?, TestState> + private lateinit var treeMatcher: TreeMatcher + + @Before + fun setup() { + val codeStyleProviders = CodeStyleProviders() + 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>( + """ + /** Multiline + Comment + */ + """.trimIndent(), + "bar", + "// Inlined", + "// Line comment") + } + + @Test + fun strings() { + val ast = parser.parse(""" + ```go + x := "Hello" + println(`world`) + ``` + """.trimIndent(), TestState()) + + ast.assertNodeContents>( + """"Hello"""", + "println", + "`world`") + } + + @Test + fun stringsMultiline() { + val ast = parser.parse(""" + ```go + text := ` + hello + world + ` + ``` + """.trimIndent(), TestState()) + + ast.assertNodeContents>( + """ + ` + hello + world + ` + """.trimIndent()) + } + + @Test + fun functions() { + val ast = parser.parse(""" + ```go + func bar(a int) bool { + return false + } + + bar(1) + ``` + """.trimIndent(), TestState()) + + ast.assertNodeContents>( + "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>( + "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>( + "0", "12", "add", "123", "456" + ) + } + + @Test + fun structDef() { + val ast = parser.parse(""" + ```go + type User struct { + ID uint64 + Name string + } + ``` + """.trimIndent(), TestState()) + + ast.assertNodeContents>("type User") + ast.assertNodeContents>( + "type", + " User", + "struct", + "uint64", + "string") + } +}