Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add string interpolation #62

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Test.hx
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ class Test extends TestCase {
assertScript("var a:Array<Dynamic>=[1,2,4]; a[2]", 4, null, true);
assertScript("/**/0", 0);
assertScript("x=1;x*=-2", -2);
assertScript("'$$'", "$");
assertScript("'$x'", 10, { x:10 });
assertScript("'${x + 5}'", 15, { x:10 });
assertScript("'h$s'", "hscript", { s:"script" });
assertScript("'${function(x) {return x * x;}(3)}'", 9);
}

function testMap():Void {
Expand Down
2 changes: 1 addition & 1 deletion hscript/Expr.hx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ package hscript;
enum Const {
CInt( v : Int );
CFloat( f : Float );
CString( s : String );
CString( s : String, ?interpolated:Bool );
#if !haxe3
CInt32( v : haxe.Int32 );
#end
Expand Down
72 changes: 71 additions & 1 deletion hscript/Parser.hx
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,12 @@ class Parser {
e = mk(EIdent(id));
return parseExprNext(e);
case TConst(c):
switch (c) {
case CString(s, interpolated):
if (interpolated)
return parseExprNext(interpolate(s));
default:
}
return parseExprNext(mk(EConst(c)));
case TPOpen:
var e = parseExpr();
Expand Down Expand Up @@ -389,6 +395,70 @@ class Parser {
return unexpected(tk);
}
}

function interpolate(s:String) {
var exprs:Array<Expr> = [];
var dollarIndex = s.indexOf('$');

while (dollarIndex > -1) {
var i = dollarIndex;
var char = s.charAt(++i);
switch (char) {
case '$':
s = s.substring(0, i) + s.substr(i + 1);
case '{':
var expr = "";
var depth = 0;

var precedingSub = s.substring(0, i - 1);
if (precedingSub != "")
exprs.push(mk(EConst(CString(precedingSub))));

while (i < s.length) {
char = s.charAt(++i);
if (char == '{')
depth++;
else if (char == '}') {
depth--;
if (depth < 0)
break;
}
expr += char;
}

exprs.push(parseString(expr #if hscriptPos, origin #end));
s = s.substr(i + 1);
case c if (c == '_' || c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z'): // [a-zA-Z_]
var ident = c;
var precedingSub = s.substring(0, i - 1);
if (precedingSub != "")
exprs.push(mk(EConst(CString(precedingSub))));
while (true) {
char = s.charAt(++i);
if (char == '_'
|| char >= 'a' && char <= 'z'
|| char >= 'A' && char <= 'Z'
|| char >= '0' && char <= '9') // [a-zA-Z0-9_]
ident += char;
else break;
}
exprs.push(mk(EIdent(ident)));
s = s.substr(i);
}
dollarIndex = s.indexOf('$', i);
}

if (exprs.length == 0)
exprs.push(mk(EConst(CString(s))));

var expr = exprs[0];
for (i in 1...exprs.length) {
var nextExpr = exprs[i];
expr = mk(EBinop('+', expr, nextExpr));
}

return expr;
}

function mapCompr( tmp : String, e : Expr ) {
var edef = switch( expr(e) ) {
Expand Down Expand Up @@ -1070,7 +1140,7 @@ class Parser {
case 125: return TBrClose;
case 91: return TBkOpen;
case 93: return TBkClose;
case 39: return TConst( CString(readString(39)) );
case 39: return TConst( CString(readString(39), true) );
case 34: return TConst( CString(readString(34)) );
case 63: return TQuestion;
case 58: return TDoubleDot;
Expand Down