Regex improvements

This commit is contained in:
Ganesh Viswanathan 2019-10-28 10:48:53 -05:00
commit 872873c07a

View file

@ -102,7 +102,7 @@ proc checkIdentifier(name, kind, parent, origName: string) =
doAssert name[0] != '_' and name[^1] != '_', errmsg & " leading/trailing underscores '_'" doAssert name[0] != '_' and name[^1] != '_', errmsg & " leading/trailing underscores '_'"
doAssert (not name.contains(re"_[_]+")): errmsg & " more than one consecutive underscore '_'" doAssert (not name.contains("__")): errmsg & " consecutive underscores '_'"
if parent.nBl: if parent.nBl:
doAssert name.nBl, &"Blank identifier, originally '{parentStr}{name}' ({kind}), cannot be empty" doAssert name.nBl, &"Blank identifier, originally '{parentStr}{name}' ({kind}), cannot be empty"
@ -191,11 +191,10 @@ proc getPtrType*(str: string): string =
proc getLit*(str: string): string = proc getLit*(str: string): string =
let let
str = str.replace(re"//.*?$", "").replace(re"/\*.*?\*/", "").strip() str = str.replace(re"/[/*].*?(?:\*/)?$", "").strip()
if str.contains(re"^[\-]?[\d]+$") or if str.contains(re"^[\-]?[\d]*[.]?[\d]+$") or
str.contains(re"^[\-]?[\d]*\.[\d]+$") or str.contains(re"^0x[\da-fA-F]+$"):
str.contains(re"^0x[\d]+$"):
return str return str
proc getNodeVal*(nimState: NimState, node: TSNode): string = proc getNodeVal*(nimState: NimState, node: TSNode): string =
@ -211,7 +210,7 @@ proc getLineCol*(gState: State, node: TSNode): tuple[line, col: int] =
result.col += 1 result.col += 1
proc getCurrentHeader*(fullpath: string): string = proc getCurrentHeader*(fullpath: string): string =
("header" & fullpath.splitFile().name.replace(re"[-.]+", "")) ("header" & fullpath.splitFile().name.multiReplace([(".", ""), ("-", "")]))
proc removeStatic(content: string): string = proc removeStatic(content: string): string =
## Replace static function bodies with a semicolon and commented ## Replace static function bodies with a semicolon and commented
@ -269,12 +268,11 @@ proc getPreprocessor*(gState: State, fullpath: string, mode = "cpp"): string =
if start: if start:
if "#undef" in line: if "#undef" in line:
continue continue
rdata.add( rdata.add line
line. return rdata.join("\n").
replace("__restrict", ""). replace("__restrict", "").
replace(re"__attribute__[ ]*\(\(.*?\)\)([ ,;])", "$1") replace(re"__attribute__[ ]*\(\(.*?\)\)([ ,;])", "$1").
) removeStatic()
return rdata.join("\n").removeStatic()
converter toString*(kind: Kind): string = converter toString*(kind: Kind): string =
return case kind: return case kind: