Macaulay2 » Documentation
Packages » Parsing » Parser » orP
next | previous | forward | backward | up | index | toc

# orP -- parsing alternatives

- Usage:
    `r = orP(p,q,...)`
- Inputs:
  - (p,q,...), a sequence of parsers (of type Parser)
- Outputs:
  - r, a parser, a parser that accepts any sequence of tokens acceptable to one of the parsers p,q,...

## Description

An abbreviation for orP(p,q) is p|q.

In case of ambiguity, the value returned by the left-most accepting parser is provided.

If one of the arguments is a string then constParser is used to convert it into a parser.

In an efficient grammar, the first token presented to r will be acceptable to at most one of the input parsers, and then the parser returned by r will be the parser returned by the single accepting input parser.

    i1 : (constParser "abc" | constParser "def" : charAnalyzer) "abc"

    o1 = abc

    i2 : (constParser "abc" | constParser "def" : charAnalyzer) "def"

    o2 = def

    i3 : (constParser "abc" | "def" : charAnalyzer) "def"

    o3 = def

## Code

    ../Parsing.m2:84:19-89:49: --source code:
    Parser | Parser := (p,q) -> new Parser from ( c -> (
              p' := p c;
              q' := q c;
              if p' === null then q'
              else if q' === null then p'
              else if c === null then p' else p'|q'))

## See also

- constParser -- produce a parser that accepts a fixed string, one character at a time
- charAnalyzer -- a lexical analyzer that provides characters from a string one at a time
- Parser : Analyzer -- combine a parser with a lexical analyzer to make a complete system

## For the programmer

The object orP is a function closure.

The source of this document is in /build/macaulay2-88fgJW/macaulay2-1.25.11+ds/M2/Macaulay2/packages/Parsing.m2:358:0.