Skip to content

Commit

Permalink
Started if statement parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
jmeaster30 committed Jun 8, 2023
1 parent cba5b75 commit bd2142c
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 8 deletions.
47 changes: 39 additions & 8 deletions BlastPDF.Template/AST.cs
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ public bool Is<T>()

public IEnumerable<Diagnostic> GetErrors(string filepath)
{
return new List<Diagnostic>();
return Contents.GetErrors(filepath);
}
}

Expand All @@ -462,7 +462,7 @@ public bool Is<T>()

public IEnumerable<Diagnostic> GetErrors(string filepath)
{
return new List<Diagnostic>();
return Contents.GetErrors(filepath);
}
}

Expand All @@ -479,7 +479,7 @@ public bool Is<T>()

public IEnumerable<Diagnostic> GetErrors(string filepath)
{
return new List<Diagnostic>();
return Contents.GetErrors(filepath);
}
}

Expand Down Expand Up @@ -517,15 +517,46 @@ public IEnumerable<Diagnostic> GetErrors(string filepath)
}
}

/*public class BranchNode : IContentNode, IPageNode, IDocumentNode
public class BranchContentNode : IContentNode
{
public Token IfToken { get; set; } = default!;
public IExpressionNode Expression { get; set; } = default!;
public Token ThenToken { get; set; } = default!;
public List<IContentNode> TrueContents { get; set; } = default!;
public Token? ElseToken { get; set; } = default!;
public List<IContentNode> FalseContents { get; set; } = default!;
public Token EndToken { get; set; } = default!;
public bool Is<T>()
{
return typeof(T) == typeof(BranchContentNode);
}

public IEnumerable<Diagnostic> GetErrors(string filepath)
{
return Expression.GetErrors(filepath).Concat(TrueContents.GetErrors(filepath))
.Concat(FalseContents.GetErrors(filepath));
}
}

public class LoopNode : IContentNode, IPageNode, IDocumentNode
public class LoopContentNode : IContentNode
{
}*/
public Token LoopToken { get; set; } = default!;
public Token? Identifier { get; set; } = default!;
public Token? InToken { get; set; } = default!;
public IExpressionNode Expression { get; set; } = default!;
public Token ThenToken { get; set; } = default!;
public List<IContentNode> Body { get; set; } = default!;
public Token EndToken { get; set; } = default!;
public bool Is<T>()
{
return typeof(T) == typeof(LoopContentNode);
}

public IEnumerable<Diagnostic> GetErrors(string filepath)
{
return Expression.GetErrors(filepath).Concat(Body.GetErrors(filepath));
}
}

public static class AstExtensions
{
Expand Down
95 changes: 95 additions & 0 deletions BlastPDF.Template/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -958,6 +958,7 @@ private static (List<IContentNode>, int) ParseContentNodes(IReadOnlyList<Token>
var (node, idx) = tokens[tokenIndex].Type switch
{
TokenType.Text => ParseTextNode(tokens, tokenIndex),
TokenType.If => ParseIfContentStatement(tokens, tokenIndex),
_ => (default!, -1)
};

Expand Down Expand Up @@ -1049,4 +1050,98 @@ private static (IContentNode, int) ParseTextOutput(IReadOnlyList<Token> tokens,
Expression = expr
}, tokenIndex + 1);
}

private static (IContentNode, int) ParseIfContentStatement(IReadOnlyList<Token> tokens, int tokenIndex)
{
var ifToken = tokens[tokenIndex];
tokenIndex += 1;
if (tokenIndex >= tokens.Count)
{
return (new ContentError
{
ErroredTokens = new List<Token> { ifToken },
Message = "Hit end of document while parsing an if statement node",
Severity = DiagnosticSeverity.Error,
}, tokenIndex);
}

var conditionToken = tokens[tokenIndex];
if (conditionToken.Type != TokenType.EmbeddedExpression)
{
var (errorTokens, nextIndex) = ConsumeUntilNextTokenType(tokens, tokenIndex, IsContentNodeToken);
return (new ContentError
{
ErroredTokens = errorTokens,
Message = $"Expected an expression but got a ({conditionToken.Type}, '{conditionToken.Lexeme}').",
Severity = DiagnosticSeverity.Error,
}, nextIndex);
}
tokenIndex += 1;
if (tokenIndex >= tokens.Count)
{
return (new ContentError
{
ErroredTokens = new List<Token> { ifToken, conditionToken },
Message = "Hit end of document while parsing an if statement node",
Severity = DiagnosticSeverity.Error,
}, tokenIndex);
}

var thenToken = tokens[tokenIndex];
if (thenToken.Type != TokenType.Then)
{
var (errorTokens, nextIndex) = ConsumeUntilNextTokenType(tokens, tokenIndex, IsContentNodeToken);
return (new ContentError
{
ErroredTokens = errorTokens,
Message = $"Expected 'then' but got a ({thenToken.Type}, '{thenToken.Lexeme}').",
Severity = DiagnosticSeverity.Error,
}, nextIndex);
}
tokenIndex += 1;
if (tokenIndex >= tokens.Count)
{
return (new ContentError
{
ErroredTokens = new List<Token> { ifToken, conditionToken, thenToken },
Message = "Hit end of document while parsing an if statement node",
Severity = DiagnosticSeverity.Error,
}, tokenIndex);
}

var (contents, idx) = ParseContentNodes(tokens, tokenIndex);
tokenIndex = idx;
if (tokenIndex >= tokens.Count)
{
return (new ContentError
{
ErroredTokens = new List<Token> { ifToken, conditionToken, thenToken },
Message = "Hit end of document while parsing an if statement node",
Severity = DiagnosticSeverity.Error,
}, tokenIndex);
}

var endToken = tokens[tokenIndex];
if (endToken.Type == TokenType.End)
{
return (new BranchContentNode
{
IfToken = ifToken,
Expression = new ExpressionValue
{
Value = conditionToken
},
ThenToken = thenToken,
TrueContents = contents,
EndToken = endToken,
}, tokenIndex + 1);
}

return (new ContentError
{
ErroredTokens = new List<Token> { endToken },
Message = "AAAAAAAAAAAAAAAAA",
Severity = DiagnosticSeverity.Error,
}, tokenIndex);
}
}
4 changes: 4 additions & 0 deletions BlastPDF/BlastPDF.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,9 @@
<ProjectReference Include="..\BlastIMG\BlastIMG.csproj" />
<ProjectReference Include="..\BlastSharp\BlastSharp.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="LayoutEngine\" />
</ItemGroup>

</Project>
4 changes: 4 additions & 0 deletions ShowCase/templates/TestLayout.bxpdf
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ page single
text "The goal of this message is to be a long message that should wrap. I would like to make it so you can write paragraphs as long as you want and the lines will automatically wrap. Perhaps that should be configurable with a command. It probably should be on by default as well. I think newlines and tabs should be ignored honestly."

text "This would show as a wholy new paragraph! My test variable is @{Test}"

if @{Condition} then
text "Conditionally render this"
end
end
footer
text "good foot"
Expand Down

0 comments on commit bd2142c

Please sign in to comment.