Regex search/replace support - #2
This commit is contained in:
parent
1dbff5cbd1
commit
32debc5534
5 changed files with 23 additions and 29 deletions
|
|
@ -174,15 +174,17 @@ The following keys apply to library source code (before processing) and generate
|
||||||
|
|
||||||
```create``` = create a file at exact location with contents specified. File needs to be in the _[n.exclude]_ list in order to be created.
|
```create``` = create a file at exact location with contents specified. File needs to be in the _[n.exclude]_ list in order to be created.
|
||||||
|
|
||||||
|
```pipe``` = execute a command on a file and store the output of the command as the new file contents. E.g. pipe = "cat $file | grep 'static inline'"
|
||||||
|
|
||||||
```search``` = search string providing context for following prepend/append/replace directives
|
```search``` = search string providing context for following prepend/append/replace directives
|
||||||
|
|
||||||
```pipe``` = execute a command on a file and store the output of the command as the new file contents. Ex: pipe = "cat $file | grep 'static inline'"
|
```regex``` = regex search string providing context for the following replace directive. Specify using """ to avoid regex parsing issues
|
||||||
|
|
||||||
```prepend``` = string value to prepend into file at beginning or before search
|
```prepend``` = string value to prepend into file at beginning or before search
|
||||||
|
|
||||||
```append``` = string value to append into file at the end or after search
|
```append``` = string value to append into file at the end or after search
|
||||||
|
|
||||||
```replace``` = string value to replace search string in file
|
```replace``` = string value to replace search string in file. Regex captures can be referred to using $1, $2, etc.
|
||||||
|
|
||||||
```comment``` = number of lines to comment from search location
|
```comment``` = number of lines to comment from search location
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -95,7 +95,7 @@ proc c2nim*(fl, outfile: string, c2nimConfig: c2nimConfigObj) =
|
||||||
discard
|
discard
|
||||||
|
|
||||||
# Nim doesn't like {.cdecl.} for type proc()
|
# Nim doesn't like {.cdecl.} for type proc()
|
||||||
freplace(outfile, re"(?m)(.*? = proc.*?)\{.cdecl.\}", "$#")
|
freplace(outfile, re"(?m)(.*? = proc.*?)\{.cdecl.\}", "$1")
|
||||||
freplace(outfile, " {.cdecl.})", ")")
|
freplace(outfile, " {.cdecl.})", ")")
|
||||||
|
|
||||||
# Include {.compile.} directives
|
# Include {.compile.} directives
|
||||||
|
|
|
||||||
|
|
@ -38,13 +38,7 @@ proc freplace*(file: string, pattern: string, repl="") =
|
||||||
|
|
||||||
proc freplace*(file: string, pattern: Regex, repl="") =
|
proc freplace*(file: string, pattern: Regex, repl="") =
|
||||||
withFile(file):
|
withFile(file):
|
||||||
var m: RegexMatch
|
content = content.replace(pattern, repl)
|
||||||
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)
|
|
||||||
|
|
||||||
proc comment*(file: string, pattern: string, numlines: string) =
|
proc comment*(file: string, pattern: string, numlines: string) =
|
||||||
let
|
let
|
||||||
|
|
|
||||||
|
|
@ -4,24 +4,16 @@ import file, globals
|
||||||
|
|
||||||
proc addEnv*(str: string): string =
|
proc addEnv*(str: string): string =
|
||||||
var newStr = str
|
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]
|
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
|
for pair in envPairs():
|
||||||
if newStr.contains("$") and not newStr.contains("$replace("):
|
if pair.key.len() == 0:
|
||||||
echo "WARNING: \"", newStr, "\" still contains an uninterpolated value!"
|
continue
|
||||||
|
|
||||||
|
if ("$" & pair.key) in newStr or ("${" & pair.key & "}") in newStr:
|
||||||
|
newStr = newStr % [pair.key, pair.value.string]
|
||||||
|
|
||||||
return newStr
|
return newStr
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -44,6 +44,7 @@ proc runFile*(file: string, cfgin: OrderedTableRef = newOrderedTable[string, str
|
||||||
|
|
||||||
var
|
var
|
||||||
srch = ""
|
srch = ""
|
||||||
|
rgx = ""
|
||||||
|
|
||||||
c2nimConfig = c2nimConfigObj(
|
c2nimConfig = c2nimConfigObj(
|
||||||
flags: "--stdcall", ppflags: "",
|
flags: "--stdcall", ppflags: "",
|
||||||
|
|
@ -78,6 +79,8 @@ proc runFile*(file: string, cfgin: OrderedTableRef = newOrderedTable[string, str
|
||||||
elif action == "replace":
|
elif action == "replace":
|
||||||
if srch != "":
|
if srch != "":
|
||||||
freplace(sfile, cfg[srch], cfg[act])
|
freplace(sfile, cfg[srch], cfg[act])
|
||||||
|
elif rgx != "":
|
||||||
|
freplace(sfile, toPattern(cfg[rgx]), cfg[act])
|
||||||
elif action == "comment":
|
elif action == "comment":
|
||||||
if srch != "":
|
if srch != "":
|
||||||
comment(sfile, cfg[srch], cfg[act])
|
comment(sfile, cfg[srch], cfg[act])
|
||||||
|
|
@ -92,8 +95,11 @@ proc runFile*(file: string, cfgin: OrderedTableRef = newOrderedTable[string, str
|
||||||
elif action == "pipe":
|
elif action == "pipe":
|
||||||
pipe(sfile, cfg[act])
|
pipe(sfile, cfg[act])
|
||||||
srch = ""
|
srch = ""
|
||||||
|
rgx = ""
|
||||||
elif action == "search":
|
elif action == "search":
|
||||||
srch = act
|
srch = act
|
||||||
|
elif action == "regex":
|
||||||
|
rgx = act
|
||||||
|
|
||||||
if file.splitFile().ext != ".nim":
|
if file.splitFile().ext != ".nim":
|
||||||
var
|
var
|
||||||
|
|
@ -131,11 +137,11 @@ proc runFile*(file: string, cfgin: OrderedTableRef = newOrderedTable[string, str
|
||||||
fixFuncProtos(sfile)
|
fixFuncProtos(sfile)
|
||||||
|
|
||||||
let outfile = getNimout(sfile)
|
let outfile = getNimout(sfile)
|
||||||
|
var incout = ""
|
||||||
if c2nimConfig.recurse or c2nimConfig.inline:
|
if c2nimConfig.recurse or c2nimConfig.inline:
|
||||||
var
|
var
|
||||||
cfg = newOrderedTable[string, string]()
|
cfg = newOrderedTable[string, string]()
|
||||||
incls = getIncls(sfile)
|
incls = getIncls(sfile)
|
||||||
incout = ""
|
|
||||||
|
|
||||||
for name, value in c2nimConfig.fieldPairs:
|
for name, value in c2nimConfig.fieldPairs:
|
||||||
when value is string:
|
when value is string:
|
||||||
|
|
@ -154,12 +160,12 @@ proc runFile*(file: string, cfgin: OrderedTableRef = newOrderedTable[string, str
|
||||||
if c2nimConfig.recurse:
|
if c2nimConfig.recurse:
|
||||||
incout &= "import $#\n" % inc.search().getNimout()[0 .. ^5]
|
incout &= "import $#\n" % inc.search().getNimout()[0 .. ^5]
|
||||||
|
|
||||||
if c2nimConfig.recurse and incout.len() != 0:
|
|
||||||
prepend(outfile, incout)
|
|
||||||
|
|
||||||
if not noprocess:
|
if not noprocess:
|
||||||
c2nim(file, outfile, c2nimConfig)
|
c2nim(file, outfile, c2nimConfig)
|
||||||
|
|
||||||
|
if c2nimConfig.recurse and incout.len() != 0:
|
||||||
|
prepend(outfile, incout)
|
||||||
|
|
||||||
if reset:
|
if reset:
|
||||||
gitCheckout(sfile)
|
gitCheckout(sfile)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue