aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--nimgen.nim37
-rw-r--r--nimgen.nimble2
2 files changed, 21 insertions, 18 deletions
diff --git a/nimgen.nim b/nimgen.nim
index 97f8809..4e540d8 100644
--- a/nimgen.nim
+++ b/nimgen.nim
@@ -1,4 +1,4 @@
-import nre, os, ospaths, osproc, parsecfg, pegs, ropes, sequtils, streams, strutils, tables
+import os, ospaths, osproc, parsecfg, pegs, regex, ropes, sequtils, streams, strutils, tables
const
cCompilerEnv = "CC"
@@ -303,10 +303,11 @@ proc freplace(file: string, pattern: string, repl="") =
proc freplace(file: string, pattern: Regex, repl="") =
withFile(file):
- if content.find(pattern).isSome():
+ var m: RegexMatch
+ if content.find(pattern, m):
if "$#" in repl:
- for m in content.findIter(pattern):
- content = content.replace(m.match, repl % m.captures[0])
+ content = content.replace(pattern,
+ proc (m: RegexMatch, s: string): string = repl % s[m.group(0)[0]])
else:
content = content.replace(pattern, repl)
@@ -406,10 +407,11 @@ proc compile(dir="", file=""): string =
proc fixFuncProtos(file: string) =
withFile(file):
- for fp in content.findIter(re"(?m)(^.*?)[ ]*\(\*(.*?)\((.*?)\)\)[ \r\n]*\((.*?[\r\n]*.*?)\);"):
- var tdout = "typedef $# (*type_$#)($#);\n" % [fp.captures[0], fp.captures[1], fp.captures[3]] &
- "type_$# $#($#);" % [fp.captures[1], fp.captures[1], fp.captures[2]]
- content = content.replace(fp.match, tdout)
+ content = content.replace(re"(?m)(^.*?)[ ]*\(\*(.*?)\((.*?)\)\)[ \r\n]*\((.*?[\r\n]*.*?)\);",
+ proc (m: RegexMatch, s: string): string =
+ (("typedef $# (*type_$#)($#);\n" % [s[m.group(0)[0]], s[m.group(1)[0]], s[m.group(3)[0]]]) &
+ ("type_$# $#($#);" % [s[m.group(1)[0]], s[m.group(1)[0]], s[m.group(2)[0]]]))
+ )
# ###
# Convert to Nim
@@ -421,8 +423,8 @@ proc getIncls(file: string, inline=false): seq[string] =
let curPath = splitFile(expandFileName(file)).dir
withFile(file):
- for f in content.findIter(re"(?m)^\s*#\s*include\s+(.*?)$"):
- var inc = f.captures[0].strip()
+ for f in content.findAll(re"(?m)^\s*#\s*include\s+(.*?)$"):
+ var inc = content[f.group(0)[0]].strip()
if ((gQuotes and inc.contains("\"")) or (gFilter != "" and gFilter in inc)) and (not exclude(inc)):
let addInc = inc.replace(re"""[<>"]""", "").replace(re"\/[\*\/].*$", "").strip()
try:
@@ -465,8 +467,8 @@ proc getDefines(file: string, inline=false): string =
echo "Inlining " & sincl
result &= getDefines(sincl)
withFile(file):
- for def in content.findIter(re"(?m)^(\s*#\s*define\s+[\w\d_]+\s+[\d\-.xf]+)(?:\r|//|/*).*?$"):
- result &= def.captures[0] & "\n"
+ for def in content.findAll(re"(?m)^(\s*#\s*define\s+[\w\d_]+\s+[\d\-.xf]+)(?:\r|//|/*).*?$"):
+ result &= content[def.group(0)[0]] & "\n"
proc runPreprocess(file, ppflags, flags: string, inline: bool): string =
var
@@ -519,9 +521,9 @@ proc runCtags(file: string): string =
if spl.len() > 4:
if spl[0] != "main" and spl[3] != "member":
var fn = ""
- var match = spl[2].find(re"/\^(.*?)\(")
- if match.isSome():
- fn = match.get().captures[0]
+ var match: RegexMatch
+ if spl[2].find(re"/\^(.*?)\(", match):
+ fn = spl[2][match.group(0)[0]]
fn &= spl[4].replace("signature:", "") & ";"
fdata &= fn & "\n"
@@ -650,7 +652,7 @@ proc c2nim(fl, outfile: string, c2nimConfig: c2nimConfigObj) =
prepend(outfile, incout)
# Nim doesn't like {.cdecl.} for type proc()
- freplace(outfile, re"(?m)(.*? = proc.*?){.cdecl.}", "$#")
+ freplace(outfile, re"(?m)(.*? = proc.*?)\{.cdecl.\}", "$#")
freplace(outfile, " {.cdecl.})", ")")
# Include {.compile.} directives
@@ -682,8 +684,9 @@ proc runFile(file: string, cfgin: OrderedTableRef) =
sfile = search(file)
for pattern in gWildcards.keys():
+ var match: RegexMatch
let pat = pattern.replace(".", "\\.").replace("*", ".*").replace("?", ".?")
- if file.find(re(pat)).isSome():
+ if file.find(toPattern(pat), match):
echo "Appending " & file & " " & pattern
for key in gWildcards[pattern].keys():
cfg[key & "." & pattern] = gWildcards[pattern][key]
diff --git a/nimgen.nimble b/nimgen.nimble
index 28967d4..03dfb78 100644
--- a/nimgen.nimble
+++ b/nimgen.nimble
@@ -9,7 +9,7 @@ skipDirs = @["tests"]
# Dependencies
-requires "nim >= 0.17.0", "c2nim >= 0.9.13"
+requires "nim >= 0.17.0", "c2nim >= 0.9.13", "regex >= 0.7.1"
bin = @["nimgen"]