### Bind Order Example Source: https://github.com/nim-lang/fusion/blob/master/src/fusion/matching.rst Illustrates immediate variable binding, allowing the bound variable to be used in subsequent checks within the same pattern. ```nim [@head, any @tail != head] ``` -------------------------------- ### Sequence Matching: 'any' with Condition Source: https://github.com/nim-lang/fusion/blob/master/src/fusion/matching.rst Example of capturing elements using 'any' when a specific condition is met. ```nim [any @a(it > 100)] ``` -------------------------------- ### Constructing HTML Node Tree with makeTree Source: https://github.com/nim-lang/fusion/blob/master/src/fusion/matching.rst Example of constructing an HTML node structure using the makeTree function in Nim. ```nim type HtmlNodeKind = enum htmlBase = "base" htmlHead = "head" htmlLink = "link" HtmlNode = object kind*: HtmlNodeKind text*: string subn*: seq[HtmlNode] func add(n: var HtmlNode, s: HtmlNode) = n.subn.add s discard makeTree(HtmlNode): base: link(text: "hello") ``` -------------------------------- ### Sequence Capture From Index Source: https://github.com/nim-lang/fusion/blob/master/src/fusion/matching.rst Capture all elements starting from index 1 into '@val'. Requires 'len(): int' and 'items(): T'. ```nim [_, all @val] ``` -------------------------------- ### Binding Field with 'is' Operator Source: https://github.com/nim-lang/fusion/blob/master/src/fusion/matching.rst Example of binding a field to a variable, checking if it is one of the specified string values. ```nim (fld: @hello is ("2" | "3")) ``` -------------------------------- ### String Scanning with Pattern Matching Source: https://github.com/nim-lang/fusion/blob/master/src/fusion/matching.rst Demonstrates using pattern matching to scan strings and validate parts of a date string. Requires the `allIt` helper function. ```nim func allIs(str: string, chars: set[char]): bool = str.allIt(it in chars) "2019-10-11 school start".split({'-', ' '}).assertMatch([ pref @dateParts(it.allIs({'0' .. '9'})), pref _(it.allIs({' '})), all @text ]) doAssert dateParts == @["2019", "10", "11"] doAssert text == @["school", "start"] ``` -------------------------------- ### Sequence Matching: 'any' Operator Source: https://github.com/nim-lang/fusion/blob/master/src/fusion/matching.rst Shows how to use the 'any' operator to capture elements that satisfy a condition. ```nim [any @a(it < 10)] ``` -------------------------------- ### Sequence Matching: Basic Patterns Source: https://github.com/nim-lang/fusion/blob/master/src/fusion/matching.rst Demonstrates fundamental sequence matching patterns, including exact size match and capturing the first element. ```nim [_] ``` ```nim [.._] ``` ```nim [@a] ``` ```nim [@a, .._] ``` -------------------------------- ### Sequence Matching: 'all' Operator Source: https://github.com/nim-lang/fusion/blob/master/src/fusion/matching.rst Demonstrates the 'all' operator to match leading elements that satisfy a condition. ```nim [all @a == 6, .._] ``` -------------------------------- ### Object Field Matching with Conditions Source: https://github.com/nim-lang/fusion/blob/master/src/fusion/matching.rst Shows how to match object fields using various conditions like range checks (`<`, `>`), set membership (`in`), and capturing values (`@capture`). It also demonstrates matching based on function results. ```nim type Obj = object fld1: int8 func len(o: Obj): int = 0 case Obj(): of (fld1: < -10): discard of (len: > 10): # can use results of function evaluation as fields - same idea as # method call syntax in regular code. discard of (fld1: in {1 .. 10}): discard of (fld1: @capture): doAssert capture == 0 ``` -------------------------------- ### Nim: Tree Matching with Block Syntax Source: https://github.com/nim-lang/fusion/blob/master/src/fusion/matching.rst Illustrates using block syntax for complex pattern matching on nested structures like ASTs. Braces can be omitted for case objects when the structure is clear. ```nim ProcDef: @name is Ident() | Postfix[_, @name is Ident()] # Other pattern parts ``` -------------------------------- ### Matching Nim AST Procedure Declaration Source: https://github.com/nim-lang/fusion/blob/master/src/fusion/matching.rst Demonstrates matching a Nim AST procedure declaration using a pattern-matching syntax. ```nim procDecl.assertMatch: ProcDef: Ident(strVal: @name) | Postfix[_, Ident(strVal: @name)] _ # Term rewriting template _ # Generic params FormalParams: @returnType all IdentDefs[@trailArgsName, _, _] @pragmas _ # Reserved @implementation ``` -------------------------------- ### Sequence Matching: Greedy 'any' Consumption Source: https://github.com/nim-lang/fusion/blob/master/src/fusion/matching.rst Shows how 'any' greedily consumes elements until the end, succeeding if at least one matches. ```nim [any @val is "d"] ``` -------------------------------- ### Basic Variable Declaration and Console Log Source: https://github.com/nim-lang/fusion/blob/master/tests/demo.html Demonstrates a simple variable declaration and logging to the console in JavaScript. ```javascript var myNumber = 0; console.log('Hello, world!') ``` -------------------------------- ### Variant Object Matching with Kind Sugar Source: https://github.com/nim-lang/fusion/blob/master/src/fusion/matching.rst Demonstrates the concise syntax for matching variant object kinds, such as `ForStmt()`, which is equivalent to `(kind: nnkForStmt)`. It also shows advanced matching on fields and kind values. ```nim ForStmt Ident "i" Infix Ident ".." IntLit 1 IntLit 10 StmtList Command Ident "echo" IntLit 12 ``` -------------------------------- ### Sequence Matching: Optional Binding Source: https://github.com/nim-lang/fusion/blob/master/src/fusion/matching.rst Demonstrates optional element matching with and without a default value. ```nim [opt @a] ``` ```nim [opt @a or "default"] ``` -------------------------------- ### Tuple Unpacking with Pattern Matching Source: https://github.com/nim-lang/fusion/blob/master/src/fusion/matching.rst Illustrates unpacking a nested tuple structure using pattern matching. The pattern `(@a, (@b, _), _)` assigns values to `a` and `b`. ```nim (@a, (@b, _), _) := ("hello", ("world", 11), 0.2) ``` -------------------------------- ### Basic Pattern Matching with 'in' Source: https://github.com/nim-lang/fusion/blob/master/src/fusion/matching.rst Demonstrates creating an expression using the 'in' operator for field matching. ```nim (fld: in {1, 3, 3}) or (fld: in Anything) ``` -------------------------------- ### Sequence Matching: Range and Condition Source: https://github.com/nim-lang/fusion/blob/master/src/fusion/matching.rst Illustrates matching a specific range of elements with a condition. ```nim [0 .. 2 is < 10, .._] ``` ```nim [0 .. 2 is < 10] ``` -------------------------------- ### Nim: KV-Pair Matching for JSON Source: https://github.com/nim-lang/fusion/blob/master/src/fusion/matching.rst Demonstrates matching nested key-value pairs, specifically shown with a JSON-like structure. It checks for the existence and type of a specific nested field. ```json {"menu": { "id": "file", "value": "File", "popup": { "menuitem": [ {"value": "New", "onclick": "CreateNewDoc()"}, {"value": "Open", "onclick": "OpenDoc()"}, {"value": "Close", "onclick": "CloseDoc()"} ] } }} ``` ```nim case inj: of {"menu" : {"file": @file is JString()}}: # ... else: raiseAssert("Expected [menu][file] as string, but found " & $inj) ``` -------------------------------- ### Binding Field to Variable Source: https://github.com/nim-lang/fusion/blob/master/src/fusion/matching.rst Shows how to bind a matched field to a new variable without additional checks. ```nim (fld: @varname) ``` -------------------------------- ### Nim: Custom Predicate Matching Source: https://github.com/nim-lang/fusion/blob/master/src/fusion/matching.rst Shows how to define and use custom predicate functions for matching. The underscore '_' can be used as a placeholder if the matched element is not captured. ```nim proc lenEq(s: openarray[int], value: int): bool = s.len == value case [1, 2]: of _.lenEq(3): # fails of _.lenEq(2): # matches ``` -------------------------------- ### Sequence Matching: 'until' Operator Source: https://github.com/nim-lang/fusion/blob/master/src/fusion/matching.rst Illustrates using the 'until' operator to match elements up to a specific condition. ```nim [until @a == 6, .._] ``` -------------------------------- ### Nim AST Representation of Procedure Declaration Source: https://github.com/nim-lang/fusion/blob/master/src/fusion/matching.rst A text-based representation of the AST for the 'testProc1' procedure declaration. ```text ProcDef Ident "testProc1" Empty Empty FormalParams Empty IdentDefs Ident "arg1" Ident "int" Empty Empty Empty StmtList DiscardStmt Empty ``` -------------------------------- ### Sequence Matching: Slice Capture Source: https://github.com/nim-lang/fusion/blob/master/src/fusion/matching.rst Illustrates capturing a slice of elements from a sequence using index ranges. ```nim [m .. n @capture] ``` -------------------------------- ### Field Comparison with Operator Source: https://github.com/nim-lang/fusion/blob/master/src/fusion/matching.rst Generate 'expr.fld ' for any prefix operator. ```nim (fld: == ident("33")) ``` -------------------------------- ### Nim: Matching with Infix Operator =~ Source: https://github.com/nim-lang/fusion/blob/master/src/fusion/matching.rst Demonstrates using an infix operator like '=~' from std/pegs for pattern matching within a case statement. The 'matches' variable is implicitly available. ```nim case (parent: (field: "string")): of (parent.field: =~ peg"str{\w+}"): doAssert matches[0] == "ing" ``` -------------------------------- ### Tuple First Element Capture Source: https://github.com/nim-lang/fusion/blob/master/src/fusion/matching.rst Capture the first element of a tuple into '@val', ignoring the rest. ```nim (@val, _) ``` -------------------------------- ### Custom Unpacking for Objects Source: https://github.com/nim-lang/fusion/blob/master/src/fusion/matching.rst Explains how to enable tuple-like unpacking for regular objects by overloading the `[]` operator. This allows accessing object fields using tuple assignment syntax. ```nim type Point = object x: int y: int proc `[]`(p: Point, idx: static[FieldIndex]): auto = when idx == 0: p.x elif idx == 1: p.y else: static: error("Cannot unpack `Point` into three-tuple") let point = Point(x: 12, y: 13) (@x, @y) := point assertEq x, 12 assertEq y, 13 ``` -------------------------------- ### Predicate Check Binding Source: https://github.com/nim-lang/fusion/blob/master/src/fusion/matching.rst Demonstrates binding a variable using a predicate check on a field. ```nim fld: @a.matchPredicate() ``` -------------------------------- ### Nim: Capturing Values with Custom Predicates Source: https://github.com/nim-lang/fusion/blob/master/src/fusion/matching.rst Illustrates capturing matched elements using the '@capture' pattern within a custom predicate. This allows subsequent assertions on the captured values. ```nim let arr = @[@[1, 2], @[2, 3], @[4]] discard arr.matches([any @capture.lenEq(2)]) doAssert capture == @[@[1, 2], @[2, 3]] ``` -------------------------------- ### Tuple Element Comparison Source: https://github.com/nim-lang/fusion/blob/master/src/fusion/matching.rst Generate access using '[1]' for each field in a tuple. ```nim ("2") ``` -------------------------------- ### Basic Field Capture Source: https://github.com/nim-lang/fusion/blob/master/src/fusion/matching.rst Capture a field 'fld' into a variable '@val'. ```nim (fld: @val) ``` -------------------------------- ### Nim Procedure Declaration AST Source: https://github.com/nim-lang/fusion/blob/master/src/fusion/matching.rst Represents the Abstract Syntax Tree (AST) for a simple Nim procedure declaration. ```nim proc testProc1(arg1: int) {.unpackProc.} = discard ``` -------------------------------- ### Nim: Matching with 'of' Prefix Operator Source: https://github.com/nim-lang/fusion/blob/master/src/fusion/matching.rst Shows the usage of the 'of' operator as a prefix for matching derived object kinds or specific fields within them. ```nim case Obj(): of of StmtList(subfield: @capture): # do something with `capture` ``` -------------------------------- ### Sequence Length Check (Less Than) Source: https://github.com/nim-lang/fusion/blob/master/src/fusion/matching.rst Check if the sequence length is less than 10. 'it' refers to the sequence length. ```nim (len: _(it < 10)) ``` -------------------------------- ### Sequence Capture Until Value Source: https://github.com/nim-lang/fusion/blob/master/src/fusion/matching.rst Capture all elements until the first element equals '2'. The sequence must fully match and end with '.._' to accept arbitrary length. ```nim [until @val == "2", .._] ``` -------------------------------- ### Sequence Matching: 'none' Operator Source: https://github.com/nim-lang/fusion/blob/master/src/fusion/matching.rst Shows how the 'none' operator checks if no elements satisfy a given condition. ```nim [none @a(it in {6 .. 10})] ``` -------------------------------- ### Tuple Equal Elements Match Source: https://github.com/nim-lang/fusion/blob/master/src/fusion/matching.rst Match a tuple containing two equal elements. ```nim (@val, @val) ``` -------------------------------- ### Object Field Pattern Match Source: https://github.com/nim-lang/fusion/blob/master/src/fusion/matching.rst Check if the field 'fld' matches a given pattern 'Patt()'. ```nim (fld: Patt()) ``` -------------------------------- ### Object Kind Matching Source: https://github.com/nim-lang/fusion/blob/master/src/fusion/matching.rst Match an object where the '.kind' field equals 'Kind()'. ```nim Kind() ``` -------------------------------- ### Nim: Ref Object Matching with 'of' Operator Source: https://github.com/nim-lang/fusion/blob/master/src/fusion/matching.rst Demonstrates matching derived ref objects using the 'of' operator for runtime type selection. This pattern also performs an isNil() check. ```nim type Base1 = ref object of RootObj fld: int First1 = ref object of Base1 first: float Second1 = ref object of Base1 second: string let elems: seq[Base1] = @[ Base1(fld: 123), First1(fld: 456, first: 0.123), Second1(fld: 678, second: "test"), nil ] for elem in elems: case elem: of of First1(fld: @capture1, first: @first): # Only capture `Frist1` elements doAssert capture1 == 456 doAssert first == 0.123 of of Second1(fld: @capture2, second: @second): # Capture `second` field in derived object doAssert capture2 == 678 doAssert second == "test" of of Base1(fld: @default): # Match all *non-nil* base elements doAssert default == 123 else: doAssert isNil(elem) ``` -------------------------------- ### Variable Type Inference Table Source: https://github.com/nim-lang/fusion/blob/master/src/fusion/matching.rst Provides a reference for how different pattern syntaxes inject variables and infer their types. ```nim [@a] var a: typeof(expr[0]) ``` ```nim {"key": @val} var val: typeof(expr["key"]) ``` ```nim [all @a] var a: seq[typeof(expr[0])] ``` ```nim [opt @val] var a: Option[typeof(expr[0])] ``` ```nim [opt @val or default] var a: typeof(expr[0]) ``` ```nim (fld: @val) var val: typeof(expr.fld) ``` -------------------------------- ### Sequence All Elements Match Source: https://github.com/nim-lang/fusion/blob/master/src/fusion/matching.rst Match if all elements in a sequence are equal to 12, capturing them into '@val'. Requires 'len(): int' and 'items(): T'. ```nim [all @val == 12] ``` -------------------------------- ### Object Field Value Comparison Source: https://github.com/nim-lang/fusion/blob/master/src/fusion/matching.rst Match field 'fld' against the string '3'. Generates 'expr.fld == "3"'. ```nim (fld: "3") ``` -------------------------------- ### Arbitrary Expression Binding Source: https://github.com/nim-lang/fusion/blob/master/src/fusion/matching.rst Shows binding a variable using an arbitrary expression, where the expression's type is inferred. ```nim fld: @a(it mod 2 == 0) ``` -------------------------------- ### Binding Field with Operator Check Source: https://github.com/nim-lang/fusion/blob/master/src/fusion/matching.rst Illustrates binding a field to a variable after performing an operator check. ```nim (fld: @varname Value) ``` -------------------------------- ### Table Key-Value Capture Source: https://github.com/nim-lang/fusion/blob/master/src/fusion/matching.rst Match a table with a specific key 'key' and capture its value into '@val'. Requires 'contains' and '[]' to be defined for the type. ```nim {"key" : @val} ``` -------------------------------- ### Set Element Membership Source: https://github.com/nim-lang/fusion/blob/master/src/fusion/matching.rst Check if 'key' is in the expression and if expr['key'] equals 'val'. Fails match on missing keys. ```nim {"key": "val"} ``` -------------------------------- ### Sequence Element Comparison Source: https://github.com/nim-lang/fusion/blob/master/src/fusion/matching.rst Match the first element of a sequence against the string '2'. Generates 'expr[pos] == "2"'. ```nim ["2"] ``` -------------------------------- ### Sequence Some Elements Match Source: https://github.com/nim-lang/fusion/blob/master/src/fusion/matching.rst Match if at least one element in a sequence equals 12, capturing all matching elements into '@val'. Requires 'len(): int' and 'items(): T'. ```nim [some @val == 12] ``` -------------------------------- ### Sequence Matching: Trailing Ellipsis Source: https://github.com/nim-lang/fusion/blob/master/src/fusion/matching.rst Explains the requirement for a trailing '.._' for a sequence to match if not fully consumed by patterns. ```nim [1, .._] ``` ```nim [1,2] ``` -------------------------------- ### Object Field Predicate Call Source: https://github.com/nim-lang/fusion/blob/master/src/fusion/matching.rst Check if calling 'matchesPredicate' on 'expr.fld' evaluates to true. ```nim (fld: _.matchesPredicate()) ``` -------------------------------- ### Sequence Capture Including Value Source: https://github.com/nim-lang/fusion/blob/master/src/fusion/matching.rst Capture all elements up to and including the first element that equals '1'. ```nim [until @val == 1, @val] ``` -------------------------------- ### Sequence Element Capture (Wildcard) Source: https://github.com/nim-lang/fusion/blob/master/src/fusion/matching.rst Match a sequence containing a single element, captured by a wildcard. ```nim [_] ``` -------------------------------- ### Sequence Minimum Length Match Source: https://github.com/nim-lang/fusion/blob/master/src/fusion/matching.rst Match a sequence with at least one element. ```nim [_, .._] ``` -------------------------------- ### Sequence Length Check (Range) Source: https://github.com/nim-lang/fusion/blob/master/src/fusion/matching.rst Check if the sequence length is within the range 0 to 10. ```nim (len: in {0 .. 10}) ``` -------------------------------- ### Sequence Length Match Source: https://github.com/nim-lang/fusion/blob/master/src/fusion/matching.rst Match a sequence with exactly two elements. Requires 'len(): int' and 'items(): T' to be defined. ```nim [_, _] ``` -------------------------------- ### Field Membership Check Source: https://github.com/nim-lang/fusion/blob/master/src/fusion/matching.rst Check if 'expr.fld' is within the set {2, 3, 4}. ```nim (fld: in {2,3,4}) ``` -------------------------------- ### Derived Object Matching Source: https://github.com/nim-lang/fusion/blob/master/src/fusion/matching.rst Match an object that is of a derived type. ```nim of Derived() ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.