@codincod/codemirror-lang-awk
v0.1.1
Published
AWK language support for the CodeMirror code editor
Maintainers
Readme
@codincod/codemirror-lang-awk 
[ CHANGELOG ]
This package implements AWK language support for the CodeMirror code editor, using a Lezer grammar written for this package.
CodeMirror has never shipped AWK, in either version. Editors that offer it
usually reach for a Perl or C mode, which colours the keywords and gets the
rest wrong: a regular expression comes out as two divisions, print a > b
comes out as a comparison, and there is no tree underneath either way.
Written in part for CodinCod, a competitive coding platform, where it colours the editor people solve puzzles in.
This code is released under an MIT license.
Usage
import {EditorView, basicSetup} from "codemirror"
import {awk} from "@codincod/codemirror-lang-awk"
const view = new EditorView({
parent: document.body,
doc: `BEGIN { print "Hello World!" }`,
extensions: [basicSetup, awk()]
})Coverage
POSIX AWK, as specified in IEEE 1003.1, with the gawk extensions that are
everywhere in practice: switch, delete a, nextfile, **, func,
BEGINFILE and ENDFILE, arrays of arrays, the |& coprocess pipe, indirect
calls written @f(x), and the @load and @include directives.
Measured against 680 AWK files from Rosetta Code, 89.12% parse with no error
node. Most of the rest is not AWK: shell command lines showing how to run the
program, and fragments that are a bare if with no rule around them.
Three things AWK decides by position
A character means one thing after a value and another before one, and all three of these are settled by asking the parser what it has room for.
/ opens a regular expression where a value cannot follow, and divides
where one can. a / b / c is two divisions; $0 ~ /b/ is a match. The scan
handles a / inside a bracket expression, so /[/]/ is one regex.
> redirects where a print can still take a redirection, and compares
everywhere else. print a > b writes to the file named by b; comparing the
two takes parentheses, which is AWK's own rule rather than this parser's. The
same goes for |, which pipes to a command in a print and feeds getline
elsewhere.
A newline ends a statement where a statement can end, and is ignored
everywhere else. That one rule covers every place AWK allows a line to be
broken, so a line may be split after {, &&, , or else with nothing to
mark it.
The same question decides - (a sign before a value, subtraction after one),
++ (prefix before, postfix after) and the < in getline line < file.
What it gives an editor
Syntax highlighting, folding for blocks and parameter lists, indentation that
knows a brace closes a block and that a braceless body is indented for one
statement, and completion for the keywords, the built-in functions and the
special variables like NR and FS.
awkLanguage is exported for use with LanguageSupport, and parser for use
on its own.
Testing it on your own code
node test/corpus.ts path/to/your/awkIt prints the files that failed, the first line of each that did, and the proportion that came out clean. The corpus itself is not included; Rosetta Code is CC BY-SA and cannot be redistributed under this licence.
