From 90684a4e8209476c4663bf1df2e0a58fc8058d86 Mon Sep 17 00:00:00 2001 From: AlexHaxe Date: Mon, 2 Nov 2020 22:32:33 +0100 Subject: [PATCH] fixed type parameter detection fixed Dollar handling --- CHANGELOG.md | 2 ++ checkstyle.json | 1 + src/tokentree/TokenStream.hx | 29 +++++++++++++++++-- src/tokentree/walk/WalkLtGt.hx | 2 ++ src/tokentree/walk/WalkStatement.hx | 14 ++++++++- test/tokentree/TokenTreeBuilderParsingTest.hx | 25 ++++++++++++++++ 6 files changed, 70 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6f2313a..058b8ad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,8 @@ ## dev branch / next version (1.x.x) - Fixed arrow functions +- Fixed type parameter detection +- Fixed Dollar handling ## version 1.0.29 (2020-11-01) diff --git a/checkstyle.json b/checkstyle.json index bdaf943..2ec1191 100644 --- a/checkstyle.json +++ b/checkstyle.json @@ -526,6 +526,7 @@ "tokentree/utils/TokenTreeCheckUtils:determinBrChildren", "tokentree/utils/TokenTreeCheckUtils:findColonParent", "tokentree/ToTokenTreeDef", + "tokentree/TokenStream:isTypedParam", "tokentree/TokenTreeDefPrinter" ], "MethodLength": [ diff --git a/src/tokentree/TokenStream.hx b/src/tokentree/TokenStream.hx index 86628f9..1b6c13b 100644 --- a/src/tokentree/TokenStream.hx +++ b/src/tokentree/TokenStream.hx @@ -141,21 +141,46 @@ class TokenStream { default: return false; } + var depth:Int = 1; + var brDepth:Int = 0; + var pDepth:Int = 0; while (true) { token = tokens[index++]; switch (token.tok) { case Dot: case DblDot: case Comma: + case Arrow: + case POpen: + pDepth++; + case PClose: + if (pDepth <= 0) { + return false; + } + pDepth--; + case BrOpen: + brDepth++; + case BrClose: + if (brDepth <= 0) { + return false; + } + brDepth--; case Const(_): - case Kwd(_): + // case Kwd(_): case Dollar(_): case Binop(OpLt): + depth++; case Binop(OpGt): - return true; + depth--; + if (depth <= 0) { + return true; + } default: return false; } + if (index >= tokens.length) { + return false; + } } return false; } diff --git a/src/tokentree/walk/WalkLtGt.hx b/src/tokentree/walk/WalkLtGt.hx index 462a8fd..9e436ae 100644 --- a/src/tokentree/walk/WalkLtGt.hx +++ b/src/tokentree/walk/WalkLtGt.hx @@ -18,6 +18,8 @@ class WalkLtGt { var dblDot:TokenTree = stream.consumeToken(); ltTok.addChild(dblDot); WalkTypeNameDef.walkTypeNameDef(stream, ltTok); + case POpen: + WalkPOpen.walkPOpen(stream, ltTok); default: WalkFieldDef.walkFieldDef(stream, ltTok); } diff --git a/src/tokentree/walk/WalkStatement.hx b/src/tokentree/walk/WalkStatement.hx index 09171a7..64a276d 100644 --- a/src/tokentree/walk/WalkStatement.hx +++ b/src/tokentree/walk/WalkStatement.hx @@ -132,6 +132,7 @@ class WalkStatement { parent.addChild(newChild); stream.applyTempStore(newChild); walkTrailingComment(stream, newChild); + if (wantMore) walkStatementWithoutSemicolon(stream, newChild); walkStatementContinue(stream, newChild); walkTrailingComment(stream, newChild); @@ -169,6 +170,17 @@ class WalkStatement { walkOpBool(stream, parent); case Binop(OpAdd), Binop(OpSub): walkOpAdd(stream, parent); + case Binop(OpGt): + var ltParent:TokenTree = parent; + while (true) { + switch (ltParent.tok) { + case Root: break; + case Dot | DblDot | Comma | Arrow | POpen | Const(_) | Dollar(_) | Binop(OpGt): ltParent = ltParent.parent; + case Binop(OpLt): return; + default: break; + } + } + walkStatementWithoutSemicolon(stream, parent); case Binop(_): walkStatementWithoutSemicolon(stream, parent); case Const(CIdent("is")): @@ -445,7 +457,7 @@ class WalkStatement { var dollarTok:TokenTree = stream.consumeToken(); parent.addChild(dollarTok); switch (stream.token()) { - case POpen | BrOpen | BkOpen | Binop(_) | Const(CIdent("is")): + case POpen | BrOpen | BkOpen | Dot | Binop(_) | Const(CIdent("is")): WalkBlock.walkBlock(stream, dollarTok); default: } diff --git a/test/tokentree/TokenTreeBuilderParsingTest.hx b/test/tokentree/TokenTreeBuilderParsingTest.hx index bdf8d22..af4fa2c 100644 --- a/test/tokentree/TokenTreeBuilderParsingTest.hx +++ b/test/tokentree/TokenTreeBuilderParsingTest.hx @@ -121,6 +121,9 @@ class TokenTreeBuilderParsingTest implements ITest { assertCodeParses(SEMICOLON_BINOP); assertCodeParses(NEW_ARRAY_ACCESS); assertCodeParses(MACRO_COMPLEXTYPE); + assertCodeParses(CALLBACK_TYPE_PARAM); + assertCodeParses(MAP_INIT_WITH_COMMENT); + assertCodeParses(DOLLAR_CHAIN); } public function assertCodeParses(code:String, ?pos:PosInfos) { @@ -1643,4 +1646,26 @@ import #if haxe4 js.lib.Promise #else js.Promise #end as JsPromise; class Test { var retType = macro : Map; }"; + + var CALLBACK_TYPE_PARAM = " + class HaxeServer { + var stopProgressCallback:Null<() -> Void>; + }"; + + var MAP_INIT_WITH_COMMENT = " + final flags = [ + CompilationServer => [ + // server + ], + ];"; + + var DOLLAR_CHAIN = " + class Main { + function foobar() { + assignments.push(macro { + if ($struct.$name == null) + $struct.$name = $defaults.$name; + }); + } + }"; } \ No newline at end of file