Fixed strictFuncs support for std/pegs (#18951)
* Fixed `strictFuncs` support for `std/pegs` Enabled `std/pegs` in the `strictFuncs` import test. Fixes #18057 Fixes #16892 See #18111 * Rebased from `devel` * Conditionally compile `std/pegs` in `koch` This is for supporting `csources` bootstrap. Co-authored-by: quantimnot <quantimnot@users.noreply.github.com>
This commit is contained in:
parent
f8d6a53227
commit
19774a72e7
3 changed files with 151 additions and 142 deletions
|
|
@ -87,26 +87,26 @@ type
|
||||||
else: sons: seq[Peg]
|
else: sons: seq[Peg]
|
||||||
NonTerminal* = ref NonTerminalObj
|
NonTerminal* = ref NonTerminalObj
|
||||||
|
|
||||||
proc kind*(p: Peg): PegKind = p.kind
|
func kind*(p: Peg): PegKind = p.kind
|
||||||
## Returns the *PegKind* of a given *Peg* object.
|
## Returns the *PegKind* of a given *Peg* object.
|
||||||
|
|
||||||
proc term*(p: Peg): string = p.term
|
func term*(p: Peg): string = p.term
|
||||||
## Returns the *string* representation of a given *Peg* variant object
|
## Returns the *string* representation of a given *Peg* variant object
|
||||||
## where present.
|
## where present.
|
||||||
|
|
||||||
proc ch*(p: Peg): char = p.ch
|
func ch*(p: Peg): char = p.ch
|
||||||
## Returns the *char* representation of a given *Peg* variant object
|
## Returns the *char* representation of a given *Peg* variant object
|
||||||
## where present.
|
## where present.
|
||||||
|
|
||||||
proc charChoice*(p: Peg): ref set[char] = p.charChoice
|
func charChoice*(p: Peg): ref set[char] = p.charChoice
|
||||||
## Returns the *charChoice* field of a given *Peg* variant object
|
## Returns the *charChoice* field of a given *Peg* variant object
|
||||||
## where present.
|
## where present.
|
||||||
|
|
||||||
proc nt*(p: Peg): NonTerminal = p.nt
|
func nt*(p: Peg): NonTerminal = p.nt
|
||||||
## Returns the *NonTerminal* object of a given *Peg* variant object
|
## Returns the *NonTerminal* object of a given *Peg* variant object
|
||||||
## where present.
|
## where present.
|
||||||
|
|
||||||
proc index*(p: Peg): range[-MaxSubpatterns..MaxSubpatterns-1] = p.index
|
func index*(p: Peg): range[-MaxSubpatterns..MaxSubpatterns-1] = p.index
|
||||||
## Returns the back-reference index of a captured sub-pattern in the
|
## Returns the back-reference index of a captured sub-pattern in the
|
||||||
## *Captures* object for a given *Peg* variant object where present.
|
## *Captures* object for a given *Peg* variant object where present.
|
||||||
|
|
||||||
|
|
@ -120,59 +120,59 @@ iterator pairs*(p: Peg): (int, Peg) {.inline.} =
|
||||||
for i in 0 ..< p.sons.len:
|
for i in 0 ..< p.sons.len:
|
||||||
yield (i, p.sons[i])
|
yield (i, p.sons[i])
|
||||||
|
|
||||||
proc name*(nt: NonTerminal): string = nt.name
|
func name*(nt: NonTerminal): string = nt.name
|
||||||
## Gets the name of the symbol represented by the parent *Peg* object variant
|
## Gets the name of the symbol represented by the parent *Peg* object variant
|
||||||
## of a given *NonTerminal*.
|
## of a given *NonTerminal*.
|
||||||
|
|
||||||
proc line*(nt: NonTerminal): int = nt.line
|
func line*(nt: NonTerminal): int = nt.line
|
||||||
## Gets the line number of the definition of the parent *Peg* object variant
|
## Gets the line number of the definition of the parent *Peg* object variant
|
||||||
## of a given *NonTerminal*.
|
## of a given *NonTerminal*.
|
||||||
|
|
||||||
proc col*(nt: NonTerminal): int = nt.col
|
func col*(nt: NonTerminal): int = nt.col
|
||||||
## Gets the column number of the definition of the parent *Peg* object variant
|
## Gets the column number of the definition of the parent *Peg* object variant
|
||||||
## of a given *NonTerminal*.
|
## of a given *NonTerminal*.
|
||||||
|
|
||||||
proc flags*(nt: NonTerminal): set[NonTerminalFlag] = nt.flags
|
func flags*(nt: NonTerminal): set[NonTerminalFlag] = nt.flags
|
||||||
## Gets the *NonTerminalFlag*-typed flags field of the parent *Peg* variant
|
## Gets the *NonTerminalFlag*-typed flags field of the parent *Peg* variant
|
||||||
## object of a given *NonTerminal*.
|
## object of a given *NonTerminal*.
|
||||||
|
|
||||||
proc rule*(nt: NonTerminal): Peg = nt.rule
|
func rule*(nt: NonTerminal): Peg = nt.rule
|
||||||
## Gets the *Peg* object representing the rule definition of the parent *Peg*
|
## Gets the *Peg* object representing the rule definition of the parent *Peg*
|
||||||
## object variant of a given *NonTerminal*.
|
## 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
|
## constructs a PEG from a terminal string
|
||||||
if t.len != 1:
|
if t.len != 1:
|
||||||
result = Peg(kind: pkTerminal, term: t)
|
result = Peg(kind: pkTerminal, term: t)
|
||||||
else:
|
else:
|
||||||
result = Peg(kind: pkChar, ch: t[0])
|
result = Peg(kind: pkChar, ch: t[0])
|
||||||
|
|
||||||
proc termIgnoreCase*(t: string): Peg {.
|
func termIgnoreCase*(t: string): Peg {.
|
||||||
noSideEffect, rtl, extern: "npegs$1".} =
|
rtl, extern: "npegs$1".} =
|
||||||
## constructs a PEG from a terminal string; ignore case for matching
|
## constructs a PEG from a terminal string; ignore case for matching
|
||||||
result = Peg(kind: pkTerminalIgnoreCase, term: t)
|
result = Peg(kind: pkTerminalIgnoreCase, term: t)
|
||||||
|
|
||||||
proc termIgnoreStyle*(t: string): Peg {.
|
func termIgnoreStyle*(t: string): Peg {.
|
||||||
noSideEffect, rtl, extern: "npegs$1".} =
|
rtl, extern: "npegs$1".} =
|
||||||
## constructs a PEG from a terminal string; ignore style for matching
|
## constructs a PEG from a terminal string; ignore style for matching
|
||||||
result = Peg(kind: pkTerminalIgnoreStyle, term: t)
|
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
|
## constructs a PEG from a terminal char
|
||||||
assert t != '\0'
|
assert t != '\0'
|
||||||
result = Peg(kind: pkChar, ch: t)
|
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`
|
## constructs a PEG from a character set `s`
|
||||||
assert '\0' notin s
|
assert '\0' notin s
|
||||||
result = Peg(kind: pkCharChoice)
|
result = Peg(kind: pkCharChoice)
|
||||||
new(result.charChoice)
|
new(result.charChoice)
|
||||||
result.charChoice[] = s
|
result.charChoice[] = s
|
||||||
|
|
||||||
proc len(a: Peg): int {.inline.} = return a.sons.len
|
func len(a: Peg): int {.inline.} = return a.sons.len
|
||||||
proc add(d: var Peg, s: Peg) {.inline.} = add(d.sons, s)
|
func add(d: var Peg, s: Peg) {.inline.} = add(d.sons, s)
|
||||||
|
|
||||||
proc addChoice(dest: var Peg, elem: Peg) =
|
func addChoice(dest: var Peg, elem: Peg) =
|
||||||
var L = dest.len-1
|
var L = dest.len-1
|
||||||
if L >= 0 and dest.sons[L].kind == pkCharChoice:
|
if L >= 0 and dest.sons[L].kind == pkCharChoice:
|
||||||
# caution! Do not introduce false aliasing here!
|
# caution! Do not introduce false aliasing here!
|
||||||
|
|
@ -195,12 +195,12 @@ template multipleOp(k: PegKind, localOpt: untyped) =
|
||||||
if result.len == 1:
|
if result.len == 1:
|
||||||
result = result.sons[0]
|
result = result.sons[0]
|
||||||
|
|
||||||
proc `/`*(a: varargs[Peg]): Peg {.
|
func `/`*(a: varargs[Peg]): Peg {.
|
||||||
noSideEffect, rtl, extern: "npegsOrderedChoice".} =
|
rtl, extern: "npegsOrderedChoice".} =
|
||||||
## constructs an ordered choice with the PEGs in `a`
|
## constructs an ordered choice with the PEGs in `a`
|
||||||
multipleOp(pkOrderedChoice, addChoice)
|
multipleOp(pkOrderedChoice, addChoice)
|
||||||
|
|
||||||
proc addSequence(dest: var Peg, elem: Peg) =
|
func addSequence(dest: var Peg, elem: Peg) =
|
||||||
var L = dest.len-1
|
var L = dest.len-1
|
||||||
if L >= 0 and dest.sons[L].kind == pkTerminal:
|
if L >= 0 and dest.sons[L].kind == pkTerminal:
|
||||||
# caution! Do not introduce false aliasing here!
|
# caution! Do not introduce false aliasing here!
|
||||||
|
|
@ -212,12 +212,12 @@ proc addSequence(dest: var Peg, elem: Peg) =
|
||||||
else: add(dest, elem)
|
else: add(dest, elem)
|
||||||
else: add(dest, elem)
|
else: add(dest, elem)
|
||||||
|
|
||||||
proc sequence*(a: varargs[Peg]): Peg {.
|
func sequence*(a: varargs[Peg]): Peg {.
|
||||||
noSideEffect, rtl, extern: "npegs$1".} =
|
rtl, extern: "npegs$1".} =
|
||||||
## constructs a sequence with all the PEGs from `a`
|
## constructs a sequence with all the PEGs from `a`
|
||||||
multipleOp(pkSequence, addSequence)
|
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`
|
## constructs an optional for the PEG `a`
|
||||||
if a.kind in {pkOption, pkGreedyRep, pkGreedyAny, pkGreedyRepChar,
|
if a.kind in {pkOption, pkGreedyRep, pkGreedyAny, pkGreedyRepChar,
|
||||||
pkGreedyRepSet}:
|
pkGreedyRepSet}:
|
||||||
|
|
@ -227,7 +227,7 @@ proc `?`*(a: Peg): Peg {.noSideEffect, rtl, extern: "npegsOptional".} =
|
||||||
else:
|
else:
|
||||||
result = Peg(kind: pkOption, sons: @[a])
|
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`
|
## constructs a "greedy repetition" for the PEG `a`
|
||||||
case a.kind
|
case a.kind
|
||||||
of pkGreedyRep, pkGreedyRepChar, pkGreedyRepSet, pkGreedyAny, pkOption:
|
of pkGreedyRep, pkGreedyRepChar, pkGreedyRepSet, pkGreedyAny, pkOption:
|
||||||
|
|
@ -242,94 +242,94 @@ proc `*`*(a: Peg): Peg {.noSideEffect, rtl, extern: "npegsGreedyRep".} =
|
||||||
else:
|
else:
|
||||||
result = Peg(kind: pkGreedyRep, sons: @[a])
|
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`
|
## constructs a "search" for the PEG `a`
|
||||||
result = Peg(kind: pkSearch, sons: @[a])
|
result = Peg(kind: pkSearch, sons: @[a])
|
||||||
|
|
||||||
proc `!*\`*(a: Peg): Peg {.noSideEffect, rtl,
|
func `!*\`*(a: Peg): Peg {.rtl,
|
||||||
extern: "npgegsCapturedSearch".} =
|
extern: "npgegsCapturedSearch".} =
|
||||||
## constructs a "captured search" for the PEG `a`
|
## constructs a "captured search" for the PEG `a`
|
||||||
result = Peg(kind: pkCapturedSearch, sons: @[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`
|
## constructs a "greedy positive repetition" with the PEG `a`
|
||||||
return sequence(a, *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`
|
## constructs an "and predicate" with the PEG `a`
|
||||||
result = Peg(kind: pkAndPredicate, sons: @[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`
|
## constructs a "not predicate" with the PEG `a`
|
||||||
result = Peg(kind: pkNotPredicate, sons: @[a])
|
result = Peg(kind: pkNotPredicate, sons: @[a])
|
||||||
|
|
||||||
proc any*: Peg {.inline.} =
|
func any*: Peg {.inline.} =
|
||||||
## constructs the PEG `any character`:idx: (``.``)
|
## constructs the PEG `any character`:idx: (``.``)
|
||||||
result = Peg(kind: pkAny)
|
result = Peg(kind: pkAny)
|
||||||
|
|
||||||
proc anyRune*: Peg {.inline.} =
|
func anyRune*: Peg {.inline.} =
|
||||||
## constructs the PEG `any rune`:idx: (``_``)
|
## constructs the PEG `any rune`:idx: (``_``)
|
||||||
result = Peg(kind: pkAnyRune)
|
result = Peg(kind: pkAnyRune)
|
||||||
|
|
||||||
proc newLine*: Peg {.inline.} =
|
func newLine*: Peg {.inline.} =
|
||||||
## constructs the PEG `newline`:idx: (``\n``)
|
## constructs the PEG `newline`:idx: (``\n``)
|
||||||
result = Peg(kind: pkNewLine)
|
result = Peg(kind: pkNewLine)
|
||||||
|
|
||||||
proc unicodeLetter*: Peg {.inline.} =
|
func unicodeLetter*: Peg {.inline.} =
|
||||||
## constructs the PEG ``\letter`` which matches any Unicode letter.
|
## constructs the PEG ``\letter`` which matches any Unicode letter.
|
||||||
result = Peg(kind: pkLetter)
|
result = Peg(kind: pkLetter)
|
||||||
|
|
||||||
proc unicodeLower*: Peg {.inline.} =
|
func unicodeLower*: Peg {.inline.} =
|
||||||
## constructs the PEG ``\lower`` which matches any Unicode lowercase letter.
|
## constructs the PEG ``\lower`` which matches any Unicode lowercase letter.
|
||||||
result = Peg(kind: pkLower)
|
result = Peg(kind: pkLower)
|
||||||
|
|
||||||
proc unicodeUpper*: Peg {.inline.} =
|
func unicodeUpper*: Peg {.inline.} =
|
||||||
## constructs the PEG ``\upper`` which matches any Unicode uppercase letter.
|
## constructs the PEG ``\upper`` which matches any Unicode uppercase letter.
|
||||||
result = Peg(kind: pkUpper)
|
result = Peg(kind: pkUpper)
|
||||||
|
|
||||||
proc unicodeTitle*: Peg {.inline.} =
|
func unicodeTitle*: Peg {.inline.} =
|
||||||
## constructs the PEG ``\title`` which matches any Unicode title letter.
|
## constructs the PEG ``\title`` which matches any Unicode title letter.
|
||||||
result = Peg(kind: pkTitle)
|
result = Peg(kind: pkTitle)
|
||||||
|
|
||||||
proc unicodeWhitespace*: Peg {.inline.} =
|
func unicodeWhitespace*: Peg {.inline.} =
|
||||||
## constructs the PEG ``\white`` which matches any Unicode
|
## constructs the PEG ``\white`` which matches any Unicode
|
||||||
## whitespace character.
|
## whitespace character.
|
||||||
result = Peg(kind: pkWhitespace)
|
result = Peg(kind: pkWhitespace)
|
||||||
|
|
||||||
proc startAnchor*: Peg {.inline.} =
|
func startAnchor*: Peg {.inline.} =
|
||||||
## constructs the PEG ``^`` which matches the start of the input.
|
## constructs the PEG ``^`` which matches the start of the input.
|
||||||
result = Peg(kind: pkStartAnchor)
|
result = Peg(kind: pkStartAnchor)
|
||||||
|
|
||||||
proc endAnchor*: Peg {.inline.} =
|
func endAnchor*: Peg {.inline.} =
|
||||||
## constructs the PEG ``$`` which matches the end of the input.
|
## constructs the PEG ``$`` which matches the end of the input.
|
||||||
result = !any()
|
result = !any()
|
||||||
|
|
||||||
proc capture*(a: Peg = Peg(kind: pkEmpty)): Peg {.noSideEffect, rtl, extern: "npegsCapture".} =
|
func capture*(a: Peg = Peg(kind: pkEmpty)): Peg {.rtl, extern: "npegsCapture".} =
|
||||||
## constructs a capture with the PEG `a`
|
## constructs a capture with the PEG `a`
|
||||||
result = Peg(kind: pkCapture, sons: @[a])
|
result = Peg(kind: pkCapture, sons: @[a])
|
||||||
|
|
||||||
proc backref*(index: range[1..MaxSubpatterns], reverse: bool = false): Peg {.
|
func backref*(index: range[1..MaxSubpatterns], reverse: bool = false): Peg {.
|
||||||
noSideEffect, rtl, extern: "npegs$1".} =
|
rtl, extern: "npegs$1".} =
|
||||||
## constructs a back reference of the given `index`. `index` starts counting
|
## constructs a back reference of the given `index`. `index` starts counting
|
||||||
## from 1. `reverse` specifies wether indexing starts from the end of the
|
## from 1. `reverse` specifies wether indexing starts from the end of the
|
||||||
## capture list.
|
## capture list.
|
||||||
result = Peg(kind: pkBackRef, index: (if reverse: -index else: index - 1))
|
result = Peg(kind: pkBackRef, index: (if reverse: -index else: index - 1))
|
||||||
|
|
||||||
proc backrefIgnoreCase*(index: range[1..MaxSubpatterns], reverse: bool = false): Peg {.
|
func backrefIgnoreCase*(index: range[1..MaxSubpatterns], reverse: bool = false): Peg {.
|
||||||
noSideEffect, rtl, extern: "npegs$1".} =
|
rtl, extern: "npegs$1".} =
|
||||||
## constructs a back reference of the given `index`. `index` starts counting
|
## constructs a back reference of the given `index`. `index` starts counting
|
||||||
## from 1. `reverse` specifies wether indexing starts from the end of the
|
## from 1. `reverse` specifies wether indexing starts from the end of the
|
||||||
## capture list. Ignores case for matching.
|
## capture list. Ignores case for matching.
|
||||||
result = Peg(kind: pkBackRefIgnoreCase, index: (if reverse: -index else: index - 1))
|
result = Peg(kind: pkBackRefIgnoreCase, index: (if reverse: -index else: index - 1))
|
||||||
|
|
||||||
proc backrefIgnoreStyle*(index: range[1..MaxSubpatterns], reverse: bool = false): Peg {.
|
func backrefIgnoreStyle*(index: range[1..MaxSubpatterns], reverse: bool = false): Peg {.
|
||||||
noSideEffect, rtl, extern: "npegs$1".} =
|
rtl, extern: "npegs$1".} =
|
||||||
## constructs a back reference of the given `index`. `index` starts counting
|
## constructs a back reference of the given `index`. `index` starts counting
|
||||||
## from 1. `reverse` specifies wether indexing starts from the end of the
|
## from 1. `reverse` specifies wether indexing starts from the end of the
|
||||||
## capture list. Ignores style for matching.
|
## capture list. Ignores style for matching.
|
||||||
result = Peg(kind: pkBackRefIgnoreStyle, index: (if reverse: -index else: index - 1))
|
result = Peg(kind: pkBackRefIgnoreStyle, index: (if reverse: -index else: index - 1))
|
||||||
|
|
||||||
proc spaceCost(n: Peg): int =
|
func spaceCost(n: Peg): int =
|
||||||
case n.kind
|
case n.kind
|
||||||
of pkEmpty: discard
|
of pkEmpty: discard
|
||||||
of pkTerminal, pkTerminalIgnoreCase, pkTerminalIgnoreStyle, pkChar,
|
of pkTerminal, pkTerminalIgnoreCase, pkTerminalIgnoreStyle, pkChar,
|
||||||
|
|
@ -344,8 +344,8 @@ proc spaceCost(n: Peg): int =
|
||||||
inc(result, spaceCost(n.sons[i]))
|
inc(result, spaceCost(n.sons[i]))
|
||||||
if result >= InlineThreshold: break
|
if result >= InlineThreshold: break
|
||||||
|
|
||||||
proc nonterminal*(n: NonTerminal): Peg {.
|
func nonterminal*(n: NonTerminal): Peg {.
|
||||||
noSideEffect, rtl, extern: "npegs$1".} =
|
rtl, extern: "npegs$1".} =
|
||||||
## constructs a PEG that consists of the nonterminal symbol
|
## constructs a PEG that consists of the nonterminal symbol
|
||||||
assert n != nil
|
assert n != nil
|
||||||
if ntDeclared in n.flags and spaceCost(n.rule) < InlineThreshold:
|
if ntDeclared in n.flags and spaceCost(n.rule) < InlineThreshold:
|
||||||
|
|
@ -354,8 +354,8 @@ proc nonterminal*(n: NonTerminal): Peg {.
|
||||||
else:
|
else:
|
||||||
result = Peg(kind: pkNonTerminal, nt: n)
|
result = Peg(kind: pkNonTerminal, nt: n)
|
||||||
|
|
||||||
proc newNonTerminal*(name: string, line, column: int): NonTerminal {.
|
func newNonTerminal*(name: string, line, column: int): NonTerminal {.
|
||||||
noSideEffect, rtl, extern: "npegs$1".} =
|
rtl, extern: "npegs$1".} =
|
||||||
## constructs a nonterminal symbol
|
## constructs a nonterminal symbol
|
||||||
result = NonTerminal(name: name, line: line, col: column)
|
result = NonTerminal(name: name, line: line, col: column)
|
||||||
|
|
||||||
|
|
@ -390,7 +390,7 @@ template natural*: Peg =
|
||||||
|
|
||||||
# ------------------------- debugging -----------------------------------------
|
# ------------------------- debugging -----------------------------------------
|
||||||
|
|
||||||
proc esc(c: char, reserved = {'\0'..'\255'}): string =
|
func esc(c: char, reserved = {'\0'..'\255'}): string =
|
||||||
case c
|
case c
|
||||||
of '\b': result = "\\b"
|
of '\b': result = "\\b"
|
||||||
of '\t': result = "\\t"
|
of '\t': result = "\\t"
|
||||||
|
|
@ -406,14 +406,14 @@ proc esc(c: char, reserved = {'\0'..'\255'}): string =
|
||||||
elif c in reserved: result = '\\' & c
|
elif c in reserved: result = '\\' & c
|
||||||
else: result = $c
|
else: result = $c
|
||||||
|
|
||||||
proc singleQuoteEsc(c: char): string = return "'" & esc(c, {'\''}) & "'"
|
func singleQuoteEsc(c: char): string = return "'" & esc(c, {'\''}) & "'"
|
||||||
|
|
||||||
proc singleQuoteEsc(str: string): string =
|
func singleQuoteEsc(str: string): string =
|
||||||
result = "'"
|
result = "'"
|
||||||
for c in items(str): add result, esc(c, {'\''})
|
for c in items(str): add result, esc(c, {'\''})
|
||||||
add result, '\''
|
add result, '\''
|
||||||
|
|
||||||
proc charSetEscAux(cc: set[char]): string =
|
func charSetEscAux(cc: set[char]): string =
|
||||||
const reserved = {'^', '-', ']'}
|
const reserved = {'^', '-', ']'}
|
||||||
result = ""
|
result = ""
|
||||||
var c1 = 0
|
var c1 = 0
|
||||||
|
|
@ -430,13 +430,13 @@ proc charSetEscAux(cc: set[char]): string =
|
||||||
c1 = c2
|
c1 = c2
|
||||||
inc(c1)
|
inc(c1)
|
||||||
|
|
||||||
proc charSetEsc(cc: set[char]): string =
|
func charSetEsc(cc: set[char]): string =
|
||||||
if card(cc) >= 128+64:
|
if card(cc) >= 128+64:
|
||||||
result = "[^" & charSetEscAux({'\1'..'\xFF'} - cc) & ']'
|
result = "[^" & charSetEscAux({'\1'..'\xFF'} - cc) & ']'
|
||||||
else:
|
else:
|
||||||
result = '[' & charSetEscAux(cc) & ']'
|
result = '[' & charSetEscAux(cc) & ']'
|
||||||
|
|
||||||
proc toStrAux(r: Peg, res: var string) =
|
func toStrAux(r: Peg, res: var string) =
|
||||||
case r.kind
|
case r.kind
|
||||||
of pkEmpty: add(res, "()")
|
of pkEmpty: add(res, "()")
|
||||||
of pkAny: add(res, '.')
|
of pkAny: add(res, '.')
|
||||||
|
|
@ -522,7 +522,7 @@ proc toStrAux(r: Peg, res: var string) =
|
||||||
of pkStartAnchor:
|
of pkStartAnchor:
|
||||||
add(res, '^')
|
add(res, '^')
|
||||||
|
|
||||||
proc `$` *(r: Peg): string {.noSideEffect, rtl, extern: "npegsToString".} =
|
func `$` *(r: Peg): string {.rtl, extern: "npegsToString".} =
|
||||||
## converts a PEG to its string representation
|
## converts a PEG to its string representation
|
||||||
result = ""
|
result = ""
|
||||||
toStrAux(r, result)
|
toStrAux(r, result)
|
||||||
|
|
@ -535,7 +535,7 @@ type
|
||||||
ml: int
|
ml: int
|
||||||
origStart: int
|
origStart: int
|
||||||
|
|
||||||
proc bounds*(c: Captures,
|
func bounds*(c: Captures,
|
||||||
i: range[0..MaxSubpatterns-1]): tuple[first, last: int] =
|
i: range[0..MaxSubpatterns-1]): tuple[first, last: int] =
|
||||||
## returns the bounds ``[first..last]`` of the `i`'th capture.
|
## returns the bounds ``[first..last]`` of the `i`'th capture.
|
||||||
result = c.matches[i]
|
result = c.matches[i]
|
||||||
|
|
@ -548,11 +548,11 @@ when not useUnicode:
|
||||||
inc(i)
|
inc(i)
|
||||||
template runeLenAt(s, i): untyped = 1
|
template runeLenAt(s, i): untyped = 1
|
||||||
|
|
||||||
proc isAlpha(a: char): bool {.inline.} = return a in {'a'..'z', 'A'..'Z'}
|
func isAlpha(a: char): bool {.inline.} = return a in {'a'..'z', 'A'..'Z'}
|
||||||
proc isUpper(a: char): bool {.inline.} = return a in {'A'..'Z'}
|
func isUpper(a: char): bool {.inline.} = return a in {'A'..'Z'}
|
||||||
proc isLower(a: char): bool {.inline.} = return a in {'a'..'z'}
|
func isLower(a: char): bool {.inline.} = return a in {'a'..'z'}
|
||||||
proc isTitle(a: char): bool {.inline.} = return false
|
func isTitle(a: char): bool {.inline.} = return false
|
||||||
proc isWhiteSpace(a: char): bool {.inline.} = return a in {' ', '\9'..'\13'}
|
func isWhiteSpace(a: char): bool {.inline.} = return a in {' ', '\9'..'\13'}
|
||||||
|
|
||||||
template matchOrParse(mopProc: untyped) =
|
template matchOrParse(mopProc: untyped) =
|
||||||
# Used to make the main matcher proc *rawMatch* as well as event parser
|
# Used to make the main matcher proc *rawMatch* as well as event parser
|
||||||
|
|
@ -860,8 +860,8 @@ template matchOrParse(mopProc: untyped) =
|
||||||
leave(pkStartAnchor, s, p, start, result)
|
leave(pkStartAnchor, s, p, start, result)
|
||||||
of pkRule, pkList: assert false
|
of pkRule, pkList: assert false
|
||||||
|
|
||||||
proc rawMatch*(s: string, p: Peg, start: int, c: var Captures): int
|
func rawMatch*(s: string, p: Peg, start: int, c: var Captures): int
|
||||||
{.noSideEffect, rtl, extern: "npegs$1".} =
|
{.rtl, extern: "npegs$1".} =
|
||||||
## low-level matching proc that implements the PEG interpreter. Use this
|
## low-level matching proc that implements the PEG interpreter. Use this
|
||||||
## for maximum efficiency (every other PEG operation ends up calling this
|
## for maximum efficiency (every other PEG operation ends up calling this
|
||||||
## proc).
|
## proc).
|
||||||
|
|
@ -873,6 +873,10 @@ proc rawMatch*(s: string, p: Peg, start: int, c: var Captures): int
|
||||||
template leave(pk, s, p, start, length) =
|
template leave(pk, s, p, start, length) =
|
||||||
discard
|
discard
|
||||||
matchOrParse(matchIt)
|
matchOrParse(matchIt)
|
||||||
|
{.cast(noSideEffect).}:
|
||||||
|
# This cast is allowed because the `matchOrParse` template is used for
|
||||||
|
# both matching and parsing, but side effects are only possible when it's
|
||||||
|
# used by `eventParser`.
|
||||||
result = matchIt(s, p, start, c)
|
result = matchIt(s, p, start, c)
|
||||||
|
|
||||||
macro mkHandlerTplts(handlers: untyped): untyped =
|
macro mkHandlerTplts(handlers: untyped): untyped =
|
||||||
|
|
@ -900,7 +904,7 @@ macro mkHandlerTplts(handlers: untyped): untyped =
|
||||||
# StmtList
|
# StmtList
|
||||||
# <handler code block>
|
# <handler code block>
|
||||||
# ...
|
# ...
|
||||||
proc mkEnter(hdName, body: NimNode): NimNode =
|
func mkEnter(hdName, body: NimNode): NimNode =
|
||||||
template helper(hdName, body) {.dirty.} =
|
template helper(hdName, body) {.dirty.} =
|
||||||
template hdName(s, p, start) =
|
template hdName(s, p, start) =
|
||||||
let s {.inject.} = s
|
let s {.inject.} = s
|
||||||
|
|
@ -1069,8 +1073,8 @@ template fillMatches(s, caps, c) =
|
||||||
else:
|
else:
|
||||||
caps[k] = ""
|
caps[k] = ""
|
||||||
|
|
||||||
proc matchLen*(s: string, pattern: Peg, matches: var openArray[string],
|
func matchLen*(s: string, pattern: Peg, matches: var openArray[string],
|
||||||
start = 0): int {.noSideEffect, rtl, extern: "npegs$1Capture".} =
|
start = 0): int {.rtl, extern: "npegs$1Capture".} =
|
||||||
## the same as ``match``, but it returns the length of the match,
|
## 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
|
## 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
|
## of zero can happen. It's possible that a suffix of `s` remains
|
||||||
|
|
@ -1080,8 +1084,8 @@ proc matchLen*(s: string, pattern: Peg, matches: var openArray[string],
|
||||||
result = rawMatch(s, pattern, start, c)
|
result = rawMatch(s, pattern, start, c)
|
||||||
if result >= 0: fillMatches(s, matches, c)
|
if result >= 0: fillMatches(s, matches, c)
|
||||||
|
|
||||||
proc matchLen*(s: string, pattern: Peg,
|
func matchLen*(s: string, pattern: Peg,
|
||||||
start = 0): int {.noSideEffect, rtl, extern: "npegs$1".} =
|
start = 0): int {.rtl, extern: "npegs$1".} =
|
||||||
## the same as ``match``, but it returns the length of the match,
|
## 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
|
## 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
|
## of zero can happen. It's possible that a suffix of `s` remains
|
||||||
|
|
@ -1090,22 +1094,22 @@ proc matchLen*(s: string, pattern: Peg,
|
||||||
c.origStart = start
|
c.origStart = start
|
||||||
result = rawMatch(s, pattern, start, c)
|
result = rawMatch(s, pattern, start, c)
|
||||||
|
|
||||||
proc match*(s: string, pattern: Peg, matches: var openArray[string],
|
func match*(s: string, pattern: Peg, matches: var openArray[string],
|
||||||
start = 0): bool {.noSideEffect, rtl, extern: "npegs$1Capture".} =
|
start = 0): bool {.rtl, extern: "npegs$1Capture".} =
|
||||||
## returns ``true`` if ``s[start..]`` matches the ``pattern`` and
|
## returns ``true`` if ``s[start..]`` matches the ``pattern`` and
|
||||||
## the captured substrings in the array ``matches``. If it does not
|
## the captured substrings in the array ``matches``. If it does not
|
||||||
## match, nothing is written into ``matches`` and ``false`` is
|
## match, nothing is written into ``matches`` and ``false`` is
|
||||||
## returned.
|
## returned.
|
||||||
result = matchLen(s, pattern, matches, start) != -1
|
result = matchLen(s, pattern, matches, start) != -1
|
||||||
|
|
||||||
proc match*(s: string, pattern: Peg,
|
func match*(s: string, pattern: Peg,
|
||||||
start = 0): bool {.noSideEffect, rtl, extern: "npegs$1".} =
|
start = 0): bool {.rtl, extern: "npegs$1".} =
|
||||||
## returns ``true`` if ``s`` matches the ``pattern`` beginning from ``start``.
|
## returns ``true`` if ``s`` matches the ``pattern`` beginning from ``start``.
|
||||||
result = matchLen(s, pattern, start) != -1
|
result = matchLen(s, pattern, start) != -1
|
||||||
|
|
||||||
|
|
||||||
proc find*(s: string, pattern: Peg, matches: var openArray[string],
|
func find*(s: string, pattern: Peg, matches: var openArray[string],
|
||||||
start = 0): int {.noSideEffect, rtl, extern: "npegs$1Capture".} =
|
start = 0): int {.rtl, extern: "npegs$1Capture".} =
|
||||||
## returns the starting position of ``pattern`` in ``s`` and the captured
|
## returns the starting position of ``pattern`` in ``s`` and the captured
|
||||||
## substrings in the array ``matches``. If it does not match, nothing
|
## substrings in the array ``matches``. If it does not match, nothing
|
||||||
## is written into ``matches`` and -1 is returned.
|
## is written into ``matches`` and -1 is returned.
|
||||||
|
|
@ -1119,9 +1123,9 @@ proc find*(s: string, pattern: Peg, matches: var openArray[string],
|
||||||
return -1
|
return -1
|
||||||
# could also use the pattern here: (!P .)* P
|
# 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] {.
|
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``
|
## returns the starting position and end position of ``pattern`` in ``s``
|
||||||
## and the captured
|
## and the captured
|
||||||
## substrings in the array ``matches``. If it does not match, nothing
|
## substrings in the array ``matches``. If it does not match, nothing
|
||||||
|
|
@ -1136,8 +1140,8 @@ proc findBounds*(s: string, pattern: Peg, matches: var openArray[string],
|
||||||
return (i, i+L-1)
|
return (i, i+L-1)
|
||||||
return (-1, 0)
|
return (-1, 0)
|
||||||
|
|
||||||
proc find*(s: string, pattern: Peg,
|
func find*(s: string, pattern: Peg,
|
||||||
start = 0): int {.noSideEffect, rtl, extern: "npegs$1".} =
|
start = 0): int {.rtl, extern: "npegs$1".} =
|
||||||
## returns the starting position of ``pattern`` in ``s``. If it does not
|
## returns the starting position of ``pattern`` in ``s``. If it does not
|
||||||
## match, -1 is returned.
|
## match, -1 is returned.
|
||||||
var c: Captures
|
var c: Captures
|
||||||
|
|
@ -1160,8 +1164,8 @@ iterator findAll*(s: string, pattern: Peg, start = 0): string =
|
||||||
yield substr(s, i, i+L-1)
|
yield substr(s, i, i+L-1)
|
||||||
inc(i, L)
|
inc(i, L)
|
||||||
|
|
||||||
proc findAll*(s: string, pattern: Peg, start = 0): seq[string] {.
|
func findAll*(s: string, pattern: Peg, start = 0): seq[string] {.
|
||||||
noSideEffect, rtl, extern: "npegs$1".} =
|
rtl, extern: "npegs$1".} =
|
||||||
## returns all matching *substrings* of `s` that match `pattern`.
|
## returns all matching *substrings* of `s` that match `pattern`.
|
||||||
## If it does not match, @[] is returned.
|
## If it does not match, @[] is returned.
|
||||||
result = @[]
|
result = @[]
|
||||||
|
|
@ -1192,31 +1196,31 @@ template `=~`*(s: string, pattern: Peg): bool =
|
||||||
|
|
||||||
# ------------------------- more string handling ------------------------------
|
# ------------------------- more string handling ------------------------------
|
||||||
|
|
||||||
proc contains*(s: string, pattern: Peg, start = 0): bool {.
|
func contains*(s: string, pattern: Peg, start = 0): bool {.
|
||||||
noSideEffect, rtl, extern: "npegs$1".} =
|
rtl, extern: "npegs$1".} =
|
||||||
## same as ``find(s, pattern, start) >= 0``
|
## same as ``find(s, pattern, start) >= 0``
|
||||||
return find(s, pattern, start) >= 0
|
return find(s, pattern, start) >= 0
|
||||||
|
|
||||||
proc contains*(s: string, pattern: Peg, matches: var openArray[string],
|
func contains*(s: string, pattern: Peg, matches: var openArray[string],
|
||||||
start = 0): bool {.noSideEffect, rtl, extern: "npegs$1Capture".} =
|
start = 0): bool {.rtl, extern: "npegs$1Capture".} =
|
||||||
## same as ``find(s, pattern, matches, start) >= 0``
|
## same as ``find(s, pattern, matches, start) >= 0``
|
||||||
return find(s, pattern, matches, start) >= 0
|
return find(s, pattern, matches, start) >= 0
|
||||||
|
|
||||||
proc startsWith*(s: string, prefix: Peg, start = 0): bool {.
|
func startsWith*(s: string, prefix: Peg, start = 0): bool {.
|
||||||
noSideEffect, rtl, extern: "npegs$1".} =
|
rtl, extern: "npegs$1".} =
|
||||||
## returns true if `s` starts with the pattern `prefix`
|
## returns true if `s` starts with the pattern `prefix`
|
||||||
result = matchLen(s, prefix, start) >= 0
|
result = matchLen(s, prefix, start) >= 0
|
||||||
|
|
||||||
proc endsWith*(s: string, suffix: Peg, start = 0): bool {.
|
func endsWith*(s: string, suffix: Peg, start = 0): bool {.
|
||||||
noSideEffect, rtl, extern: "npegs$1".} =
|
rtl, extern: "npegs$1".} =
|
||||||
## returns true if `s` ends with the pattern `suffix`
|
## returns true if `s` ends with the pattern `suffix`
|
||||||
var c: Captures
|
var c: Captures
|
||||||
c.origStart = start
|
c.origStart = start
|
||||||
for i in start .. s.len-1:
|
for i in start .. s.len-1:
|
||||||
if rawMatch(s, suffix, i, c) == s.len - i: return true
|
if rawMatch(s, suffix, i, c) == s.len - i: return true
|
||||||
|
|
||||||
proc replacef*(s: string, sub: Peg, by: string): string {.
|
func replacef*(s: string, sub: Peg, by: string): string {.
|
||||||
noSideEffect, rtl, extern: "npegs$1".} =
|
rtl, extern: "npegs$1".} =
|
||||||
## Replaces `sub` in `s` by the string `by`. Captures can be accessed in `by`
|
## Replaces `sub` in `s` by the string `by`. Captures can be accessed in `by`
|
||||||
## with the notation ``$i`` and ``$#`` (see strutils.`%`). Examples:
|
## with the notation ``$i`` and ``$#`` (see strutils.`%`). Examples:
|
||||||
##
|
##
|
||||||
|
|
@ -1244,8 +1248,8 @@ proc replacef*(s: string, sub: Peg, by: string): string {.
|
||||||
inc(i, x)
|
inc(i, x)
|
||||||
add(result, substr(s, i))
|
add(result, substr(s, i))
|
||||||
|
|
||||||
proc replace*(s: string, sub: Peg, by = ""): string {.
|
func replace*(s: string, sub: Peg, by = ""): string {.
|
||||||
noSideEffect, rtl, extern: "npegs$1".} =
|
rtl, extern: "npegs$1".} =
|
||||||
## Replaces `sub` in `s` by the string `by`. Captures cannot be accessed
|
## Replaces `sub` in `s` by the string `by`. Captures cannot be accessed
|
||||||
## in `by`.
|
## in `by`.
|
||||||
result = ""
|
result = ""
|
||||||
|
|
@ -1261,9 +1265,9 @@ proc replace*(s: string, sub: Peg, by = ""): string {.
|
||||||
inc(i, x)
|
inc(i, x)
|
||||||
add(result, substr(s, i))
|
add(result, substr(s, i))
|
||||||
|
|
||||||
proc parallelReplace*(s: string, subs: varargs[
|
func parallelReplace*(s: string, subs: varargs[
|
||||||
tuple[pattern: Peg, repl: string]]): string {.
|
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`
|
## Returns a modified copy of `s` with the substitutions in `subs`
|
||||||
## applied in parallel.
|
## applied in parallel.
|
||||||
result = ""
|
result = ""
|
||||||
|
|
@ -1288,7 +1292,7 @@ proc parallelReplace*(s: string, subs: varargs[
|
||||||
when not defined(nimHasEffectsOf):
|
when not defined(nimHasEffectsOf):
|
||||||
{.pragma: effectsOf.}
|
{.pragma: effectsOf.}
|
||||||
|
|
||||||
proc replace*(s: string, sub: Peg, cb: proc(
|
func replace*(s: string, sub: Peg, cb: proc(
|
||||||
match: int, cnt: int, caps: openArray[string]): string): string {.
|
match: int, cnt: int, caps: openArray[string]): string): string {.
|
||||||
rtl, extern: "npegs$1cb", effectsOf: cb.} =
|
rtl, extern: "npegs$1cb", effectsOf: cb.} =
|
||||||
## Replaces `sub` in `s` by the resulting strings from the callback.
|
## Replaces `sub` in `s` by the resulting strings from the callback.
|
||||||
|
|
@ -1297,7 +1301,7 @@ proc replace*(s: string, sub: Peg, cb: proc(
|
||||||
##
|
##
|
||||||
## .. code-block:: nim
|
## .. code-block:: nim
|
||||||
##
|
##
|
||||||
## proc handleMatches*(m: int, n: int, c: openArray[string]): string =
|
## func handleMatches*(m: int, n: int, c: openArray[string]): string =
|
||||||
## result = ""
|
## result = ""
|
||||||
## if m > 0:
|
## if m > 0:
|
||||||
## result.add ", "
|
## result.add ", "
|
||||||
|
|
@ -1380,8 +1384,8 @@ iterator split*(s: string, sep: Peg): string =
|
||||||
if first < last:
|
if first < last:
|
||||||
yield substr(s, first, last-1)
|
yield substr(s, first, last-1)
|
||||||
|
|
||||||
proc split*(s: string, sep: Peg): seq[string] {.
|
func split*(s: string, sep: Peg): seq[string] {.
|
||||||
noSideEffect, rtl, extern: "npegs$1".} =
|
rtl, extern: "npegs$1".} =
|
||||||
## Splits the string `s` into substrings.
|
## Splits the string `s` into substrings.
|
||||||
result = @[]
|
result = @[]
|
||||||
for it in split(s, sep): result.add it
|
for it in split(s, sep): result.add it
|
||||||
|
|
@ -1445,20 +1449,20 @@ const
|
||||||
"@", "built-in", "escaped", "$", "$", "^"
|
"@", "built-in", "escaped", "$", "$", "^"
|
||||||
]
|
]
|
||||||
|
|
||||||
proc handleCR(L: var PegLexer, pos: int): int =
|
func handleCR(L: var PegLexer, pos: int): int =
|
||||||
assert(L.buf[pos] == '\c')
|
assert(L.buf[pos] == '\c')
|
||||||
inc(L.lineNumber)
|
inc(L.lineNumber)
|
||||||
result = pos+1
|
result = pos+1
|
||||||
if result < L.buf.len and L.buf[result] == '\L': inc(result)
|
if result < L.buf.len and L.buf[result] == '\L': inc(result)
|
||||||
L.lineStart = result
|
L.lineStart = result
|
||||||
|
|
||||||
proc handleLF(L: var PegLexer, pos: int): int =
|
func handleLF(L: var PegLexer, pos: int): int =
|
||||||
assert(L.buf[pos] == '\L')
|
assert(L.buf[pos] == '\L')
|
||||||
inc(L.lineNumber)
|
inc(L.lineNumber)
|
||||||
result = pos+1
|
result = pos+1
|
||||||
L.lineStart = result
|
L.lineStart = result
|
||||||
|
|
||||||
proc init(L: var PegLexer, input, filename: string, line = 1, col = 0) =
|
func init(L: var PegLexer, input, filename: string, line = 1, col = 0) =
|
||||||
L.buf = input
|
L.buf = input
|
||||||
L.bufpos = 0
|
L.bufpos = 0
|
||||||
L.lineNumber = line
|
L.lineNumber = line
|
||||||
|
|
@ -1466,18 +1470,18 @@ proc init(L: var PegLexer, input, filename: string, line = 1, col = 0) =
|
||||||
L.lineStart = 0
|
L.lineStart = 0
|
||||||
L.filename = filename
|
L.filename = filename
|
||||||
|
|
||||||
proc getColumn(L: PegLexer): int {.inline.} =
|
func getColumn(L: PegLexer): int {.inline.} =
|
||||||
result = abs(L.bufpos - L.lineStart) + L.colOffset
|
result = abs(L.bufpos - L.lineStart) + L.colOffset
|
||||||
|
|
||||||
proc getLine(L: PegLexer): int {.inline.} =
|
func getLine(L: PegLexer): int {.inline.} =
|
||||||
result = L.lineNumber
|
result = L.lineNumber
|
||||||
|
|
||||||
proc errorStr(L: PegLexer, msg: string, line = -1, col = -1): string =
|
func errorStr(L: PegLexer, msg: string, line = -1, col = -1): string =
|
||||||
var line = if line < 0: getLine(L) else: line
|
var line = if line < 0: getLine(L) else: line
|
||||||
var col = if col < 0: getColumn(L) else: col
|
var col = if col < 0: getColumn(L) else: col
|
||||||
result = "$1($2, $3) Error: $4" % [L.filename, $line, $col, msg]
|
result = "$1($2, $3) Error: $4" % [L.filename, $line, $col, msg]
|
||||||
|
|
||||||
proc getEscapedChar(c: var PegLexer, tok: var Token) =
|
func getEscapedChar(c: var PegLexer, tok: var Token) =
|
||||||
inc(c.bufpos)
|
inc(c.bufpos)
|
||||||
if c.bufpos >= len(c.buf):
|
if c.bufpos >= len(c.buf):
|
||||||
tok.kind = tkInvalid
|
tok.kind = tkInvalid
|
||||||
|
|
@ -1537,7 +1541,7 @@ proc getEscapedChar(c: var PegLexer, tok: var Token) =
|
||||||
add(tok.literal, c.buf[c.bufpos])
|
add(tok.literal, c.buf[c.bufpos])
|
||||||
inc(c.bufpos)
|
inc(c.bufpos)
|
||||||
|
|
||||||
proc skip(c: var PegLexer) =
|
func skip(c: var PegLexer) =
|
||||||
var pos = c.bufpos
|
var pos = c.bufpos
|
||||||
while pos < c.buf.len:
|
while pos < c.buf.len:
|
||||||
case c.buf[pos]
|
case c.buf[pos]
|
||||||
|
|
@ -1554,7 +1558,7 @@ proc skip(c: var PegLexer) =
|
||||||
break # EndOfFile also leaves the loop
|
break # EndOfFile also leaves the loop
|
||||||
c.bufpos = pos
|
c.bufpos = pos
|
||||||
|
|
||||||
proc getString(c: var PegLexer, tok: var Token) =
|
func getString(c: var PegLexer, tok: var Token) =
|
||||||
tok.kind = tkStringLit
|
tok.kind = tkStringLit
|
||||||
var pos = c.bufpos + 1
|
var pos = c.bufpos + 1
|
||||||
var quote = c.buf[pos-1]
|
var quote = c.buf[pos-1]
|
||||||
|
|
@ -1575,7 +1579,7 @@ proc getString(c: var PegLexer, tok: var Token) =
|
||||||
inc(pos)
|
inc(pos)
|
||||||
c.bufpos = pos
|
c.bufpos = pos
|
||||||
|
|
||||||
proc getDollar(c: var PegLexer, tok: var Token) =
|
func getDollar(c: var PegLexer, tok: var Token) =
|
||||||
var pos = c.bufpos + 1
|
var pos = c.bufpos + 1
|
||||||
var neg = false
|
var neg = false
|
||||||
if pos < c.buf.len and c.buf[pos] == '^':
|
if pos < c.buf.len and c.buf[pos] == '^':
|
||||||
|
|
@ -1595,7 +1599,7 @@ proc getDollar(c: var PegLexer, tok: var Token) =
|
||||||
tok.kind = tkDollar
|
tok.kind = tkDollar
|
||||||
c.bufpos = pos
|
c.bufpos = pos
|
||||||
|
|
||||||
proc getCharSet(c: var PegLexer, tok: var Token) =
|
func getCharSet(c: var PegLexer, tok: var Token) =
|
||||||
tok.kind = tkCharSet
|
tok.kind = tkCharSet
|
||||||
tok.charset = {}
|
tok.charset = {}
|
||||||
var pos = c.bufpos + 1
|
var pos = c.bufpos + 1
|
||||||
|
|
@ -1652,7 +1656,7 @@ proc getCharSet(c: var PegLexer, tok: var Token) =
|
||||||
c.bufpos = pos
|
c.bufpos = pos
|
||||||
if caret: tok.charset = {'\1'..'\xFF'} - tok.charset
|
if caret: tok.charset = {'\1'..'\xFF'} - tok.charset
|
||||||
|
|
||||||
proc getSymbol(c: var PegLexer, tok: var Token) =
|
func getSymbol(c: var PegLexer, tok: var Token) =
|
||||||
var pos = c.bufpos
|
var pos = c.bufpos
|
||||||
while pos < c.buf.len:
|
while pos < c.buf.len:
|
||||||
add(tok.literal, c.buf[pos])
|
add(tok.literal, c.buf[pos])
|
||||||
|
|
@ -1661,7 +1665,7 @@ proc getSymbol(c: var PegLexer, tok: var Token) =
|
||||||
c.bufpos = pos
|
c.bufpos = pos
|
||||||
tok.kind = tkIdentifier
|
tok.kind = tkIdentifier
|
||||||
|
|
||||||
proc getBuiltin(c: var PegLexer, tok: var Token) =
|
func getBuiltin(c: var PegLexer, tok: var Token) =
|
||||||
if c.bufpos+1 < c.buf.len and c.buf[c.bufpos+1] in strutils.Letters:
|
if c.bufpos+1 < c.buf.len and c.buf[c.bufpos+1] in strutils.Letters:
|
||||||
inc(c.bufpos)
|
inc(c.bufpos)
|
||||||
getSymbol(c, tok)
|
getSymbol(c, tok)
|
||||||
|
|
@ -1670,7 +1674,7 @@ proc getBuiltin(c: var PegLexer, tok: var Token) =
|
||||||
tok.kind = tkEscaped
|
tok.kind = tkEscaped
|
||||||
getEscapedChar(c, tok) # may set tok.kind to tkInvalid
|
getEscapedChar(c, tok) # may set tok.kind to tkInvalid
|
||||||
|
|
||||||
proc getTok(c: var PegLexer, tok: var Token) =
|
func getTok(c: var PegLexer, tok: var Token) =
|
||||||
tok.kind = tkInvalid
|
tok.kind = tkInvalid
|
||||||
tok.modifier = modNone
|
tok.modifier = modNone
|
||||||
setLen(tok.literal, 0)
|
setLen(tok.literal, 0)
|
||||||
|
|
@ -1792,7 +1796,7 @@ proc getTok(c: var PegLexer, tok: var Token) =
|
||||||
add(tok.literal, c.buf[c.bufpos])
|
add(tok.literal, c.buf[c.bufpos])
|
||||||
inc(c.bufpos)
|
inc(c.bufpos)
|
||||||
|
|
||||||
proc arrowIsNextTok(c: PegLexer): bool =
|
func arrowIsNextTok(c: PegLexer): bool =
|
||||||
# the only look ahead we need
|
# the only look ahead we need
|
||||||
var pos = c.bufpos
|
var pos = c.bufpos
|
||||||
while pos < c.buf.len and c.buf[pos] in {'\t', ' '}: inc(pos)
|
while pos < c.buf.len and c.buf[pos] in {'\t', ' '}: inc(pos)
|
||||||
|
|
@ -1813,23 +1817,23 @@ type
|
||||||
identIsVerbatim: bool
|
identIsVerbatim: bool
|
||||||
skip: Peg
|
skip: Peg
|
||||||
|
|
||||||
proc pegError(p: PegParser, msg: string, line = -1, col = -1) =
|
func pegError(p: PegParser, msg: string, line = -1, col = -1) =
|
||||||
var e: ref EInvalidPeg
|
var e: ref EInvalidPeg
|
||||||
new(e)
|
new(e)
|
||||||
e.msg = errorStr(p, msg, line, col)
|
e.msg = errorStr(p, msg, line, col)
|
||||||
raise e
|
raise e
|
||||||
|
|
||||||
proc getTok(p: var PegParser) =
|
func getTok(p: var PegParser) =
|
||||||
getTok(p, p.tok)
|
getTok(p, p.tok)
|
||||||
if p.tok.kind == tkInvalid: pegError(p, "'" & p.tok.literal & "' is invalid token")
|
if p.tok.kind == tkInvalid: pegError(p, "'" & p.tok.literal & "' is invalid token")
|
||||||
|
|
||||||
proc eat(p: var PegParser, kind: TokKind) =
|
func eat(p: var PegParser, kind: TokKind) =
|
||||||
if p.tok.kind == kind: getTok(p)
|
if p.tok.kind == kind: getTok(p)
|
||||||
else: pegError(p, tokKindToStr[kind] & " expected")
|
else: pegError(p, tokKindToStr[kind] & " expected")
|
||||||
|
|
||||||
proc parseExpr(p: var PegParser): Peg {.gcsafe.}
|
func parseExpr(p: var PegParser): Peg {.gcsafe.}
|
||||||
|
|
||||||
proc getNonTerminal(p: var PegParser, name: string): NonTerminal =
|
func getNonTerminal(p: var PegParser, name: string): NonTerminal =
|
||||||
for i in 0..high(p.nonterms):
|
for i in 0..high(p.nonterms):
|
||||||
result = p.nonterms[i]
|
result = p.nonterms[i]
|
||||||
if cmpIgnoreStyle(result.name, name) == 0: return
|
if cmpIgnoreStyle(result.name, name) == 0: return
|
||||||
|
|
@ -1837,13 +1841,13 @@ proc getNonTerminal(p: var PegParser, name: string): NonTerminal =
|
||||||
result = newNonTerminal(name, getLine(p), getColumn(p))
|
result = newNonTerminal(name, getLine(p), getColumn(p))
|
||||||
add(p.nonterms, result)
|
add(p.nonterms, result)
|
||||||
|
|
||||||
proc modifiedTerm(s: string, m: Modifier): Peg =
|
func modifiedTerm(s: string, m: Modifier): Peg =
|
||||||
case m
|
case m
|
||||||
of modNone, modVerbatim: result = term(s)
|
of modNone, modVerbatim: result = term(s)
|
||||||
of modIgnoreCase: result = termIgnoreCase(s)
|
of modIgnoreCase: result = termIgnoreCase(s)
|
||||||
of modIgnoreStyle: result = termIgnoreStyle(s)
|
of modIgnoreStyle: result = termIgnoreStyle(s)
|
||||||
|
|
||||||
proc modifiedBackref(s: int, m: Modifier): Peg =
|
func modifiedBackref(s: int, m: Modifier): Peg =
|
||||||
var
|
var
|
||||||
reverse = s < 0
|
reverse = s < 0
|
||||||
index = if reverse: -s else: s
|
index = if reverse: -s else: s
|
||||||
|
|
@ -1852,7 +1856,7 @@ proc modifiedBackref(s: int, m: Modifier): Peg =
|
||||||
of modIgnoreCase: result = backrefIgnoreCase(index, reverse)
|
of modIgnoreCase: result = backrefIgnoreCase(index, reverse)
|
||||||
of modIgnoreStyle: result = backrefIgnoreStyle(index, reverse)
|
of modIgnoreStyle: result = backrefIgnoreStyle(index, reverse)
|
||||||
|
|
||||||
proc builtin(p: var PegParser): Peg =
|
func builtin(p: var PegParser): Peg =
|
||||||
# do not use "y", "skip" or "i" as these would be ambiguous
|
# do not use "y", "skip" or "i" as these would be ambiguous
|
||||||
case p.tok.literal
|
case p.tok.literal
|
||||||
of "n": result = newLine()
|
of "n": result = newLine()
|
||||||
|
|
@ -1872,11 +1876,11 @@ proc builtin(p: var PegParser): Peg =
|
||||||
of "white": result = unicodeWhitespace()
|
of "white": result = unicodeWhitespace()
|
||||||
else: pegError(p, "unknown built-in: " & p.tok.literal)
|
else: pegError(p, "unknown built-in: " & p.tok.literal)
|
||||||
|
|
||||||
proc token(terminal: Peg, p: PegParser): Peg =
|
func token(terminal: Peg, p: PegParser): Peg =
|
||||||
if p.skip.kind == pkEmpty: result = terminal
|
if p.skip.kind == pkEmpty: result = terminal
|
||||||
else: result = sequence(p.skip, terminal)
|
else: result = sequence(p.skip, terminal)
|
||||||
|
|
||||||
proc primary(p: var PegParser): Peg =
|
func primary(p: var PegParser): Peg =
|
||||||
case p.tok.kind
|
case p.tok.kind
|
||||||
of tkAmp:
|
of tkAmp:
|
||||||
getTok(p)
|
getTok(p)
|
||||||
|
|
@ -1968,7 +1972,7 @@ proc primary(p: var PegParser): Peg =
|
||||||
getTok(p)
|
getTok(p)
|
||||||
else: break
|
else: break
|
||||||
|
|
||||||
proc seqExpr(p: var PegParser): Peg =
|
func seqExpr(p: var PegParser): Peg =
|
||||||
result = primary(p)
|
result = primary(p)
|
||||||
while true:
|
while true:
|
||||||
case p.tok.kind
|
case p.tok.kind
|
||||||
|
|
@ -1982,13 +1986,13 @@ proc seqExpr(p: var PegParser): Peg =
|
||||||
else: break
|
else: break
|
||||||
else: break
|
else: break
|
||||||
|
|
||||||
proc parseExpr(p: var PegParser): Peg =
|
func parseExpr(p: var PegParser): Peg =
|
||||||
result = seqExpr(p)
|
result = seqExpr(p)
|
||||||
while p.tok.kind == tkBar:
|
while p.tok.kind == tkBar:
|
||||||
getTok(p)
|
getTok(p)
|
||||||
result = result / seqExpr(p)
|
result = result / seqExpr(p)
|
||||||
|
|
||||||
proc parseRule(p: var PegParser): NonTerminal =
|
func parseRule(p: var PegParser): NonTerminal =
|
||||||
if p.tok.kind == tkIdentifier and arrowIsNextTok(p):
|
if p.tok.kind == tkIdentifier and arrowIsNextTok(p):
|
||||||
result = getNonTerminal(p, p.tok.literal)
|
result = getNonTerminal(p, p.tok.literal)
|
||||||
if ntDeclared in result.flags:
|
if ntDeclared in result.flags:
|
||||||
|
|
@ -2002,7 +2006,7 @@ proc parseRule(p: var PegParser): NonTerminal =
|
||||||
else:
|
else:
|
||||||
pegError(p, "rule expected, but found: " & p.tok.literal)
|
pegError(p, "rule expected, but found: " & p.tok.literal)
|
||||||
|
|
||||||
proc rawParse(p: var PegParser): Peg =
|
func rawParse(p: var PegParser): Peg =
|
||||||
## parses a rule or a PEG expression
|
## parses a rule or a PEG expression
|
||||||
while p.tok.kind == tkBuiltin:
|
while p.tok.kind == tkBuiltin:
|
||||||
case p.tok.literal
|
case p.tok.literal
|
||||||
|
|
@ -2032,7 +2036,7 @@ proc rawParse(p: var PegParser): Peg =
|
||||||
elif ntUsed notin nt.flags and i > 0:
|
elif ntUsed notin nt.flags and i > 0:
|
||||||
pegError(p, "unused rule: " & nt.name, nt.line, nt.col)
|
pegError(p, "unused rule: " & nt.name, nt.line, nt.col)
|
||||||
|
|
||||||
proc parsePeg*(pattern: string, filename = "pattern", line = 1, col = 0): Peg =
|
func parsePeg*(pattern: string, filename = "pattern", line = 1, col = 0): Peg =
|
||||||
## constructs a Peg object from `pattern`. `filename`, `line`, `col` are
|
## constructs a Peg object from `pattern`. `filename`, `line`, `col` are
|
||||||
## used for error messages, but they only provide start offsets. `parsePeg`
|
## used for error messages, but they only provide start offsets. `parsePeg`
|
||||||
## keeps track of line and column numbers within `pattern`.
|
## keeps track of line and column numbers within `pattern`.
|
||||||
|
|
@ -2047,14 +2051,14 @@ proc parsePeg*(pattern: string, filename = "pattern", line = 1, col = 0): Peg =
|
||||||
getTok(p)
|
getTok(p)
|
||||||
result = rawParse(p)
|
result = rawParse(p)
|
||||||
|
|
||||||
proc peg*(pattern: string): Peg =
|
func peg*(pattern: string): Peg =
|
||||||
## constructs a Peg object from the `pattern`. The short name has been
|
## constructs a Peg object from the `pattern`. The short name has been
|
||||||
## chosen to encourage its use as a raw string modifier::
|
## chosen to encourage its use as a raw string modifier::
|
||||||
##
|
##
|
||||||
## peg"{\ident} \s* '=' \s* {.*}"
|
## peg"{\ident} \s* '=' \s* {.*}"
|
||||||
result = parsePeg(pattern, "pattern")
|
result = parsePeg(pattern, "pattern")
|
||||||
|
|
||||||
proc escapePeg*(s: string): string =
|
func escapePeg*(s: string): string =
|
||||||
## escapes `s` so that it is matched verbatim when used as a peg.
|
## escapes `s` so that it is matched verbatim when used as a peg.
|
||||||
result = ""
|
result = ""
|
||||||
var inQuote = false
|
var inQuote = false
|
||||||
|
|
|
||||||
|
|
@ -84,7 +84,7 @@ import
|
||||||
parseutils,
|
parseutils,
|
||||||
parsexml,
|
parsexml,
|
||||||
pathnorm,
|
pathnorm,
|
||||||
# pegs,
|
pegs,
|
||||||
posix_utils,
|
posix_utils,
|
||||||
prelude,
|
prelude,
|
||||||
punycode,
|
punycode,
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,9 @@
|
||||||
## Part of 'koch' responsible for the documentation generation.
|
## Part of 'koch' responsible for the documentation generation.
|
||||||
|
|
||||||
import os, strutils, osproc, sets, pathnorm, pegs, sequtils
|
import os, strutils, osproc, sets, pathnorm, sequtils
|
||||||
|
# XXX: Remove this feature check once the csources supports it.
|
||||||
|
when defined(nimHasCastPragmaBlocks):
|
||||||
|
import std/pegs
|
||||||
from std/private/globs import nativeToUnixPath, walkDirRecFilter, PathEntry
|
from std/private/globs import nativeToUnixPath, walkDirRecFilter, PathEntry
|
||||||
import "../compiler/nimpaths"
|
import "../compiler/nimpaths"
|
||||||
|
|
||||||
|
|
@ -338,6 +341,8 @@ proc buildDocs*(args: string, localOnly = false, localOutDir = "") =
|
||||||
if not localOnly:
|
if not localOnly:
|
||||||
buildDocsDir(args, webUploadOutput / NimVersion)
|
buildDocsDir(args, webUploadOutput / NimVersion)
|
||||||
|
|
||||||
|
# XXX: Remove this feature check once the csources supports it.
|
||||||
|
when defined(nimHasCastPragmaBlocks):
|
||||||
let gaFilter = peg"@( y'--doc.googleAnalytics:' @(\s / $) )"
|
let gaFilter = peg"@( y'--doc.googleAnalytics:' @(\s / $) )"
|
||||||
args = args.replace(gaFilter)
|
args = args.replace(gaFilter)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue