Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,24 @@ public class BuildableSimpleSkipStrategy : BuildableSkipStrategy


public override IEnumerable<Or<string, BuildableParserRule>>? RuleChildren =>
Strategy == ParserSkippingStrategy.Default || Strategy == ParserSkippingStrategy.Whitespaces
? null
: new[] { SkipRule ?? default };
new[] { SkipRule ?? default };

public override SkipStrategy BuildTyped(List<int>? ruleChildren, List<int>? tokenChildren, List<object?>? elementChildren)
{
if (ruleChildren[0] == -1)
switch (Strategy)
{
case ParserSkippingStrategy.SkipBeforeParsing:
case ParserSkippingStrategy.SkipBeforeParsingGreedy:
case ParserSkippingStrategy.SkipBeforeParsingLazy:
case ParserSkippingStrategy.TryParseThenSkip:
case ParserSkippingStrategy.TryParseThenSkipGreedy:
case ParserSkippingStrategy.TryParseThenSkipLazy:
case ParserSkippingStrategy.TryParseNonEmptyThenSkip:
case ParserSkippingStrategy.TryParseNonEmptyThenSkipGreedy:
case ParserSkippingStrategy.TryParseNonEmptyThenSkipLazy:
throw new ParserBuildingException($"Skipping sule is not set for this strategy type: {Strategy}.");
}
switch (Strategy)
{
case ParserSkippingStrategy.Default:
Expand Down
2 changes: 1 addition & 1 deletion src/RCParsing/Building/TokenBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -867,7 +867,7 @@ public TokenBuilder FailIf<T>(Action<TokenBuilder> builderAction, Func<T, bool>
return Token(new BuildableFailIfTokenPattern
{
Child = builder.BuildingPattern.Value,
Condition = v => condition((T)v),
Condition = v => v is T t && condition(t),
ErrorMessage = errorMessage
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/RCParsing/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public TokenPattern GetTokenPattern(int id)
{
if (id >= 0 && id < _tokenPatterns.Length)
return _tokenPatterns[id];
throw new ArgumentOutOfRangeException(nameof(id), "Invalid token pattern ID.");
throw new ArgumentOutOfRangeException(nameof(id), $"Invalid token pattern ID: {id}.");
}

/// <summary>
Expand All @@ -213,7 +213,7 @@ public ParserRule GetRule(int id)
{
if (id >= 0 && id < _rules.Length)
return _rules[id];
throw new ArgumentOutOfRangeException(nameof(id), "Invalid rule ID.");
throw new ArgumentOutOfRangeException(nameof(id), $"Invalid rule ID: {id}.");
}

/// <summary>
Expand Down
14 changes: 13 additions & 1 deletion src/RCParsing/RCParsing.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<PropertyGroup>
<PackageId>RCParsing</PackageId>
<Version>5.0.0</Version>
<Version>5.1.0</Version>
<Authors>Roman K.</Authors>
<Company>RomeCore</Company>
<Product>RCParsing</Product>
Expand All @@ -26,6 +26,18 @@
<None Include="..\..\assets\logo-nuget.png" Pack="true" PackagePath="\" />
</ItemGroup>

<PropertyGroup>
<IncludeSymbols>true</IncludeSymbols>
<DebugType>embedded</DebugType>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<IncludeSourceRevisionInInformationalVersion>true</IncludeSourceRevisionInInformationalVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="10.0.102" PrivateAssets="All" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="System.Memory" Version="4.6.3" />
</ItemGroup>
Expand Down
128 changes: 128 additions & 0 deletions tests/RCParsing.Tests/LLT/ModernLLTGrammarTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RCParsing.Tests.LLT
{
public class ModernLLTGrammarTests
{
[Fact]
public void SimpleTemplateParsing()
{
string templateStr =
"""
@// The main template.
@template MainTemplate {
@*
Use the template to display a greeting message.
*@
Hello, @user.name!

@// Display a number.
Value: @number

@// Method call with parameters.
Method test: @data.getItem(1, "arg").fieldName

@// Simple arithmetic and logical operations.
Result: @(a * (b + c) - 42 / value % 3 > 0 && !flag || isAdmin)

@if user.age >= 18 {
Adult content
@if user.isAdmin {
Welcome, mighty @user.role!
} else {
Regular user detected
}
} else @* AAA *@ {
@@You are too {{young}}.
}

@foreach item in user.items {
Item: @item.id - @item.name
}

@// String literals, booleans, and null values.
String literal: @'hello ''world'''
Boolean true: @true
Boolean false: @false
Null test: @null
}

@// Second template with no expressions.
@template Secondary {
Static text only
No expressions here
}

@// Third template with mixed content.
@template Mixed {
@// Nested if-else statements.
@if flag {
YES
} else {
@if otherFlag {
NESTED YES
} else {
DEEP ELSE
}
}

@// Foreach loop with nested foreach.
@foreach group in groups {
Group: @group.name
@foreach member in group.members {
Member: @member.id - @member.name
}
}
}
""";

var parser = ModernLLTParser.CreateParser();
parser.Parse(templateStr);
}

[Fact]
public void SimpleMessagesTemplateParsing()
{
string templateStr =
"""
@messages template ChatBot {
@metadata {
language: 'ru',
version: 1
}

@system message {
You are a helpful assistant.
}

@user message {
Hello!
}

@if user.isAdmin {
@assistant message {
Welcome back, admin!
}
} else {
@assistant message {
Welcome, user!
}
}

@foreach item in items {
@assistant message {
Processing item: @item
}
}
}
""";

var parser = ModernLLTParser.CreateParser();
parser.Parse(templateStr);
}
}
}
Loading
Loading