4f: Parsing (Rule-Builders)

+bass

Parser modifier: LSB ordered +list as $atom of a $base.

Accepts

.wuc is an $atom.

.tyd is a $rule.

Produces

A $rule.

Source

++  bass
  |*  [wuc=@ tyd=rule]
  %+  cook
    |=  waq=(list @)
    %+  roll
      waq
    =|([p=@ q=@] |.((add p (mul wuc q))))
  tyd

Examples


+boss

Parser modifier, LSB.

Ordered +list as $atom of a $base.

Accepts

.wuc is an $atom.

.tyd is a $rule.

Produces

A $rule.

Source

Examples


+cold

Replace with constant.

Parser modifier. Accepts a $rule .sef and produces a parser that produces a constant .cus, assuming .sef is successful.

Accepts

.cus is a constant $noun.

.sef is a $rule.

Produces

An $edge.

Source

Examples


+cook

Apply gate.

Parser modifier. Produces a parser that takes a (successful) result of a $rule .sef and slams it through .poq.

Accepts

.poq is a gate.

.sef is a $rule.

Produces

A $rule.

Source

Examples


+easy

Always parse.

Parser generator. Produces a parser that succeeds with given $noun +huf without consuming any text.

Accepts

.huf is a $noun.

Produces

A $rule.

Source

Examples


+fuss

Has A or B?

If string .sic is parsed: %.y. If string .non is parsed: %.n. Otherwise, fail.

Accepts

.sic is a @t.

.non is a @t.

Produces

A $rule.

Source

Examples


+full

Parse to end.

Parser modifier. Accepts a $rule .sef, and produces a parser that succeeds only when the $tape of .tub is fully consumed using .sef.

Accepts

.sef is a $rule.

Produces

A $rule.

Source

Examples


+funk

Add to tape.

Parser modifier: prepend text to $tape before applying parser.

Accepts

.pre is a $tape.

.sef is a $rule.

Produces

A $rule.

Source

Examples


+here

Place-based apply.

Parser modifier. Similar to +cook in that it produces a parser that takes a (successful) result of .sef and slams it through .hez. .hez accepts a $pint .a and a $noun .b, which is what the parser parsed.

Accepts

.hez is a gate.

.sef is a $rule.

Produces

A $rule.

Source

Examples

+inde

Indentation block.

Apply $rule to indented block starting at current column number, omitting the leading whitespace.

Accepts

.sef is a $rule.

Produces

A $rule.

Source

Examples

Discussion

Note the amount of indentation whitespace to be stripped from the beginning of each line is determined by the value of .q (the column) in the $hair when +inde is first called. This means something like the +pfix expression in the example above is necessary to set the level of indentation. Additionally, the $rule given to +inde must consume the whole line including the line ending.


+ifix

Infix.

Parser modifier: surround with pair of $rules, the output of which is discarded.

Accepts

.fel is a pair of $rules.

.hof is a $rule.

Produces

A $rule.

Source

Examples


+jest

Match a cord.

Match and consume a cord.

Accepts

.daf is a @t.

Produces

A $rule.

Source

Examples


+just

Match a char.

Match and consume a single character.

Accepts

.daf is a $char

Produces

A $rule.

Source

Examples


+knee

Recursive parsers.

Used for recursive parsers, which would otherwise be infinite when compiled.

Accepts

+gar is a $noun.

.sef is a gate that accepts a $rule

Produces

A $rule.

Source

Examples


+mask

Match char.

Parser generator. Matches the next character if it is in a list of characters.

Accepts

.bud is a list of $char.

Produces

A $rule.

Source

Examples


+more

Parse list with delimiter.

Parser modifier: Parse a list of matches using a delimiter $rule.

Accepts

.bus is a $rule.

.fel is a $rule.

Produces

A $rule.

Source

Examples


+most

Parse list of at least one match.

Parser modifier: parse a +list of at least one match using a delimiter $rule.

Accepts

.bus is a $rule.

.fel is a $rule.

Produces

A $rule.

Source

Examples


+next

Consume char.

Consume any character, producing it as a result.

Accepts

.tub is a $nail.

Produces

An $edge.

Source

Examples


+perk

Parse cube fork.

Given .a, a list of @tas, match any one in the list and produce it. Note the list should not be a +list type, but just a null-terminated cell like ~[%foo %bar %baz]. The type produced will be a union of the items in the given list, so you can use a ?- expression on the output.

Accepts

.a is a (pole @tas).

Produces

A $rule.

Source

Examples


+plus

List of at least one match.

Parser modifier: parse +list of at least one match.

Accepts

.fel is a $rule.

Produces

A $rule.

Source

Examples


+punt

Unitized parse.

Either successfully apply $rule .a and produce a +unit of the result, or produce ~.

Accepts

.a is a $rule.

Produces

A $rule.

Source

Example


+sear

Conditional +cook.

Conditional +cook. Slams the result through a gate that produces a unit; if that unit is empty, fail.

Accepts

.pyq is a gate that produces a +unit.

.sef is a $rule.

Produces

A $rule.

Source

Examples


+shim

Char in range.

Match characters ($char) within a range.

Accepts

.les is an $atom.

.mos is an $atom.

Produces

A $rule.

Source

Examples


+slug

Use gate to parse delimited list.

Parser modifier: By composing with a gate, parse a delimited +list of matches.

Accepts

.raq is a binary gate.

.bus is a $rule.

.fel is a $rule.

Produces

A $rule.

Source

Examples


+stag

Add label.

Add a label to an $edge parsed by a $rule.

Accepts

.gob is a $noun.

.sef is a rule.

Produces

A $rule.

Source

Examples


+star

List of matches.

Parser modifier: parse +list of matches.

Accepts

.fel is a $rule.

Produces

A $rule.

Source

Examples


+stet

Add faces.

Add faces [p q] to range-parser pairs in a list. Typically used in combination with +stew.

Accepts

.leh is a list of range-parsers.

Produces

A (list [p=?(@ [@ @]) q=rule]).

Source

Examples


+stew

Switch by first char.

Parser generator. From an associative +list of characters or character ranges to $rules, construct a +map, and parse $tapes only with $rules associated with a range that the $tape's first character falls in.

Accepts

.leh is a (list [p=?(@ [@ @]) q=rule]), where .p is a $char or $char range.

Produces

A $rule.

Source

Examples


+stir

Parse repeatedly.

Parse with $rule as many times as possible, and fold over results with a binary gate.

Accepts

.rud is a $noun.

.raq is a gate that takes two $nouns and produces a cell.

.fel is a $rule.

Produces

A $rule.

Source

Examples


+stun

Parse several times.

Parse bounded number of times.

Accepts

.lig is a cell of $atoms ([@ @]) indicating the bounds.

.fel is a $rule.

Produces

A $rule.

Source

Examples


Last updated