forked from ZeraGmbH/Blockly.Net
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParser.cs
More file actions
181 lines (147 loc) · 5.04 KB
/
Parser.cs
File metadata and controls
181 lines (147 loc) · 5.04 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
using System.Text.Json.Nodes;
using BlocklyNet.Core.Model;
using BlocklyNet.Extensions.Builder;
using BlocklyNet.Scripting.Engine;
using BlocklyNet.Scripting.Parsing;
namespace BlocklyNet.Core;
/// <summary>
///
/// </summary>
public abstract class Parser
{
private class ParserService(bool _xml, IScriptModels models, IParserConfiguration? custom) : IScriptParser
{
private readonly XmlParser _xmlParser = CreateXml().AddCustomBlocks(models, builder => custom?.Configure(builder));
private readonly JsonParser _jsonParser = CreateJson().AddCustomBlocks(models, builder => custom?.Configure(builder));
public IEnumerable<JsonObject> BlockDefinitions => _xml ? _xmlParser.BlockDefinitions : _jsonParser.BlockDefinitions;
public IEnumerable<JsonObject> ModelDefinitions => _xml ? _xmlParser.ModelDefinitions : _jsonParser.ModelDefinitions;
public IEnumerable<Tuple<string, JsonObject>> ToolboxEntries => _xml ? _xmlParser.ToolboxEntries : _jsonParser.ToolboxEntries;
private class ParsedScript(Workspace workspace) : IParsedScript
{
private readonly Workspace _workspace = workspace;
/// <inheritdoc/>
public Block[] Blocks => [.. _workspace.ParsedBlocks];
/// <inheritdoc/>
public Task<object?> RunAsync(IScriptSite engine) => _workspace.EvaluateAsync(new Context(engine, _workspace.VariableTypes));
/// <inheritdoc/>
public async Task<object?> EvaluateAsync(Dictionary<string, object?> presets, IScriptSite site)
{
var ctx = new Context(site, _workspace.VariableTypes);
foreach (var preset in presets)
ctx.Variables.Add(preset);
try
{
await _workspace.EvaluateAsync(ctx);
}
catch (ScriptStoppedEarlyException)
{
/* Planned abort, hopefully result is already set. */
}
/* If script provides a result always use this. */
if (ctx.Variables.TryGetValue("result", out var result) && result != null) return result;
/* If this is the root script see if group execution results are available. */
if (site is IScriptEngine engine) return engine.CreateFlatResultFromGroups();
/* No result at all. */
return null;
}
/// <inheritdoc/>
public Task<List<GroupInfo>> GetGroupTreeAsync() => _workspace.GetGroupTreeAsync();
/// <inheritdoc/>
public string? GetVariableType(string name) => _workspace.VariableTypes.TryGetValue(name, out var type) ? type : null;
}
public IParsedScript Parse(string scriptAsText)
{
Workspace workspace;
Parser parser = _xml ? _xmlParser : _jsonParser;
lock (parser)
workspace = parser.Parse(scriptAsText);
return new ParsedScript(workspace);
}
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public static XmlParser CreateXml() => new();
/// <summary>
///
/// </summary>
/// <returns></returns>
public static JsonParser CreateJson() => new();
/// <summary>
///
/// </summary>
/// <returns></returns>
protected readonly IDictionary<string, Func<Block>> blocks = new Dictionary<string, Func<Block>>();
/// <summary>
///
/// </summary>
/// <param name="xml"></param>
/// <param name="models"></param>
/// <param name="custom"></param>
/// <returns></returns>
public static IScriptParser CreateService(bool xml, IScriptModels models, IParserConfiguration? custom) => new ParserService(xml, models, custom);
/// <summary>
///
/// </summary>
/// <param name="script"></param>
/// <param name="preserveWhitespace"></param>
/// <returns></returns>
public abstract Workspace Parse(string script, bool preserveWhitespace = false);
}
/// <summary>
///
/// </summary>
/// <typeparam name="TParser"></typeparam>
public abstract class Parser<TParser> : Parser where TParser : Parser
{
/// <summary>
///
/// </summary>
public readonly List<JsonObject> BlockDefinitions = [];
/// <summary>
///
/// </summary>
public readonly List<JsonObject> ModelDefinitions = [];
/// <summary>
///
/// </summary>
public readonly List<Tuple<string, JsonObject>> ToolboxEntries = [];
/// <summary>
///
/// </summary>
/// <param name="type"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public TParser AddBlock<T>(string type) where T : Block, new()
{
AddBlock(type, () => new T());
return (this as TParser)!;
}
/// <summary>
///
/// </summary>
/// <param name="type"></param>
/// <param name="block"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public TParser AddBlock<T>(string type, T block) where T : Block
{
AddBlock(type, () => block);
return (this as TParser)!;
}
/// <summary>
///
/// </summary>
/// <param name="type"></param>
/// <param name="blockFactory"></param>
/// <returns></returns>
public TParser AddBlock(string type, Func<Block> blockFactory)
{
if (blocks.ContainsKey(type))
blocks[type] = blockFactory;
else
blocks.Add(type, blockFactory);
return (this as TParser)!;
}
}