diff --git a/_tools/pygments2chroma.hlb b/_tools/pygments2chroma.hlb new file mode 100644 index 000000000..3cb52401d --- /dev/null +++ b/_tools/pygments2chroma.hlb @@ -0,0 +1,29 @@ +fs default() { + gofmt fs { chromaLexer "pygments.lexers.hlb.HlbLexer"; } +} + +fs script() { + local "." with option { + includePatterns "pygments2chroma.py" + } +} + +fs runChromaPython(string package) { + image "python:alpine" with option { resolve; } + run "apk add -U git" + run "pip install -U pystache" + run "pip install -U -e git+https://github.com/hinshun/pygments.git@hlb-lexer#egg=pygments" + run string { format "python pygments2chroma.py %s > /out/lexer.go" package; } with option { + dir "/chroma" + mount script "/chroma" + mount fs { scratch; } "/out" as chromaLexer + } +} + +fs runGoFormat(fs goFiles) { + image "golang:alpine" with option { resolve; } + run "gofmt -s -w /gofmt/*.go" with option { + dir "/gofmt" + mount goFiles "/gofmt" as gofmt + } +} diff --git a/lexers/hlb.go b/lexers/hlb.go new file mode 100644 index 000000000..4dcf8ed3e --- /dev/null +++ b/lexers/hlb.go @@ -0,0 +1,54 @@ +package lexers + +import ( + . "github.com/alecthomas/chroma" // nolint + "github.com/alecthomas/chroma/lexers/internal" +) + +// HLB lexer. +var HLB = internal.Register(MustNewLexer( + &Config{ + Name: "HLB", + Aliases: []string{"hlb"}, + Filenames: []string{"*.hlb"}, + MimeTypes: []string{}, + }, + Rules{ + "root": { + {`(#.*)`, ByGroups(CommentSingle), nil}, + {`((\b(0(b|B|o|O|x|X)[a-fA-F0-9]+)\b)|(\b(0|[1-9][0-9]*)\b))`, ByGroups(LiteralNumber), nil}, + {`((\b(true|false)\b))`, ByGroups(NameBuiltin), nil}, + {`(\bstring\b|\bint\b|\bbool\b|\bfs\b|\boption\b)`, ByGroups(KeywordType), nil}, + {`(\b[a-zA-Z_][a-zA-Z0-9]*\b)(\()`, ByGroups(NameFunction, Punctuation), Push("params")}, + {`(\{)`, ByGroups(Punctuation), Push("block")}, + {`(\n|\r|\r\n)`, Text, nil}, + {`.`, Text, nil}, + }, + "string": { + {`"`, LiteralString, Pop(1)}, + {`\\"`, LiteralString, nil}, + {`[^\\"]+`, LiteralString, nil}, + }, + "block": { + {`(\})`, ByGroups(Punctuation), Pop(1)}, + {`(#.*)`, ByGroups(CommentSingle), nil}, + {`((\b(0(b|B|o|O|x|X)[a-fA-F0-9]+)\b)|(\b(0|[1-9][0-9]*)\b))`, ByGroups(LiteralNumber), nil}, + {`((\b(true|false)\b))`, ByGroups(KeywordConstant), nil}, + {`"`, LiteralString, Push("string")}, + {`(with)`, ByGroups(KeywordReserved), nil}, + {`(as)([\t ]+)(\b[a-zA-Z_][a-zA-Z0-9]*\b)`, ByGroups(KeywordReserved, Text, NameFunction), nil}, + {`(\bstring\b|\bint\b|\bbool\b|\bfs\b|\boption\b)([\t ]+)(\{)`, ByGroups(KeywordType, Text, Punctuation), Push("block")}, + {`(?!\b(?:scratch|image|resolve|http|checksum|chmod|filename|git|keepGitDir|local|includePatterns|excludePatterns|followPaths|generate|frontendInput|shell|run|readonlyRootfs|env|dir|user|network|security|host|ssh|secret|mount|target|localPath|uid|gid|mode|readonly|tmpfs|sourcePath|cache|mkdir|createParents|chown|createdTime|mkfile|rm|allowNotFound|allowWildcards|copy|followSymlinks|contentsOnly|unpack|createDestPath)\b)(\b[a-zA-Z_][a-zA-Z0-9]*\b)`, ByGroups(NameOther), nil}, + {`(\n|\r|\r\n)`, Text, nil}, + {`.`, Text, nil}, + }, + "params": { + {`(\))`, ByGroups(Punctuation), Pop(1)}, + {`(variadic)`, ByGroups(Keyword), nil}, + {`(\bstring\b|\bint\b|\bbool\b|\bfs\b|\boption\b)`, ByGroups(KeywordType), nil}, + {`(\b[a-zA-Z_][a-zA-Z0-9]*\b)`, ByGroups(NameOther), nil}, + {`(\n|\r|\r\n)`, Text, nil}, + {`.`, Text, nil}, + }, + }, +)) diff --git a/lexers/testdata/hlb.actual b/lexers/testdata/hlb.actual new file mode 100644 index 000000000..5def702f4 --- /dev/null +++ b/lexers/testdata/hlb.actual @@ -0,0 +1,29 @@ +# Run `npm test` for the node project `left-pad`. +fs default() { + npmRun leftPad fs { nodeModules leftPad; } "test" +} + +fs leftPad() { + git "https://github.com/left-pad/left-pad.git" "master" +} + +fs npmInstall(fs src) { + image "node:alpine" with option { + resolve + } + run "npm install" with option { + dir "/src" + mount src "/src" + # Name the mounted filesystem as nodeModules + mount fs { scratch; } "/src/node_modules" as nodeModules + } +} + +fs npmRun(fs src, fs nodeModules, string script) { + image "node:alpine" + run string { format "npm run %s" script; } with option { + dir "/src" + mount src "/src" + mount nodeModules "/src/node_modules" + } +} diff --git a/lexers/testdata/hlb.expected b/lexers/testdata/hlb.expected new file mode 100644 index 000000000..6bbb6f7ab --- /dev/null +++ b/lexers/testdata/hlb.expected @@ -0,0 +1,148 @@ +[ + {"type":"CommentSingle","value":"# Run `npm test` for the node project `left-pad`."}, + {"type":"Text","value":"\n"}, + {"type":"KeywordType","value":"fs"}, + {"type":"Text","value":" "}, + {"type":"NameFunction","value":"default"}, + {"type":"Punctuation","value":"()"}, + {"type":"Text","value":" "}, + {"type":"Punctuation","value":"{"}, + {"type":"Text","value":"\n\t"}, + {"type":"NameOther","value":"npmRun"}, + {"type":"Text","value":" "}, + {"type":"NameOther","value":"leftPad"}, + {"type":"Text","value":" "}, + {"type":"KeywordType","value":"fs"}, + {"type":"Text","value":" "}, + {"type":"Punctuation","value":"{"}, + {"type":"Text","value":" "}, + {"type":"NameOther","value":"nodeModules"}, + {"type":"Text","value":" "}, + {"type":"NameOther","value":"leftPad"}, + {"type":"Text","value":"; "}, + {"type":"Punctuation","value":"}"}, + {"type":"Text","value":" "}, + {"type":"LiteralString","value":"\"test\""}, + {"type":"Text","value":"\n"}, + {"type":"Punctuation","value":"}"}, + {"type":"Text","value":"\n\n"}, + {"type":"KeywordType","value":"fs"}, + {"type":"Text","value":" "}, + {"type":"NameFunction","value":"leftPad"}, + {"type":"Punctuation","value":"()"}, + {"type":"Text","value":" "}, + {"type":"Punctuation","value":"{"}, + {"type":"Text","value":"\n\tgit "}, + {"type":"LiteralString","value":"\"https://github.com/left-pad/left-pad.git\""}, + {"type":"Text","value":" "}, + {"type":"LiteralString","value":"\"master\""}, + {"type":"Text","value":"\n"}, + {"type":"Punctuation","value":"}"}, + {"type":"Text","value":"\n\n"}, + {"type":"KeywordType","value":"fs"}, + {"type":"Text","value":" "}, + {"type":"NameFunction","value":"npmInstall"}, + {"type":"Punctuation","value":"("}, + {"type":"KeywordType","value":"fs"}, + {"type":"Text","value":" "}, + {"type":"NameOther","value":"src"}, + {"type":"Punctuation","value":")"}, + {"type":"Text","value":" "}, + {"type":"Punctuation","value":"{"}, + {"type":"Text","value":"\n\timage "}, + {"type":"LiteralString","value":"\"node:alpine\""}, + {"type":"Text","value":" "}, + {"type":"KeywordReserved","value":"with"}, + {"type":"Text","value":" "}, + {"type":"KeywordType","value":"option"}, + {"type":"Text","value":" "}, + {"type":"Punctuation","value":"{"}, + {"type":"Text","value":"\n\t\tresolve\n\t"}, + {"type":"Punctuation","value":"}"}, + {"type":"Text","value":"\n\trun "}, + {"type":"LiteralString","value":"\"npm install\""}, + {"type":"Text","value":" "}, + {"type":"KeywordReserved","value":"with"}, + {"type":"Text","value":" "}, + {"type":"KeywordType","value":"option"}, + {"type":"Text","value":" "}, + {"type":"Punctuation","value":"{"}, + {"type":"Text","value":"\n\t\tdir "}, + {"type":"LiteralString","value":"\"/src\""}, + {"type":"Text","value":"\n\t\tmount "}, + {"type":"NameOther","value":"src"}, + {"type":"Text","value":" "}, + {"type":"LiteralString","value":"\"/src\""}, + {"type":"Text","value":"\n\t\t"}, + {"type":"CommentSingle","value":"# Name the mounted filesystem as nodeModules"}, + {"type":"Text","value":"\n\t\tmount "}, + {"type":"KeywordType","value":"fs"}, + {"type":"Text","value":" "}, + {"type":"Punctuation","value":"{"}, + {"type":"Text","value":" scratch; "}, + {"type":"Punctuation","value":"}"}, + {"type":"Text","value":" "}, + {"type":"LiteralString","value":"\"/src/node_modules\""}, + {"type":"Text","value":" "}, + {"type":"KeywordReserved","value":"as"}, + {"type":"Text","value":" "}, + {"type":"NameFunction","value":"nodeModules"}, + {"type":"Text","value":"\n\t"}, + {"type":"Punctuation","value":"}"}, + {"type":"Text","value":"\n"}, + {"type":"Punctuation","value":"}"}, + {"type":"Text","value":"\n\n"}, + {"type":"KeywordType","value":"fs"}, + {"type":"Text","value":" "}, + {"type":"NameFunction","value":"npmRun"}, + {"type":"Punctuation","value":"("}, + {"type":"KeywordType","value":"fs"}, + {"type":"Text","value":" "}, + {"type":"NameOther","value":"src"}, + {"type":"Text","value":", "}, + {"type":"KeywordType","value":"fs"}, + {"type":"Text","value":" "}, + {"type":"NameOther","value":"nodeModules"}, + {"type":"Text","value":", "}, + {"type":"KeywordType","value":"string"}, + {"type":"Text","value":" "}, + {"type":"NameOther","value":"script"}, + {"type":"Punctuation","value":")"}, + {"type":"Text","value":" "}, + {"type":"Punctuation","value":"{"}, + {"type":"Text","value":"\n\timage "}, + {"type":"LiteralString","value":"\"node:alpine\""}, + {"type":"Text","value":"\n\trun "}, + {"type":"KeywordType","value":"string"}, + {"type":"Text","value":" "}, + {"type":"Punctuation","value":"{"}, + {"type":"Text","value":" "}, + {"type":"NameOther","value":"format"}, + {"type":"Text","value":" "}, + {"type":"LiteralString","value":"\"npm run %s\""}, + {"type":"Text","value":" "}, + {"type":"NameOther","value":"script"}, + {"type":"Text","value":"; "}, + {"type":"Punctuation","value":"}"}, + {"type":"Text","value":" "}, + {"type":"KeywordReserved","value":"with"}, + {"type":"Text","value":" "}, + {"type":"KeywordType","value":"option"}, + {"type":"Text","value":" "}, + {"type":"Punctuation","value":"{"}, + {"type":"Text","value":"\n\t\tdir "}, + {"type":"LiteralString","value":"\"/src\""}, + {"type":"Text","value":"\n\t\tmount "}, + {"type":"NameOther","value":"src"}, + {"type":"Text","value":" "}, + {"type":"LiteralString","value":"\"/src\""}, + {"type":"Text","value":"\n\t\tmount "}, + {"type":"NameOther","value":"nodeModules"}, + {"type":"Text","value":" "}, + {"type":"LiteralString","value":"\"/src/node_modules\""}, + {"type":"Text","value":"\n\t"}, + {"type":"Punctuation","value":"}"}, + {"type":"Text","value":"\n"}, + {"type":"Punctuation","value":"}"}, + {"type":"Text","value":"\n"} +]