Skip to content

Instruction

kareman edited this page Aug 23, 2020 · 4 revisions

Instruction

The instructions used by patterns in createInstructions.

public enum Instruction<Input: BidirectionalCollection> where Input.Element: Hashable

Unless otherwise noted, each instruction moves on to the next instruction after it has finished.

Nested Type Aliases

Distance

public typealias Distance = Int

Enumeration Cases

elementEquals

Succeeds if the current element equals this element. Advances index to the next element.

case elementEquals(: Input.Element)

checkElement

Succeeds if the closure returns true when passed the current element. Advances index to the next element.

case checkElement(: (Input.Element) -> Bool)

checkIndex

Succeeds if the closure returns true when passed the input and the input index + atIndexOffset.

case checkIndex(: (Input, Input.Index) -> Bool, atIndexOffset: Int)

moveIndex

Moves the input index by offset.

case moveIndex(offset: Distance)

jump

Continues with the instruction at offset relative to this instruction.

case jump(offset: Distance)

search

Sets the input index to the output from the closure. If the output is nil, the instruction fails.

case search(: (Input, Input.Index) -> Input.Index?)

captureStart

Stores (current input index - atIndexOffset) as the beginning of capture name

case captureStart(name: String?, atIndexOffset: Int)

captureEnd

Stores (current input index - atIndexOffset) as the end of the most recently started capture.

case captureEnd(atIndexOffset: Int)

choice

Stores a snapshot of the current state, with input index set to (current + atIndexOffset).

case choice(offset: Distance, atIndexOffset: Int)

If there is a future failure the snapshot will be restored and the instruction at offset (relative to this instruction) will be called.

choiceEnd

Signals the end of a choice. Doesn't do anything else. Used as a barrier across which instructions cannot be moved.

case choiceEnd

commit

Discards the state saved by previous .choice, because the instructions since then have completed successfully and the alternative instructions at the previous .choice are no longer needed.

case commit

openCall

Will be replaced by .call in preprocessing. Is never executed.

case openCall(name: String)

call

Goes to the subpattern at offset relative to this instruction. When the subpattern finishes we move on to the instruction after this.

case call(offset: Distance)

`return`

Returns from this subpattern to the instruction after where this was called from.

case `return`

fail

Signals a failure.

case fail

The snapshot from the previous .choice is restored, if there aren't any left we stop matching altogether.

match

A match has been successfully completed!

case match

Will not continue with further instructions.

skip

Will be replaced in preprocessing. Is never executed.

case skip

Properties

isMovable

Can this instruction be moved by moveMovablesForward?

var isMovable: Bool

stopsMovables

Does this instruction prohibit moveMovablesForward from moving anything past it?

var stopsMovables: Bool

any

Succeeds anywhere except at the end of the input.

var any: Self

captureEnd

Stores the current input index as the end of the most recently started capture.

var captureEnd: Self

movesIndexBy

The offset by which this instruction will move the input index.

var movesIndexBy: Int?

doesNotDoAnything

Returns false only if instruction has no effect.

var doesNotDoAnything: Bool

Methods

captureStart(name:)

Stores the current input index as the beginning of capture name

@inlinable public static func captureStart(name: String?) -> Self

checkIndex(_:)

Succeeds if the closure returns true when passed the input and the input index.

@inlinable public static func checkIndex(_ test: @escaping (Input, Input.Index) -> Bool) -> Self

choice(offset:)

Stores a snapshot of the current state.

@inlinable public static func choice(offset: Int) -> Instruction

If there is a future failure the snapshot will be restored and the instruction at offset (relative to this instruction) will be called.

Clone this wiki locally