From 0d4a6c820c08f0034c4a07f41a26edcfe7ea5e3f Mon Sep 17 00:00:00 2001 From: HeronErin Date: Tue, 7 May 2024 11:40:12 -0400 Subject: [PATCH] Add voidable to parser --- source/main.d | 24 ++++++++---------------- source/parsing/tokenizer/tokens.d | 3 +++ source/parsing/treegen/astTypes.d | 5 +++++ source/parsing/treegen/typeParser.d | 16 ++++++++++++++-- 4 files changed, 30 insertions(+), 18 deletions(-) diff --git a/source/main.d b/source/main.d index 5b87721..b37ee3f 100644 --- a/source/main.d +++ b/source/main.d @@ -58,21 +58,13 @@ void main() { size_t index = 0; // typeFromTokens("".tokenizeText, index); - auto toks = "if ((2..5).contains(4)) 3; else 7;".tokenizeText; - auto x = IfStatementWithoutScope.matchesToken(toks, index); - // toks.writeln; - (x != null).writeln; - x.value[0].tokens.writeln; - toks[index].writeln; - // auto newScope = parseMultilineScope(GLOBAL_SCOPE_PARSE, " - // import std.file; - // int main() - // { - // print(\"Hello World!\"); - // __asm( - // ld a, b - // ); - // }"); - // newScope.tree(); + auto newScope = parseMultilineScope(GLOBAL_SCOPE_PARSE, " + import std.os; + int main() + { + int? x = 2; + writeln(x); + }"); + newScope.tree(); } diff --git a/source/parsing/tokenizer/tokens.d b/source/parsing/tokenizer/tokens.d index a3c5c8a..328d758 100644 --- a/source/parsing/tokenizer/tokens.d +++ b/source/parsing/tokenizer/tokens.d @@ -21,6 +21,7 @@ enum TokenType Quotation, Comment, ExclamationMark, + QuestionMark, Period, Filler } @@ -120,6 +121,8 @@ TokenType getVarietyOfLetter(dchar symbol) return TokenType.Comma; case '!': return TokenType.ExclamationMark; + case '?': + return TokenType.QuestionMark; default: break; } diff --git a/source/parsing/treegen/astTypes.d b/source/parsing/treegen/astTypes.d index 5e1270f..bfc5c77 100644 --- a/source/parsing/treegen/astTypes.d +++ b/source/parsing/treegen/astTypes.d @@ -44,6 +44,7 @@ enum AstAction TypePointer, // *int TypeReference, // &int TypeGeneric, // Result!(int, string) + TypeVoidable } @@ -211,6 +212,7 @@ class AstNode } TypeGenericNodeData typeGenericNodeData; // TypeGeneric + AstNode voidableType; } void toString(scope void delegate(const(char)[]) sink) const @@ -230,6 +232,9 @@ class AstNode 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; diff --git a/source/parsing/treegen/typeParser.d b/source/parsing/treegen/typeParser.d index 3dce3af..2b4b067 100644 --- a/source/parsing/treegen/typeParser.d +++ b/source/parsing/treegen/typeParser.d @@ -56,10 +56,22 @@ private Nullable!AstNode handleNodeTreegen(AstNode node, ref AstNode[] previousl 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 + .tokenBeingHeld); + + 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 result template and it's connection to a type", node + "Can't determine template and it's connection to a type", node .tokenBeingHeld); AstNode newNode = new AstNode; newNode.action = AstAction.TypeGeneric; @@ -168,6 +180,7 @@ size_t prematureTypeLength(Token[] tokens, size_t index) switch (token.tokenVariety) { + case TokenType.QuestionMark: case TokenType.Operator: case TokenType.Comment: case TokenType.WhiteSpace: @@ -196,7 +209,6 @@ Nullable!AstNode typeFromTokens(Token[] tokens, ref size_t index) return nullable!AstNode(null); // Groups parenthesis and brackets into expression groups - Token firstToken = tokens[index]; AstNode[] protoNodes = phaseOne(tokens[index .. index += length]); Nullable!(AstNode[]) maybeArray = genTypeTree(protoNodes); if (maybeArray == null)