diff --git a/src/hxParser/Converter.hx b/src/hxParser/Converter.hx index 8032cd6..797042d 100644 --- a/src/hxParser/Converter.hx +++ b/src/hxParser/Converter.hx @@ -324,7 +324,9 @@ class Converter { case "literal_float": PLiteralFloat(nextToken()); case "literal_regex": - PLiteralRegex(nextToken()); + var token = nextToken(); + token.text = escapeRegex(token.text); + PLiteralRegex(token); case "literal_string": PLiteralString(convertString(node.sub[0])); case unknown: @@ -332,6 +334,22 @@ class Converter { } } + function escapeRegex(literal:String):String { + // extract the actual regex inside of ~// + var content = literal.substr(2, literal.length - 1); + var end = content.lastIndexOf("/"); + if (end == -1) throw 'regex literal end not found: ${literal}'; + var modifiers = content.substr(end + 1); + content = content.substring(0, end); + return '~/${ + content + .replace("\n", "\\\\n") + .replace("\r", "\\\\r") + .replace("\t", "\\\\t") + .replace("/", "\\\\/") + }/' + modifiers; + } + function convertString(node:JNodeBase):StringToken { var node = node.asNode("string"); var token = nextToken();