forked from sebastienros/fluid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FluidParserExtensions.cs
122 lines (107 loc) · 4.21 KB
/
FluidParserExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
using Fluid.Ast;
using Fluid.Parser;
using Parlot.Fluent;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.Encodings.Web;
using System.Threading.Tasks;
namespace Fluid
{
public static class FluidParserExtensions
{
public static IFluidTemplate Parse(this FluidParser parser, string template)
{
var context = new FluidParseContext(template);
var success = parser.Grammar.TryParse(context, out var statements, out var parlotError);
if (parlotError != null)
{
// Extract line with error
var start = parlotError.Position.Offset - 1;
var end = parlotError.Position.Offset;
while (start > 0 && template[start] != '\n' && template[start] != '\r') start--;
while (end < template.Length && template[end] != '\n') end++;
var source = template.Substring(start, end - start).Trim('\n', '\r');
throw new ParseException($"{parlotError.Message} at {parlotError.Position}\nSource:\n{source}");
}
if (!success)
{
return null;
}
return new FluidTemplate(statements);
}
public static bool TryParse(this FluidParser parser, string template, out IFluidTemplate result, out string error)
{
try
{
error = null;
result = parser.Parse(template);
return true;
}
catch (ParseException e)
{
error = e.Message;
result = null;
return false;
}
catch (Exception e)
{
error = e.Message;
result = null;
return false;
}
}
public static bool TryParse(this FluidParser parser, string template, out IFluidTemplate result)
{
return parser.TryParse(template, out result, out _);
}
public static ValueTask<Completion> RenderStatementsAsync(this IReadOnlyList<Statement> statements, TextWriter writer, TextEncoder encoder, TemplateContext context)
{
static async ValueTask<Completion> Awaited(
ValueTask<Completion> task,
int startIndex,
IReadOnlyList<Statement> statements,
TextWriter writer,
TextEncoder encoder,
TemplateContext context)
{
var completion = await task;
if (completion != Completion.Normal)
{
// Stop processing the block statements
// We return the completion to flow it to the outer loop
return completion;
}
for (var i = startIndex; i < statements.Count; i++)
{
var statement = statements[i];
completion = await statement.WriteToAsync(writer, encoder, context);
if (completion != Completion.Normal)
{
// Stop processing the block statements
// We return the completion to flow it to the outer loop
return completion;
}
}
return Completion.Normal;
}
for (var i = 0; i < statements.Count; i++)
{
var statement = statements[i];
var task = statement.WriteToAsync(writer, encoder, context);
if (!task.IsCompletedSuccessfully)
{
return Awaited(task, i + 1, statements, writer, encoder, context);
}
var completion = task.Result;
if (completion != Completion.Normal)
{
// Stop processing the block statements
// We return the completion to flow it to the outer loop
return new ValueTask<Completion>(completion);
}
}
return new ValueTask<Completion>(Completion.Normal);
}
}
}