Compare commits

..
Author SHA1 Message Date
Jaime Humberto Macias Bustamante d344ede669 Regular Expression support (regex) 2025-09-03 20:13:39 -07:00
2 changed files with 8 additions and 147 deletions
+1 -1
View File
@@ -34,7 +34,7 @@ typedef UInt64 = #if cpp cpp.UInt64 #else Int #end;
enum Const {
CInt( v : Int );
CFloat( f : Float );
CString( s : String, ?i : Bool );
CString( s : String );
}
#if hscriptPos
+6 -145
View File
@@ -340,7 +340,7 @@ class Parser {
if( !allowJSON )
unexpected(tk);
switch( c ) {
case CString(s, _): id = s;
case CString(s): id = s;
default: unexpected(tk);
}
case TBrClose:
@@ -377,11 +377,6 @@ class Parser {
e = mk(EIdent(id));
return parseExprNext(e);
case TConst(c):
switch(c) {
case CString(s, i):
if(i) return parseExprNext(interpolateString(s));
default:
}
return parseExprNext(mk(EConst(c)));
case TPOpen:
tk = token();
@@ -1644,94 +1639,6 @@ class Parser {
return args;
}
// From hscript-iris: https://github.com/pisayesiwsi/hscript-iris/blob/dev/crowplexus/hscript/Parser.hx#L409
// by inky03
function interpolateString(s:String):Expr {
var exprs:Array<Expr> = [];
var dollarPos:Int = s.indexOf('$');
if(dollarPos == -1)
return mk(EConst(CString(s)));
final singleFirst:EReg = ~/[a-zA-Z_]/i;
final singleExpr:EReg = ~/[a-zA-Z0-9_]/i;
// TODO: optimize this using regex
while(dollarPos > -1) {
var pos:Int = dollarPos;
var pre:String = s.substr(0, pos);
var next:String = s.charAt(++pos);
if(next == '{') {
if (pre != '')
exprs.push(mk(EConst(CString(pre))));
var exprStr:String = '';
var depth:Int = 1;
while (true) {
next = s.charAt(++pos);
if (next == '{') {
depth++;
} else if (next == '}') {
depth--;
}
if (depth < 1)
break;
if (pos >= s.length) {
error(EUnterminatedString, pos, pos);
}
exprStr += next;
}
if (exprStr.trim() == '') {
error(ECustom("Expression cannot be empty"), pos, pos);
}
var prevChar = char;
var prevInput = input;
var prevReadPos = readPos; // a bit stupid innit???
#if hscriptPos
var prevOrigin = origin;
#end
var expr = parseString('($exprStr)' #if hscriptPos, origin #end);
readPos = prevReadPos; // rolling back parser state because otherwise we get problems...
input = prevInput;
char = prevChar;
#if hscriptPos
origin = prevOrigin;
#end
exprs.push(expr);
pos++;
}
else if(singleFirst.match(next)) {
if (pre != '')
exprs.push(mk(EConst(CString(pre))));
var ident: String = '';
while(singleExpr.match(next)) {
ident += next;
next = s.charAt(++pos);
}
exprs.push(mk(EIdent(ident)));
}
else if (next == '$') {
var secondToNext: String = s.charAt(pos);
if (secondToNext == "$") { // if its another dollar, skip...
s = pre + s.substr(pos, pos + 1); // remove $ ahead of the current one
break;
}
exprs.push(mk(EConst(CString(pre + '$'))));
}
s = s.substr(pos++);
dollarPos = s.indexOf('$');
}
if (exprs.length == 0) {
return mk(EConst(CString(s)));
} else {
exprs.push(mk(EConst(CString(s))));
var expr:Null<Expr> = exprs.shift();
while(true) {
if(exprs.length == 0) break;
expr = mk(EBinop('+', expr, exprs.shift()));
}
return expr;
}
}
function checkAccess(get:FieldPropertyAccess, set:FieldPropertyAccess, ?expr:Expr, ?type:CType) {
#if hscriptPos
var p1 = tokenMin;
@@ -1966,9 +1873,8 @@ class Parser {
return StringTools.fastCodeAt(input, readPos++);
}
function readString( until:Int, regex:Bool = false, interpolate:Bool = false ):String {
function readString( until:Int, regex:Bool = false ):String {
var c = 0;
var prev = 0;
var b = new StringBuf();
var esc = false;
var old = line;
@@ -2017,53 +1923,11 @@ class Parser {
}
} else if( c == 92 && !regex)
esc = true;
else if( c == until ) {
if(regex && prev == 92) {
b.addChar(c);
prev = c;
}
else
else if( c == until )
break;
}
else if (c == 36 && interpolate) { // brace for impact !!
// From hscript-iris: https://github.com/pisayesiwsi/hscript-iris/blob/dev/crowplexus/hscript/Parser.hx#L1665
// by inky03
// TODO: optimize this
b.addChar(c);
var next = readChar();
if (next == 123) {
b.addChar(next);
var depth:Int = 0;
while (true) {
next = readChar();
if (StringTools.isEof(next)) {
error(EUnterminatedString, p1, p1);
}
b.addChar(next);
if (next == "'".code) {
var nextStr:String = readString("'".code, false, true);
for (char in nextStr) {
b.addChar(char);
}
b.addChar("'".code);
next = readChar();
b.addChar(next);
}
if (next == 125) {
depth--;
if (depth < 0)
break;
}
}
} else {
readPos--;
}
}
else {
if( c == 10 ) line++;
b.addChar(c);
if(regex)
prev = c;
}
}
return b.toString();
@@ -2269,10 +2133,7 @@ class Parser {
case "}".code: return TBrClose;
case "[".code: return TBkOpen;
case "]".code: return TBkClose;
case "'".code:
return TConst( CString(readString(char, false, true), true) );
case '"'.code:
return TConst( CString(readString(char), false) );
case "'".code, '"'.code: return TConst( CString(readString(char)) );
case "?".code:
char = readChar();
switch (char) {
@@ -2533,7 +2394,7 @@ class Parser {
return switch(c) {
case CInt(v): Std.string(v);
case CFloat(f): Std.string(f);
case CString(s, _): s; // TODO : escape + quote
case CString(s): s; // TODO : escape + quote
}
}
@@ -2557,7 +2418,7 @@ class Parser {
case TDoubleDot: ":";
case TMeta(id): "@" + id;
case TPrepro(id): "#" + id;
case TRegex(e, f): '~/$e/$f';
case TRegex(e, f): '~/$e/${f == null ? '' : f}';
}
}