From c43a5a4b1f1ad16506777dae44beb161dcbad0e2 Mon Sep 17 00:00:00 2001 From: Gama11 Date: Tue, 14 Mar 2017 22:53:20 +0100 Subject: [PATCH] Handle regex escpaing in most cases (see #6) --- src/hxParser/Converter.hx | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) 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();