BadScript 2
Loading...
Searching...
No Matches
BadHtmlContext.cs
Go to the documentation of this file.
1using System.Collections.Generic;
2using System.Linq;
3
5using BadScript2.IO;
12
13using HtmlAgilityPack;
14
15// ReSharper disable once InvalidXmlDocComment
19namespace BadHtml;
20
24public class BadHtmlContext
25{
30
34 public readonly string FilePath;
35
39 public readonly HtmlNode InputNode;
40
45
49 public readonly HtmlNode OutputNode;
50
54 public readonly string Source;
55
66 public BadHtmlContext(HtmlNode inputNode,
67 HtmlNode outputNode,
68 BadExecutionContext executionContext,
69 string filePath,
70 string source,
72 IFileSystem fileSystem)
73 {
74 InputNode = inputNode;
75 OutputNode = outputNode;
76 ExecutionContext = executionContext;
77 FilePath = filePath;
78 Source = source;
79 Options = options;
80 FileSystem = fileSystem;
81 }
82
86 public IFileSystem FileSystem { get; }
87
91 public HtmlDocument InputDocument => InputNode.OwnerDocument;
92
96 public HtmlDocument OutputDocument => OutputNode.OwnerDocument;
97
108 public BadHtmlContext CreateChild(HtmlNode inputNode,
109 HtmlNode outputNode,
110 BadExecutionContext? executionContext = null)
111 {
112 return new BadHtmlContext(inputNode,
113 outputNode,
114 executionContext ?? ExecutionContext,
115 FilePath,
116 Source,
117 Options,
119 );
120 }
121
127 public BadSourcePosition CreateAttributePosition(HtmlAttribute attribute)
128 {
129 return new BadSourcePosition(FilePath, Source, attribute.ValueStartIndex, attribute.Value.Length);
130 }
131
137 {
138 return new BadSourcePosition(FilePath, Source, InputNode.InnerStartIndex, InputNode.InnerLength);
139 }
140
146 {
147 return new BadSourcePosition(FilePath, Source, InputNode.InnerStartIndex, InputNode.InnerLength);
148 }
149
155 private static IEnumerable<BadExpression> VisitAll(IEnumerable<BadExpression> expressions)
156 {
157 return expressions.SelectMany(expression => expression.GetDescendantsAndSelf());
158 }
159
168 {
169 try
170 {
172 BadExpression expression = parser.ParseExpression();
173
174 foreach (BadExpression expr in expression.GetDescendantsAndSelf())
175 {
177 Source,
178 pos.Index + expr.Position.Index,
179 expr.Position.Length
180 );
181 expr.SetPosition(newPosition);
182 }
183
184 return expression;
185 }
187 {
188 if (e.Position == null)
189 {
191 }
192
195 Source,
196 e.Position.Index + pos.Index,
198 )
199 );
200 }
201 }
202
210 public BadExpression[] Parse(string code, BadSourcePosition pos)
211 {
212 try
213 {
214 BadExpression[] expressions = BadSourceParser.Parse(FilePath, code)
215 .ToArray();
216
217 foreach (BadExpression expression in VisitAll(expressions))
218 {
220 Source,
221 pos.Index + expression.Position.Index,
222 expression.Position.Length
223 );
224 expression.SetPosition(newPosition);
225 }
226
227 return expressions;
228 }
230 {
231 if (e.Position == null)
232 {
234 }
235
238 Source,
239 e.Position.Index + pos.Index,
241 )
242 );
243 }
244 }
245
253 public BadObject Execute(IEnumerable<BadExpression> expressions, BadSourcePosition position)
254 {
255 try
256 {
257 BadObject result = ExecutionContext.ExecuteScript(expressions);
258
259 return result.Dereference(null);
260 }
261 catch (BadRuntimeException e)
262 {
263 throw new BadRuntimeException(e.Message, position);
264 }
265 }
266
276 {
277 try
278 {
279 BadObject result = ExecutionContext.ExecuteScript(expression);
280
281 // if (ExecutionContext.Scope.IsError)
282 // {
283 // throw new BadRuntimeErrorException(ExecutionContext.Scope.Error);
284 // }
285
286 return result.Dereference(null);
287 }
288 catch (BadRuntimeException e)
289 {
290 throw new BadRuntimeException(e.Message, position);
291 }
292 }
293
301 {
302 BadExpression[] expressions = Parse(code, pos);
303
304 return Execute(expressions, pos);
305 }
306
314 {
315 BadExpression expression = ParseSingle(code, pos);
316
317 return Execute(expression, pos);
318 }
319}
Implements the Html Context for the Transformation Process.
IFileSystem FileSystem
The Filesystem of the Current Template Context.
BadSourcePosition CreateAttributePosition(HtmlAttribute attribute)
Creates the Source Position of the specified Attribute.
readonly BadHtmlTemplateOptions Options
The Html Template Options.
BadSourcePosition CreateOuterPosition()
Creates the Source Position of the current Input Nodes Outer Content.
HtmlDocument OutputDocument
The Output Document.
readonly string Source
The Source Code of the Template.
BadExpression ParseSingle(string code, BadSourcePosition pos)
Parses a single Expression from the specified code and returns it with its position set to the specif...
BadHtmlContext CreateChild(HtmlNode inputNode, HtmlNode outputNode, BadExecutionContext? executionContext=null)
Creates a child context with the specified input, output node and optional execution context.
readonly string FilePath
The File Path of the Template.
BadObject ParseAndExecute(string code, BadSourcePosition pos)
Parses and executes the specified code.
BadObject Execute(BadExpression expression, BadSourcePosition position)
Executes the specified expression.
BadSourcePosition CreateInnerPosition()
Creates the Source Position of the current Input Nodes Inner Content.
BadHtmlContext(HtmlNode inputNode, HtmlNode outputNode, BadExecutionContext executionContext, string filePath, string source, BadHtmlTemplateOptions options, IFileSystem fileSystem)
Constructs a new Html Context.
HtmlDocument InputDocument
The Input Document.
static IEnumerable< BadExpression > VisitAll(IEnumerable< BadExpression > expressions)
Returns an enumeration of all expressions in the specified expressions and their descendants.
readonly HtmlNode InputNode
The Current Input Node.
readonly BadExecutionContext ExecutionContext
The Execution Context that is used to evaluate badscript code.
readonly HtmlNode OutputNode
The Current Output Node.
BadObject ParseAndExecuteSingle(string code, BadSourcePosition pos)
Parses and executes the specified code and returns the result of the last expression.
BadExpression[] Parse(string code, BadSourcePosition pos)
Parses the specified code and returns the expressions with their positions set to the specified posit...
BadObject Execute(IEnumerable< BadExpression > expressions, BadSourcePosition position)
Executes the specified expressions.
Options for the Html Template Engine.
BadSourcePosition? Position
The source position of where the error occurred.
string OriginalMessage
The Original Error Message.
Describes a specific position inside a source file.
static BadSourcePosition Create(string fileName, string source, int index, int length)
Creates a new Source Position.
int Index
The Start Index of the Position.
int Length
The Length of the Position.
The Parser of the Language. It turns Source Code into an Expression Tree.
static BadSourceParser Create(string fileName, string source)
Creates a BadSourceParser Instance based on the source and filename provided.
BadExpression ParseExpression(BadExpression? left=null, int precedence=int.MaxValue)
Parses an Expression with a precedence greater than the given precedence. Moves the reader to the nex...
static IEnumerable< BadExpression > Parse(string fileName, string source)
Parses a BadExpression from the Source Reader.
Base Implementation for all Expressions used inside the Script.
IEnumerable< BadExpression > GetDescendantsAndSelf()
Returns all Descendants of the Expression and the Expression itself.
BadSourcePosition Position
The source Position of the Expression.
void SetPosition(BadSourcePosition pos)
Sets the Source Position of the Expression.
Gets Raised if the Reader encounters an Error.
The Execution Context. Every execution of a script needs a context the script is running in....
BadObject ExecuteScript(IEnumerable< BadExpression > expressions)
Executes an enumeration of expressions and returns the last value.
The Base Class for all BadScript Objects.
Definition BadObject.cs:14
Defines the interface for a file system.
Definition IFileSystem.cs:7
A Html Template Generator based on BadScript2.
Contains Shared Data Structures and Functionality.
Contains IO Implementation for the BadScript2 Runtime.
Contains the Expressions for the BadScript2 Language.
Contains the Parser for the BadScript2 Language.
Contains the Source Reader for the BadScript2 Language.
Contains the Error Objects for the BadScript2 Language.
Contains the Runtime Objects.
Definition BadArray.cs:10
Contains the Runtime Implementation.