Modify pegs.nim such that no match will return nil
An empty match will return "" A zero-length match will return nil Add test cases Add news information
This commit is contained in:
parent
73ff0432dc
commit
bc3464ede7
2 changed files with 19 additions and 1 deletions
|
|
@ -737,7 +737,12 @@ proc rawMatch*(s: string, p: Peg, start: int, c: var Captures): int {.
|
||||||
|
|
||||||
template fillMatches(s, caps, c: expr) =
|
template fillMatches(s, caps, c: expr) =
|
||||||
for k in 0..c.ml-1:
|
for k in 0..c.ml-1:
|
||||||
caps[k] = substr(s, c.matches[k][0], c.matches[k][1])
|
let startIdx = c.matches[k][0]
|
||||||
|
let endIdx = c.matches[k][1]
|
||||||
|
if startIdx != -1:
|
||||||
|
caps[k] = substr(s, startIdx, endIdx)
|
||||||
|
else:
|
||||||
|
caps[k] = nil
|
||||||
|
|
||||||
proc match*(s: string, pattern: Peg, matches: var openArray[string],
|
proc match*(s: string, pattern: Peg, matches: var openArray[string],
|
||||||
start = 0): bool {.nosideEffect, rtl, extern: "npegs$1Capture".} =
|
start = 0): bool {.nosideEffect, rtl, extern: "npegs$1Capture".} =
|
||||||
|
|
@ -1767,3 +1772,14 @@ when isMainModule:
|
||||||
|
|
||||||
assert match("prefix/start", peg"^start$", 7)
|
assert match("prefix/start", peg"^start$", 7)
|
||||||
|
|
||||||
|
if "foo" =~ peg"{'a'}?.*":
|
||||||
|
assert matches[0] == nil
|
||||||
|
else: assert false
|
||||||
|
|
||||||
|
if "foo" =~ peg"{''}.*":
|
||||||
|
assert matches[0] == ""
|
||||||
|
else: assert false
|
||||||
|
|
||||||
|
if "foo" =~ peg"{'foo'}":
|
||||||
|
assert matches[0] == "foo"
|
||||||
|
else: assert false
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,8 @@ News
|
||||||
- String case (or any non-ordinal case) statements
|
- String case (or any non-ordinal case) statements
|
||||||
without 'else' are deprecated.
|
without 'else' are deprecated.
|
||||||
- Recursive tuple types are not allowed anymore. Use ``object`` instead.
|
- Recursive tuple types are not allowed anymore. Use ``object`` instead.
|
||||||
|
- The PEGS module returns ``nil`` instead of ``""`` when an optional capture
|
||||||
|
fails to match
|
||||||
|
|
||||||
Language Additions
|
Language Additions
|
||||||
------------------
|
------------------
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue