659 lines
41 KiB
Go
659 lines
41 KiB
Go
package editor
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"path"
|
||
|
|
"regexp"
|
||
|
|
"strings"
|
||
|
|
"sync"
|
||
|
|
)
|
||
|
|
|
||
|
|
// LanguageFor returns the language of a file by its name, as
|
||
|
|
// "main.go" or "Makefile", or nil when MyGo knows none for it. A
|
||
|
|
// template's text is in the language its name gives before the
|
||
|
|
// template's extension: "compose.yaml.j2" is Jinja in YAML, and
|
||
|
|
// "notice.text.erb" ERB in plain text; without one, as "show.erb", it is
|
||
|
|
// in the template's own, HTML for ERB and plain text for Jinja.
|
||
|
|
func LanguageFor(filename string) *Language {
|
||
|
|
base := path.Base(strings.ReplaceAll(filename, "\\", "/"))
|
||
|
|
l := languageForBase(base)
|
||
|
|
if l == nil {
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
t, ok := l.Tokenizer.(*templateLexer)
|
||
|
|
if !ok {
|
||
|
|
return l
|
||
|
|
}
|
||
|
|
inner := base[:len(base)-len(path.Ext(base))]
|
||
|
|
host := languageForBase(inner)
|
||
|
|
if host == nil && path.Ext(inner) == "" {
|
||
|
|
return l
|
||
|
|
}
|
||
|
|
var tk Tokenizer
|
||
|
|
if host != nil {
|
||
|
|
if _, nested := host.Tokenizer.(*templateLexer); nested {
|
||
|
|
return l
|
||
|
|
}
|
||
|
|
tk = host.Tokenizer
|
||
|
|
}
|
||
|
|
if tk == t.host {
|
||
|
|
return l
|
||
|
|
}
|
||
|
|
return templateIn(l, t, host)
|
||
|
|
}
|
||
|
|
|
||
|
|
// templateIn returns template l with its text in host's language, plain
|
||
|
|
// text when host is nil: one language for each pair, made once.
|
||
|
|
func templateIn(l *Language, t *templateLexer, host *Language) *Language {
|
||
|
|
key := [2]*Language{l, host}
|
||
|
|
if in, ok := templateHosts.Load(key); ok {
|
||
|
|
return in.(*Language)
|
||
|
|
}
|
||
|
|
in, lt := *l, *t
|
||
|
|
lt.host = nil
|
||
|
|
if host != nil {
|
||
|
|
lt.host = host.Tokenizer
|
||
|
|
}
|
||
|
|
in.Tokenizer = <
|
||
|
|
got, _ := templateHosts.LoadOrStore(key, &in)
|
||
|
|
return got.(*Language)
|
||
|
|
}
|
||
|
|
|
||
|
|
// templateHosts holds the templates templateIn made, by template and host.
|
||
|
|
var templateHosts sync.Map
|
||
|
|
|
||
|
|
// languageForBase returns the language of a file named base, by its name
|
||
|
|
// or else its extension.
|
||
|
|
func languageForBase(base string) *Language {
|
||
|
|
ext := strings.ToLower(path.Ext(base))
|
||
|
|
for _, l := range Languages() {
|
||
|
|
for _, f := range l.Filenames {
|
||
|
|
if f == base {
|
||
|
|
return l
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
for _, l := range Languages() {
|
||
|
|
for _, e := range l.Extensions {
|
||
|
|
if e == ext {
|
||
|
|
return l
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// LanguageForText returns the language of a file by its name, as
|
||
|
|
// LanguageFor does, else by the interpreter its text's #! line runs, as
|
||
|
|
// "#!/usr/bin/env ruby" or "#!/bin/bash -e"; nil when neither tells.
|
||
|
|
func LanguageForText(filename, text string) *Language {
|
||
|
|
if l := LanguageFor(filename); l != nil {
|
||
|
|
return l
|
||
|
|
}
|
||
|
|
name := shebangInterpreter(text)
|
||
|
|
if name == "" {
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
for _, l := range Languages() {
|
||
|
|
for _, i := range l.Interpreters {
|
||
|
|
if i == name {
|
||
|
|
return l
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// shebangInterpreter returns the program the #! line opening text runs,
|
||
|
|
// without its directory or version: "python" for "#!/usr/bin/python3.12",
|
||
|
|
// and "ruby" for "#!/usr/bin/env -S ruby -w", skipping env's options and
|
||
|
|
// variables.
|
||
|
|
func shebangInterpreter(text string) string {
|
||
|
|
line, ok := strings.CutPrefix(text, "#!")
|
||
|
|
if !ok {
|
||
|
|
return ""
|
||
|
|
}
|
||
|
|
if i := strings.IndexByte(line, '\n'); i >= 0 {
|
||
|
|
line = line[:i]
|
||
|
|
}
|
||
|
|
fields := strings.Fields(line)
|
||
|
|
if len(fields) == 0 {
|
||
|
|
return ""
|
||
|
|
}
|
||
|
|
name := path.Base(fields[0])
|
||
|
|
if name == "env" {
|
||
|
|
name = ""
|
||
|
|
for _, f := range fields[1:] {
|
||
|
|
if !strings.HasPrefix(f, "-") && !strings.Contains(f, "=") {
|
||
|
|
name = path.Base(f)
|
||
|
|
break
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return strings.TrimRight(name, "0123456789.")
|
||
|
|
}
|
||
|
|
|
||
|
|
// LanguageNamed returns the language MyGo knows by a name, as "Go",
|
||
|
|
// "TypeScript" or "tsx", ignoring case: its Name, an extension without
|
||
|
|
// the dot, or a usual alias, as Markdown's code blocks name them.
|
||
|
|
func LanguageNamed(name string) *Language {
|
||
|
|
name = strings.ToLower(strings.TrimSpace(name))
|
||
|
|
if name == "" {
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
name = languageAlias(name)
|
||
|
|
for _, l := range Languages() {
|
||
|
|
if strings.ToLower(l.Name) == name {
|
||
|
|
return l
|
||
|
|
}
|
||
|
|
for _, e := range l.Extensions {
|
||
|
|
if e[1:] == name {
|
||
|
|
return l
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// languageAlias returns the name of the language name is another name
|
||
|
|
// for.
|
||
|
|
func languageAlias(name string) string {
|
||
|
|
switch name {
|
||
|
|
case "golang":
|
||
|
|
return "go"
|
||
|
|
case "js", "node", "mjs", "cjs":
|
||
|
|
return "javascript"
|
||
|
|
case "ts":
|
||
|
|
return "typescript"
|
||
|
|
case "py", "python3":
|
||
|
|
return "python"
|
||
|
|
case "sh", "bash", "zsh", "console", "shellscript", "fish":
|
||
|
|
return "shell"
|
||
|
|
case "yml":
|
||
|
|
return "yaml"
|
||
|
|
case "eruby":
|
||
|
|
return "erb"
|
||
|
|
case "terraform":
|
||
|
|
return "hcl"
|
||
|
|
case "md":
|
||
|
|
return "markdown"
|
||
|
|
case "rs":
|
||
|
|
return "rust"
|
||
|
|
case "cpp", "cc", "cxx", "objc", "objective-c", "objectivec":
|
||
|
|
return "c++"
|
||
|
|
case "cs", "csharp":
|
||
|
|
return "c#"
|
||
|
|
case "kt":
|
||
|
|
return "kotlin"
|
||
|
|
case "rb":
|
||
|
|
return "ruby"
|
||
|
|
case "patch", "udiff":
|
||
|
|
return "diff"
|
||
|
|
case "docker":
|
||
|
|
return "dockerfile"
|
||
|
|
case "make", "mk":
|
||
|
|
return "makefile"
|
||
|
|
case "svg", "plist":
|
||
|
|
return "xml"
|
||
|
|
case "jsonc", "json5":
|
||
|
|
return "json"
|
||
|
|
case "postgres", "postgresql", "mysql", "sqlite", "plsql":
|
||
|
|
return "sql"
|
||
|
|
case "ini", "cfg", "conf":
|
||
|
|
return "toml"
|
||
|
|
case "proto":
|
||
|
|
return "protobuf"
|
||
|
|
case "scss", "less":
|
||
|
|
return "css"
|
||
|
|
}
|
||
|
|
return name
|
||
|
|
}
|
||
|
|
|
||
|
|
// Languages returns the languages MyGo knows how to highlight.
|
||
|
|
func Languages() []*Language {
|
||
|
|
languagesOnce.Do(makeLanguages)
|
||
|
|
return languageList
|
||
|
|
}
|
||
|
|
|
||
|
|
var (
|
||
|
|
languagesOnce sync.Once
|
||
|
|
languageList []*Language
|
||
|
|
)
|
||
|
|
|
||
|
|
func cAutoClose(quotes ...string) []BracketPair {
|
||
|
|
out := []BracketPair{{Open: "(", Close: ")"}, {Open: "[", Close: "]"}, {Open: "{", Close: "}"}}
|
||
|
|
for _, q := range quotes {
|
||
|
|
out = append(out, BracketPair{Open: q, Close: q, NotIn: []TokenKind{TokenString, TokenComment}})
|
||
|
|
}
|
||
|
|
return out
|
||
|
|
}
|
||
|
|
|
||
|
|
func cString(q string) lexString {
|
||
|
|
return lexString{open: q, close: q, escape: '\\', kind: TokenString}
|
||
|
|
}
|
||
|
|
|
||
|
|
const cOperators = "+-*/%=&|^!<>~?:@#"
|
||
|
|
|
||
|
|
func makeLanguages() {
|
||
|
|
cBrackets := []BracketPair{{Open: "(", Close: ")"}, {Open: "[", Close: "]"}, {Open: "{", Close: "}"}}
|
||
|
|
notInText := []TokenKind{TokenString, TokenComment}
|
||
|
|
goLang := &Language{
|
||
|
|
Name: "Go", Extensions: []string{".go"}, LineComment: "//", BlockComment: [2]string{"/*", "*/"},
|
||
|
|
Imports: regexp.MustCompile(`^import\b`),
|
||
|
|
Brackets: cBrackets, AutoClose: cAutoClose("\"", "'", "`"), TabSize: 4, HardTabs: true,
|
||
|
|
Tokenizer: &lexer{
|
||
|
|
lineComments: []string{"//"}, blockComments: [][2]string{{"/*", "*/"}},
|
||
|
|
strings: []lexString{cString("\""), cString("'"), {open: "`", close: "`", multiline: true, kind: TokenString}},
|
||
|
|
words: words(TokenKeyword, "break case chan const continue default defer else fallthrough for func go goto if import interface map package range return select struct switch type var",
|
||
|
|
TokenType, "any bool byte comparable complex64 complex128 error float32 float64 int int8 int16 int32 int64 rune string uint uint8 uint16 uint32 uint64 uintptr",
|
||
|
|
TokenConstant, "true false nil iota",
|
||
|
|
TokenFunction, "append cap clear close complex copy delete imag len make max min new panic print println real recover"),
|
||
|
|
calls: true, operators: cOperators,
|
||
|
|
},
|
||
|
|
}
|
||
|
|
jsWords := []any{TokenKeyword, "async await break case catch class const continue debugger default delete do else export extends finally for from function get if import in instanceof let new of return set static super switch this throw try typeof var void while with yield as",
|
||
|
|
TokenConstant, "true false null undefined NaN Infinity",
|
||
|
|
TokenType, "Array Boolean Date Error Function JSON Map Math Number Object Promise Proxy Reflect RegExp Set String Symbol WeakMap WeakSet BigInt console window document globalThis"}
|
||
|
|
jsStrings := []lexString{cString("\""), cString("'"), {open: "`", close: "`", escape: '\\', multiline: true, interpOpen: "${", interpClose: "}", kind: TokenString}}
|
||
|
|
javascript := &Language{
|
||
|
|
Name: "JavaScript", Extensions: []string{".js", ".mjs", ".cjs", ".jsx"}, Interpreters: []string{"node", "nodejs"}, LineComment: "//", BlockComment: [2]string{"/*", "*/"},
|
||
|
|
Imports: regexp.MustCompile(`^import[\s{*'"]`),
|
||
|
|
Brackets: cBrackets, AutoClose: cAutoClose("\"", "'", "`"), WordChars: "$", TabSize: 2,
|
||
|
|
Tokenizer: &lexer{lineComments: []string{"//"}, blockComments: [][2]string{{"/*", "*/"}}, strings: jsStrings,
|
||
|
|
words: words(jsWords...), identChars: "$", calls: true, capitalTypes: true, regexps: true, decorator: '@', operators: cOperators},
|
||
|
|
}
|
||
|
|
tsWords := append(append([]any{}, jsWords...), TokenKeyword, "abstract declare enum implements interface keyof namespace private protected public readonly type infer is satisfies override module",
|
||
|
|
TokenType, "any boolean never number object string symbol unknown void bigint")
|
||
|
|
typescript := &Language{
|
||
|
|
Name: "TypeScript", Extensions: []string{".ts", ".mts", ".cts", ".tsx"}, Interpreters: []string{"deno", "ts-node", "tsx"}, LineComment: "//", BlockComment: [2]string{"/*", "*/"},
|
||
|
|
Imports: regexp.MustCompile(`^import[\s{*'"]`),
|
||
|
|
Brackets: cBrackets, AutoClose: cAutoClose("\"", "'", "`"), WordChars: "$", TabSize: 2,
|
||
|
|
Tokenizer: &lexer{lineComments: []string{"//"}, blockComments: [][2]string{{"/*", "*/"}}, strings: jsStrings,
|
||
|
|
words: words(tsWords...), identChars: "$", calls: true, capitalTypes: true, regexps: true, decorator: '@', operators: cOperators},
|
||
|
|
}
|
||
|
|
json := &Language{
|
||
|
|
Name: "JSON", Extensions: []string{".json", ".jsonc", ".json5", ".webmanifest"}, Filenames: []string{".babelrc", ".eslintrc", "tsconfig.json"},
|
||
|
|
LineComment: "//", BlockComment: [2]string{"/*", "*/"}, Brackets: cBrackets, AutoClose: cAutoClose("\""), TabSize: 2,
|
||
|
|
Tokenizer: &jsonLexer{lexer{lineComments: []string{"//"}, blockComments: [][2]string{{"/*", "*/"}}, strings: []lexString{cString("\""), cString("'")},
|
||
|
|
words: words(TokenConstant, "true false null"), operators: ":"}},
|
||
|
|
}
|
||
|
|
pyStrings := []lexString{}
|
||
|
|
// The f-strings first, as the lexer's stack holds the index of a
|
||
|
|
// string with interpolations in five bits.
|
||
|
|
for _, p := range []string{"f", "F", "fr", "rf", "Fr", "rF", "", "r", "R", "b", "B", "u", "U", "rb", "br", "Rb", "bR"} {
|
||
|
|
raw := strings.ContainsAny(p, "rR")
|
||
|
|
f := strings.ContainsAny(p, "fF")
|
||
|
|
for _, q := range []string{`"""`, `'''`, `"`, `'`} {
|
||
|
|
s := lexString{open: q, close: q, prefix: p, multiline: len(q) == 3, kind: TokenString}
|
||
|
|
if !raw {
|
||
|
|
s.escape = '\\'
|
||
|
|
}
|
||
|
|
if f {
|
||
|
|
s.interpOpen, s.interpClose = "{", "}"
|
||
|
|
}
|
||
|
|
pyStrings = append(pyStrings, s)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
python := &Language{
|
||
|
|
Name: "Python", Extensions: []string{".py", ".pyi", ".pyw"}, Filenames: []string{"SConstruct", "SConscript"}, Interpreters: []string{"python", "pypy"}, LineComment: "#",
|
||
|
|
Imports: regexp.MustCompile(`^(import|from)\s`),
|
||
|
|
Brackets: cBrackets, AutoClose: cAutoClose("\"", "'"), TabSize: 4,
|
||
|
|
IncreaseIndent: regexp.MustCompile(`^\s*(class|def|elif|else|except|finally|for|if|try|while|with|async|match|case)\b.*:\s*(#.*)?$|[\[({]\s*$`),
|
||
|
|
Tokenizer: &lexer{lineComments: []string{"#"}, strings: pyStrings,
|
||
|
|
words: words(TokenKeyword, "and as assert async await break class continue def del elif else except finally for from global if import in is lambda nonlocal not or pass raise return try while with yield match case",
|
||
|
|
TokenConstant, "True False None NotImplemented Ellipsis __name__ __file__",
|
||
|
|
TokenType, "int float str bytes bool list dict set tuple object type complex frozenset bytearray range Exception",
|
||
|
|
TokenFunction, "print len open isinstance super self cls"),
|
||
|
|
calls: true, capitalTypes: true, decorator: '@', operators: cOperators},
|
||
|
|
}
|
||
|
|
rust := &Language{
|
||
|
|
Name: "Rust", Extensions: []string{".rs"}, LineComment: "//", BlockComment: [2]string{"/*", "*/"},
|
||
|
|
Imports: regexp.MustCompile(`^(pub(\([^)]*\))?\s+)?(use|extern\s+crate)\s`),
|
||
|
|
Brackets: cBrackets, AutoClose: cAutoClose("\""), TabSize: 4,
|
||
|
|
Tokenizer: &lexer{lineComments: []string{"//"}, blockComments: [][2]string{{"/*", "*/"}}, nestComments: true,
|
||
|
|
strings: []lexString{{open: "\"", close: "\"", escape: '\\', multiline: true, kind: TokenString},
|
||
|
|
{open: "\"", close: "\"", prefix: "b", escape: '\\', multiline: true, kind: TokenString},
|
||
|
|
{open: "#\"", close: "\"#", prefix: "r", multiline: true, kind: TokenString},
|
||
|
|
{open: "\"", close: "\"", prefix: "r", multiline: true, kind: TokenString},
|
||
|
|
{open: "'", close: "'", escape: '\\', kind: TokenString}},
|
||
|
|
words: words(TokenKeyword, "as async await break const continue crate dyn else enum extern fn for if impl in let loop match mod move mut pub ref return self Self static struct super trait type unsafe use where while yield",
|
||
|
|
TokenType, "bool char f32 f64 i8 i16 i32 i64 i128 isize str u8 u16 u32 u64 u128 usize String Vec Option Result Box",
|
||
|
|
TokenConstant, "true false None Some Ok Err"),
|
||
|
|
calls: true, capitalTypes: true, directives: false, decorator: '#', operators: cOperators},
|
||
|
|
}
|
||
|
|
cWords := []any{TokenKeyword, "auto break case const continue default do else enum extern for goto if inline register restrict return sizeof static struct switch typedef union volatile while _Alignas _Alignof _Atomic _Bool _Generic _Noreturn _Static_assert _Thread_local",
|
||
|
|
TokenType, "char double float int long short signed unsigned void size_t ssize_t int8_t int16_t int32_t int64_t uint8_t uint16_t uint32_t uint64_t bool FILE",
|
||
|
|
TokenConstant, "NULL true false"}
|
||
|
|
c := &Language{
|
||
|
|
Name: "C", Extensions: []string{".c", ".h"}, LineComment: "//", BlockComment: [2]string{"/*", "*/"},
|
||
|
|
Imports: regexp.MustCompile(`^#\s*(include|import)\b`),
|
||
|
|
Brackets: cBrackets, AutoClose: cAutoClose("\"", "'"), TabSize: 4,
|
||
|
|
Tokenizer: &lexer{lineComments: []string{"//"}, blockComments: [][2]string{{"/*", "*/"}}, strings: []lexString{cString("\""), cString("'")},
|
||
|
|
words: words(cWords...), calls: true, directives: true, operators: cOperators},
|
||
|
|
}
|
||
|
|
cpp := &Language{
|
||
|
|
Name: "C++", Extensions: []string{".cpp", ".cc", ".cxx", ".hpp", ".hh", ".hxx", ".ino", ".mm", ".m"}, LineComment: "//", BlockComment: [2]string{"/*", "*/"},
|
||
|
|
Imports: regexp.MustCompile(`^#\s*(include|import)\b`),
|
||
|
|
Brackets: cBrackets, AutoClose: cAutoClose("\"", "'"), TabSize: 4,
|
||
|
|
Tokenizer: &lexer{lineComments: []string{"//"}, blockComments: [][2]string{{"/*", "*/"}}, strings: []lexString{cString("\""), cString("'"), {open: "R\"(", close: ")\"", multiline: true, kind: TokenString}},
|
||
|
|
words: words(append(append([]any{}, cWords...), TokenKeyword, "alignas alignof and asm catch class concept consteval constexpr constinit co_await co_return co_yield decltype delete dynamic_cast explicit export friend mutable namespace new noexcept not operator or private protected public reinterpret_cast requires static_assert static_cast template this thread_local throw try typeid typename using virtual xor override final @interface @implementation @end @property self",
|
||
|
|
TokenType, "wchar_t char8_t char16_t char32_t std string vector map", TokenConstant, "nullptr nil YES NO")...),
|
||
|
|
calls: true, capitalTypes: true, directives: true, operators: cOperators},
|
||
|
|
}
|
||
|
|
javaWords := []any{TokenKeyword, "abstract assert break case catch class const continue default do else enum extends final finally for goto if implements import instanceof interface native new package private protected public return static strictfp super switch synchronized this throw throws transient try volatile while var record sealed permits yield",
|
||
|
|
TokenType, "boolean byte char double float int long short void String Object Integer",
|
||
|
|
TokenConstant, "true false null"}
|
||
|
|
java := &Language{
|
||
|
|
Name: "Java", Extensions: []string{".java"}, LineComment: "//", BlockComment: [2]string{"/*", "*/"},
|
||
|
|
Imports: regexp.MustCompile(`^import\s`),
|
||
|
|
Brackets: cBrackets, AutoClose: cAutoClose("\"", "'"), TabSize: 4,
|
||
|
|
Tokenizer: &lexer{lineComments: []string{"//"}, blockComments: [][2]string{{"/*", "*/"}}, strings: []lexString{{open: `"""`, close: `"""`, escape: '\\', multiline: true, kind: TokenString}, cString("\""), cString("'")},
|
||
|
|
words: words(javaWords...), calls: true, capitalTypes: true, decorator: '@', operators: cOperators},
|
||
|
|
}
|
||
|
|
kotlin := &Language{
|
||
|
|
Name: "Kotlin", Extensions: []string{".kt", ".kts"}, Interpreters: []string{"kotlin"}, LineComment: "//", BlockComment: [2]string{"/*", "*/"},
|
||
|
|
Imports: regexp.MustCompile(`^import\s`),
|
||
|
|
Brackets: cBrackets, AutoClose: cAutoClose("\"", "'"), TabSize: 4,
|
||
|
|
Tokenizer: &lexer{lineComments: []string{"//"}, blockComments: [][2]string{{"/*", "*/"}}, nestComments: true,
|
||
|
|
strings: []lexString{{open: `"""`, close: `"""`, multiline: true, interpOpen: "${", interpClose: "}", dollar: true, kind: TokenString},
|
||
|
|
{open: "\"", close: "\"", escape: '\\', interpOpen: "${", interpClose: "}", dollar: true, kind: TokenString}, cString("'")},
|
||
|
|
words: words(TokenKeyword, "as break class continue do else for fun if in interface is object package return super this throw try typealias typeof val var when while by catch constructor delegate dynamic field file finally get import init param property receiver set setparam where actual abstract annotation companion const crossinline data enum expect external final infix inline inner internal lateinit noinline open operator out override private protected public reified sealed suspend tailrec vararg",
|
||
|
|
TokenConstant, "true false null", TokenType, "Any Boolean Byte Char Double Float Int Long Nothing Short String Unit"),
|
||
|
|
calls: true, capitalTypes: true, decorator: '@', operators: cOperators},
|
||
|
|
}
|
||
|
|
// Scala's interpolated strings first, as the lexer's stack holds the
|
||
|
|
// index of a string with interpolations in five bits.
|
||
|
|
var scalaStrings []lexString
|
||
|
|
for _, p := range []string{"s", "f", "raw"} {
|
||
|
|
for _, q := range []string{`"""`, `"`} {
|
||
|
|
s := lexString{open: q, close: q, prefix: p, multiline: len(q) == 3, interpOpen: "${", interpClose: "}", dollar: true, kind: TokenString}
|
||
|
|
if p != "raw" {
|
||
|
|
s.escape = '\\'
|
||
|
|
}
|
||
|
|
scalaStrings = append(scalaStrings, s)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
scalaStrings = append(scalaStrings, lexString{open: `"""`, close: `"""`, multiline: true, kind: TokenString}, cString("\""), cString("'"),
|
||
|
|
lexString{open: "`", close: "`", kind: TokenVariable})
|
||
|
|
scala := &Language{
|
||
|
|
Name: "Scala", Extensions: []string{".scala", ".sc", ".sbt"}, Interpreters: []string{"scala"}, LineComment: "//", BlockComment: [2]string{"/*", "*/"},
|
||
|
|
Imports: regexp.MustCompile(`^import\s`),
|
||
|
|
Brackets: cBrackets, AutoClose: cAutoClose("\"", "'"), TabSize: 2,
|
||
|
|
Tokenizer: &lexer{lineComments: []string{"//"}, blockComments: [][2]string{{"/*", "*/"}}, nestComments: true, strings: scalaStrings,
|
||
|
|
words: words(TokenKeyword, "abstract case catch class def derives do else end enum export extends extension final finally for forSome given if implicit import inline lazy match new object opaque override package private protected return sealed super then this throw trait transparent try type using val var while with yield",
|
||
|
|
TokenType, "Any AnyRef AnyVal Boolean Byte Char Double Float Int Long Nothing Null Short String Unit",
|
||
|
|
TokenConstant, "true false null None Some Nil"),
|
||
|
|
calls: true, capitalTypes: true, decorator: '@', operators: cOperators},
|
||
|
|
}
|
||
|
|
swift := &Language{
|
||
|
|
Name: "Swift", Extensions: []string{".swift"}, Interpreters: []string{"swift"}, LineComment: "//", BlockComment: [2]string{"/*", "*/"},
|
||
|
|
Imports: regexp.MustCompile(`^(@\w+(\([^)]*\))?\s+)?import\s`),
|
||
|
|
Brackets: cBrackets, AutoClose: cAutoClose("\""), TabSize: 4,
|
||
|
|
Tokenizer: &lexer{lineComments: []string{"//"}, blockComments: [][2]string{{"/*", "*/"}}, nestComments: true,
|
||
|
|
strings: []lexString{{open: `"""`, close: `"""`, escape: '\\', multiline: true, kind: TokenString}, cString("\"")},
|
||
|
|
words: words(TokenKeyword, "associatedtype class deinit enum extension fileprivate func import init inout internal let open operator private precedencegroup protocol public rethrows static struct subscript typealias var break case catch continue default defer do else fallthrough for guard if in repeat return throw switch where while as is try await async throws some any self Self super actor nonisolated isolated mutating nonmutating override weak unowned lazy final required convenience",
|
||
|
|
TokenConstant, "true false nil", TokenType, "Int Double Float String Bool Character Array Dictionary Set Optional Void"),
|
||
|
|
calls: true, capitalTypes: true, decorator: '@', directives: true, operators: cOperators},
|
||
|
|
}
|
||
|
|
// Dart's interpolated strings first, then its raw ones, r'…'.
|
||
|
|
var dartStrings []lexString
|
||
|
|
for _, raw := range []bool{false, true} {
|
||
|
|
for _, q := range []string{`"""`, `'''`, `"`, `'`} {
|
||
|
|
s := lexString{open: q, close: q, multiline: len(q) == 3, kind: TokenString}
|
||
|
|
if raw {
|
||
|
|
s.prefix = "r"
|
||
|
|
} else {
|
||
|
|
s.escape, s.interpOpen, s.interpClose, s.dollar = '\\', "${", "}", true
|
||
|
|
}
|
||
|
|
dartStrings = append(dartStrings, s)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
dart := &Language{
|
||
|
|
Name: "Dart", Extensions: []string{".dart"}, Interpreters: []string{"dart"}, LineComment: "//", BlockComment: [2]string{"/*", "*/"},
|
||
|
|
Imports: regexp.MustCompile(`^(import|export|part)\s`),
|
||
|
|
Brackets: cBrackets, AutoClose: cAutoClose("\"", "'"), WordChars: "$", TabSize: 2,
|
||
|
|
Tokenizer: &lexer{lineComments: []string{"//"}, blockComments: [][2]string{{"/*", "*/"}}, nestComments: true, strings: dartStrings,
|
||
|
|
words: words(TokenKeyword, "abstract as assert async await base break case catch class const continue covariant default deferred do else enum export extends extension external factory final finally for get hide if implements import in interface is late library mixin new on operator part required rethrow return sealed set show static super switch sync this throw try typedef var when while with yield",
|
||
|
|
TokenType, "bool double dynamic int num void Never Null Object String List Map Set Iterable Future FutureOr Stream Function Record Symbol Type",
|
||
|
|
TokenConstant, "true false null"),
|
||
|
|
identChars: "$", calls: true, capitalTypes: true, decorator: '@', operators: cOperators},
|
||
|
|
}
|
||
|
|
csharp := &Language{
|
||
|
|
Name: "C#", Extensions: []string{".cs", ".csx"}, LineComment: "//", BlockComment: [2]string{"/*", "*/"},
|
||
|
|
Imports: regexp.MustCompile(`^(global\s+)?using\s+(static\s+)?[\w.]+(\s*=\s*[^;]+)?;`),
|
||
|
|
Brackets: cBrackets, AutoClose: cAutoClose("\"", "'"), TabSize: 4,
|
||
|
|
Tokenizer: &lexer{lineComments: []string{"//"}, blockComments: [][2]string{{"/*", "*/"}},
|
||
|
|
strings: []lexString{{open: "@\"", close: "\"", multiline: true, kind: TokenString}, {open: "$\"", close: "\"", escape: '\\', interpOpen: "{", interpClose: "}", kind: TokenString}, cString("\""), cString("'")},
|
||
|
|
words: words(TokenKeyword, "abstract as base break case catch checked class const continue default delegate do else enum event explicit extern finally fixed for foreach goto if implicit in interface internal is lock namespace new operator out override params private protected public readonly ref return sealed sizeof stackalloc static struct switch this throw try typeof unchecked unsafe using virtual volatile while async await var get set init record when where yield",
|
||
|
|
TokenType, "bool byte char decimal double float int long object sbyte short string uint ulong ushort void dynamic",
|
||
|
|
TokenConstant, "true false null"),
|
||
|
|
calls: true, capitalTypes: true, directives: true, operators: cOperators},
|
||
|
|
}
|
||
|
|
shell := &Language{
|
||
|
|
Name: "Shell", Extensions: []string{".sh", ".bash", ".zsh", ".fish", ".ksh", ".command"}, Filenames: []string{".bashrc", ".zshrc", ".profile", ".bash_profile", "PKGBUILD", ".envrc"}, Interpreters: []string{"sh", "bash", "zsh", "ksh", "dash", "ash", "fish"},
|
||
|
|
LineComment: "#", Brackets: cBrackets, AutoClose: cAutoClose("\"", "'", "`"), TabSize: 2,
|
||
|
|
IncreaseIndent: regexp.MustCompile(`(\bthen|\bdo|\{|\(|\belse)\s*$`),
|
||
|
|
DecreaseIndent: regexp.MustCompile(`^\s*(fi|done|esac|else|elif|\}|\))\b`),
|
||
|
|
Tokenizer: &lexer{lineComments: []string{"#"},
|
||
|
|
strings: []lexString{{open: "\"", close: "\"", escape: '\\', multiline: true, dollar: true, kind: TokenString}, {open: "'", close: "'", multiline: true, kind: TokenString}, {open: "`", close: "`", escape: '\\', kind: TokenString}},
|
||
|
|
words: words(TokenKeyword, "if then else elif fi case esac for select while until do done in function time coproc return exit break continue local export declare readonly unset shift source alias eval exec set trap",
|
||
|
|
TokenFunction, "echo printf cd pwd test read mkdir rm cp mv ls cat grep sed awk find xargs sudo chmod chown curl git go bun npm"),
|
||
|
|
identChars: "-", variables: true, operators: "|&;<>=!*?"},
|
||
|
|
}
|
||
|
|
yaml := &Language{
|
||
|
|
Name: "YAML", Extensions: []string{".yaml", ".yml"}, Filenames: []string{".clang-format"}, LineComment: "#",
|
||
|
|
Brackets: cBrackets, AutoClose: cAutoClose("\"", "'"), TabSize: 2,
|
||
|
|
IncreaseIndent: regexp.MustCompile(`:\s*(#.*)?$|^\s*-\s*$`),
|
||
|
|
Tokenizer: &lexer{lineComments: []string{"#"}, strings: []lexString{cString("\""), {open: "'", close: "'", kind: TokenString}},
|
||
|
|
words: words(TokenConstant, "true false null yes no on off True False Null ~"), keys: true, identChars: "-./", operators: "|>-*&!?:"},
|
||
|
|
}
|
||
|
|
toml := &Language{
|
||
|
|
Name: "TOML", Extensions: []string{".toml", ".ini", ".cfg", ".conf", ".properties", ".editorconfig"}, Filenames: []string{"Cargo.lock", ".gitconfig", ".npmrc"},
|
||
|
|
LineComment: "#", Brackets: cBrackets, AutoClose: cAutoClose("\"", "'"), TabSize: 2,
|
||
|
|
Tokenizer: &tomlLexer{lexer{lineComments: []string{"#", ";"},
|
||
|
|
strings: []lexString{{open: `"""`, close: `"""`, escape: '\\', multiline: true, kind: TokenString}, {open: "'''", close: "'''", multiline: true, kind: TokenString}, cString("\""), {open: "'", close: "'", kind: TokenString}},
|
||
|
|
words: words(TokenConstant, "true false"), identChars: "-.", operators: "="}},
|
||
|
|
}
|
||
|
|
sql := &Language{
|
||
|
|
Name: "SQL", Extensions: []string{".sql"}, LineComment: "--", BlockComment: [2]string{"/*", "*/"},
|
||
|
|
Brackets: cBrackets, AutoClose: cAutoClose("\"", "'"), TabSize: 2,
|
||
|
|
Tokenizer: &lexer{lineComments: []string{"--"}, blockComments: [][2]string{{"/*", "*/"}}, foldCase: true,
|
||
|
|
strings: []lexString{{open: "'", close: "'", multiline: true, kind: TokenString}, {open: "\"", close: "\"", kind: TokenVariable}, {open: "`", close: "`", kind: TokenVariable}},
|
||
|
|
words: words(TokenKeyword, "select from where and or not insert into values update set delete create table drop alter add column index view as on join left right inner outer full cross group by order having limit offset union all distinct case when then else end is null in exists between like primary key foreign references default unique check constraint begin commit rollback transaction if returning with recursive asc desc trigger function procedure replace temporary temp database schema grant revoke cascade conflict do nothing using natural explain analyze pragma vacuum",
|
||
|
|
TokenType, "int integer bigint smallint tinyint real float double decimal numeric text varchar char boolean bool date time timestamp blob json jsonb uuid serial",
|
||
|
|
TokenConstant, "true false null"),
|
||
|
|
calls: true, operators: "+-*/%=<>!|"},
|
||
|
|
}
|
||
|
|
// R's strings go on over lines; its raw strings, r"(…)", R'[…]' and
|
||
|
|
// the like, have no escapes; and its %op% operators, as %in% and %>%,
|
||
|
|
// are lexed as strings are.
|
||
|
|
rStrings := []lexString{{open: "\"", close: "\"", escape: '\\', multiline: true, kind: TokenString}, {open: "'", close: "'", escape: '\\', multiline: true, kind: TokenString},
|
||
|
|
{open: "%", close: "%", kind: TokenOperator}, {open: "`", close: "`", kind: TokenVariable}}
|
||
|
|
for _, p := range []string{"r", "R"} {
|
||
|
|
for _, q := range []string{"\"", "'"} {
|
||
|
|
for _, b := range []string{"()", "[]", "{}"} {
|
||
|
|
rStrings = append(rStrings, lexString{open: q + b[:1], close: b[1:] + q, prefix: p, multiline: true, kind: TokenString})
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
rLang := &Language{
|
||
|
|
Name: "R", Extensions: []string{".r"}, Filenames: []string{".Rprofile"}, Interpreters: []string{"Rscript"}, LineComment: "#",
|
||
|
|
Imports: regexp.MustCompile(`^(library|require)\(`),
|
||
|
|
Brackets: cBrackets, AutoClose: cAutoClose("\"", "'", "`"), WordChars: ".", TabSize: 2,
|
||
|
|
Tokenizer: &lexer{lineComments: []string{"#"}, strings: rStrings,
|
||
|
|
words: words(TokenKeyword, "if else repeat while function for in next break return",
|
||
|
|
TokenConstant, "TRUE FALSE NULL NA NA_integer_ NA_real_ NA_complex_ NA_character_ Inf NaN",
|
||
|
|
TokenFunction, "library require source print cat paste paste0 stop warning message invisible"),
|
||
|
|
identChars: ".", calls: true, operators: "+-*/^=&|!<>~?:$@\\"},
|
||
|
|
}
|
||
|
|
ruby := &Language{
|
||
|
|
Name: "Ruby", Extensions: []string{".rb", ".rake", ".gemspec", ".rbs"}, Filenames: []string{"Gemfile", "Rakefile", "Podfile", "Fastfile"}, Interpreters: []string{"ruby", "jruby", "truffleruby"}, LineComment: "#",
|
||
|
|
Imports: regexp.MustCompile(`^require(_relative)?\b`),
|
||
|
|
Brackets: cBrackets, AutoClose: cAutoClose("\"", "'"), TabSize: 2,
|
||
|
|
IncreaseIndent: regexp.MustCompile(`^\s*(class|module|def|if|unless|else|elsif|while|until|for|begin|rescue|ensure|case|when)\b|\bdo(\s*\|.*\|)?\s*$|[\[({]\s*$`),
|
||
|
|
DecreaseIndent: regexp.MustCompile(`^\s*(end|else|elsif|rescue|ensure|when)\b`),
|
||
|
|
Tokenizer: &lexer{lineComments: []string{"#"},
|
||
|
|
strings: []lexString{{open: "\"", close: "\"", escape: '\\', multiline: true, interpOpen: "#{", interpClose: "}", kind: TokenString}, {open: "'", close: "'", escape: '\\', multiline: true, kind: TokenString}},
|
||
|
|
words: words(TokenKeyword, "alias and begin break case class def defined? do else elsif end ensure for if in module next not or redo rescue retry return self super then undef unless until when while yield require require_relative attr_accessor attr_reader attr_writer private protected public",
|
||
|
|
TokenConstant, "true false nil"),
|
||
|
|
identChars: "?!", calls: true, capitalTypes: true, variables: false, operators: cOperators},
|
||
|
|
}
|
||
|
|
lua := &Language{
|
||
|
|
Name: "Lua", Extensions: []string{".lua"}, Interpreters: []string{"lua", "luajit"}, LineComment: "--", BlockComment: [2]string{"--[[", "]]"},
|
||
|
|
Imports: regexp.MustCompile(`^local\s+\w+\s*=\s*require\b`),
|
||
|
|
Brackets: cBrackets, AutoClose: cAutoClose("\"", "'"), TabSize: 2,
|
||
|
|
IncreaseIndent: regexp.MustCompile(`\b(then|do|else|function\b.*\))\s*$|^\s*(local\s+)?function\b|[\[({]\s*$`),
|
||
|
|
DecreaseIndent: regexp.MustCompile(`^\s*(end|else|elseif|until|\}|\))`),
|
||
|
|
Tokenizer: &lexer{lineComments: []string{"--"}, blockComments: [][2]string{{"--[[", "]]"}},
|
||
|
|
strings: []lexString{{open: "[[", close: "]]", multiline: true, kind: TokenString}, cString("\""), cString("'")},
|
||
|
|
words: words(TokenKeyword, "and break do else elseif end for function goto if in local not or repeat return then until while", TokenConstant, "true false nil"),
|
||
|
|
calls: true, operators: "+-*/%^#=<>~.:"},
|
||
|
|
}
|
||
|
|
php := &Language{
|
||
|
|
Name: "PHP", Extensions: []string{".php"}, Interpreters: []string{"php"}, LineComment: "//", BlockComment: [2]string{"/*", "*/"},
|
||
|
|
Imports: regexp.MustCompile(`^(use|require|require_once|include|include_once)\s`),
|
||
|
|
Brackets: cBrackets, AutoClose: cAutoClose("\"", "'"), TabSize: 4,
|
||
|
|
Tokenizer: &lexer{lineComments: []string{"//", "#"}, blockComments: [][2]string{{"/*", "*/"}},
|
||
|
|
strings: []lexString{{open: "\"", close: "\"", escape: '\\', multiline: true, dollar: true, kind: TokenString}, {open: "'", close: "'", escape: '\\', multiline: true, kind: TokenString}},
|
||
|
|
words: words(TokenKeyword, "abstract and array as break callable case catch class clone const continue declare default do echo else elseif empty enddeclare endfor endforeach endif endswitch endwhile extends final finally fn for foreach function global goto if implements include include_once instanceof insteadof interface isset list match namespace new or print private protected public readonly require require_once return static switch throw trait try unset use var while xor yield",
|
||
|
|
TokenConstant, "true false null TRUE FALSE NULL"),
|
||
|
|
calls: true, capitalTypes: true, variables: true, operators: cOperators},
|
||
|
|
}
|
||
|
|
css := &Language{
|
||
|
|
Name: "CSS", Extensions: []string{".css", ".scss", ".less", ".sass", ".pcss"}, BlockComment: [2]string{"/*", "*/"},
|
||
|
|
Imports: regexp.MustCompile(`^@(import|use|forward)\s`),
|
||
|
|
Brackets: cBrackets, AutoClose: cAutoClose("\"", "'"), WordChars: "-", TabSize: 2,
|
||
|
|
Tokenizer: &cssLexer{},
|
||
|
|
}
|
||
|
|
html := &Language{
|
||
|
|
Name: "HTML", Extensions: []string{".html", ".htm", ".xhtml", ".vue"}, BlockComment: [2]string{"<!--", "-->"},
|
||
|
|
Brackets: []BracketPair{{Open: "<", Close: ">"}, {Open: "(", Close: ")"}, {Open: "[", Close: "]"}, {Open: "{", Close: "}"}},
|
||
|
|
AutoClose: cAutoClose("\"", "'"), WordChars: "-", TabSize: 2,
|
||
|
|
IncreaseIndent: regexp.MustCompile(`<(\w[\w-]*)(\s[^>]*)?>[^<]*$|[\[({]\s*$`),
|
||
|
|
DecreaseIndent: regexp.MustCompile(`^\s*</`),
|
||
|
|
}
|
||
|
|
xml := &Language{
|
||
|
|
Name: "XML", Extensions: []string{".xml", ".svg", ".plist", ".xsd", ".xsl", ".rss", ".atom", ".csproj", ".storyboard", ".xib"}, BlockComment: [2]string{"<!--", "-->"},
|
||
|
|
Brackets: []BracketPair{{Open: "<", Close: ">"}}, AutoClose: cAutoClose("\"", "'"), WordChars: "-:", TabSize: 2,
|
||
|
|
IncreaseIndent: regexp.MustCompile(`<(\w[\w:-]*)(\s[^>]*)?>[^<]*$`),
|
||
|
|
DecreaseIndent: regexp.MustCompile(`^\s*</`),
|
||
|
|
Tokenizer: &markupLexer{},
|
||
|
|
}
|
||
|
|
markdown := &Language{
|
||
|
|
Name: "Markdown", Extensions: []string{".md", ".markdown", ".mdx", ".mkd"}, Filenames: []string{"README", "CHANGELOG"},
|
||
|
|
BlockComment: [2]string{"<!--", "-->"}, Brackets: cBrackets,
|
||
|
|
AutoClose: []BracketPair{{Open: "(", Close: ")"}, {Open: "[", Close: "]"}, {Open: "{", Close: "}"}, {Open: "`", Close: "`", NotIn: notInText}},
|
||
|
|
TabSize: 2,
|
||
|
|
Tokenizer: &markdownLexer{},
|
||
|
|
}
|
||
|
|
diffLang := &Language{
|
||
|
|
Name: "Diff", Extensions: []string{".diff", ".patch", ".rej"}, TabSize: 4,
|
||
|
|
Tokenizer: TokenizerFunc(tokenizeDiff),
|
||
|
|
}
|
||
|
|
dockerfile := &Language{
|
||
|
|
Name: "Dockerfile", Extensions: []string{".dockerfile"}, Filenames: []string{"Dockerfile", "Containerfile"}, LineComment: "#",
|
||
|
|
Brackets: cBrackets, AutoClose: cAutoClose("\"", "'"), TabSize: 4,
|
||
|
|
Tokenizer: &lexer{lineComments: []string{"#"}, foldCase: false,
|
||
|
|
strings: []lexString{{open: "\"", close: "\"", escape: '\\', dollar: true, kind: TokenString}, {open: "'", close: "'", kind: TokenString}},
|
||
|
|
words: words(TokenKeyword, "FROM AS RUN CMD LABEL MAINTAINER EXPOSE ENV ADD COPY ENTRYPOINT VOLUME USER WORKDIR ARG ONBUILD STOPSIGNAL HEALTHCHECK SHELL"),
|
||
|
|
variables: true, identChars: "-", operators: "=&|;<>"},
|
||
|
|
}
|
||
|
|
makefile := &Language{
|
||
|
|
Name: "Makefile", Extensions: []string{".mk", ".mak"}, Filenames: []string{"Makefile", "makefile", "GNUmakefile"}, Interpreters: []string{"make"}, LineComment: "#",
|
||
|
|
Brackets: cBrackets, AutoClose: cAutoClose("\"", "'"), TabSize: 4, HardTabs: true,
|
||
|
|
Tokenizer: &lexer{lineComments: []string{"#"},
|
||
|
|
strings: []lexString{{open: "\"", close: "\"", escape: '\\', dollar: true, kind: TokenString}, {open: "'", close: "'", kind: TokenString}},
|
||
|
|
words: words(TokenKeyword, "ifeq ifneq ifdef ifndef else endif include define endef export override .PHONY"),
|
||
|
|
variables: true, keys: true, identChars: "-./", operators: "=:?+!@"},
|
||
|
|
}
|
||
|
|
protobuf := &Language{
|
||
|
|
Name: "Protobuf", Extensions: []string{".proto"}, LineComment: "//", BlockComment: [2]string{"/*", "*/"},
|
||
|
|
Imports: regexp.MustCompile(`^import\s`),
|
||
|
|
Brackets: cBrackets, AutoClose: cAutoClose("\""), TabSize: 2,
|
||
|
|
Tokenizer: &lexer{lineComments: []string{"//"}, blockComments: [][2]string{{"/*", "*/"}}, strings: []lexString{cString("\""), cString("'")},
|
||
|
|
words: words(TokenKeyword, "syntax package import option message enum service rpc returns repeated optional required oneof map reserved extend extensions to max stream public weak",
|
||
|
|
TokenType, "double float int32 int64 uint32 uint64 sint32 sint64 fixed32 fixed64 sfixed32 sfixed64 bool string bytes",
|
||
|
|
TokenConstant, "true false"),
|
||
|
|
capitalTypes: true, operators: "=<>"},
|
||
|
|
}
|
||
|
|
graphql := &Language{
|
||
|
|
Name: "GraphQL", Extensions: []string{".graphql", ".gql", ".graphqls"}, LineComment: "#",
|
||
|
|
Brackets: cBrackets, AutoClose: cAutoClose("\""), TabSize: 2,
|
||
|
|
Tokenizer: &lexer{lineComments: []string{"#"},
|
||
|
|
// A spread, ..., is a string that ends where it starts, so that
|
||
|
|
// the name after it is no property, as one after a dot is.
|
||
|
|
strings: []lexString{{open: `"""`, close: `"""`, multiline: true, kind: TokenString}, cString("\""), {open: "...", kind: TokenPunctuation}},
|
||
|
|
words: words(TokenKeyword, "query mutation subscription fragment on type interface union enum input scalar schema extend implements directive repeatable",
|
||
|
|
TokenType, "Int Float String Boolean ID",
|
||
|
|
TokenConstant, "true false null"),
|
||
|
|
capitalTypes: true, keys: true, variables: true, decorator: '@', operators: "!=:|&"},
|
||
|
|
}
|
||
|
|
zig := &Language{
|
||
|
|
Name: "Zig", Extensions: []string{".zig", ".zon"}, LineComment: "//", Brackets: cBrackets, AutoClose: cAutoClose("\"", "'"), TabSize: 4,
|
||
|
|
Imports: regexp.MustCompile(`^(pub\s+)?const\s+\w+\s*=\s*@import\(`),
|
||
|
|
Tokenizer: &lexer{lineComments: []string{"//"}, strings: []lexString{cString("\""), cString("'"), {open: "\\\\", close: "\x00", kind: TokenString}},
|
||
|
|
words: words(TokenKeyword, "addrspace align allowzero and anyframe anytype asm async await break callconv catch comptime const continue defer else enum errdefer error export extern fn for if inline linksection noalias noinline nosuspend opaque or orelse packed pub resume return struct suspend switch test threadlocal try union unreachable usingnamespace var volatile while",
|
||
|
|
TokenType, "i8 u8 i16 u16 i32 u32 i64 u64 i128 u128 isize usize f16 f32 f64 f80 f128 bool void noreturn type anyerror comptime_int comptime_float",
|
||
|
|
TokenConstant, "true false null undefined"),
|
||
|
|
calls: true, capitalTypes: true, decorator: '@', operators: cOperators},
|
||
|
|
}
|
||
|
|
nix := &Language{
|
||
|
|
Name: "Nix", Extensions: []string{".nix"}, LineComment: "#", BlockComment: [2]string{"/*", "*/"},
|
||
|
|
Brackets: cBrackets, AutoClose: cAutoClose("\""), WordChars: "-", TabSize: 2,
|
||
|
|
Tokenizer: &lexer{lineComments: []string{"#"}, blockComments: [][2]string{{"/*", "*/"}},
|
||
|
|
// ''…'' strings lex without their escapes, ''$, ''' and ''\,
|
||
|
|
// which end them early: an escape here is one byte.
|
||
|
|
strings: []lexString{{open: "\"", close: "\"", escape: '\\', multiline: true, interpOpen: "${", interpClose: "}", kind: TokenString},
|
||
|
|
{open: "''", close: "''", multiline: true, interpOpen: "${", interpClose: "}", kind: TokenString}},
|
||
|
|
words: words(TokenKeyword, "assert else if in inherit let or rec then with",
|
||
|
|
TokenConstant, "true false null builtins",
|
||
|
|
TokenFunction, "import abort throw derivation toString map removeAttrs baseNameOf dirOf isNull fetchTarball fetchGit"),
|
||
|
|
identChars: "-", assignKeys: true, operators: "+-*/=<>!&|?:@"},
|
||
|
|
}
|
||
|
|
hcl := &Language{
|
||
|
|
Name: "HCL", Extensions: []string{".hcl", ".tf", ".tfvars"}, LineComment: "#", BlockComment: [2]string{"/*", "*/"},
|
||
|
|
Brackets: cBrackets, AutoClose: cAutoClose("\""), WordChars: "-", TabSize: 2,
|
||
|
|
IncreaseIndent: regexp.MustCompile(`[\[({]\s*$`),
|
||
|
|
Tokenizer: &hclLexer{lexer{lineComments: []string{"#", "//"}, blockComments: [][2]string{{"/*", "*/"}},
|
||
|
|
strings: []lexString{{open: "\"", close: "\"", escape: '\\', interpOpen: "${", interpClose: "}", kind: TokenString}},
|
||
|
|
words: words(TokenKeyword, "for in if else endif endfor resource data variable output module provider locals terraform",
|
||
|
|
TokenType, "string number bool list map set object tuple any",
|
||
|
|
TokenConstant, "true false null"),
|
||
|
|
identChars: "-", assignKeys: true, calls: true, operators: "+-*/%=<>!&|?:"}},
|
||
|
|
}
|
||
|
|
// HTML's tokenizer highlights the scripts and styles inside it.
|
||
|
|
html.Tokenizer = &markupLexer{html: true, script: javascript.Tokenizer, style: css.Tokenizer}
|
||
|
|
// ERB's text is HTML, unless its name says otherwise: LanguageFor gives
|
||
|
|
// "config.json.erb" JSON text, and "notice.text.erb" plain text.
|
||
|
|
erb := &Language{
|
||
|
|
Name: "ERB", Extensions: []string{".erb", ".rhtml", ".tt"}, BlockComment: html.BlockComment,
|
||
|
|
Brackets: html.Brackets, AutoClose: html.AutoClose, WordChars: "-", TabSize: 2,
|
||
|
|
IncreaseIndent: html.IncreaseIndent, DecreaseIndent: html.DecreaseIndent,
|
||
|
|
Tokenizer: &templateLexer{host: html.Tokenizer, code: ruby.Tokenizer,
|
||
|
|
tags: []templateTag{{open: "<%#", close: "%>", comment: true}, {open: "<%", close: "%>"}}, marks: "=-", trims: "-", escape: "<%%"},
|
||
|
|
}
|
||
|
|
// Jinja's text is plain, unless its name says what it makes: LanguageFor
|
||
|
|
// gives "compose.yaml.j2" YAML text. The tags may cut that text's
|
||
|
|
// syntax anywhere, and the text resumes after a tag as it was before.
|
||
|
|
jinja := &Language{
|
||
|
|
Name: "Jinja", Extensions: []string{".j2", ".jinja", ".jinja2"}, BlockComment: [2]string{"{#", "#}"},
|
||
|
|
Brackets: cBrackets, AutoClose: cAutoClose("\"", "'"), TabSize: 2,
|
||
|
|
Tokenizer: &templateLexer{
|
||
|
|
code: &lexer{strings: []lexString{cString("\""), cString("'")},
|
||
|
|
words: words(TokenKeyword, "for in if elif else endif endfor set endset macro endmacro call endcall block endblock "+
|
||
|
|
"extends include import from as with endwith without context filter endfilter raw endraw autoescape endautoescape "+
|
||
|
|
"trans endtrans pluralize do continue break and or not is recursive scoped required ignore missing",
|
||
|
|
TokenConstant, "true false none True False None"),
|
||
|
|
calls: true, operators: "+-*/%=<>!|~:?"},
|
||
|
|
tags: []templateTag{{open: "{#", close: "#}", comment: true}, {open: "{{", close: "}}"}, {open: "{%", close: "%}"}},
|
||
|
|
marks: "-+", trims: "-+"},
|
||
|
|
}
|
||
|
|
svelte := &Language{
|
||
|
|
Name: "Svelte", Extensions: []string{".svelte"}, BlockComment: html.BlockComment,
|
||
|
|
Brackets: html.Brackets, AutoClose: html.AutoClose, WordChars: "-", TabSize: 2,
|
||
|
|
IncreaseIndent: regexp.MustCompile(`<(\w[\w-]*)(\s[^>]*)?>[^<]*$|\{[#:][^}]*\}\s*$|[\[({]\s*$`),
|
||
|
|
DecreaseIndent: regexp.MustCompile(`^\s*(</|\{[/:])`),
|
||
|
|
Tokenizer: &markupLexer{html: true, script: javascript.Tokenizer, style: css.Tokenizer, braces: true, ts: typescript.Tokenizer},
|
||
|
|
}
|
||
|
|
languageList = []*Language{goLang, javascript, typescript, json, python, rust, c, cpp, java, kotlin, scala, swift, dart, csharp,
|
||
|
|
shell, yaml, toml, sql, rLang, ruby, lua, php, css, html, erb, svelte, xml, markdown, diffLang, dockerfile, makefile, protobuf, graphql, zig, nix, jinja, hcl}
|
||
|
|
}
|