~/bend-docscommunity

parse.bend checks

raw source on the hub · import yaml-for-bend@0.1.1.0/parse.bend as Parse

The parser: tokens to events.

It follows libyaml's parser: a state machine with a stack of the states to return to. Each step looks at the next token and may take it and may emit an event. Where libyaml looks at two tokens in one call, this parser takes two steps, with a state in between, so a step never looks past one token. Where YAML 1.2 and libyaml disagree, it reads YAML 1.2: ": value" with no key has an empty key, a flow mapping's key may end on an earlier line than its ":", a stream may start with "...", and a bare document may follow "...".

3 imports
import Base
import ./token.bend as T
import ./scan.bend as S

Types

type Tag source · line 19 · raw

Data

A node's tag, resolved: none, or its full name ("!" when non-specific).

type Ev source · line 24 · raw

Data

An event, as libyaml's are.

type Evt source · line 37 · raw

Data

An event and where its node starts.

type Written source · line 41 · raw

Data

A tag as written: its handle and suffix.

type Prefix source · line 45 · raw

Data

A %TAG directive: a handle and the prefix it stands for.

type St source · line 51 · raw

Data

The parser's states. A collection's state keeps where it started, for errors; "After" states have just taken an indicator and keep where it ended, where an empty node would be.

type Ps source · line 84 · raw

Data

The parser: the tokens left, the state, the states to return to, the document's tag prefixes, and the events so far (newest first).

type PStep source · line 87 · raw

Data

type Parsed source · line 92 · raw

Data

The events, and the error that ended them early, if one did.

type Dirs source · line 101 · raw

Data

The directives before a document: its %TAG prefixes (over the defaults), checking that %YAML comes at most once and is 1.x and that no handle repeats.

Definitions

def when source · line 105 · raw

@-A:Type -> @hit:Bool -> @yes:(@_:Unit -> A) -> @no:(@_:Unit -> A) -> A

def ps.out source · line 115 · raw

@ps:Ps -> List<&2, Evt>

def ps.tags source · line 120 · raw

@ps:Ps -> List<&2, Prefix>

def ps.state source · line 125 · raw

@ps:Ps -> St

def take source · line 131 · raw

@ps:Ps -> Ps

Takes the next token.

def emit source · line 136 · raw

@ps:Ps -> @ev:Ev -> @at:0x856d414a32087762da315b6efc605eaa/token.Mark -> Ps

def goto source · line 141 · raw

@ps:Ps -> @s:St -> Ps

def push source · line 146 · raw

@ps:Ps -> @s:St -> Ps

def pop source · line 152 · raw

@ps:Ps -> Ps

Returns to the state on the stack.

def with.tags source · line 161 · raw

@ps:Ps -> @ts:List<&2, Prefix> -> Ps

def fail source · line 166 · raw

@ps:Ps -> @why:String -> @at:0x856d414a32087762da315b6efc605eaa/token.Mark -> PStep

def empty source · line 169 · raw

@ps:Ps -> @at:0x856d414a32087762da315b6efc605eaa/token.Mark -> Ps

def tok.kind source · line 175 · raw

@t:0x856d414a32087762da315b6efc605eaa/token.Token -> 0x856d414a32087762da315b6efc605eaa/token.Tok

def tok.start source · line 180 · raw

@t:0x856d414a32087762da315b6efc605eaa/token.Token -> 0x856d414a32087762da315b6efc605eaa/token.Mark

def tok.end source · line 185 · raw

@t:0x856d414a32087762da315b6efc605eaa/token.Token -> 0x856d414a32087762da315b6efc605eaa/token.Mark

def kind.index source · line 191 · raw

@t:0x856d414a32087762da315b6efc605eaa/token.Tok -> Nat

A token's kind as a number, to test it against a few.

def tok.is source · line 238 · raw

@+t:0x856d414a32087762da315b6efc605eaa/token.Token -> @k:Nat -> Bool

def tok.any source · line 241 · raw

@+t:0x856d414a32087762da315b6efc605eaa/token.Token -> @ks:List<&2, Nat> -> Bool

def STREAM_END source · line 244 · raw

Nat

def VERSION source · line 247 · raw

Nat

def TAG_DIR source · line 250 · raw

Nat

def DOC_START source · line 253 · raw

Nat

def DOC_END source · line 256 · raw

Nat

def BEND source · line 259 · raw

Nat

def FSEQ_END source · line 262 · raw

Nat

def FMAP_END source · line 265 · raw

Nat

def BENTRY source · line 268 · raw

Nat

def FENTRY source · line 271 · raw

Nat

def KEY source · line 274 · raw

Nat

def VALUE source · line 277 · raw

Nat

def defaults source · line 283 · raw

List<&2, Prefix>

def prefix.find source · line 286 · raw

@ts:List<&2, Prefix> -> @+handle:String -> Maybe<&2, String>

def tag.found source · line 295 · raw

@m:Maybe<&2, String> -> @suffix:String -> @at:0x856d414a32087762da315b6efc605eaa/token.Mark -> Result<&2, &2, 0x856d414a32087762da315b6efc605eaa/token.Error, Tag>

def tag.resolve source · line 303 · raw

@w:Maybe<&2, Written> -> @ts:List<&2, Prefix> -> @at:0x856d414a32087762da315b6efc605eaa/token.Mark -> Result<&2, &2, 0x856d414a32087762da315b6efc605eaa/token.Error, Tag>

A written tag's full name, through the document's %TAG prefixes.

def is_found source · line 313 · raw

@m:Maybe<&2, String> -> Bool

def dirs.go source · line 320 · raw

@toks:List<&2, 0x856d414a32087762da315b6efc605eaa/token.Token> -> @+user:List<&2, Prefix> -> @+seen:Bool -> Dirs

def toks.with source · line 339 · raw

@ps:Ps -> @ts:List<&2, 0x856d414a32087762da315b6efc605eaa/token.Token> -> Ps

def ps.toks source · line 344 · raw

@ps:Ps -> List<&2, 0x856d414a32087762da315b6efc605eaa/token.Token>

def head.is source · line 349 · raw

@ts:List<&2, 0x856d414a32087762da315b6efc605eaa/token.Token> -> @k:Nat -> Bool

def head.start source · line 356 · raw

@ts:List<&2, 0x856d414a32087762da315b6efc605eaa/token.Token> -> 0x856d414a32087762da315b6efc605eaa/token.Mark

def dirs.done source · line 364 · raw

@d:Dirs -> @+ps:Ps -> PStep

After the directives, "---" must start the document.

def block.node source · line 373 · raw

St

def h.doc.start source · line 378 · raw

@+ended:Bool -> @+t:0x856d414a32087762da315b6efc605eaa/token.Token -> @+ps:Ps -> PStep

A document starts: bare, when nothing came before or the last one ended with "..."; or with directives and "---"; or the stream ends.

def h.doc.end source · line 387 · raw

@+t:0x856d414a32087762da315b6efc605eaa/token.Token -> @+ps:Ps -> PStep

def mark.or source · line 394 · raw

@m:Maybe<&2, 0x856d414a32087762da315b6efc605eaa/token.Mark> -> @d:0x856d414a32087762da315b6efc605eaa/token.Mark -> 0x856d414a32087762da315b6efc605eaa/token.Mark

def is_none.s source · line 401 · raw

@m:Maybe<&2, String> -> Bool

def is_none.w source · line 408 · raw

@m:Maybe<&2, Written> -> Bool

def content.none source · line 416 · raw

@+block:Bool -> @+props:Bool -> @anchor:Maybe<&2, String> -> @tag:Tag -> @+at:0x856d414a32087762da315b6efc605eaa/token.Mark -> @+ps:Ps -> PStep

No content: an empty scalar, when the node has an anchor or a tag.

def content source · line 422 · raw

@k:0x856d414a32087762da315b6efc605eaa/token.Tok -> @+block:Bool -> @+indentless:Bool -> @+anchor:Maybe<&2, String> -> @+tag:Tag -> @+props:Bool -> @+at:0x856d414a32087762da315b6efc605eaa/token.Mark -> @+ps:Ps -> PStep

The node's content, after its anchor and tag.

def content.tagged source · line 442 · raw

@r:Result<&2, &2, 0x856d414a32087762da315b6efc605eaa/token.Error, Tag> -> @+k:0x856d414a32087762da315b6efc605eaa/token.Tok -> @+block:Bool -> @+indentless:Bool -> @+anchor:Maybe<&2, String> -> @+props:Bool -> @+at:0x856d414a32087762da315b6efc605eaa/token.Mark -> @+ps:Ps -> PStep

def node.rest source · line 449 · raw

@+block:Bool -> @+indentless:Bool -> @+anchor:Maybe<&2, String> -> @+tag:Maybe<&2, Written> -> @+bare:Bool -> @+at:0x856d414a32087762da315b6efc605eaa/token.Mark -> @+t:0x856d414a32087762da315b6efc605eaa/token.Token -> @+ps:Ps -> PStep

def h.node.kind source · line 452 · raw

@k:0x856d414a32087762da315b6efc605eaa/token.Tok -> @+block:Bool -> @+indentless:Bool -> @+anchor:Maybe<&2, String> -> @+tag:Maybe<&2, Written> -> @+bare:Bool -> @+at:0x856d414a32087762da315b6efc605eaa/token.Mark -> @+t:0x856d414a32087762da315b6efc605eaa/token.Token -> @+ps:Ps -> PStep

def h.node source · line 466 · raw

@+block:Bool -> @+indentless:Bool -> @+anchor:Maybe<&2, String> -> @+tag:Maybe<&2, Written> -> @+start:Maybe<&2, 0x856d414a32087762da315b6efc605eaa/token.Mark> -> @+t:0x856d414a32087762da315b6efc605eaa/token.Token -> @+ps:Ps -> PStep

A node: its anchor and tag (in either order), then an alias or content.

def h.doc.content source · line 471 · raw

@+t:0x856d414a32087762da315b6efc605eaa/token.Token -> @+ps:Ps -> PStep

def node.next source · line 478 · raw

@+block:Bool -> @+indentless:Bool -> @back:St -> @+t:0x856d414a32087762da315b6efc605eaa/token.Token -> @ps:Ps -> PStep

def h.bseq.entry source · line 481 · raw

@+m:0x856d414a32087762da315b6efc605eaa/token.Mark -> @+t:0x856d414a32087762da315b6efc605eaa/token.Token -> @+ps:Ps -> PStep

def h.bseq.after source · line 486 · raw

@+m:0x856d414a32087762da315b6efc605eaa/token.Mark -> @e:0x856d414a32087762da315b6efc605eaa/token.Mark -> @+t:0x856d414a32087762da315b6efc605eaa/token.Token -> @+ps:Ps -> PStep

def h.indentless source · line 490 · raw

@+m:0x856d414a32087762da315b6efc605eaa/token.Mark -> @+t:0x856d414a32087762da315b6efc605eaa/token.Token -> @+ps:Ps -> PStep

def h.indentless.after source · line 494 · raw

@+m:0x856d414a32087762da315b6efc605eaa/token.Mark -> @e:0x856d414a32087762da315b6efc605eaa/token.Mark -> @+t:0x856d414a32087762da315b6efc605eaa/token.Token -> @+ps:Ps -> PStep

def h.bmap.key source · line 498 · raw

@+m:0x856d414a32087762da315b6efc605eaa/token.Mark -> @+t:0x856d414a32087762da315b6efc605eaa/token.Token -> @+ps:Ps -> PStep

def h.bmap.after.key source · line 504 · raw

@+m:0x856d414a32087762da315b6efc605eaa/token.Mark -> @e:0x856d414a32087762da315b6efc605eaa/token.Mark -> @+t:0x856d414a32087762da315b6efc605eaa/token.Token -> @+ps:Ps -> PStep

def h.bmap.value source · line 508 · raw

@+m:0x856d414a32087762da315b6efc605eaa/token.Mark -> @+t:0x856d414a32087762da315b6efc605eaa/token.Token -> @+ps:Ps -> PStep

def h.bmap.after.value source · line 512 · raw

@+m:0x856d414a32087762da315b6efc605eaa/token.Mark -> @e:0x856d414a32087762da315b6efc605eaa/token.Mark -> @+t:0x856d414a32087762da315b6efc605eaa/token.Token -> @+ps:Ps -> PStep

def h.fseq.entry source · line 519 · raw

@+m:0x856d414a32087762da315b6efc605eaa/token.Mark -> @+first:Bool -> @+t:0x856d414a32087762da315b6efc605eaa/token.Token -> @+ps:Ps -> PStep

def h.fseq.map.key source · line 529 · raw

@+m:0x856d414a32087762da315b6efc605eaa/token.Mark -> @+t:0x856d414a32087762da315b6efc605eaa/token.Token -> @+ps:Ps -> PStep

An implicit pair's key in a flow sequence.

def h.fseq.map.value source · line 534 · raw

@+m:0x856d414a32087762da315b6efc605eaa/token.Mark -> @+t:0x856d414a32087762da315b6efc605eaa/token.Token -> @+ps:Ps -> PStep

def h.fseq.map.after.value source · line 538 · raw

@+m:0x856d414a32087762da315b6efc605eaa/token.Mark -> @e:0x856d414a32087762da315b6efc605eaa/token.Mark -> @+t:0x856d414a32087762da315b6efc605eaa/token.Token -> @+ps:Ps -> PStep

def h.fseq.map.end source · line 542 · raw

@m:0x856d414a32087762da315b6efc605eaa/token.Mark -> @+t:0x856d414a32087762da315b6efc605eaa/token.Token -> @ps:Ps -> PStep

def h.fmap.key source · line 545 · raw

@+m:0x856d414a32087762da315b6efc605eaa/token.Mark -> @+first:Bool -> @+t:0x856d414a32087762da315b6efc605eaa/token.Token -> @+ps:Ps -> PStep

def h.fmap.after.key source · line 554 · raw

@+m:0x856d414a32087762da315b6efc605eaa/token.Mark -> @e:0x856d414a32087762da315b6efc605eaa/token.Mark -> @+t:0x856d414a32087762da315b6efc605eaa/token.Token -> @+ps:Ps -> PStep

def h.fmap.value source · line 558 · raw

@+m:0x856d414a32087762da315b6efc605eaa/token.Mark -> @+t:0x856d414a32087762da315b6efc605eaa/token.Token -> @+ps:Ps -> PStep

def h.fmap.after.value source · line 562 · raw

@+m:0x856d414a32087762da315b6efc605eaa/token.Mark -> @e:0x856d414a32087762da315b6efc605eaa/token.Mark -> @+t:0x856d414a32087762da315b6efc605eaa/token.Token -> @+ps:Ps -> PStep

def h.fmap.empty source · line 568 · raw

@+m:0x856d414a32087762da315b6efc605eaa/token.Mark -> @+t:0x856d414a32087762da315b6efc605eaa/token.Token -> @+ps:Ps -> PStep

After a key with no KeyTok: a ":" on a later line is still its value (a flow mapping's key may run over lines), else the value is empty.

def h.state source · line 575 · raw

@s:St -> @+t:0x856d414a32087762da315b6efc605eaa/token.Token -> @+ps:Ps -> PStep

def h.token source · line 637 · raw

@k:0x856d414a32087762da315b6efc605eaa/token.Tok -> @+t:0x856d414a32087762da315b6efc605eaa/token.Token -> @+ps:Ps -> PStep

A Broken token stops the parser with its error.

def h.head source · line 644 · raw

@ts:List<&2, 0x856d414a32087762da315b6efc605eaa/token.Token> -> @+ps:Ps -> PStep

def p.step source · line 651 · raw

@+ps:Ps -> PStep

def p.run source · line 654 · raw

@fuel:Nat -> @s:PStep -> Parsed

def parse source · line 670 · raw

@+toks:List<&2, 0x856d414a32087762da315b6efc605eaa/token.Token> -> Parsed

The events of a token stream.

def events source · line 675 · raw

@text:String -> @max:Nat -> Parsed

The events of a YAML text, with flow collections nesting at most +max+ deep.

def escaped.other source · line 678 · raw

@c:Char -> @rest:String -> String

def escaped source · line 688 · raw

@s:String -> String

A scalar's text as the YAML test suite writes it: backslashes, breaks and tabs escaped.

def style.mark source · line 705 · raw

@s:0x856d414a32087762da315b6efc605eaa/token.Style -> String

def anchor.text source · line 718 · raw

@a:Maybe<&2, String> -> String

def tag.text source · line 725 · raw

@t:Tag -> String

def flow.text source · line 732 · raw

@+flow:Bool -> @brackets:String -> String

def ev.text source · line 736 · raw

@e:Ev -> String

An event as the YAML test suite writes it: "+MAP {} &a <tag>", "=VAL :x".

def evts.text source · line 759 · raw

@es:List<&2, Evt> -> String

def error.text source · line 768 · raw

@m:Maybe<&2, 0x856d414a32087762da315b6efc605eaa/token.Error> -> String

def parsed.text source · line 776 · raw

@p:Parsed -> String

The events of a parse, one per line, and the error after them.