Pattern

public class Pattern<T, I> : PatternProtocol where I : Interpreter

Pattern consists of array of elements

  • Matcher instances are capable of recognising patterns described in the elements collection. It only remains effective, if the Variable instances are surrounded by Keyword instances, so no two Variables should be next to each other. Otherwise, their matching result and value would be undefined. This collection should be provided during the initialisation, and cannot be modified once the Matcher instance has been created.

    Declaration

    Swift

    public let elements: [PatternElement]
  • The block to process the elements with

    Declaration

    Swift

    let matcher: MatcherBlock<T, I>
  • Options that modify the pattern matching algorithm

    Declaration

    Swift

    public let options: PatternOptions
  • Optional name to identify the pattern. If not provided during initialisation, it will fall back to the textual representation of the elements array

    Declaration

    Swift

    public let name: String
  • The first parameter is the pattern, that needs to be recognised. The matcher ending closure is called whenever the pattern has successfully been recognised and allows the users of this framework to provide custom computations using the matched Variable values.

    Declaration

    Swift

    public init(_ elements: [PatternElement],
                name: String? = nil,
                options: PatternOptions = [],
                matcher: @escaping MatcherBlock<T, I>)

    Parameters

    elemenets

    The pattern to recognise

    name

    Optional identifier for the pattern. Defaults to the string representation of the elements

    options

    Options that modify the pattern matching algorithm

    matcher

    The block to process the input with

  • If the last element in the elements pattern is a variable, shortest match will not match until the end of the input string, but just until the first empty character.

    Declaration

    Swift

    static func elementsByReplacingTheLastVariableNotToBeShortestMatch(in elements: [PatternElement], options: PatternOptions) -> [PatternElement]

    Parameters

    in

    The elements array where the last element should be replaced

    options

    Options that modify the pattern matching algorithm

    Return Value

    A new collection of elements, where the last element is replaced, whether it’s a variable with shortest flag on

  • This matcher provides the main logic of the Eval framework, performing the pattern matching, trying to identify, whether the input string is somehow related, or completely matches the pattern of the Pattern instance. Uses the Matcher class for the evaluation

    Declaration

    Swift

    func matches(string: String, from start: String.Index? = nil, interpreter: I, context: Context, connectedRanges: [ClosedRange<String.Index>] = []) -> MatchResult<T>

    Parameters

    string

    The input

    from

    The start of the range to analyse the result in

    interpreter

    An interpreter instance - if the variables need any further evaluation

    context

    The context - if the block uses any contextual data

    connectedRanges

    Ranges of string indices that are connected with opening-closing tag pairs, respectively

    Return Value

    The result of the matching operation

  • A textual representation of the Pattern’s elements array

    Declaration

    Swift

    func elementsAsString() -> String

    Return Value

    A stringified version of the input elements

  • A textual representation of the elements array

    Declaration

    Swift

    static func stringify(elements: [PatternElement]) -> String

    Return Value

    A stringified version of the input elements