diff options
| author | Ganesh Viswanathan <dev@genotrance.com> | 2018-08-05 01:10:47 -0500 |
|---|---|---|
| committer | Ganesh Viswanathan <dev@genotrance.com> | 2018-08-05 01:10:47 -0500 |
| commit | 32debc5534bc4a82e5f944adb2e4b42043bf5fab (patch) | |
| tree | 9d1644f8a1fb892827ba8aef788de37965d719fb /src | |
| parent | 1dbff5cbd14f69529869720e72e153f6f9029957 (diff) | |
| download | nimgen-32debc5534bc4a82e5f944adb2e4b42043bf5fab.tar.gz nimgen-32debc5534bc4a82e5f944adb2e4b42043bf5fab.zip | |
Regex search/replace support - #2
Diffstat (limited to 'src')
| -rw-r--r-- | src/nimgen/c2nim.nim | 2 | ||||
| -rw-r--r-- | src/nimgen/fileops.nim | 8 | ||||
| -rw-r--r-- | src/nimgen/gencore.nim | 24 | ||||
| -rw-r--r-- | src/nimgen/runcfg.nim | 14 |
4 files changed, 20 insertions, 28 deletions
diff --git a/src/nimgen/c2nim.nim b/src/nimgen/c2nim.nim index 73e4ebb..05954e6 100644 --- a/src/nimgen/c2nim.nim +++ b/src/nimgen/c2nim.nim @@ -95,7 +95,7 @@ proc c2nim*(fl, outfile: string, c2nimConfig: c2nimConfigObj) = discard # Nim doesn't like {.cdecl.} for type proc() - freplace(outfile, re"(?m)(.*? = proc.*?)\{.cdecl.\}", "$#") + freplace(outfile, re"(?m)(.*? = proc.*?)\{.cdecl.\}", "$1") freplace(outfile, " {.cdecl.})", ")") # Include {.compile.} directives diff --git a/src/nimgen/fileops.nim b/src/nimgen/fileops.nim index c4e67a0..4e8f858 100644 --- a/src/nimgen/fileops.nim +++ b/src/nimgen/fileops.nim @@ -38,13 +38,7 @@ proc freplace*(file: string, pattern: string, repl="") = proc freplace*(file: string, pattern: Regex, repl="") = withFile(file): - var m: RegexMatch - if content.find(pattern, m): - if "$#" in repl: - content = content.replace(pattern, - proc (m: RegexMatch, s: string): string = repl % s[m.group(0)[0]]) - else: - content = content.replace(pattern, repl) + content = content.replace(pattern, repl) proc comment*(file: string, pattern: string, numlines: string) = let diff --git a/src/nimgen/gencore.nim b/src/nimgen/gencore.nim index ff070df..4e79d04 100644 --- a/src/nimgen/gencore.nim +++ b/src/nimgen/gencore.nim @@ -4,24 +4,16 @@ import file, globals proc addEnv*(str: string): string = var newStr = str - for pair in envPairs(): - try: - newStr = newStr % [pair.key, pair.value.string] - except ValueError: - # Ignore if there are no values to replace. We - # want to continue anyway - discard - try: + if "$output" in newStr or "${output}" in newStr: newStr = newStr % ["output", gOutput] - except ValueError: - # Ignore if there are no values to replace. We - # want to continue anyway - discard - - # if there are still format args, print a warning - if newStr.contains("$") and not newStr.contains("$replace("): - echo "WARNING: \"", newStr, "\" still contains an uninterpolated value!" + + for pair in envPairs(): + if pair.key.len() == 0: + continue + + if ("$" & pair.key) in newStr or ("${" & pair.key & "}") in newStr: + newStr = newStr % [pair.key, pair.value.string] return newStr diff --git a/src/nimgen/runcfg.nim b/src/nimgen/runcfg.nim index fd65708..a0e51c5 100644 --- a/src/nimgen/runcfg.nim +++ b/src/nimgen/runcfg.nim @@ -44,6 +44,7 @@ proc runFile*(file: string, cfgin: OrderedTableRef = newOrderedTable[string, str var srch = "" + rgx = "" c2nimConfig = c2nimConfigObj( flags: "--stdcall", ppflags: "", @@ -78,6 +79,8 @@ proc runFile*(file: string, cfgin: OrderedTableRef = newOrderedTable[string, str elif action == "replace": if srch != "": freplace(sfile, cfg[srch], cfg[act]) + elif rgx != "": + freplace(sfile, toPattern(cfg[rgx]), cfg[act]) elif action == "comment": if srch != "": comment(sfile, cfg[srch], cfg[act]) @@ -92,8 +95,11 @@ proc runFile*(file: string, cfgin: OrderedTableRef = newOrderedTable[string, str elif action == "pipe": pipe(sfile, cfg[act]) srch = "" + rgx = "" elif action == "search": srch = act + elif action == "regex": + rgx = act if file.splitFile().ext != ".nim": var @@ -131,11 +137,11 @@ proc runFile*(file: string, cfgin: OrderedTableRef = newOrderedTable[string, str fixFuncProtos(sfile) let outfile = getNimout(sfile) + var incout = "" if c2nimConfig.recurse or c2nimConfig.inline: var cfg = newOrderedTable[string, string]() incls = getIncls(sfile) - incout = "" for name, value in c2nimConfig.fieldPairs: when value is string: @@ -154,12 +160,12 @@ proc runFile*(file: string, cfgin: OrderedTableRef = newOrderedTable[string, str if c2nimConfig.recurse: incout &= "import $#\n" % inc.search().getNimout()[0 .. ^5] - if c2nimConfig.recurse and incout.len() != 0: - prepend(outfile, incout) - if not noprocess: c2nim(file, outfile, c2nimConfig) + if c2nimConfig.recurse and incout.len() != 0: + prepend(outfile, incout) + if reset: gitCheckout(sfile) |
