Skip to content

Commit

Permalink
Handle regex escpaing in most cases (see #6)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gama11 committed Mar 14, 2017
1 parent 03922cf commit c43a5a4
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/hxParser/Converter.hx
Original file line number Diff line number Diff line change
Expand Up @@ -324,14 +324,32 @@ 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:
throw 'Unknown literal type: $unknown';
}
}

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();
Expand Down

0 comments on commit c43a5a4

Please sign in to comment.