Pattern
public class Pattern<T, I> : PatternProtocol where I : Interpreter
Pattern consists of array of elements
-
Matcherinstances are capable of recognising patterns described in theelementscollection. It only remains effective, if theVariableinstances are surrounded byKeywordinstances, so no twoVariables 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 theMatcherinstance 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
matcherending closure is called whenever the pattern has successfully been recognised and allows the users of this framework to provide custom computations using the matchedVariablevalues.Declaration
Swift
public init(_ elements: [PatternElement], name: String? = nil, options: PatternOptions = [], matcher: @escaping MatcherBlock<T, I>)Parameters
elemenetsThe pattern to recognise
nameOptional identifier for the pattern. Defaults to the string representation of the elements
optionsOptions that modify the pattern matching algorithm
matcherThe 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
inThe elements array where the last element should be replaced
optionsOptions 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
Evalframework, performing the pattern matching, trying to identify, whether the input string is somehow related, or completely matches the pattern of thePatterninstance. Uses theMatcherclass for the evaluationDeclaration
Swift
func matches(string: String, from start: String.Index? = nil, interpreter: I, context: Context, connectedRanges: [ClosedRange<String.Index>] = []) -> MatchResult<T>Parameters
stringThe input
fromThe start of the range to analyse the result in
interpreterAn interpreter instance - if the variables need any further evaluation
contextThe context - if the block uses any contextual data
connectedRangesRanges 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() -> StringReturn Value
A stringified version of the input elements
-
A textual representation of the elements array
Declaration
Swift
static func stringify(elements: [PatternElement]) -> StringReturn Value
A stringified version of the input elements
View on GitHub
Install in Dash
Pattern Class Reference