Skip to content

Commit

Permalink
scopeParser to string
Browse files Browse the repository at this point in the history
  • Loading branch information
PsychedelicPalimpsest committed Apr 25, 2024
1 parent 3cee025 commit 0f3f043
Showing 1 changed file with 44 additions and 16 deletions.
60 changes: 44 additions & 16 deletions source/parsing/treegen/scopeParser.d
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ struct ImportStatement
NameUnit nameUnit;
NameUnit[] importSelection; // empty for importing everything
}

struct DeclaredFunction
{
dchar[][] precedingKeywords;
Expand All @@ -25,6 +26,7 @@ struct DeclaredFunction
// TODO: Args
ScopeData functionScope;
}

struct DeclaredVariable
{
NameUnit name;
Expand All @@ -42,6 +44,27 @@ class ScopeData
DeclaredFunction[] declaredFunctions;
DeclaredVariable[] declaredVariables;
Array!AstNode instructions;

void toString(scope void delegate(const(char)[]) sink) const {
import std.conv;
sink("ScopeData{isPartialModule = ");
sink(isPartialModule.to!string);
sink(", moduleName = ");
if (moduleName == null)
sink("null");
else
sink(moduleName.value.to!string);
sink(", imports = ");
sink(imports.to!string);
sink(", declaredVariables = ");
sink(declaredVariables.to!string);

sink(", declaredFunctions = ");
sink(declaredFunctions.to!string);
sink(", instructions = ");
sink(instructions.to!string);
sink("}");
}
}

struct LineVarietyTestResult
Expand All @@ -59,7 +82,8 @@ LineVarietyTestResult getLineVarietyTestResult(
foreach (method; scopeParseMethod)
{
Nullable!(TokenGrepResult[]) grepResults = method.test.matchesToken(tokens, temp_index);
if (null != grepResults){
if (null != grepResults)
{
return LineVarietyTestResult(
method.variety, temp_index - index, grepResults.value
);
Expand Down Expand Up @@ -129,8 +153,9 @@ LineVarietyTestResult parseLine(const(VarietyTestPair[]) scopeParseMethod, Token

auto statement = ImportStatement(
keywords,
lineVariety.tokenMatches[IMPORT_PACKAGE_NAME].assertAs(TokenGrepMethod.NameUnit).name,
[]
lineVariety.tokenMatches[IMPORT_PACKAGE_NAME].assertAs(TokenGrepMethod.NameUnit)
.name,
[]
);

statement.importSelection ~= lineVariety
Expand All @@ -147,10 +172,11 @@ LineVarietyTestResult parseLine(const(VarietyTestPair[]) scopeParseMethod, Token
scope (exit)
index = endingIndex;

NameUnit declarationType = lineVariety.tokenMatches[DECLARATION_TYPE].assertAs(TokenGrepMethod.NameUnit).name;
NameUnit declarationType = lineVariety.tokenMatches[DECLARATION_TYPE].assertAs(
TokenGrepMethod.NameUnit).name;
NameUnit[] declarationNames = lineVariety.tokenMatches[DECLARATION_VARS]
.assertAs(TokenGrepMethod.PossibleCommaSeperated)
.commaSeperated.collectNameUnits();
.assertAs(TokenGrepMethod.PossibleCommaSeperated)
.commaSeperated.collectNameUnits();
AstNode[] nameNodes;
foreach (NameUnit name; declarationNames)
{
Expand All @@ -165,8 +191,8 @@ LineVarietyTestResult parseLine(const(VarietyTestPair[]) scopeParseMethod, Token
break;

auto nodes = lineVariety.tokenMatches[DECLARATION_EXPRESSION]
.assertAs(TokenGrepMethod.Glob)
.tokens.expressionNodeFromTokens();
.assertAs(TokenGrepMethod.Glob)
.tokens.expressionNodeFromTokens();

if (nodes.length != 1)
throw new SyntaxError(
Expand All @@ -188,13 +214,16 @@ LineVarietyTestResult parseLine(const(VarietyTestPair[]) scopeParseMethod, Token
parent.declaredFunctions ~= DeclaredFunction(
keywords,
[],
lineVariety.tokenMatches[FUNCTION_NAME].assertAs(TokenGrepMethod.NameUnit).name,
lineVariety.tokenMatches[FUNCTION_RETURN_TYPE].assertAs(TokenGrepMethod.NameUnit).name,
parseMultilineScope(
FUNCTION_SCOPE_PARSE,
lineVariety.tokenMatches[FUNCTION_SCOPE].assertAs(TokenGrepMethod.Glob).tokens,
temp,
nullable!ScopeData(parent)
lineVariety.tokenMatches[FUNCTION_NAME].assertAs(TokenGrepMethod.NameUnit)
.name,
lineVariety.tokenMatches[FUNCTION_RETURN_TYPE].assertAs(TokenGrepMethod.NameUnit)
.name,
parseMultilineScope(
FUNCTION_SCOPE_PARSE,
lineVariety.tokenMatches[FUNCTION_SCOPE].assertAs(TokenGrepMethod.Glob)
.tokens,
temp,
nullable!ScopeData(parent)
)
);

Expand Down Expand Up @@ -305,4 +334,3 @@ unittest
"floaty".makeUnicodeString
]);
}

0 comments on commit 0f3f043

Please sign in to comment.