@codincod/codemirror-lang-ada
v0.1.1
Published
Ada language support for the CodeMirror code editor
Maintainers
Readme
@codincod/codemirror-lang-ada 
[ CHANGELOG ]
This package implements Ada language support for the CodeMirror code editor, using a Lezer grammar written for this package.
CodeMirror has never shipped Ada, in either version. Editors that offer it
usually reach for a Pascal or VHDL mode, which colours some of the keywords and
gets the rest wrong: Integer'First comes out as an unterminated string,
'a' and 'Length are the same thing, 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 {ada} from "@codincod/codemirror-lang-ada"
const view = new EditorView({
parent: document.body,
doc: `with Ada.Text_IO; use Ada.Text_IO;
procedure Main is
begin
Put_Line ("Hello World!");
end Main;
`,
extensions: [basicSetup, ada()]
})Coverage
Ada 2012, with the Ada 2022 additions that have already reached real code:
square-bracket array aggregates, iterated component associations, the @
target name, iterator filters and parallel-free loop syntax. Tasking,
protected objects, generics, representation clauses, aspect specifications,
extension aggregates and expression functions are all in.
Measured against 1447 Ada files from Rosetta Code, 94.61% parse with no error
node. Most of the rest is not Ada: wiki markup left in the sources, GNAT project
files, distributed-systems configuration files, and fragments with ... written
where the author elided a page.
Two things a tokenizer has to settle
Almost all of Ada can be written down as a grammar directly. Two things cannot.
Case does not matter. IF, If and if are one word, and so are
Put_Line and PUT_LINE. Reserved words are recognised by a function that
lower-cases the word before looking it up, so a file written in the shouting
style of 1983 highlights the same as one written last week.
An apostrophe is two different things. 'a' is a character and
Integer'First is an attribute. Ada settles it by what stands in front: after a
name or a closing parenthesis the apostrophe is an attribute tick, and elsewhere
it opens a literal. That rule alone gets when 'a' => wrong, because when is a
word too, so the parser is asked as well: where an attribute could not stand, the
apostrophe is a literal whatever preceded it.
One thing about the grammar is worth saying out loud. Ada writes a call, an
index and an aggregate the same way, with a parenthesised list, and cannot tell
them apart without knowing what the name means. Neither does this, so there is
one Arguments node for all three and no guessing.
What it gives an editor
Syntax highlighting, folding for subprograms, packages, tasks, blocks, loops and
records, indentation that knows end, else, when and exception come back
out, and completion for the reserved words, the predefined types, the attributes
and the packages of the standard library.
adaLanguage 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/adaIt 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.
