This is a trivial example using the Replace Source Code Filter:
#? replace(sub="\t", by=" ")
I want to create my enhanced version where sub is a regexp:
#? regexReplace(sub="...", by="...")
Please help me a little. I only know the basic theory of files and regexes:
import os, re
var
newLine: string
input = open("originalFile.nim")
output = open("processedFile.nim", fmWrite)
sub = re"..."
by = "..."
for line in input.lines:
newLine = line.replacef(sub, by)
output.writeLine(newLine)
input.close
output.close
In order to create my own Source Code Filter, it would be enough if someone could give a trivial example on how to build a personal SCF.
I have not been able to figure it out by looking at the documentation.
And searching the web I could not find any example.
The examples in the linked manual worked for me when I last needed to use SCF.
Can you paste a minimal working example of what you tried and didn't work? Then someone can help you out.
The documentation says that there are three available filters: Replace, Strip and StdTmpl.
But can I create my own filter? how? (I want to create a RegexReplace filter)
New source code filters can only be done by patching the compiler. :-)
And I doubt a PR would be accepted, source code filters are not loved.
A lot could be achieved by abusing the stdtmpl filter:
#? stdtmpl(emit="")
#import processcode
#regexReplace r".a", "mi", "" &
echo "Manamana"
where
# processcode.nim
import macros, re
macro regexReplace*(sub, by, src: string): untyped =
src.strVal.replace(sub.strVal.rex, by.strVal).parseStmt
but unfortunately the re module doesn't work in the VM :o/but unfortunately the re module doesn't work in the VM :o/
What about the Nim-native regex module from nimble?
Thx, didn't know about regex. With that installed and
# processcode.nim
import macros, regex
macro regexReplace*(sub, by, src: string): untyped =
src.strVal.replace(sub.strVal.re, by.strVal).parseStmt
the code actually works.