Grammar & Parser

OcaltQL has a published grammar and a parser. The grammar describes every statement the language accepts, written two ways: EBNF for people and AI assistants to read, and GBNF for holding a model's output to valid OcaltQL while it writes. The parser checks a script before it runs and tells you the line and the reason when something is wrong. Both are made from the same verb table the OcaltQL runtime uses, so they agree with it.

Check a Script

Write or paste a script and press Check. The check runs in your browser with the same parser the runtime uses. Nothing runs, nothing is sent anywhere and no queries are spent.

Loading the parser...

The Files

Everything lives at ql.ocalt.com/grammar/. Each file is made from the runtime's verb table whenever the language changes, so it always matches the version that runs your scripts.

FileWhat it is for
ocaltql.ebnfThe grammar to read. Every rule has a plain explanation above it, and every verb lists its operand, its modifiers, which of them are required, whether it returns a value and whether it takes a block. Give it to an AI assistant along with llms.txt.
ocaltql.gbnfThe same grammar in GBNF, the grammar format that local model engines use to restrict what a model may write. A model held to it can only write statements OcaltQL accepts.
ocaltql-parser.js and ocaltql-parser.wasmThe parser for JavaScript, in the browser and in Node. Keep the two files together.
ocaltql-check.mjsA command-line checker for any computer with Node 18 or later. Keep it next to the two parser files.
ocaltql-check-linux-x64The same checker as a single program for 64-bit Linux, with nothing else to install.
verbs.jsonEvery verb as data: its words, its operand, each modifier with its spellings, what value it takes, and whether it is required or may repeat.
ocaltql-grammar.tar.gzAll of the above in one download, with a short guide.

Reading the Grammar

The EBNF file uses the common W3C notation. A rule names a part of the language and says what it is made of:

WrittenMeans
a ba then b
a | ba or b
a?   a*   a+optional, zero or more, one or more
"SET"a keyword. Keywords are not case sensitive; uppercase is the house style.
STRING, VARIABLE, NAMEa token: a quoted string, a ?variable, a bare word. The token rules are at the end of the file.

A script is a sequence: statements joined by AFTER, OR and AND. Here are the first rules of the file:

From ocaltql.ebnf
script          ::= sequence
sequence        ::= or-chain ( "AFTER" or-chain )*
or-chain        ::= and-group ( "OR" and-group )*
and-group       ::= statement ( "AND" statement )*
block           ::= "OPEN" sequence? "CLOSE"

Every verb then has a rule of its own, with a note that lists its parts. This is the one for CRAWL:

From ocaltql.ebnf
/* CRAWL <operand>
   operand required (value)
   modifiers, in any order, each once unless repeatable:
     DEPTH <n>
     LIMIT <n>
     WITHIN DOMAIN  (also written: WITHIN SITE, SAME SITE)
     ANYWHERE
     MATCH <pattern>
     EXCLUDE <pattern>
     FLAT
     TIMEOUT <n>
     SECONDS  (also written: SECOND)
   returns a value: capture it with SET ?var */
verb-crawl        ::= "CRAWL" verb-crawl-part*
verb-crawl-part   ::= expression | "DEPTH" expression | "LIMIT" expression
                    | ( "WITHIN" "DOMAIN" | "WITHIN" "SITE" | "SAME" "SITE" ) | "ANYWHERE"
                    | "MATCH" expression | "EXCLUDE" expression | "FLAT" | "TIMEOUT" expression
                    | ( "SECONDS" | "SECOND" ) | capture

Read it as: CRAWL, then any of its parts, in any order. The note above the rule says which parts are required. expression is any value, and capture is SET ?var.

The General Shape of a Statement

Most verbs share one shape: the verb's words, then its parts in any order. The parts are its operand (the value it works on), its modifiers, a SET capture if the verb returns a value, and an OPEN ... CLOSE block if it takes one. Each modifier appears once unless the grammar marks it repeatable. Order is yours to choose, so both of these are the same statement:

Two Orders, One Statement
FETCH "https://example.ocalt.com/api/echo.php" METHOD "POST" TIMEOUT 10 SET ?r
AFTER FETCH "https://example.ocalt.com/api/echo.php" SET ?r TIMEOUT 10 METHOD "POST"

About thirty verbs have a shape of their own because their parts depend on each other: IF, LOOP, SWITCH, FOREACH, NEW OPERATION, NEW CLASS, NEW QUERY, MEDIA, GRAPHIC, EDITOR, BROWSER, EMAIL, FTP, HTML, presentations and a few more. Their rules are written out in full near the top of the file.

Holding a Model to OcaltQL

A model that writes with the GBNF grammar attached cannot write a statement OcaltQL would reject for its shape: an unknown verb, a missing CLOSE, a modifier that verb does not have, a required part left out. Local model engines that support GBNF take the file as a grammar option, for example --grammar-file ocaltql.gbnf. Keywords in the GBNF are accepted in any letter case, and modifiers in any order, exactly as in the language.

Two things a grammar file cannot count, so the parser checks them after the model has written:

The reliable loop for an AI assistant is: write with the grammar, check with the parser, fix the line it names, then run.

Using the Parser

In JavaScript

Browser or Node
import { load } from 'https://ql.ocalt.com/grammar/ocaltql-parser.js';

const oql = await load();   // finds ocaltql-parser.wasm next to the .js file
oql.check('SET ?x AS 5 AFTER EMIT ?x');
// { ok: true, ast: { ... } }
oql.check('EMIT');
// { ok: false, line: 1, error: "Line 1: EMIT requires a value." }
oql.verbs().length;          // every verb, with its modifiers

load() also accepts a URL, a file path in Node, or the bytes of the .wasm file, for setups that keep it somewhere else.

On the Command Line

ocaltql-check
node ocaltql-check.mjs report.oql            # report.oql: ok
node ocaltql-check.mjs --json *.oql          # one JSON line per file
node ocaltql-check.mjs --ast report.oql      # the syntax tree
./ocaltql-check-linux-x64 report.oql         # the same, without Node

The program ends with code 0 when every script parses and 1 when any does not, so it fits a build step or a commit check.

Through the API

The API's dry run gives the same answer from the server: send the script with dry_run=1. Nothing runs and no queries are spent.

What a Check Tells You

A check answers one question: will the runtime read this script, and how. It does not run anything, so it cannot know whether a file exists, whether a variable will hold a value, or whether a remote service will answer. Those show up when the script runs, as catchable errors (see Error Catching).

The syntax tree shows exactly how the script was read: each statement, its verb, its operand, each modifier with its value, and how statements are chained. When a script does something unexpected, the tree usually shows why.

Things to Know

These rules are in the grammar's notes too. They are the places where a script can mean something other than it seems.

Keeping Up to Date

The grammar, the parser and verbs.json are rebuilt from the runtime whenever a verb is added or changed. The parser's answer and the runtime's answer were compared on every example in this tutorial, every command the Ocalt apps send, and a set of scripts built from every verb in every modifier order: they agree on all of them.