Skip to content

Grammar

kareman edited this page Aug 13, 2020 · 4 revisions

Grammar

Allows for recursive patterns, also indirectly.

@dynamicMemberLookup public class Grammar<Input: BidirectionalCollection>: Pattern where Input.Element: Hashable

Define subpatterns using <-, like this arithmetic pattern:

let g = Grammar { g in
  g.all <- g.expr • !any
  g.expr <- g.sum
  g.sum <- g.product • (("+" / "-") • g.product)*
  g.product <- g.power • (("*" / "/") • g.power)*
  g.power <- g.value • ("^" • g.power)¿
  g.value <- digit+ / "(" • g.expr • ")"
}

This recognises e.g. "1+2-3*(4+3)"

Warning: ``` g.a <- g.a • g.b ```

will lead to infinite recursion.

Inheritance

Pattern

Initializers

init()

@inlinable public init()

init()

@inlinable public init() where Input == String

init(_:)

@inlinable public convenience init(_ closure: (Grammar<Input>) -> Void)

init(_:)

@inlinable public convenience init(_ closure: (Grammar<String>) -> Void) where Input == String

Properties

description

var description: String

patterns

All the subpatterns and their names.

var patterns: [(name: String, pattern: AnyPattern<Input>)]

firstPattern

The main subpattern, which will be called when this Grammar is being used.

var firstPattern: String?

Methods

createInstructions(_:)

@inlinable public func createInstructions(_ instructions: inout ContiguousArray<Instruction<Input>>) throws

==(lhs:rhs:)

public static func ==<Input>(lhs: Grammar<Input>, rhs: Grammar<Input>) -> Bool
Clone this wiki locally