gl : Grammar.GLReading page · generated from lean/Ecma/Model/NodeRel.lean
The parser's laws
What a model's parser must give: a Parse Node that is a derivation of the source text under the grammar (Model.ParseLaws), and a covered node that is a parse of its cover's text (Model.CoverLaws). Both are stated and owed: no theorem proves them of the engine, and no claim depends on them. The laws of a Parse Node's children and descendants (Model.NodeLaws) are proved.
Parse Nodes, derivations and their laws
From lean/Ecma/Model/NodeRel.lean
Model.NodeRel M src n t: the model's Parse Node n is the derivation tree t of Grammar.Derives (§5.1.2): the same nonterminal, production, parameters (as a set) and children (its nonterminals', a terminal giving none), and as source text the code points src i j of the input elements t spans. A token that is an instance of a lexical nonterminal is its lexical derivation, over the token's own code points.
Model.NodeRel
NodeRel.lean:24The model's Parse Node n is the derivation tree t of parser invocation st over the code points text; span i j is the span of text that input elements i to j occupy (§5.2.2: from the first's start to the last's end, the white space and comments between them included).
Lean, 27 lines
inductive Model.NodeRel (M : Model) (st : Nat) (text : List Nat) : (Nat → Nat → Nat × Nat) → M.Node → Grammar.Tree → Prop | node {span : Nat → Nat → Nat × Nat} {n : M.Node} {nt : Grammar.NT} {id : Nat} {ps : List Grammar.Param} {i j : Nat} {kids : List Grammar.Tree} : M.nodeNt n = nt.ctorIdx → M.nodeProd n = id → (∀ p, p ∈ M.nodeArgs n ↔ p ∈ ps) → M.nodeStamp n = st → M.nodeSpan n = span i j → M.seqLen (M.nodeSource n) = M.ofNat (Grammar.slice text (span i j).1 (span i j).2).length → (∀ k : Nat, M.seqGet (M.nodeSource n) (M.ofNat k) = ((Grammar.slice text (span i j).1 (span i j).2)[k]?).map M.ofNat) → M.seqLen (M.nodeKids n) = M.ofNat (kids.filter (!·.isLeaf)).length → (∀ k, (M.seqGet (M.nodeKids n) (M.ofNat k)).isSome = (k < (kids.filter (!·.isLeaf)).length)) → (∀ k t c, (kids.filter (!·.isLeaf))[k]? = some t → M.seqGet (M.nodeKids n) (M.ofNat k) = some c → Model.NodeRel M st text span c t) → Model.NodeRel M st text span n (.node nt id ps i j kids) /-- a terminal (§5.1.2: its Parse Node has no children) -/ | leaf {span : Nat → Nat → Nat × Nat} {n : M.Node} {i j : Nat} : M.nodeStamp n = st → M.nodeSpan n = span i j → M.seqLen (M.nodeSource n) = M.ofNat (Grammar.slice text (span i j).1 (span i j).2).length → (∀ k : Nat, M.seqGet (M.nodeSource n) (M.ofNat k) = ((Grammar.slice text (span i j).1 (span i j).2)[k]?).map M.ofNat) → M.seqLen (M.nodeKids n) = M.ofNat 0 → Model.NodeRel M st text span n (.leaf i j) /-- input element `k` as an instance of a lexical nonterminal: its derivation `t`, over the element's code points (spans from the element's start) -/ | lex {span : Nat → Nat → Nat × Nat} {n : M.Node} {k : Nat} {t : Grammar.Tree} : Model.NodeRel M st text (fun i j => ((span k (k + 1)).1 + i, (span k (k + 1)).1 + j)) n t → Model.NodeRel M st text span n (.lex k t)
Grammar.tokenSpan
NodeRel.lean:54The span of tokens i to j of toks in the source text: from the start of the first real token's input element to the end of the last's (a virtual ; has none).
def Grammar.tokenSpan (els : List Grammar.Element) (toks : List Grammar.Token) (i j : Nat) : Nat × Nat := let es := els.filter (·.isToken) let a := (toks.take i).countP (·.virt.isNone) let b := (toks.take j).countP (·.virt.isNone) let start := (es[a]?.map (·.i)).getD (((es.getLast?).map (·.j)).getD 0) if a < b then (start, (es[b - 1]?.map (·.j)).getD start) else (start, start)
Grammar.tokenText
NodeRel.lean:62The code points of src that tokens i to j of toks span.
def Grammar.tokenText (src : List Nat) (els : List Grammar.Element) (toks : List Grammar.Token) (i j : Nat) : List Nat := Grammar.slice src (Grammar.tokenSpan els toks i j).1 (Grammar.tokenSpan els toks i j).2
GrammarCtx
NodeRel.lean:69What a model's parse is stated against: the grammar data, the overlay's readings and context, the code point grammars' primitives, and whether its source text is strict mode code from outside (§11.2.2: eval code of a direct eval contained in strict mode code).
structure GrammarCtx whererulesUnder
NodeRel.lean:78The overlay's grammar readings in effect when the errata read as sel: the interpreting ones, and an erratum's when it does not read literally.
def rulesUnder (rs : List Grammar.RuleReading) (sel : ErratumId → Reading) : List Grammar.RuleReading := rs.filter fun x => x.erratum.all (sel · != .literal)
GrammarCtx.under
NodeRel.lean:82The context with the grammar readings in effect when the errata read as sel.
def GrammarCtx.under (G : GrammarCtx) (sel : ErratumId → Reading) : GrammarCtx := { G with rd := rulesUnder G.rd sel }
GrammarCtx.at
NodeRel.lean:87The context of a parser invocation under pc: Annex B's grammar, the Module goal (B.1.1) and strict mode code from outside as pc says.
def GrammarCtx.at (G : GrammarCtx) (pc : ParseCx) : GrammarCtx := { G with cx := ⟨pc.module, pc.annexB⟩, strict := pc.strict }
Grammar.Tree.height
NodeRel.lean:91One more than the depth of the tree.
def Grammar.Tree.height : Grammar.Tree → Nat | .node _ _ _ _ _ kids => (kids.attach.map fun ⟨k, _⟩ => k.height).foldl max 0 + 1 | _ => 1
Grammar.Tree.view
NodeRel.lean:98The tree's Parse Nodes as §11.2.2 reads them (Grammar.NodeView), text i j the code points the span i to j matches: a token that is an instance of a lexical nonterminal is one node, its own derivation not read.
def Grammar.Tree.view (text : Nat → Nat → List Nat) : Grammar.NodeView Grammar.Tree where isA t n := t.nt? == some n kids | .node _ _ _ _ _ kids => kids.filter (!·.isLeaf) | _ => [] span := Grammar.Tree.span textIs t cs := text t.span.1 t.span.2 == cs height := Grammar.Tree.height
Grammar.StrictCode
NodeRel.lean:110§11.2.2 ("Code is interpreted as strict mode code in the following situations"): the source text t matches is strict mode code by the tree it is in, up being its ancestors nearest first (Grammar.strictCode).
def Grammar.StrictCode (text : Nat → Nat → List Nat) (up : List Grammar.Tree) (t : Grammar.Tree) : Prop := Grammar.strictCode (Grammar.Tree.view text) up t = true
GrammarCtx.StrictIn
NodeRel.lean:115§11.2.2 in a parse of src scanned as els and toks: the source text the node t (its ancestors up) matches is strict mode code, from outside or by the tree (Grammar.StrictCode).
def GrammarCtx.StrictIn (G : GrammarCtx) (src : List Nat) (els : List Grammar.Element) (toks : List Grammar.Token) (up : List Grammar.Tree) (t : Grammar.Tree) : Prop := G.strict = true ∨ Grammar.StrictCode (Grammar.tokenText src els toks) up t
ParamsOf
NodeRel.lean:120The grammar parameters a goal symbol's arguments switch on.
def ParamsOf (args : List (ParamId × Bool)) (ps : List Grammar.Param) : Prop := ∀ p, p ∈ ps ↔ (p.ctorIdx, true) ∈ args
GrammarCtx.ParseIs
NodeRel.lean:127§5.1.2, §11.1.6: t is a parse of the code points src with goal goal, and the model's Parse Node n of parser invocation st is it: by ParsesText's steps for the syntactic grammar (its tokens named, a node's source the span of its input elements, white space and comments included), by ParsesCode for a code point grammar. B.1's productions modify those grammars (GL.overlays) and are no grammar a goal is parsed in (spec.html:53976, 54038).
def GrammarCtx.ParseIs (G : GrammarCtx) (M : Model) (st : Nat) (goal : Grammar.NT) (ps : List Grammar.Param) (src : List Nat) (n : M.Node) (t : Grammar.Tree) : Prop := if (G.gl.rulesOf .syntactic).any (·.lhs == goal) then ∃ els toks, Grammar.Scans G.gl G.rd G.cx G.P src els ∧ Grammar.GoalsAt G.gl G.rd G.cx G.P goal ps src els toks t ∧ toks.filter (·.virt.isNone) = Grammar.tokensOf src els ∧ (toks.all fun k => k.virt.isNone || !k.lt) = true ∧ Grammar.ASIOk G.gl G.rd G.cx G.P goal ps toks ∧ Grammar.Sentence G.gl G.rd G.cx G.P goal ps toks t ∧ Grammar.ForHeadersExplicit toks t ∧ Grammar.NonStrictOk G.gl (G.StrictIn src els toks) [] t ∧ Model.NodeRel M st src (Grammar.tokenSpan els toks) n t else ∃ g ∈ Grammar.codeGrammars, (G.gl.rulesOf g).any (·.lhs == goal) ∧ Grammar.ParsesCode G.gl G.rd G.cx G.P g goal ps src t ∧ Model.NodeRel M st src (fun i j => (i, j)) n t
GrammarCtx.Parses
NodeRel.lean:143§5.1.2, §11.1.6: t is a parse of the code points src with goal goal: ParseIs without the model's Parse Node.
def GrammarCtx.Parses (G : GrammarCtx) (goal : Grammar.NT) (ps : List Grammar.Param) (src : List Nat) (t : Grammar.Tree) : Prop := if (G.gl.rulesOf .syntactic).any (·.lhs == goal) then ∃ els toks, Grammar.Scans G.gl G.rd G.cx G.P src els ∧ Grammar.GoalsAt G.gl G.rd G.cx G.P goal ps src els toks t ∧ toks.filter (·.virt.isNone) = Grammar.tokensOf src els ∧ (toks.all fun k => k.virt.isNone || !k.lt) = true ∧ Grammar.ASIOk G.gl G.rd G.cx G.P goal ps toks ∧ Grammar.Sentence G.gl G.rd G.cx G.P goal ps toks t ∧ Grammar.ForHeadersExplicit toks t ∧ Grammar.NonStrictOk G.gl (G.StrictIn src els toks) [] t else ∃ g ∈ Grammar.codeGrammars, (G.gl.rulesOf g).any (·.lhs == goal) ∧ Grammar.ParsesCode G.gl G.rd G.cx G.P g goal ps src t
Model.seqOfNats
NodeRel.lean:157The code points of a List as the model's sequence.
def Model.seqOfNats (M : Model) (src : List Nat) : M.Seq M.Math := src.foldl (fun s c => M.seqPush s (M.ofNat c)) M.seqNil
Model.ParseLaws
NodeRel.lean:163Model.parse's laws, stated and never assumed, per reading of the errata and invocation context: a Parse Node it gives is a parse under that reading's grammar in that context, its nodes carrying the invocation's stamp, and where it gives none no tree is a parse (GrammarCtx.Parses).
Owed Stated and not proved: part of no claim.
structure Model.ParseLaws (M : Model) (G : GrammarCtx) : Prop where2 laws
If the parse succeeded and no early errors were found, return the Parse Node (an instance of goalSymbol) at the root of the parse tree resulting from the parse.
5.1.2 Parsing sec-parsingWhen a parse is successful, it constructs a parse tree, a rooted tree structure in which each node is a Parse Node.
A Parse Node the model's parse gives is the root of a parse of the code points with the goal, under the errata's grammar and the invocation's context, its nodes carrying the invocation's stamp (GrammarCtx.ParseIs).
An input stream of terminal symbols can be parsed using a goal symbol when it is matched by that goal symbol with no terminal symbols left over.
Where the model's parse gives no Parse Node, no tree is a parse of the code points with the goal under the errata's grammar and the invocation's context (GrammarCtx.Parses).
Model.Parsed
NodeRel.lean:182The Parse Nodes of parser invocations under the errata read as sel in context pc: a parse's result, the covered node the model gives from such a node (Model.cover), and their descendants.
inductive Model.Parsed (M : Model) (sel : ErratumId → Reading) (pc : ParseCx) : M.Node → Prop | parse {st : Nat} {src : M.Seq M.Math} {goal : NtId} {args : List (ParamId × Bool)} {n : M.Node} : M.parse sel pc st src goal args = some (some n) → Model.Parsed M sel pc n | cover {st : Nat} {n : M.Node} {k : NtId} {args : List (ParamId × Bool)} {r : M.Node} : Model.Parsed M sel pc n → M.cover sel pc st n k args = some r → Model.Parsed M sel pc r | desc {n d : M.Node} : Model.Parsed M sel pc n → d ∈ seqList (M.nodeDescendants n) → Model.Parsed M sel pc d
Model.CoverLaws
NodeRel.lean:194Model.cover's law (§5.1.5 "P must cover K"), stated and never assumed, per reading of the errata and invocation context: the node it gives from a Parse Node n of parser invocations in that context, with a goal and arguments, is a parse of n's source text with that goal under that reading's grammar in that context, its nodes carrying the new invocation's stamp: what ParseLaws.sound says of Model.parse's node for that text. Nothing is said of a node no parser invocation made, nor of one made in another context.
Owed Stated and not proved: part of no claim.
structure Model.CoverLaws (M : Model) (G : GrammarCtx) : Prop where1 laws
The sequence of tokens originally matched by p is parsed again using n as the goal symbol. If n takes grammatical parameters, then they are set to the same values used when p was originally parsed.
The covered node the model gives from a Parse Node n of a parse in this context is a parse of n's source text with the goal and the arguments, its nodes carrying the new invocation's stamp (GrammarCtx.ParseIs).
soundLean
sound : ∀ sel pc st n (goal : Grammar.NT) args ps src r, Model.Parsed M sel pc n → ParamsOf args ps → M.nodeSource n = M.seqOfNats src → M.cover sel pc st n goal.ctorIdx args = some r → ∃ t, ((G.under sel).at pc).ParseIs M st goal ps src r t
Grammar.occsOf
NodeRel.lean:208The nonterminal occurrences of a right-hand side in written order, with its optional symbols' presences m: each with whether it is present.
def occsOf : List Item → List Bool → List (NT × Bool) | [], _ => [] | .sym (.nt n _) true :: is, b :: bs => (n, b) :: occsOf is bs | .sym (.t _) true :: is, _ :: bs => occsOf is bs | .sym (.nt n _) false :: is, bs => (n, true) :: occsOf is bs | .butNot (.nt n _) _ :: is, bs => (n, true) :: occsOf is bs | _ :: is, bs => occsOf is bs
Grammar.prodAt
NodeRel.lean:218The right-hand side with production id id (GL.prodId): GL's (GL.byId), then past GL's numbers the readings'.
def prodAt (gl : GL) (rd : List RuleReading) (id : Nat) : Option Prod := (gl.byId.toList ++ (prodsOf (rd.map fun x => (x.grammar, [x.rule]))).map some)[id]?.join
Grammar.occsAt
NodeRel.lean:222The nonterminal occurrences of production id's right-hand side, with their presences.
def occsAt (gl : GL) (rd : List RuleReading) (id : Nat) : List (NT × Bool) := ((prodAt gl rd id).bind fun p => p.altOf.map fun a => occsOf a.rhs p.mask).getD []
Grammar.fillOccs
NodeRel.lean:226The written occurrences, each with its child: a present one takes the next of kids.
def fillOccs {N : Type} : List (NT × Bool) → List N → List (Nat × Option N) | [], _ => [] | (n, true) :: os, k :: ks => (n.ctorIdx, some k) :: fillOccs os ks | (n, _) :: os, ks => (n.ctorIdx, none) :: fillOccs os ks
Model.NodeLaws
NodeRel.lean:237The laws of nodeChild and nodeDescendants (§5.1.2), over the grammar G: a written occurrence's child is the next child with its nonterminal; in a parse and in a covered node the model gives, the absent ones are the production's absent optional symbols; the descendants are the children's pre-order flattening.
Proved of the engine's model, for the specification's grammar under the engine's readings: Engine.Claims.nodeLaws. Not a field of Implements.
structure Model.NodeLaws (M : Model) (G : GrammarCtx) : Prop where4 laws
Moreover, it has zero or more children, one for each symbol on the production's right-hand side: each child is a Parse Node that is an instance of the corresponding symbol.
The written occurrences of nt, first to last, have as children the node's children whose nonterminal is nt, in order.
child_kidsLean
child_kids : ∀ n nt, ∃ os : List (Option M.Node), (∀ k, M.nodeChild n nt k = os[k]?) ∧ os.filterMap id = (seqList (M.nodeKids n)).filter (M.nodeNt · == nt)
"the X of" a node of a parse under the errata read as sel is the child of the k-th written occurrence of nt in its production, none where that occurrence is an absent optional symbol.
child_parseLean
child_parse : ∀ sel pc st src goal args n d nt k, M.parse sel pc st (M.seqOfNats src) goal args = some (some n) → (d = n ∨ d ∈ seqList (M.nodeDescendants n)) → M.nodeChild d nt k = (((Grammar.fillOccs (Grammar.occsAt G.gl (rulesUnder G.rd sel) (M.nodeProd d)) (seqList (M.nodeKids d))).filter (·.1 == nt)).map (·.2))[k]?
The same in a covered node the model gives (Model.cover) and its descendants, whatever the cover.
child_coverLean
child_cover : ∀ sel pc st c goal args n d nt k, M.cover sel pc st c goal args = some n → (d = n ∨ d ∈ seqList (M.nodeDescendants n)) → M.nodeChild d nt k = (((Grammar.fillOccs (Grammar.occsAt G.gl (rulesUnder G.rd sel) (M.nodeProd d)) (seqList (M.nodeKids d))).filter (·.1 == nt)).map (·.2))[k]?
No early error rules are applied to p or any of its descendants.
A node's descendants are its children, each followed by its own descendants (pre-order).