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.
|
||||
|
||||
```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
|
||||
|
||||
```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
|
||||
|
||||
```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
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue