Tramaj.Parser
- Package
- purescript-tramaj-purs
- Repository
- lucasdicioccio/purescript-tramaj-purs
Surface syntax to core AST. Everything the surface offers beyond
Tramaj.Ast's constructors is desugared here rather than represented:
@name=exprbinding lines become nestedLets;- string interpolation becomes
Concatover thestrbuiltin; branch(fallback, p1, v1, ...)becomes nestedBranch;- object shorthand
{foo}becomes{"foo": $foo}; ?shape(key, shape)becomes aLetof its key and the shape, each~namemarker in it anAllocat the form's one site;- escape sequences are resolved into the
StringLitthey denote.
The grammar keeps three leader characters from v1: . introduces a
document, $ reads a binding, @ defines one. What changed is that
there is now a single expression grammar — v1's separate template-phase
productions (node, nodeArg, childArg, templateSpecialForm,
pathOrCallChild) are gone, because documents are expressions.
A fourth character is lexical rather than grammatical: -- begins a
comment that runs to the end of the line. It is discarded by
skipSpaces along with whitespace, so it never reaches the AST and
there is nothing to desugar.
Two conventions are load-bearing and carried over deliberately:
- A special form's
trycovers name recognition only. Onceimportormaphas matched, its shape is parsed without backtracking, so a malformed one is a hard parse error instead of silently falling through to a meaninglessCallthat would only fail much later at eval time. The same holds for every form, not only the special ones: atrycovers what tells a form from its alternatives (name(,key:,(params) =>) and never the form's body. Atryaround a body throws away the error inside it, and the program is then reported as wrong at wherever the outermost alternative started — line 1, column 1 for an unclosed bracket anywhere in a document. Each closer also names its opener and where it is (closing). - A field-access suffix is parsed with no whitespace skipped before it,
so
f().renderedis a field access whilef()followed by a newline and.div(...)is two separate things.
Kept in lockstep with ../tramaj-hs/src/Tramaj/Parser.hs.
#parseProgram Source
parseProgram :: String -> Either ParseError ProgramA program is a sequence of statements and a root expression. Which kind of program it is follows from the root's own form — a document root is exactly one written as a document — so there is no mode to declare and no separate entry point to pick.