Skip to content

Commit

Permalink
Add hlb lexer (#332)
Browse files Browse the repository at this point in the history
  • Loading branch information
hinshun committed Feb 26, 2020
1 parent 866d0c8 commit e5d9650
Show file tree
Hide file tree
Showing 4 changed files with 260 additions and 0 deletions.
29 changes: 29 additions & 0 deletions _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
}
}
54 changes: 54 additions & 0 deletions 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},
},
},
))
29 changes: 29 additions & 0 deletions 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"
}
}
148 changes: 148 additions & 0 deletions 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"}
]

0 comments on commit e5d9650

Please sign in to comment.