TextMate grammar based syntax highlighter for MoonBit
moon add mizchi/tmgrammartmgrammar/
├── oniguruma/ # Oniguruma regex to JS RegExp converter
├── shiki/ # Shiki-compatible HTML highlighter
├── tui/ # Terminal ANSI highlighter
└── grammars/ # Language grammar definitions (37 languages){
"import": [
"mizchi/tmgrammar/oniguruma"
]
}// Convert pattern with default options (ES2024 target)
///|
let result = @oniguruma.to_pattern("hello")
// Convert with flags
///|
let result = @oniguruma.to_pattern("pattern", flags="im")
// Convert with specific target
///|
let result = @oniguruma.to_pattern("pattern", target=@oniguruma.ES2018){
"import": [
"mizchi/tmgrammar/shiki"
]
}// Create highlighted lines
///|
let lines = [
@shiki.HighlightedLine::new([
@shiki.HighlightToken::new("const", scopes=["keyword"]),
@shiki.HighlightToken::new(" x", scopes=["variable"]),
]),
]
// Render to HTML with theme
///|
let html = @shiki.render_to_html(lines, @shiki.dark_plus_theme()){
"import": [
"mizchi/tmgrammar/tui"
]
}// Basic colorization
///|
let colored = @tui.colorize("Hello", fg=@tui.Red, style=@tui.Bold)
// RGB TrueColor support
///|
let rgb_text = @tui.colorize("Orange", fg=@tui.Rgb(255, 165, 0))
// Render tokens with theme
///|
let lines = [
@tui.TermLine::new([
@tui.TermToken::new("fn ", scopes=["keyword"]),
@tui.TermToken::new("main", scopes=["entity.name.function"]),
]),
]
let output = @tui.render_to_ansi(lines, @tui.dark_theme())
// With line numbers
///|
let options = @tui.TermRenderOptions::new(line_numbers=true)
let output = @tui.render_to_ansi(lines, @tui.monokai_theme(), options~){
"import": [
{ "path": "mizchi/tmgrammar/grammars/javascript", "alias": "js_grammar" },
{ "path": "mizchi/tmgrammar/grammars/typescript", "alias": "ts_grammar" }
]
}// Get grammar definition
///|
let js = @js_grammar.grammar()
// Compile all patterns to JS RegExp
///|
let compiled = js.compile()
// Access raw patterns
///|
let patterns = @js_grammar.all_patterns()
///|
let count = @js_grammar.pattern_count()| Language | Patterns | Passed | Success Rate |
|---|---|---|---|
| JavaScript | 346 | 346 | 100% |
| TypeScript | 333 | 333 | 100% |
| Swift | 329 | 319 | 97% |
| PHP | 320 | 256 | 80% |
| C++ | 216 | 183 | 85% |
| Python | 216 | 216 | 100% |
| Ruby | 205 | 204 | 99% |
| C | 171 | 171 | 100% |
| Bash | 144 | 141 | 98% |
| Java | 140 | 140 | 100% |
| CSS | 136 | 135 | 99% |
| Go | 125 | 99 | 79% |
| Markdown | 123 | 123 | 100% |
| Lua | 112 | 112 | 100% |
| Scala | 112 | 112 | 100% |
| HTML | 112 | 109 | 97% |
| Svelte | 109 | 109 | 100% |
| SCSS | 104 | 104 | 100% |
| Rust | 89 | 89 | 100% |
| Vue | 72 | 72 | 100% |
| Vim | 66 | 66 | 100% |
| SQL | 66 | 66 | 100% |
| GraphQL | 62 | 62 | 100% |
| Kotlin | 58 | 58 | 100% |
| Make | 51 | 51 | 100% |
| YAML | 45 | 44 | 98% |
| TOML | 44 | 44 | 100% |
| MoonBit | 43 | 43 | 100% |
| Astro | 35 | 35 | 100% |
| Haskell | 35 | 35 | 100% |
| Protobuf | 33 | 33 | 100% |
| XML | 31 | 31 | 100% |
| Elixir | 26 | 26 | 100% |
| Dart | 26 | 26 | 100% |
| C# | 24 | 24 | 100% |
| JSON | 19 | 19 | 100% |
| Docker | 7 | 7 | 100% |
| Total | 4003 | 3861 | 96% |
TextMate grammar based syntax highlighter for MoonBit