1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-25 09:47:17 +00:00

Update Peg.md text

This commit is contained in:
Calvin Rose 2019-01-15 11:09:22 -05:00
parent 2a333f8359
commit f0fcdf6bc5

View File

@ -72,7 +72,7 @@ given to the 0 byte, or the string terminator in many languages.
| `(range "az" "AZ")` | Matches characters in a range and advances 1 character. Multiple ranges can be combined together. | | `(range "az" "AZ")` | Matches characters in a range and advances 1 character. Multiple ranges can be combined together. |
| `(set "abcd")` | Match any character in the argument string. Advances 1 character. | | `(set "abcd")` | Match any character in the argument string. Advances 1 character. |
Primitve patterns are not that useful by themselves, but can be passed to `peg/match` and `peg/compile` as any pattern. Primitive patterns are not that useful by themselves, but can be passed to `peg/match` and `peg/compile` like any other pattern.
```clojure ```clojure
(peg/match "hello" "hello") # -> @[] (peg/match "hello" "hello") # -> @[]
@ -87,18 +87,11 @@ Primitve patterns are not that useful by themselves, but can be passed to `peg/m
## Combining Patterns ## Combining Patterns
These primitive patterns are combined with a few specials to match a wide number of languages. These specials These primitive patterns can be combined with several combinators to match a wide number of
languages. These combinators
can be thought of as the looping and branching forms in a traditional language can be thought of as the looping and branching forms in a traditional language
(that is how they are implemented when compiled to bytecode). (that is how they are implemented when compiled to bytecode).
PEGs try to match an input text with a pattern in a greedy manner.
This means that if a rule fails to match, that rule will fail and not try again. The only
backtracking provided in a peg is provided by the `(choice x y z ...)` special, which will
try rules in order until one succeeds, and the whole pattern succeeds. If no sub pattern
succeeds, then the whole pattern fails. Note that this means that the order of `x y z` in choice
DOES matter. If y matches everything that z matches, z will never succeed.
| Pattern Signature | What it matches | | Pattern Signature | What it matches |
| ------- | --------------- | | ------- | --------------- |
| `(choice a b c ...)` | Tries to match a, then b, and so on. Will succeed on the first successful match, and fails if none of the arguments match the text. | | `(choice a b c ...)` | Tries to match a, then b, and so on. Will succeed on the first successful match, and fails if none of the arguments match the text. |
@ -117,6 +110,13 @@ DOES matter. If y matches everything that z matches, z will never succeed.
| `(look offset patt)` | Matches only if patt matches at a fixed offset. offset can be any integer. patt will not produce captures and the peg will not advance any characters. | | `(look offset patt)` | Matches only if patt matches at a fixed offset. offset can be any integer. patt will not produce captures and the peg will not advance any characters. |
| `(> offset patt)` | Alias for `(look offset patt)` | | `(> offset patt)` | Alias for `(look offset patt)` |
PEGs try to match an input text with a pattern in a greedy manner.
This means that if a rule fails to match, that rule will fail and not try again. The only
backtracking provided in a peg is provided by the `(choice x y z ...)` special, which will
try rules in order until one succeeds, and the whole pattern succeeds. If no sub pattern
succeeds, then the whole pattern fails. Note that this means that the order of `x y z` in choice
DOES matter. If y matches everything that z matches, z will never succeed.
## Captures ## Captures
So far we have only been concerned with "does this text match this language?". This is useful, but So far we have only been concerned with "does this text match this language?". This is useful, but