Skip to content

Commit

Permalink
3.4.1-alpha1 : performance
Browse files Browse the repository at this point in the history
  • Loading branch information
b3b00 committed Oct 29, 2024
1 parent 7f81780 commit 3c4eecc
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 deletions.
41 changes: 26 additions & 15 deletions src/sly/parser/generator/visitor/EBNFSyntaxTreeVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using sly.lexer;
using sly.parser.parser;
using sly.parser.syntax.tree;
using System;

namespace sly.parser.generator.visitor
{
Expand Down Expand Up @@ -87,51 +88,59 @@ private SyntaxVisitorResult<IN, OUT> Visit(SyntaxNode<IN> node, object context =
var result = SyntaxVisitorResult<IN, OUT>.NoneResult();
if (node.Visitor != null || node.IsByPassNode)
{
int parametersArrayLength = node.Children.Count + (context is NoContext ? 0 : 1);
var parameters = new object[parametersArrayLength];


var args = new List<object>();
int parametersCount = 0;

foreach (var n in node.Children)
{
var v = Visit(n, context);

if (v.IsToken)
if (v.IsToken && !n.Discarded)
{
if (!n.Discarded) args.Add(v.TokenResult);
parameters[parametersCount] = v.TokenResult;
parametersCount++;
}
else if (v.IsValue)
{
args.Add(v.ValueResult);
parameters[parametersCount] = v.ValueResult;
parametersCount++;
}
else if (v.IsOption)
{
args.Add(v.OptionResult);
parameters[parametersCount] = v.OptionResult;
parametersCount++;
}
else if (v.IsOptionGroup)
{
args.Add(v.OptionGroupResult);
parameters[parametersCount] = v.OptionGroupResult;
parametersCount++;
}
else if (v.IsGroup)
{
args.Add(v.GroupResult);
parameters[parametersCount] = v.GroupResult;
parametersCount++;
}
else if (v.IsTokenList)
{
args.Add(v.TokenListResult);
parameters[parametersCount] = v.TokenListResult;
parametersCount++;
}
else if (v.IsValueList)
{
args.Add(v.ValueListResult);
parameters[parametersCount] = v.ValueListResult;
parametersCount++;
}
else if (v.IsGroupList)
{
args.Add(v.GroupListResult);
parameters[parametersCount] = v.GroupListResult;
parametersCount++;
}
}

if (node.IsByPassNode)
{
result = SyntaxVisitorResult<IN, OUT>.NewValue((OUT)args[0]);
result = SyntaxVisitorResult<IN, OUT>.NewValue((OUT)parameters[0]);
}
else
{
Expand All @@ -140,11 +149,13 @@ private SyntaxVisitorResult<IN, OUT> Visit(SyntaxNode<IN> node, object context =
{
if (!(context is NoContext))
{
args.Add(context);
parameters[parametersCount] = context;
parametersCount++;
}

method = node.Visitor;
var t = method.Invoke(ParserVsisitorInstance, args.ToArray());
Array.Resize(ref parameters, parametersCount);
var t = method.Invoke(ParserVsisitorInstance, parameters);
var res = (OUT) t;
result = SyntaxVisitorResult<IN, OUT>.NewValue(res);
}
Expand Down
4 changes: 2 additions & 2 deletions src/sly/sly.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
<Description>#LY is a parser generator halfway between parser combinators and parser generator like ANTLR</Description>
<Authors>b3b00</Authors>
<version>3.4.0</version>
<version>3.4.1-alpha1</version>
<PackageProjectUrl>https://github.com/b3b00/sly</PackageProjectUrl>
<RepositoryUrl>https://github.com/b3b00/sly</RepositoryUrl>
<PackageVersion>3.4.0</PackageVersion>
<PackageVersion>3.4.1-alpha1</PackageVersion>
<ApplicationIcon/>
<OutputType>Library</OutputType>
<StartupObject/>
Expand Down

0 comments on commit 3c4eecc

Please sign in to comment.