@codincod/codemirror-lang-ada
v0.4.0
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 18805 files of published Ada, counting files that parse with no error node:
| Corpus | Files | Clean | | --- | ---: | ---: | | GNATprove and the SPARK 2014 testsuite | 13055 | 98.41% | | Ada Language Server | 1763 | 94.61% | | AWS, the Ada Web Server | 1229 | 99.59% | | GtkAda | 774 | 100.00% | | Ada Util | 546 | 100.00% | | GNATColl | 493 | 100.00% | | Alire | 479 | 98.12% | | VSS | 466 | 99.36% | | All of it | 18805 | 98.30% |
Rosetta Code is the other measurement and the softer one: 1447 files, 95.02%.
A snippet there is a fragment somebody pasted to demonstrate a task, so it never
reaches for a contract, a private child or a subunit, and half of what still
fails under it is not Ada at all. Wiki markup left in the source, GNAT project
files, shell transcripts, and pages elided with a literal ....
The two disagreed by 27 points before 0.2.0, and that is why the harder one is quoted first.
Three things a tokenizer has to settle
Almost all of Ada can be written down as a grammar directly. Three 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.
A quoted operator is sometimes a name. "+" (A, B) calls the addition
function and "/"'Result names its answer in a contract, but Put ("+")
prints a plus sign. Two questions the grammar cannot ask settle it: whether the
quoted text is one of the operators a subprogram may be named after, and
whether a ' or a ( follows the closing quote. Both have to hold.
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
npm run corpus -- 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 under the GNU Free Documentation License 1.2 and cannot be redistributed under this one.
