Skip to content

Commit 4591ba9

Browse files
committed
Improve and fix documentation for Skip.
1 parent 4ab25c7 commit 4591ba9

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

Sources/Patterns/Operations on Patterns/Skip.swift

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,32 @@
55
// Created by Kåre Morstøl on 25/05/2020.
66
//
77

8-
/// Skips 0 or more elements until a match for the next patterns are found.
8+
/// Skips 0 or more elements until a match for the next patterns is found.
99
///
10-
/// If this is at the end of a pattern, it skips to the end of input.
10+
/// ```swift
11+
/// let s = Skip() • a
12+
/// ```
13+
/// is the same as `|S <- A / . <S>|` in standard PEG.
14+
///
15+
/// - note:
16+
/// If `Skip` is at the end of a pattern, it just succeeds without consuming input. So it will be pointless.
17+
///
18+
/// But this works:
19+
/// ```swift
20+
/// let s = Skip()
21+
/// let p = s • " "
22+
/// ```
23+
/// because here the `s` pattern is "inlined".
24+
///
25+
/// This, however, does not work:
26+
/// ```swift
27+
/// let g = Grammar { g in
28+
/// g.nextSpace <- g.skip • " "
29+
/// g.skip <- Skip()
30+
/// }
31+
/// ```
32+
/// because in grammars the subexpressions are called, like functions, not "inlined", like Swift variables.
33+
/// So the `Skip()` in `g.skip` can't tell what will come after it.
1134
public struct Skip<Input: BidirectionalCollection>: Pattern where Input.Element: Hashable {
1235
public var description: String { "Skip()" }
1336

Tests/PatternsTests/SkipTests.swift

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,6 @@ class SkipTests: XCTestCase {
4646
input: lines, result: ["1", "2", "", "3"])
4747
assertParseAll(Capture(Line.start Skip() Line.end),
4848
input: lines, result: ["1", "2", "", "3"])
49-
50-
// undefined (Skip at end)
51-
_ = try Parser(search: " " Capture(Skip()))
52-
.matches(in: text)
5349
}
5450

5551
func testInsideOptional() throws {
@@ -73,6 +69,22 @@ class SkipTests: XCTestCase {
7369
assertParseMarkers(try Parser(Skip() Skip() " "), input: "This |is")
7470
}
7571

72+
func testAtTheEnd() throws {
73+
assertParseMarkers(" " Skip(), input: "a |bee")
74+
assertParseAll(" " Capture(Skip()), input: "a bee", result: [""])
75+
76+
// used in documentation for Skip.
77+
78+
let s = Skip()
79+
assertParseMarkers(try Parser(s " "), input: "jfd | |jlj |")
80+
81+
let g = Grammar { g in
82+
g.nextSpace <- g.skip " "
83+
g.skip <- Skip() // Does not work.
84+
}
85+
assertParseMarkers(try Parser(g), input: "sdf ksj")
86+
}
87+
7688
func testBeforeGrammarTailCall() throws {
7789
let recursive = Grammar { g in
7890
g.a <- " " Skip() g.a

0 commit comments

Comments
 (0)