From a3a8f602eac439f63a50fd2011d96e7dab281e8c Mon Sep 17 00:00:00 2001 From: Jaime Humberto Macias Bustamante Date: Thu, 12 Jun 2025 14:24:51 -0700 Subject: [PATCH] Mandatory "allowTypes" for Property + "isVar" parsing --- hscript/Expr.hx | 2 +- hscript/Interp.hx | 2 +- hscript/Parser.hx | 21 +++++++++++++++++---- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/hscript/Expr.hx b/hscript/Expr.hx index c8d4246..0d9f4d3 100644 --- a/hscript/Expr.hx +++ b/hscript/Expr.hx @@ -53,7 +53,7 @@ enum Expr { #end EConst( c : Const ); EIdent( v : String ); - EVar( n : String, ?t : CType, ?e : Expr, ?isPublic : Bool, ?isStatic : Bool, ?isPrivate : Bool, ?isFinal : Bool, ?isInline : Bool, ?get : FieldPropertyAccess, ?set : FieldPropertyAccess ); + EVar( n : String, ?t : CType, ?e : Expr, ?isPublic : Bool, ?isStatic : Bool, ?isPrivate : Bool, ?isFinal : Bool, ?isInline : Bool, ?get : FieldPropertyAccess, ?set : FieldPropertyAccess, ?isVar:Bool ); EParent( e : Expr ); EBlock( e : Array ); EField( e : Expr, f : String , ?safe : Bool ); diff --git a/hscript/Interp.hx b/hscript/Interp.hx index 5e5f288..0552893 100644 --- a/hscript/Interp.hx +++ b/hscript/Interp.hx @@ -703,7 +703,7 @@ class Interp { } case EIdent(id): return resolve(id); - case EVar(n, _, e, isPublic, isStatic, _, isFinal, _, getter, setter): + case EVar(n, _, e, isPublic, isStatic, _, isFinal, _, getter, setter, isVar): var hasGetSet:Bool = (getter != null || setter != null); if(depth > 0 && hasGetSet) { error(ECustom("Property Accessor for local variables is not allowed")); diff --git a/hscript/Parser.hx b/hscript/Parser.hx index 1c19b88..b822abf 100644 --- a/hscript/Parser.hx +++ b/hscript/Parser.hx @@ -490,7 +490,14 @@ class Parser { return parseExprNext(mk(EArrayDecl(a, nextType), p1)); case TMeta(id) if( allowMetadata ): var args = parseMetaArgs(); - return mk(EMeta(id, args, parseExpr()),p1); + var e = parseExpr(); + if(id == ':isVar') { + isVar = switch(e) { + case EVar(_): true; + default: false; + } + } + return mk(EMeta(id, args, e),p1); default: return unexpected(tk); } @@ -831,6 +838,7 @@ class Parser { var ident = getIdent(); var get = ADefault, set = ADefault; var hasGetSet:Bool = false; + var oldIsVar:Bool = isVar; var tk = token(); if( tk == TPOpen) { @@ -881,7 +889,8 @@ class Parser { checkAccess(get, set, e, t); nextType = null; - mk(EVar(ident, t, e, nextIsPublic, nextIsStatic, nextIsPrivate, id == "final", nextIsInline, get, set), p1, (e == null) ? tokenMax : pmax(e)); + if(isVar) isVar = false; + mk(EVar(ident, t, e, nextIsPublic, nextIsStatic, nextIsPrivate, id == "final", nextIsInline, get, set, oldIsVar), p1, (e == null) ? tokenMax : pmax(e)); case "while": var econd = parseExpr(); var e = parseExpr(); @@ -1491,17 +1500,21 @@ class Parser { #if hscriptPos var p1 = tokenMin; #end + if(!allowTypes) { + error(ECustom('"allowTypes" must be enabled for Property fields'), p1, pmax(expr)); + return; + } switch([get, set]) { case [AGet, ASet] | [AGet, ANever] | [ANever, ASet]: if(expr != null && !isVar) error(ECustom("Attempt to assign on field that is not a real variable"), p1, pmax(expr)); else if(type == null) - error(ECustom('Property requires type-hint${(!allowTypes) ? '. "allowTypes" must be enabled' : ''}'), p1, tokenMax); + error(ECustom('Property requires type-hint'), p1, tokenMax); case [ANever, ANever]: error(ECustom("Unsupported property combination"), p1, (expr == null) ? tokenMax : pmax(expr)); default: if(expr == null && type == null) - error(ECustom('Property requires ${(allowTypes) ? 'type-hint or' : ''} initialization'), p1, tokenMax); + error(ECustom('Property requires type-hint or initialization'), p1, tokenMax); } }