textmate-validate
Validation of VSCode TextMate grammars.
Links:
carlwr/textmate-validate
@carlwr/textmate-validate
The following examples use an example grammar grammar.json
that can be created with this shell command:
cat >grammar.json <<EOF
{ "patterns": [
{ "match": ".*" }, `# <- a valid regex`
{ "match": ".)" } `# <- INVALID REGEX!`
]
}
EOF
Prerequisites: none; npx
will fetch the package from npm on-the-fly
Example:
npx @carlwr/textmate-validate grammar.json || echo "failed!"
# failed!
npx @carlwr/textmate-validate --verbose --verbose grammar.json
# [INFO] number of regexes:
# total: 2
# valid: 1
# invalid: 1
#
# [INFO] valid regex:
# regex: .*
# path: patterns[0].match
#
# [ERROR] invalid regex:
# error: unmatched close parenthesis
# regex: .)
# path: patterns[1].match
#
# [ERROR] validation failed
npx @carlwr/textmate-validate --help # show help
Prerequisites: npm install @carlwr/textmate-validate
Example:
import { validateGrammar, passed, failed, printResult } from '@carlwr/textmate-validate'
const result = await validateGrammar('grammar.json')
passed(result) // false
failed(result) // true
const verbosity = 2
printResult(result, verbosity) // same as the CLI output above
JSON.stringify(result, null, 2)
// [
// {
// "rgx": ".*",
// "loc": "patterns[0].match",
// "valid": true
// },
// {
// "rgx": ".)",
// "loc": "patterns[1].match",
// "valid": false,
// "err": "unmatched close parenthesis"
// }
// ]
For a given TextMate grammar, the regexes in it are extracted and then validated.
Validation is done by exercising each regex with the Oniguruma regex engine, which is what VSCode uses.
Extracting the regexes:
Validating the extracted regexes:
Oniguruma engine:
onig.wasm
file that vscode-oniguruma includesThe package is intended to be used as a validation check for generated or hand-written TextMate grammars for VSCode, typically as part of a build script, CI pipeline, prepublish package.json
lifecycle hook, etc. It complements the following that are also useful in a similar context:
General:
borama
)apeth
2014)*.md
files in documentation/
of RedCMD/TmLanguage-Syntax-Highlighter
From Macromates:
TextMate grammar data model and structure:
apeth
s blograwGrammar.ts
of microsoft/vscode-textmate
IRawGrammar
is the entry-point/top-level typedocumentation/rules.md
of RedCMD/TmLanguage-Syntax-Highlighter
Scope selectors:
Scope naming conventions:
apeth
s blog, section Standard Scopes
References:
documentation/README.md
of RedCMD/TmLanguage-Syntax-Highlighter