Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pegs.nim now compiles with strictFuncs #18111

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ci/funs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ nimInternalBuildKochAndRunCI(){
echo_run nim c koch
if ! echo_run ./koch runCI; then
echo_run echo "runCI failed"
echo_run nim r -tools/ci_testresults.nim
echo_run nim r tools/ci_testresults.nim
return 1
fi
}
Expand Down
134 changes: 67 additions & 67 deletions lib/pure/pegs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -140,29 +140,29 @@ proc rule*(nt: NonTerminal): Peg = nt.rule
## Gets the *Peg* object representing the rule definition of the parent *Peg*
## object variant of a given *NonTerminal*.

proc term*(t: string): Peg {.noSideEffect, rtl, extern: "npegs$1Str".} =
func term*(t: string): Peg {.rtl, extern: "npegs$1Str".} =
## constructs a PEG from a terminal string
if t.len != 1:
result = Peg(kind: pkTerminal, term: t)
else:
result = Peg(kind: pkChar, ch: t[0])

proc termIgnoreCase*(t: string): Peg {.
noSideEffect, rtl, extern: "npegs$1".} =
func termIgnoreCase*(t: string): Peg {.
rtl, extern: "npegs$1".} =
## constructs a PEG from a terminal string; ignore case for matching
result = Peg(kind: pkTerminalIgnoreCase, term: t)

proc termIgnoreStyle*(t: string): Peg {.
noSideEffect, rtl, extern: "npegs$1".} =
func termIgnoreStyle*(t: string): Peg {.
rtl, extern: "npegs$1".} =
## constructs a PEG from a terminal string; ignore style for matching
result = Peg(kind: pkTerminalIgnoreStyle, term: t)

proc term*(t: char): Peg {.noSideEffect, rtl, extern: "npegs$1Char".} =
func term*(t: char): Peg {.rtl, extern: "npegs$1Char".} =
## constructs a PEG from a terminal char
assert t != '\0'
result = Peg(kind: pkChar, ch: t)

proc charSet*(s: set[char]): Peg {.noSideEffect, rtl, extern: "npegs$1".} =
func charSet*(s: set[char]): Peg {.rtl, extern: "npegs$1".} =
## constructs a PEG from a character set `s`
assert '\0' notin s
result = Peg(kind: pkCharChoice)
Expand Down Expand Up @@ -195,8 +195,8 @@ template multipleOp(k: PegKind, localOpt: untyped) =
if result.len == 1:
result = result.sons[0]

proc `/`*(a: varargs[Peg]): Peg {.
noSideEffect, rtl, extern: "npegsOrderedChoice".} =
func `/`*(a: varargs[Peg]): Peg {.
rtl, extern: "npegsOrderedChoice".} =
## constructs an ordered choice with the PEGs in `a`
multipleOp(pkOrderedChoice, addChoice)

Expand All @@ -212,12 +212,12 @@ proc addSequence(dest: var Peg, elem: Peg) =
else: add(dest, elem)
else: add(dest, elem)

proc sequence*(a: varargs[Peg]): Peg {.
noSideEffect, rtl, extern: "npegs$1".} =
func sequence*(a: varargs[Peg]): Peg {.
rtl, extern: "npegs$1".} =
## constructs a sequence with all the PEGs from `a`
multipleOp(pkSequence, addSequence)

proc `?`*(a: Peg): Peg {.noSideEffect, rtl, extern: "npegsOptional".} =
func `?`*(a: Peg): Peg {.rtl, extern: "npegsOptional".} =
## constructs an optional for the PEG `a`
if a.kind in {pkOption, pkGreedyRep, pkGreedyAny, pkGreedyRepChar,
pkGreedyRepSet}:
Expand All @@ -227,7 +227,7 @@ proc `?`*(a: Peg): Peg {.noSideEffect, rtl, extern: "npegsOptional".} =
else:
result = Peg(kind: pkOption, sons: @[a])

proc `*`*(a: Peg): Peg {.noSideEffect, rtl, extern: "npegsGreedyRep".} =
func `*`*(a: Peg): Peg {.rtl, extern: "npegsGreedyRep".} =
## constructs a "greedy repetition" for the PEG `a`
case a.kind
of pkGreedyRep, pkGreedyRepChar, pkGreedyRepSet, pkGreedyAny, pkOption:
Expand All @@ -242,24 +242,24 @@ proc `*`*(a: Peg): Peg {.noSideEffect, rtl, extern: "npegsGreedyRep".} =
else:
result = Peg(kind: pkGreedyRep, sons: @[a])

proc `!*`*(a: Peg): Peg {.noSideEffect, rtl, extern: "npegsSearch".} =
func `!*`*(a: Peg): Peg {.rtl, extern: "npegsSearch".} =
## constructs a "search" for the PEG `a`
result = Peg(kind: pkSearch, sons: @[a])

proc `!*\`*(a: Peg): Peg {.noSideEffect, rtl,
func `!*\`*(a: Peg): Peg {.rtl,
extern: "npgegsCapturedSearch".} =
## constructs a "captured search" for the PEG `a`
result = Peg(kind: pkCapturedSearch, sons: @[a])

proc `+`*(a: Peg): Peg {.noSideEffect, rtl, extern: "npegsGreedyPosRep".} =
func `+`*(a: Peg): Peg {.rtl, extern: "npegsGreedyPosRep".} =
## constructs a "greedy positive repetition" with the PEG `a`
return sequence(a, *a)

proc `&`*(a: Peg): Peg {.noSideEffect, rtl, extern: "npegsAndPredicate".} =
func `&`*(a: Peg): Peg {.rtl, extern: "npegsAndPredicate".} =
## constructs an "and predicate" with the PEG `a`
result = Peg(kind: pkAndPredicate, sons: @[a])

proc `!`*(a: Peg): Peg {.noSideEffect, rtl, extern: "npegsNotPredicate".} =
func `!`*(a: Peg): Peg {.rtl, extern: "npegsNotPredicate".} =
## constructs a "not predicate" with the PEG `a`
result = Peg(kind: pkNotPredicate, sons: @[a])

Expand Down Expand Up @@ -304,29 +304,29 @@ proc endAnchor*: Peg {.inline.} =
## constructs the PEG ``$`` which matches the end of the input.
result = !any()

proc capture*(a: Peg): Peg {.noSideEffect, rtl, extern: "npegsCapture".} =
func capture*(a: Peg): Peg {.rtl, extern: "npegsCapture".} =
## constructs a capture with the PEG `a`
result = Peg(kind: pkCapture, sons: @[a])

proc backref*(index: range[1..MaxSubpatterns]): Peg {.
noSideEffect, rtl, extern: "npegs$1".} =
func backref*(index: range[1..MaxSubpatterns]): Peg {.
rtl, extern: "npegs$1".} =
## constructs a back reference of the given `index`. `index` starts counting
## from 1.
result = Peg(kind: pkBackRef, index: index-1)

proc backrefIgnoreCase*(index: range[1..MaxSubpatterns]): Peg {.
noSideEffect, rtl, extern: "npegs$1".} =
func backrefIgnoreCase*(index: range[1..MaxSubpatterns]): Peg {.
rtl, extern: "npegs$1".} =
## constructs a back reference of the given `index`. `index` starts counting
## from 1. Ignores case for matching.
result = Peg(kind: pkBackRefIgnoreCase, index: index-1)

proc backrefIgnoreStyle*(index: range[1..MaxSubpatterns]): Peg {.
noSideEffect, rtl, extern: "npegs$1".} =
func backrefIgnoreStyle*(index: range[1..MaxSubpatterns]): Peg {.
rtl, extern: "npegs$1".} =
## constructs a back reference of the given `index`. `index` starts counting
## from 1. Ignores style for matching.
result = Peg(kind: pkBackRefIgnoreStyle, index: index-1)

proc spaceCost(n: Peg): int =
func spaceCost(n: Peg): int =
case n.kind
of pkEmpty: discard
of pkTerminal, pkTerminalIgnoreCase, pkTerminalIgnoreStyle, pkChar,
Expand All @@ -341,8 +341,8 @@ proc spaceCost(n: Peg): int =
inc(result, spaceCost(n.sons[i]))
if result >= InlineThreshold: break

proc nonterminal*(n: NonTerminal): Peg {.
noSideEffect, rtl, extern: "npegs$1".} =
func nonterminal*(n: NonTerminal): Peg {.
rtl, extern: "npegs$1".} =
## constructs a PEG that consists of the nonterminal symbol
assert n != nil
if ntDeclared in n.flags and spaceCost(n.rule) < InlineThreshold:
Expand All @@ -351,8 +351,8 @@ proc nonterminal*(n: NonTerminal): Peg {.
else:
result = Peg(kind: pkNonTerminal, nt: n)

proc newNonTerminal*(name: string, line, column: int): NonTerminal {.
noSideEffect, rtl, extern: "npegs$1".} =
func newNonTerminal*(name: string, line, column: int): NonTerminal {.
rtl, extern: "npegs$1".} =
## constructs a nonterminal symbol
result = NonTerminal(name: name, line: line, col: column)

Expand Down Expand Up @@ -433,7 +433,7 @@ proc charSetEsc(cc: set[char]): string =
else:
result = '[' & charSetEscAux(cc) & ']'

proc toStrAux(r: Peg, res: var string) =
func toStrAux(r: Peg, res: var string) =
case r.kind
of pkEmpty: add(res, "()")
of pkAny: add(res, '.')
Expand Down Expand Up @@ -519,7 +519,7 @@ proc toStrAux(r: Peg, res: var string) =
of pkStartAnchor:
add(res, '^')

proc `$` *(r: Peg): string {.noSideEffect, rtl, extern: "npegsToString".} =
func `$` *(r: Peg): string {.rtl, extern: "npegsToString".} =
## converts a PEG to its string representation
result = ""
toStrAux(r, result)
Expand Down Expand Up @@ -556,7 +556,7 @@ template matchOrParse(mopProc: untyped) =
# procs. For the former, *enter* and *leave* event handler code generators
# are provided which just return *discard*.

proc mopProc(s: string, p: Peg, start: int, c: var Captures): int =
func mopProc(s: string, p: Peg, start: int, c: var Captures): int =
proc matchBackRef(s: string, p: Peg, start: int, c: var Captures): int =
# Parse handler code must run in an *of* clause of its own for each
# *PegKind*, so we encapsulate the identical clause body for
Expand Down Expand Up @@ -851,8 +851,8 @@ template matchOrParse(mopProc: untyped) =
leave(pkStartAnchor, s, p, start, result)
of pkRule, pkList: assert false

proc rawMatch*(s: string, p: Peg, start: int, c: var Captures): int
{.noSideEffect, rtl, extern: "npegs$1".} =
func rawMatch*(s: string, p: Peg, start: int, c: var Captures): int
{.rtl, extern: "npegs$1".} =
## low-level matching proc that implements the PEG interpreter. Use this
## for maximum efficiency (every other PEG operation ends up calling this
## proc).
Expand Down Expand Up @@ -1060,8 +1060,8 @@ template fillMatches(s, caps, c) =
else:
caps[k] = ""

proc matchLen*(s: string, pattern: Peg, matches: var openArray[string],
start = 0): int {.noSideEffect, rtl, extern: "npegs$1Capture".} =
func matchLen*(s: string, pattern: Peg, matches: var openArray[string],
start = 0): int {.rtl, extern: "npegs$1Capture".} =
## the same as ``match``, but it returns the length of the match,
## if there is no match, -1 is returned. Note that a match length
## of zero can happen. It's possible that a suffix of `s` remains
Expand All @@ -1071,8 +1071,8 @@ proc matchLen*(s: string, pattern: Peg, matches: var openArray[string],
result = rawMatch(s, pattern, start, c)
if result >= 0: fillMatches(s, matches, c)

proc matchLen*(s: string, pattern: Peg,
start = 0): int {.noSideEffect, rtl, extern: "npegs$1".} =
func matchLen*(s: string, pattern: Peg,
start = 0): int {.rtl, extern: "npegs$1".} =
## the same as ``match``, but it returns the length of the match,
## if there is no match, -1 is returned. Note that a match length
## of zero can happen. It's possible that a suffix of `s` remains
Expand All @@ -1081,22 +1081,22 @@ proc matchLen*(s: string, pattern: Peg,
c.origStart = start
result = rawMatch(s, pattern, start, c)

proc match*(s: string, pattern: Peg, matches: var openArray[string],
start = 0): bool {.noSideEffect, rtl, extern: "npegs$1Capture".} =
func match*(s: string, pattern: Peg, matches: var openArray[string],
start = 0): bool {.rtl, extern: "npegs$1Capture".} =
## returns ``true`` if ``s[start..]`` matches the ``pattern`` and
## the captured substrings in the array ``matches``. If it does not
## match, nothing is written into ``matches`` and ``false`` is
## returned.
result = matchLen(s, pattern, matches, start) != -1

proc match*(s: string, pattern: Peg,
start = 0): bool {.noSideEffect, rtl, extern: "npegs$1".} =
func match*(s: string, pattern: Peg,
start = 0): bool {.rtl, extern: "npegs$1".} =
## returns ``true`` if ``s`` matches the ``pattern`` beginning from ``start``.
result = matchLen(s, pattern, start) != -1


proc find*(s: string, pattern: Peg, matches: var openArray[string],
start = 0): int {.noSideEffect, rtl, extern: "npegs$1Capture".} =
func find*(s: string, pattern: Peg, matches: var openArray[string],
start = 0): int {.rtl, extern: "npegs$1Capture".} =
## returns the starting position of ``pattern`` in ``s`` and the captured
## substrings in the array ``matches``. If it does not match, nothing
## is written into ``matches`` and -1 is returned.
Expand All @@ -1110,9 +1110,9 @@ proc find*(s: string, pattern: Peg, matches: var openArray[string],
return -1
# could also use the pattern here: (!P .)* P

proc findBounds*(s: string, pattern: Peg, matches: var openArray[string],
func findBounds*(s: string, pattern: Peg, matches: var openArray[string],
start = 0): tuple[first, last: int] {.
noSideEffect, rtl, extern: "npegs$1Capture".} =
rtl, extern: "npegs$1Capture".} =
## returns the starting position and end position of ``pattern`` in ``s``
## and the captured
## substrings in the array ``matches``. If it does not match, nothing
Expand All @@ -1127,8 +1127,8 @@ proc findBounds*(s: string, pattern: Peg, matches: var openArray[string],
return (i, i+L-1)
return (-1, 0)

proc find*(s: string, pattern: Peg,
start = 0): int {.noSideEffect, rtl, extern: "npegs$1".} =
func find*(s: string, pattern: Peg,
start = 0): int {.rtl, extern: "npegs$1".} =
## returns the starting position of ``pattern`` in ``s``. If it does not
## match, -1 is returned.
var c: Captures
Expand All @@ -1151,8 +1151,8 @@ iterator findAll*(s: string, pattern: Peg, start = 0): string =
yield substr(s, i, i+L-1)
inc(i, L)

proc findAll*(s: string, pattern: Peg, start = 0): seq[string] {.
noSideEffect, rtl, extern: "npegs$1".} =
func findAll*(s: string, pattern: Peg, start = 0): seq[string] {.
rtl, extern: "npegs$1".} =
## returns all matching *substrings* of `s` that match `pattern`.
## If it does not match, @[] is returned.
result = @[]
Expand Down Expand Up @@ -1183,31 +1183,31 @@ template `=~`*(s: string, pattern: Peg): bool =

# ------------------------- more string handling ------------------------------

proc contains*(s: string, pattern: Peg, start = 0): bool {.
noSideEffect, rtl, extern: "npegs$1".} =
func contains*(s: string, pattern: Peg, start = 0): bool {.
rtl, extern: "npegs$1".} =
## same as ``find(s, pattern, start) >= 0``
return find(s, pattern, start) >= 0

proc contains*(s: string, pattern: Peg, matches: var openArray[string],
start = 0): bool {.noSideEffect, rtl, extern: "npegs$1Capture".} =
func contains*(s: string, pattern: Peg, matches: var openArray[string],
start = 0): bool {.rtl, extern: "npegs$1Capture".} =
## same as ``find(s, pattern, matches, start) >= 0``
return find(s, pattern, matches, start) >= 0

proc startsWith*(s: string, prefix: Peg, start = 0): bool {.
noSideEffect, rtl, extern: "npegs$1".} =
func startsWith*(s: string, prefix: Peg, start = 0): bool {.
rtl, extern: "npegs$1".} =
## returns true if `s` starts with the pattern `prefix`
result = matchLen(s, prefix, start) >= 0

proc endsWith*(s: string, suffix: Peg, start = 0): bool {.
noSideEffect, rtl, extern: "npegs$1".} =
func endsWith*(s: string, suffix: Peg, start = 0): bool {.
rtl, extern: "npegs$1".} =
## returns true if `s` ends with the pattern `suffix`
var c: Captures
c.origStart = start
for i in start .. s.len-1:
if rawMatch(s, suffix, i, c) == s.len - i: return true

proc replacef*(s: string, sub: Peg, by: string): string {.
noSideEffect, rtl, extern: "npegs$1".} =
func replacef*(s: string, sub: Peg, by: string): string {.
rtl, extern: "npegs$1".} =
## Replaces `sub` in `s` by the string `by`. Captures can be accessed in `by`
## with the notation ``$i`` and ``$#`` (see strutils.`%`). Examples:
##
Expand Down Expand Up @@ -1235,8 +1235,8 @@ proc replacef*(s: string, sub: Peg, by: string): string {.
inc(i, x)
add(result, substr(s, i))

proc replace*(s: string, sub: Peg, by = ""): string {.
noSideEffect, rtl, extern: "npegs$1".} =
func replace*(s: string, sub: Peg, by = ""): string {.
rtl, extern: "npegs$1".} =
## Replaces `sub` in `s` by the string `by`. Captures cannot be accessed
## in `by`.
result = ""
Expand All @@ -1252,9 +1252,9 @@ proc replace*(s: string, sub: Peg, by = ""): string {.
inc(i, x)
add(result, substr(s, i))

proc parallelReplace*(s: string, subs: varargs[
func parallelReplace*(s: string, subs: varargs[
tuple[pattern: Peg, repl: string]]): string {.
noSideEffect, rtl, extern: "npegs$1".} =
rtl, extern: "npegs$1".} =
## Returns a modified copy of `s` with the substitutions in `subs`
## applied in parallel.
result = ""
Expand Down Expand Up @@ -1368,8 +1368,8 @@ iterator split*(s: string, sep: Peg): string =
if first < last:
yield substr(s, first, last-1)

proc split*(s: string, sep: Peg): seq[string] {.
noSideEffect, rtl, extern: "npegs$1".} =
func split*(s: string, sep: Peg): seq[string] {.
rtl, extern: "npegs$1".} =
## Splits the string `s` into substrings.
result = @[]
for it in split(s, sep): result.add it
Expand Down