Make static bodies replacement use regex
This commit is contained in:
parent
5321014983
commit
0d0506675c
1 changed files with 5 additions and 59 deletions
64
nimgen.nim
64
nimgen.nim
|
|
@ -325,65 +325,11 @@ proc comment(file: string, pattern: string, numlines: string) =
|
||||||
break
|
break
|
||||||
|
|
||||||
proc removeStatic(filename: string) =
|
proc removeStatic(filename: string) =
|
||||||
|
## Replace static function bodies with a semicolon
|
||||||
if not fileExists(filename):
|
withFile(filename):
|
||||||
echo "Missing file: " & filename
|
content = content.replace(
|
||||||
return
|
re"(?m)(static inline.*?\))(\s*\{(\s*?.*?$)*[\n\r]\})", "$1;"
|
||||||
|
)
|
||||||
# This function should ideally be a regex. However, I could not
|
|
||||||
# get it to work as intended with the current re implementation
|
|
||||||
# in Nim.
|
|
||||||
#
|
|
||||||
# withFile(filename):
|
|
||||||
# content = content.replacef(
|
|
||||||
# re"(static inline.*?\))([ \r\n]*?\{([ \r\n]*?.*?)*[\n\r]\})", "$1;")
|
|
||||||
# )
|
|
||||||
#
|
|
||||||
# This currently won't even run, but if the replacef function is modified
|
|
||||||
# to not have nil checks, it will run on 1/3 of the input file. Maybe there's
|
|
||||||
# a buffer length issue.
|
|
||||||
|
|
||||||
var
|
|
||||||
file = open(filename)
|
|
||||||
stack: seq[string] = @[]
|
|
||||||
foundBrace = false
|
|
||||||
foundStatic = false
|
|
||||||
writeOutput = true
|
|
||||||
output = newStringofCap(getFileSize(filename))
|
|
||||||
|
|
||||||
for line in file.lines():
|
|
||||||
var modLine = line
|
|
||||||
|
|
||||||
if not foundStatic:
|
|
||||||
writeOutput = true
|
|
||||||
if line.startswith("static inline"):
|
|
||||||
foundStatic = true
|
|
||||||
let index = modLine.find("{")
|
|
||||||
if index != -1:
|
|
||||||
foundBrace = true
|
|
||||||
modLine.setLen(index)
|
|
||||||
elif not foundBrace:
|
|
||||||
writeOutput = true
|
|
||||||
if modLine.strip().startswith("{"):
|
|
||||||
foundBrace = true
|
|
||||||
writeOutput = false
|
|
||||||
else:
|
|
||||||
if modLine.startswith("}"):
|
|
||||||
foundBrace = false
|
|
||||||
foundStatic = false
|
|
||||||
output[^1] = ';'
|
|
||||||
output &= "\n"
|
|
||||||
|
|
||||||
if writeOutput:
|
|
||||||
output &= modLine
|
|
||||||
output &= "\n"
|
|
||||||
writeOutput = false
|
|
||||||
|
|
||||||
file.close()
|
|
||||||
|
|
||||||
var f = open(filename, fmWrite)
|
|
||||||
write(f, output)
|
|
||||||
f.close()
|
|
||||||
|
|
||||||
proc rename(file: string, renfile: string) =
|
proc rename(file: string, renfile: string) =
|
||||||
if file.splitFile().ext == ".nim":
|
if file.splitFile().ext == ".nim":
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue