From aaa78abd10ad59c86c25e65d862becb1c77719d6 Mon Sep 17 00:00:00 2001 From: HeronErin Date: Tue, 14 May 2024 12:36:03 -0400 Subject: [PATCH] Unstable removal of the old type parser. --- source/fnc/errors.d | 28 ++-- source/fnc/treegen/ast_types.d | 102 ++---------- source/fnc/treegen/expression_parser.d | 122 ++++++++++---- source/fnc/treegen/package.d | 1 - source/fnc/treegen/relationships.d | 57 ++++--- source/fnc/treegen/scope_parser.d | 4 +- source/fnc/treegen/type_parser.d | 216 ------------------------- source/main.d | 14 +- 8 files changed, 164 insertions(+), 380 deletions(-) delete mode 100644 source/fnc/treegen/type_parser.d diff --git a/source/fnc/errors.d b/source/fnc/errors.d index d4fa47e..2ade27b 100644 --- a/source/fnc/errors.d +++ b/source/fnc/errors.d @@ -94,23 +94,27 @@ class SyntaxError : Error string errorString = msg; import tern.string : AnsiColor; + import core.exception; + try{ + errorString ~= "\n Line: " ~ errData.lineCount.to!string; + errorString ~= "\n Col: " ~ errData.afterLineStart.to!string; + errorString ~= "\n\t"; - errorString ~= "\n Line: " ~ errData.lineCount.to!string; - errorString ~= "\n Col: " ~ errData.afterLineStart.to!string; - errorString ~= "\n\t"; + errorString ~= GLOBAL_ERROR_STATE[errData.startOfLine .. start]; - errorString ~= GLOBAL_ERROR_STATE[errData.startOfLine .. start]; + errorString ~= AnsiColor.BackgroundRed; - errorString ~= AnsiColor.BackgroundRed; + errorString ~= GLOBAL_ERROR_STATE[start .. end]; + errorString ~= AnsiColor.Reset; + import std.algorithm : max; + // TODO: Make this better. + errorString ~= GLOBAL_ERROR_STATE[end .. max(errData.endOfProblemLine, end)]; - errorString ~= GLOBAL_ERROR_STATE[start .. end]; - errorString ~= AnsiColor.Reset; - import std.algorithm : max; - // TODO: Make this better. - errorString ~= GLOBAL_ERROR_STATE[end .. max(errData.endOfProblemLine, end)]; - - return errorString; + return errorString; + }catch(ArraySliceError e){ + return msg ~ " (line and col can't be resolved. Please report this as a github issue with code samples)"; + } } protected this(string msg, string file, size_t line, Throwable next = null) @nogc nothrow pure @safe diff --git a/source/fnc/treegen/ast_types.d b/source/fnc/treegen/ast_types.d index e04f652..841ccd9 100644 --- a/source/fnc/treegen/ast_types.d +++ b/source/fnc/treegen/ast_types.d @@ -29,6 +29,7 @@ enum AstAction SingleArgumentOperation, // Ex: x++, ++x DoubleArgumentOperation, // Ex: 9+10 + GenericOf, // Not to be confused with TypeGeneric, ex: foo!bar(), foo!(bar)(), or "auto x classFoo!bar"; Call, // Ex: foo(bar); // Misc tokens: @@ -38,15 +39,6 @@ enum AstAction LiteralUnit, // Ex: 6, 6L, "Hello world" TokenHolder, // A temporary Node that is yet to be parsed - - // Type tokens - TypeTuple, // [int, float] - TypeArray, // int[3] OR int[] - TypeCall, // const(int) Note: const is ALSO a keyword - TypePointer, // *int - TypeReference, // &int - TypeGeneric, // Result!(int, string) - TypeVoidable } bool isExpressionLike(AstAction action) @@ -61,6 +53,7 @@ bool isCallable(AstAction action) || action == AstAction.SingleArgumentOperation || action == AstAction.Call || action == AstAction.Expression + || action == AstAction.GenericOf || action == AstAction.LiteralUnit || action == AstAction.NamedUnit; } @@ -134,6 +127,7 @@ enum OperationVariety NotEqualTo, Period, // foo.bar + Arrow, // foo->bar Range, // x..y OR 0..99 } @@ -201,10 +195,10 @@ struct CallNodeData } /+++++++/ -struct TypeGenericNodeData +struct GenericNodeData // Generic used in code. Ex: foo.bar!baz { - AstNode left; - AstNode right; + AstNode symbolUsedAsGeneric; + AstNode genericData; } class AstNode @@ -229,23 +223,21 @@ class AstNode AstNode nodeToReturn; // ReturnStatement IndexIntoNodeData indexIntoNodeData; // IndexInto - struct - { // TypeArray - AstNode firstNodeOperand; // This might be the thing being indexed - bool isIntegerLiteral; - AstNode[][] commaSeperatedNodes; // Declaring arrays, array types, typles, etc - } + GenericNodeData genericNodeData; // GenericOf - TypeGenericNodeData typeGenericNodeData; // TypeGeneric AstNode voidableType; } - static AstNode VOID_NAMED_UNIT(){ + + static AstNode VOID_NAMED_UNIT() + { AstNode voidNamedUnit = new AstNode(); voidNamedUnit.action = AstAction.NamedUnit; import fnc.tokenizer.tokens : makeUnicodeString; + voidNamedUnit.namedUnit = NamedUnit(["void".makeUnicodeString]); return voidNamedUnit; } + void toString(scope void delegate(const(char)[]) sink) const { import std.conv; @@ -260,13 +252,9 @@ class AstNode case AstAction.TokenHolder: sink(tokenBeingHeld.to!string); break; - case AstAction.TypePointer: case AstAction.Expression: sink(expressionNodeData.components.to!string); break; - case AstAction.TypeVoidable: - sink(voidableType.to!string); - break; case AstAction.NamedUnit: sink(namedUnit.names.to!string); break; @@ -285,27 +273,6 @@ class AstNode sink(doubleArgumentOperationNodeData.left.to!string); sink(", "); sink(doubleArgumentOperationNodeData.right.to!string); - break; - case AstAction.TypeArray: - bool hasFirstOperand = (cast(void*) firstNodeOperand) != null; - if (hasFirstOperand) - { - sink("Array of: "); - sink(firstNodeOperand.to!string); - sink(" "); - } - if (isIntegerLiteral) - { - sink("with "); - sink(commaSeperatedNodes[0][0].to!string); - sink(" elements"); - } - else - foreach (const(AstNode[]) containingReductions; commaSeperatedNodes) - { - sink(commaSeperatedNodes.to!string); - } - break; default: break; @@ -332,46 +299,13 @@ class AstNode switch (action) { - case AstAction.TypeGeneric: + case AstAction.GenericOf: write(action); writeln(":"); - typeGenericNodeData.left.tree(tabCount + 1); - typeGenericNodeData.right.tree(tabCount + 1); - break; - case AstAction.TypePointer: - case AstAction.TypeReference: - write(action); - writeln(":"); - foreach (subnode; expressionNodeData.components) - { - subnode.tree(tabCount + 1); - } - break; - case AstAction.TypeArray: - bool hasFirstOperand = (cast(void*) firstNodeOperand) != null; - if (hasFirstOperand && commaSeperatedNodes.length) - writeln("List of N indexed with X"); - else - writeln("List of X"); - if (firstNodeOperand) - firstNodeOperand.tree(tabCount + 1); - foreach (AstNode[] possibleReducedNodes; commaSeperatedNodes) - { - if (possibleReducedNodes.length > 0) - possibleReducedNodes[0].tree(tabCount + 1); - - } - break; - case AstAction.TypeTuple: - write(action); - writeln(":"); - foreach (AstNode[] possibleReducedNodes; commaSeperatedNodes) - { - if (possibleReducedNodes.length > 0) - possibleReducedNodes[0].tree(tabCount + 1); - - } + genericNodeData.symbolUsedAsGeneric.tree(tabCount + 1); + genericNodeData.genericData.tree(tabCount + 1); break; + case AstAction.Call: writeln("Calling function resolved from:"); callNodeData.func.tree(tabCount + 1); @@ -485,10 +419,6 @@ void getRelatedTokens(AstNode node, ref Token[] output) switch (node.action) { // TODO: Improve all of this - case AstAction.TypePointer: - case AstAction.TypeReference: - getRelatedTokens(node.firstNodeOperand, output); - break; case AstAction.SingleArgumentOperation: getRelatedTokens(node.singleArgumentOperationNodeData.value, output); break; diff --git a/source/fnc/treegen/expression_parser.d b/source/fnc/treegen/expression_parser.d index b564089..a57b794 100644 --- a/source/fnc/treegen/expression_parser.d +++ b/source/fnc/treegen/expression_parser.d @@ -107,16 +107,13 @@ private AstNode[][] splitNodesAtComma(AstNode[] inputNodes) nodes ~= current; return nodes; } -// Handle function calls and operators +// Handle function calls, arrays, Generics, and operators public void phaseTwo(ref Array!AstNode nodes) { size_t[] nonWhiteIndexStack; Array!AstNode newNodesArray; - scope (exit) - nodes = newNodesArray; - AstNode lastNonWhite; alias popNonWhiteNode() = { size_t lindex = nonWhiteIndexStack[$ - 1]; @@ -125,7 +122,40 @@ public void phaseTwo(ref Array!AstNode nodes) lastNonWhite = newNodesArray[lindex]; newNodesArray.linearRemove(newNodesArray[lindex .. $]); }; - + scanAndMergeAttrOp(nodes); + // TODO: FIX THIS SHIT + for (size_t index = 0; index < nodes.length; index++) + { + AstNode node = nodes[index]; + if (node.action != AstAction.TokenHolder + || node.tokenBeingHeld.tokenVariety != TokenType.ExclamationMark) + { + GENRIC_ADD: + newNodesArray ~= node; + if (!node.isWhite) + nonWhiteIndexStack ~= newNodesArray.length - 1; + continue; + } + if (!nonWhiteIndexStack.length) + throw new SyntaxError("Can't result thing generic of.", node.tokenBeingHeld); + popNonWhiteNode(); + index++; + Nullable!AstNode maybeNode = nodes.nextNonWhiteNode(index); + index--; + if (maybeNode == null) + throw new SyntaxError("Trailing exclamation mark is an invalid generic.", node.tokenBeingHeld); + AstNode generic = new AstNode; + generic.action = AstAction.GenericOf; + generic.genericNodeData.symbolUsedAsGeneric = lastNonWhite; + generic.genericNodeData.genericData = maybeNode; + node = generic; + goto GENRIC_ADD; + } + nodes = newNodesArray; + Array!AstNode temp; + newNodesArray = temp; + nonWhiteIndexStack = new size_t[0]; + // Handle functions, arrays, and indexing for (size_t index = 0; index < nodes.length; index++) { AstNode node = nodes[index]; @@ -134,6 +164,7 @@ public void phaseTwo(ref Array!AstNode nodes) && newNodesArray[nonWhiteIndexStack[$ - 1]].action.isCallable ) { + popNonWhiteNode(); AstNode functionCall = new AstNode(); functionCall.action = AstAction.Call; @@ -171,7 +202,7 @@ public void phaseTwo(ref Array!AstNode nodes) component.source = components[0]; continue; } - components.writeln; + // components.writeln; if (components[1].action != AstAction.TokenHolder) throw new SyntaxError("Function argument parsing error (Must include colon for named arguments)", firstToken); if (components[1].tokenBeingHeld.tokenVariety != TokenType.Colon) @@ -195,9 +226,6 @@ public void phaseTwo(ref Array!AstNode nodes) indexNode.action = AstAction.IndexInto; indexNode.indexIntoNodeData.indexInto = lastNonWhite; - newNodesArray ~= indexNode; - nonWhiteIndexStack ~= newNodesArray.length - 1; - Array!AstNode components; components ~= node.expressionNodeData.components; phaseTwo(components); @@ -208,6 +236,9 @@ public void phaseTwo(ref Array!AstNode nodes) indexNode.indexIntoNodeData.index = components[0]; + newNodesArray ~= indexNode; + nonWhiteIndexStack ~= newNodesArray.length - 1; + } else if (node.action.isExpressionLike) { @@ -228,28 +259,8 @@ public void phaseTwo(ref Array!AstNode nodes) nonWhiteIndexStack ~= newNodesArray.length - 1; } - - // if (node.action == AstAction.Expression && lastNonWhite != null ) - // { - // AstNode functionCall = new AstNode(); - // AstNode args = nodes[index + 1]; - - // Array!AstNode components; - // components ~= args.expressionNodeData.components; - // phaseTwo(components); - // scanAndMergeOperators(components); - // args.expressionNodeData.components.length = components.data.length; - // args.expressionNodeData.components[0 .. $] = components.data[0 .. $]; - - // functionCall.action = AstAction.Call; - // functionCall.callNodeData = CallNodeData( - // node.namedUnit, - // args - // ); - // nodes[index] = functionCall; - // nodes.linearRemove(nodes[index + 1 .. index + 2]); - // } } + nodes = newNodesArray; } void trimAstNodes(ref Array!AstNode nodes) @@ -302,3 +313,54 @@ size_t findNearestSemiColon(Token[] tokens, size_t index, TokenType stopToken = } return -1; } + +// Gets the length of a single "group" of tokens, that can be parsed into a single ASTnode +size_t prematureSingleTokenGroupLength(Token[] tokens, size_t index) +{ + size_t originalIndex = index; + int braceCount = 0; + bool wasLastFinalToken = false; + while (1) + { + Nullable!Token ntoken = tokens.nextNonWhiteToken(index); + if (ntoken == null) + break; + Token token = ntoken; + if (token.tokenVariety == TokenType.OpenBraces) + { + wasLastFinalToken = true; + braceCount++; + continue; + } + else if (token.tokenVariety == TokenType.CloseBraces) + { + braceCount--; + if (braceCount == -1) + break; + continue; + } + + if (braceCount > 0) + continue; + + switch (token.tokenVariety) + { + case TokenType.QuestionMark: + case TokenType.Comment: + case TokenType.WhiteSpace: + break; + case TokenType.Operator: + case TokenType.Period: + case TokenType.ExclamationMark: + wasLastFinalToken = false; + break; + default: + if (wasLastFinalToken) + return index - originalIndex - 1; + wasLastFinalToken = true; + break; + } + + } + return index - originalIndex - 1; +} \ No newline at end of file diff --git a/source/fnc/treegen/package.d b/source/fnc/treegen/package.d index 4026007..cafae75 100644 --- a/source/fnc/treegen/package.d +++ b/source/fnc/treegen/package.d @@ -6,4 +6,3 @@ public import fnc.treegen.keywords; public import fnc.treegen.scope_parser; public import fnc.treegen.relationships; public import fnc.treegen.utils; -public import fnc.treegen.type_parser; diff --git a/source/fnc/treegen/relationships.d b/source/fnc/treegen/relationships.d index c98eeeb..d071753 100644 --- a/source/fnc/treegen/relationships.d +++ b/source/fnc/treegen/relationships.d @@ -3,7 +3,7 @@ module fnc.treegen.relationships; import fnc.tokenizer.tokens; import fnc.treegen.ast_types; import fnc.treegen.utils; -import fnc.treegen.type_parser; + import tern.typecons.common : Nullable, nullable; /+ @@ -286,8 +286,8 @@ const int OBJECT_NAME = 0; const int OBJECT_BODY = 1; const StructDeclaration = [ TokenGrepPacketToken(TokenGrepMethod.MatchesTokens, [ - Token(TokenType.Letter, "struct".makeUnicodeString) - ]), + Token(TokenType.Letter, "struct".makeUnicodeString) + ]), TokenGrepPacketToken(TokenGrepMethod.NamedUnit, []), TokenGrepPacketToken(TokenGrepMethod.MatchesTokens, [ Token(TokenType.OpenBraces, ['{']) @@ -299,8 +299,8 @@ const StructDeclaration = [ ]; const ClassDeclaration = [ TokenGrepPacketToken(TokenGrepMethod.MatchesTokens, [ - Token(TokenType.Letter, "class".makeUnicodeString) - ]), + Token(TokenType.Letter, "class".makeUnicodeString) + ]), TokenGrepPacketToken(TokenGrepMethod.NamedUnit, []), TokenGrepPacketToken(TokenGrepMethod.MatchesTokens, [ Token(TokenType.OpenBraces, ['{']) @@ -313,8 +313,8 @@ const ClassDeclaration = [ const TaggedDeclaration = [ TokenGrepPacketToken(TokenGrepMethod.MatchesTokens, [ - Token(TokenType.Letter, "tagged".makeUnicodeString) - ]), + Token(TokenType.Letter, "tagged".makeUnicodeString) + ]), TokenGrepPacketToken(TokenGrepMethod.NamedUnit, []), TokenGrepPacketToken(TokenGrepMethod.MatchesTokens, [ Token(TokenType.OpenBraces, ['{']) @@ -397,15 +397,13 @@ const VarietyTestPair[] OBJECT_DEFINITION_PARSE = [ ]; const VarietyTestPair[] TAGGED_DEFINITION_PARS = OBJECT_DEFINITION_PARSE ~ [ VarietyTestPair(LineVariety.TaggedUntypedItem, [ - TokenGrepPacketToken(TokenGrepMethod.NamedUnit, []), - TokenGrepPacketToken(TokenGrepMethod.MatchesTokenType, [ - Token(TokenType.Semicolon, []) + TokenGrepPacketToken(TokenGrepMethod.NamedUnit, []), + TokenGrepPacketToken(TokenGrepMethod.MatchesTokenType, [ + Token(TokenType.Semicolon, []) + ]) ]) - ]) ]; - - Nullable!(TokenGrepResult[]) matchesToken(in TokenGrepPacket[] testWith, Token[] tokens) { size_t index = 0; @@ -499,15 +497,13 @@ Nullable!(TokenGrepResult[]) matchesToken(in TokenGrepPacket[] testWith, Token[] index = commaSearchIndex; break; case TokenGrepMethod.Type: - size_t potentialSize = prematureTypeLength(tokens, index); + import fnc.treegen.expression_parser : prematureSingleTokenGroupLength; + + size_t potentialSize = prematureSingleTokenGroupLength(tokens, index); if (!potentialSize) return tokenGrepBox(null); - size_t temp = index; - Nullable!AstNode maybeNull = typeFromTokens(tokens, temp); - if (maybeNull == null) - return tokenGrepBox(null); - AstNode type = maybeNull; + AstNode type = new AstNode; TokenGrepResult tokenGrep; tokenGrep.method = TokenGrepMethod.Type; tokenGrep.type = type; @@ -626,6 +622,11 @@ const OperatorPrecedenceLayer[] operatorPrecedence = [ Token(TokenType.Filler), Token(TokenType.Period, ['.']), Token(TokenType.Filler) ]), + OperationPrecedenceEntry(OperationVariety.Arrow, [ + Token(TokenType.Filler), Token(TokenType.Operator, ['-']), + Token(TokenType.Operator, ['>']), + Token(TokenType.Filler) + ]), ]), OperatorPrecedenceLayer(OperatorOrder.LeftToRight, [ OperationPrecedenceEntry(OperationVariety.PreIncrement, [ @@ -888,10 +889,25 @@ trim: return true; } +void scanAndMergeAttrOp(Array!AstNode nodes) +{ + for (size_t index = 0; index < nodes.length; index++) + { + foreach (entry; operatorPrecedence[0].layer) + { + if (entry.testAndJoin(nodes, index)) + { + index--; + continue; + } + + } + } +} + void scanAndMergeOperators(Array!AstNode nodes) { // OperatorOrder order; - auto data = nodes.data; static foreach (layer; operatorPrecedence) { static if (layer.order == OperatorOrder.LeftToRight) @@ -907,7 +923,6 @@ void scanAndMergeOperators(Array!AstNode nodes) } } - } } static if (layer.order == OperatorOrder.RightToLeft) diff --git a/source/fnc/treegen/scope_parser.d b/source/fnc/treegen/scope_parser.d index 20ebc2e..5180aa3 100644 --- a/source/fnc/treegen/scope_parser.d +++ b/source/fnc/treegen/scope_parser.d @@ -307,9 +307,7 @@ LineVarietyTestResult parseLine(const(VarietyTestPair[]) scopeParseMethod, Token auto nodes = lineVariety.tokenMatches[DECLARATION_EXPRESSION] .assertAs(TokenGrepMethod.Glob) .tokens.expressionNodeFromTokens(); - lineVariety.tokenMatches[DECLARATION_EXPRESSION] - .assertAs(TokenGrepMethod.Glob) - .tokens.writeln; + if (nodes.length != 1) throw new SyntaxError( "Expression node tree could not be parsed properly (Not reducable into single node)", diff --git a/source/fnc/treegen/type_parser.d b/source/fnc/treegen/type_parser.d deleted file mode 100644 index 9a66881..0000000 --- a/source/fnc/treegen/type_parser.d +++ /dev/null @@ -1,216 +0,0 @@ -module fnc.treegen.type_parser; - -import fnc.tokenizer.tokens; -import fnc.treegen.ast_types; - -import tern.typecons.common : Nullable, nullable; -import fnc.errors; - -import std.array; -import std.stdio; - -AstNode[][] splitNodesAtCommas(AstNode[] protoNodes) -{ - AstNode[][] ret; - AstNode[] current; - foreach (AstNode node; protoNodes) - { - if (node.action == AstAction.TokenHolder && node.tokenBeingHeld.tokenVariety == TokenType - .Comma) - { - ret ~= current; - current = new AstNode[0]; - continue; - } - current ~= node; - } - ret ~= current; - return ret; -} - -import std.container.array; - -private Nullable!AstNode handleNodeTreegen(AstNode node, ref AstNode[] previouslyParsedNodes, AstNode[] protoNodes, ref size_t index) -{ - switch (node.action) - { - case AstAction.Expression: - case AstAction.ArrayGrouping: - AstNode newNode = new AstNode; - newNode.action = node.action == AstAction.Expression ? AstAction.TypeTuple - : AstAction.TypeArray; - newNode.commaSeperatedNodes = new AstNode[][0]; - newNode.firstNodeOperand = null; - newNode.isIntegerLiteral = false; - foreach (i, subArray; splitNodesAtCommas(node.expressionNodeData.components)) - { - newNode.commaSeperatedNodes ~= genTypeTree(subArray); - if (i != 0) - newNode.isIntegerLiteral = false; - else if (i == 0 && subArray.length && subArray[0].action == AstAction.LiteralUnit) - newNode.isIntegerLiteral = true; - } - return nullable!AstNode(newNode); - case AstAction.TokenHolder: - if (node.tokenBeingHeld.tokenVariety == TokenType.WhiteSpace) - return nullable!AstNode(null); - switch (node.tokenBeingHeld.tokenVariety) - { - case TokenType.QuestionMark: - if (previouslyParsedNodes.length == 0) - throw new SyntaxError( - "Can't determine voidable and it's connection to a type", node); - - AstNode newNode = new AstNode; - newNode.action = AstAction.TypeVoidable; - newNode.voidableType = previouslyParsedNodes[$ - 1]; - previouslyParsedNodes[$ - 1] = newNode; - return nullable!AstNode(null); - - case TokenType.ExclamationMark: - if (previouslyParsedNodes.length == 0) - throw new SyntaxError( - "Can't determine template and it's connection to a type", node); - AstNode newNode = new AstNode; - newNode.action = AstAction.TypeGeneric; - AstNode[] followingNodes = genTypeTree(protoNodes[index + 1 .. $]); - if (followingNodes.length == 0) - throw new SyntaxError( - "Generic has no nodes", node); - newNode.typeGenericNodeData = TypeGenericNodeData( - previouslyParsedNodes[$ - 1], - followingNodes[0] - ); - - index = protoNodes.length; - previouslyParsedNodes[$ - 1] = newNode; - - if (followingNodes.length != 1) - previouslyParsedNodes ~= followingNodes[1 .. $]; - - return nullable!AstNode(null); - case TokenType.Operator: - AstNode newNode = new AstNode; - AstNode[] followingNodes = genTypeTree(protoNodes[index + 1 .. $]); - - newNode.expressionNodeData = ExpressionNodeData(node.tokenBeingHeld.value[0], 0, followingNodes); - if (newNode.expressionNodeData.opener == '*') - newNode.action = AstAction.TypePointer; - else if (newNode.expressionNodeData.opener == '&') - newNode.action = AstAction.TypeReference; - else - return nullable!AstNode(null); - // throw new SyntaxError( - // "Unknown type operator", node.tokenBeingHeld); - - index = protoNodes.length; - return nullable!AstNode(newNode); - case TokenType.Semicolon: - return nullable!AstNode(null); - default: - node.tokenBeingHeld.writeln; - assert(0); - } - node.tokenBeingHeld.writeln; - return nullable!AstNode(null); - - case AstAction.LiteralUnit: - case AstAction.NamedUnit: - return nullable!AstNode(node); - default: - node.action.writeln; - assert(0); - } -} - -Nullable!(AstNode[]) genTypeTree(AstNode[] protoNodes) -{ - AstNode[] nodes; - for (size_t index = 0; index < protoNodes.length; index++) - { - Nullable!AstNode maybeNode = handleNodeTreegen(protoNodes[index], nodes, protoNodes, index); - if (maybeNode == null) - continue; - AstNode node = maybeNode; - if (node.action == AstAction.TypeArray && nodes.length) - { - node.firstNodeOperand = nodes[$ - 1]; - nodes[$ - 1] = node; - continue; - } - nodes ~= node; - } - return nullable!(AstNode[])(nodes); -} - -size_t prematureTypeLength(Token[] tokens, size_t index) -{ - size_t originalIndex = index; - int braceCount = 0; - bool wasLastFinalToken = false; - while (1) - { - Nullable!Token ntoken = tokens.nextNonWhiteToken(index); - if (ntoken == null) - break; - Token token = ntoken; - if (token.tokenVariety == TokenType.OpenBraces) - { - wasLastFinalToken = true; - braceCount++; - continue; - } - else if (token.tokenVariety == TokenType.CloseBraces) - { - braceCount--; - if (braceCount == -1) - break; - continue; - } - - if (braceCount > 0) - continue; - - switch (token.tokenVariety) - { - case TokenType.QuestionMark: - case TokenType.Operator: - case TokenType.Comment: - case TokenType.WhiteSpace: - break; - case TokenType.Period: - case TokenType.ExclamationMark: - wasLastFinalToken = false; - break; - default: - if (wasLastFinalToken) - return index - originalIndex - 1; - wasLastFinalToken = true; - break; - } - - } - return index - originalIndex - 1; -} - -Nullable!AstNode typeFromTokens(Token[] tokens, ref size_t index) -{ - import fnc.treegen.expression_parser : phaseOne; - - size_t length = tokens.prematureTypeLength(index); - if (length == 0) - return nullable!AstNode(null); - - // Groups parenthesis and brackets into expression groups - AstNode[] protoNodes = phaseOne(tokens[index .. index += length]); - Nullable!(AstNode[]) maybeArray = genTypeTree(protoNodes); - if (maybeArray == null) - return nullable!AstNode(null); - AstNode[] array = maybeArray; - if (array.length == 0) - return nullable!AstNode(null); - if (array.length != 1) - return nullable!AstNode(null); - - return nullable!AstNode(array[0]); -} diff --git a/source/main.d b/source/main.d index 0036e48..745a775 100644 --- a/source/main.d +++ b/source/main.d @@ -5,7 +5,6 @@ import fnc.tokenizer.make_tokens; import tern.typecons.common : Nullable, nullable; import fnc.treegen.scope_parser; import fnc.treegen.relationships; -import fnc.treegen.type_parser; import std.stdio; @@ -61,19 +60,12 @@ void main() auto newScope = parseMultilineScope(GLOBAL_SCOPE_PARSE, " - private tagged Element{ - kind; - idk; - int anElmentWithNumber; - } - int main(){ - const Element e = Element.idk; - } + int x = 4; "); newScope.tree; - // DeclarationAndAssignment.matchesToken("y=4".tokenizeText).ptr.writeln; - // parseLine(FUNCTION_SCOPE_PARSE, "y=4".tokenizeText, index).writeln; + // DeclarationAndAssignment.matchesToken("int+1 y=4;".tokenizeText).ptr.writeln; + // parseLine(FUNCTION_SCOPE_PARSE, "y=4;".tokenizeText, index).writeln;