io.github.dingyi222666.monarch.languages.LanguageMarkdown.kt Maven / Gradle / Ivy
The newest version!
package io.github.dingyi222666.monarch.languages
import io.github.dingyi222666.monarch.common.*
import io.github.dingyi222666.monarch.loader.dsl.*
import io.github.dingyi222666.monarch.types.IMonarchLanguage
public val MarkdownLanguage: IMonarchLanguage by lazy {
buildMonarchLanguage {
tokenPostfix = ".md"
defaultToken = ""
control("[\\\\`*_\\[\\]{}()#+\\-\\.!]")
"noncontrol" and "[^\\\\`*_\\[\\]{}()#+\\-\\.!]"
escapes("\\\\(?:@control)")
"jsescapes" and "\\\\(?:[btnfr\\\\\"']|[0-7][0-7]?|[0-3][0-7]{2})"
"empty" and listOf("area", "base", "basefont", "br", "col", "frame", "hr", "img", "input",
"isindex", "link", "meta", "param")
tokenizer {
root {
"^\\s*\\|".action("@rematch").state("@table_header")
"^(\\s{0,3})(#+)((?:[^\\\\#]|@escapes)+)((?:#+)?)".actionArray {
token("white")
token("keyword")
token("keyword")
token("keyword")
}
"^\\s*(=+|\\-+)\\s*${'$'}".token("keyword")
"^\\s*((\\*[ ]?)+)\\s*${'$'}".token("meta.separator")
"^\\s*>+".token("comment")
"^\\s*([\\*\\-+:]|\\d+\\.)\\s".token("keyword")
"^(\\t|[ ]{4})[^ ].*${'$'}".token("string")
"^\\s*~~~\\s*((?:\\w|[\\/\\-#])+)?\\s*${'$'}".action {
token = "string"
next = "@codeblock"
}
"^\\s*```\\s*((?:\\w|[\\/\\-#])+).*${'$'}".action {
token = "string"
next = "@codeblockgh"
nextEmbedded = "${'$'}1"
nextEmbedded = "${'$'}1"
}
"^\\s*```\\s*${'$'}".action {
token = "string"
next = "@codeblock"
}
include("@linecontent")
}
"table_header" rules {
include("@table_common")
"[^\\|]+".token("keyword.table.header")
}
"table_body" rules {
include("@table_common")
include("@linecontent")
}
"table_common" rules {
"\\s*[\\-:]+\\s*".action {
token = "keyword"
switchTo = "table_body"
}
"^\\s*\\|".token("keyword.table.left")
"^\\s*[^\\|]".action("@rematch").state("@pop")
"^\\s*${'$'}".action("@rematch").state("@pop")
"\\|".action {
cases {
"@eos" and "keyword.table.right"
"@default" and "keyword.table.middle"
}
}
}
"codeblock" rules {
"^\\s*~~~\\s*${'$'}".action {
token = "string"
next = "@pop"
}
"^\\s*```\\s*${'$'}".action {
token = "string"
next = "@pop"
}
".*${'$'}".token("variable.source")
}
"codeblockgh" rules {
"```\\s*${'$'}".action {
token = "string"
next = "@pop"
nextEmbedded = "@pop"
nextEmbedded = "@pop"
}
"[^`]+".token("variable.source")
}
"linecontent" rules {
"&\\w+;".token("string.escape")
"@escapes".token("escape")
"\\b__([^\\\\_]|@escapes|_(?!_))+__\\b".token("strong")
"\\*\\*([^\\\\*]|@escapes|\\*(?!\\*))+\\*\\*".token("strong")
"\\b_[^_]+_\\b".token("emphasis")
"\\*([^\\\\*]|@escapes)+\\*".token("emphasis")
"`([^\\\\`]|@escapes)+`".token("variable")
"\\{+[^}]+\\}+".token("string.target")
"(!?\\[)((?:[^\\]\\\\]|@escapes)*)(\\]\\([^\\)]+\\))".actionArray {
token("string.link")
token("")
token("string.link")
}
"(!?\\[)((?:[^\\]\\\\]|@escapes)*)(\\])".token("string.link")
include("html")
}
"html" rules {
"<(\\w+)\\/>".token("tag")
"<(\\w+)(\\-|\\w)*".action {
cases {
"@empty" and {
token = "tag"
next = "@tag.${'$'}1"
}
"@default" and {
token = "tag"
next = "@tag.${'$'}1"
}
}
}
"<\\/(\\w+)(\\-|\\w)*\\s*>".action {
token = "tag"
}
"".action("comment").state("@pop")
"