You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+15-15Lines changed: 15 additions & 15 deletions
Original file line number
Diff line number
Diff line change
@@ -45,11 +45,11 @@ Patterns are defined directly in code, instead of in a text string.
45
45
46
46
### Standard PEG
47
47
48
-
`"text"`
48
+
##### `"text"`
49
49
50
50
Text within double quotes matches that exact text, no need to escape special letters with `\`. If you want to turn a string variable `s` into a pattern, use `Literal(s)`.
51
51
52
-
`OneOf(...)`
52
+
##### `OneOf(...)`
53
53
54
54
This is like character classes (`[...]`) from regular expressions, and matches 1 character. `OneOf("aeiouAEIOU")` matches any single character in that string, and `OneOf("a"..."e")` matches any of "abcde". They can also be combined, like `OneOf("aeiou", punctuation, "x"..."z")`. To match any character _except_ ..., use `OneOf(not: ...)`.
55
55
@@ -64,31 +64,31 @@ OneOf(description: "ten") { character in
64
64
65
65
It takes a closure `@escaping (Character) -> Bool` and matches any character for which the closure returns `true`. The description parameter is only used when creating a textual representation of the pattern.
66
66
67
-
`a • b • c`
67
+
##### `a • b • c`
68
68
69
69
The • operator (Option-8 on U.S. keyboards, Option-Q on Norwegian ones) first matches `a`, then `b` and then `c`. It is used to create a pattern from a sequence of other patterns.
70
70
71
-
`a*`
71
+
##### `a*`
72
72
73
-
matches 0 or more, as many as it can (it is greedy, like the regex `a*?`). So a pattern like `a* • a` will never match anything because the repeated `a` pattern will always match all it can, leaving nothing left for the last `a`.
73
+
matches 0 or more, as many as it can (it is greedy, like the regex `a*?`). So a pattern like `a* • a` will never match anything because the `a*` pattern will always match all it can, leaving nothing left for the last `a`.
74
74
75
-
`a+`
75
+
##### `a+`
76
76
77
77
matches 1 or more, also as many as it can (like the regex `a+?`).
78
78
79
-
`a¿`
79
+
##### `a¿`
80
80
81
81
makes `a` optional, but it always matches if it can (the `¿` character is Option-Shift-TheKeyWith?OnIt on most keyboards).
82
82
83
-
`a / b`
83
+
##### `a / b`
84
84
85
85
This first tries the pattern on the left. If that fails it tries the pattern on the right. This is _ordered choice_, once `a` has matched it will never go back and try `b` if a later part of the expression fails. This is the main difference between PEGs and most other grammars and regex'es.
86
86
87
-
`&&a • b`
87
+
##### `&&a • b`
88
88
89
89
The "and predicate" first verifies that `a` matches, then moves the position in the input back to where `a` began and continues with `b`. In other words it verifies that both `a` and `b` match from the same position. So to match one ASCII letter you can use `&&ascii • letter`.
90
90
91
-
`!a • b`
91
+
##### `!a • b`
92
92
93
93
The "not predicate" verifies that `a` does _not_ match, then just like above it moves the position in the input back to where `a` began and continues with `b`. You can read it like "b and not a".
94
94
@@ -119,25 +119,25 @@ They all have the same name as the last part of the property, except for `wholeN
119
119
120
120
There is also `alphanumeric`, which is a `letter` or a `digit`.
121
121
122
-
`any`
122
+
##### `any`
123
123
124
124
Matches any character. `!any` matches only the end of the text.
125
125
126
-
`Line()`
126
+
##### `Line()`
127
127
128
128
Matches a single line, not including the newline characters. So `Line() • Line()` will never match anything, but `Line() • "\n" • Line()` matches 2 lines.
129
129
130
130
`Line.start` matches at the beginning of the text, and after any newline characters. `Line.end` matches at the end of the text, and right before any newline characters. They both have a length of 0, which means the next pattern will start at the same position in the text.
131
131
132
-
`Word.boundary`
132
+
##### `Word.boundary`
133
133
134
134
Matches the position right before or right after a word. Like `Line.start` and `Line.end` it also has a length of 0.
135
135
136
-
`a.repeat(...)`
136
+
##### `a.repeat(...)`
137
137
138
138
`a.repeat(2)` matches 2 of that pattern in a row. `a.repeat(...2)` matches 0, 1 or 2, `a.repeat(2...)` matches 2 or more and `a.repeat(3...6)` between 3 and 6.
139
139
140
-
`Skip() • a • b`
140
+
##### `Skip() • a • b`
141
141
142
142
Finds the first match of `a • b` from the current position.
0 commit comments