Skip to content

Commit

Permalink
feat: Do not add InvalidNode to result lists
Browse files Browse the repository at this point in the history
  • Loading branch information
furesoft committed Jun 30, 2024
1 parent cc4a685 commit 0c3c623
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 84 deletions.
6 changes: 5 additions & 1 deletion Source/Silverfly.Testing/SnapshotParserTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ public static Task Test(string source)
useStatementsAtToplevel: _options.UseStatementsAtToplevel,
filename: _options.Filename);

var result = new TestResult(parsed.Tree.Accept(new PrintVisitor()), parsed.Document);
object result = parsed.Tree;
if (_options.OutputMode == OutputMode.Small)
{
result = new TestResult(parsed.Tree.Accept(new PrintVisitor()), parsed.Document);
}

return Verify(result, _settings);
}
Expand Down
8 changes: 7 additions & 1 deletion Source/Silverfly.Testing/TestOptions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
namespace Silverfly.Testing;

public record TestOptions(bool UseStatementsAtToplevel, string Filename = "syntetic.dsl")
public enum OutputMode
{
Small,
Full
}

public record TestOptions(bool UseStatementsAtToplevel, string Filename = "test.src", OutputMode OutputMode = OutputMode.Small)
{
}
2 changes: 1 addition & 1 deletion Source/Silverfly.Testing/TestResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

namespace Silverfly.Testing;

public record TestResult(string ParsedTree, SourceDocument Document)
public record TestResult(object ParsedTree, SourceDocument Document)
{
}
6 changes: 5 additions & 1 deletion Source/Silverfly/Parselets/BlockParselet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ public AstNode Parse(Parser parser, Token token)
while (!parser.Match(Terminator))
{
var node = parser.ParseStatement(wrapExpressions);
children.Add(node with { Parent = block });

if (node is not InvalidNode)
{
children.Add(node with { Parent = block });
}

if (Seperator != null && parser.IsMatch(Seperator))
{
Expand Down
27 changes: 21 additions & 6 deletions Source/Silverfly/Parser.Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,17 @@ public ImmutableList<AstNode> ParseSeperated(Symbol seperator, Symbol terminator

do
{
args.Add(Parse(bindingPower));
var node = Parse(bindingPower);

if (node is not InvalidNode)
{
args.Add(node);
}
} while (Match(seperator));

Consume(terminator);

return args.ToImmutableList();
return [.. args];
}

public ImmutableList<AstNode> ParseList(Symbol terminator, int bindingPower = 0)
Expand All @@ -81,12 +86,17 @@ public ImmutableList<AstNode> ParseList(Symbol terminator, int bindingPower = 0)

while (!IsMatch(terminator))
{
args.Add(Parse(bindingPower));
var node = Parse(bindingPower);

if (node is not InvalidNode)
{
args.Add(node);
}
}

Consume(terminator);

return args.ToImmutableList();
return [.. args];
}

public ImmutableList<AstNode> ParseSeperated(Symbol seperator, int bindingPower = 0, params Symbol[] terminators)
Expand All @@ -100,11 +110,16 @@ public ImmutableList<AstNode> ParseSeperated(Symbol seperator, int bindingPower

do
{
args.Add(Parse(bindingPower));
var node = Parse(bindingPower);

if (node is not InvalidNode)
{
args.Add(node);
}
} while (Match(seperator));

Match(terminators);

return args.ToImmutableList();
return [.. args];
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,69 +1,6 @@
{
$type: TranslationUnit,
Tree: {
$type: BlockNode,
SeperatorSymbol: ;,
Terminator: #eof,
Children: {
$type: ImmutableList<AstNode>,
$values: [
{
$type: CallNode,
FunctionExpr: {
$type: NameNode,
Name: a,
Range: 1:1-1:1
},
Arguments: {
$type: ImmutableList<AstNode>,
$values: [
{
$type: TernaryOperatorNode,
FirstExpr: {
$type: NameNode,
Name: b,
Range: 1:3-1:3
},
SecondExpr: {
$type: NameNode,
Name: c,
Range: 1:7-1:7
},
ThirdExpr: {
$type: NameNode,
Name: d,
Range: 1:11-1:11
},
Range: 1:3-1:5
},
{
$type: BinaryOperatorNode,
LeftExpr: {
$type: NameNode,
Name: e,
Range: 1:14-1:14
},
Operator: +,
RightExpr: {
$type: NameNode,
Name: f,
Range: 1:18-1:18
},
Range: 1:14-1:18
}
]
},
Range: 1:1-1:2,
Parent: {
$type: BlockNode,
SeperatorSymbol: ;,
Terminator: #eof
}
}
]
},
Range: 1:1-1:23
},
$type: TestResult,
Tree: ((a) (b ? c : d), (e + f)); ,
Document: {
Filename: syntethic.dsl,
Source: a(b ? c : d, e + f),
Expand Down

0 comments on commit 0c3c623

Please sign in to comment.