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
67 HtmlNode inputNode,
68 HtmlNode outputNode,
69 BadExecutionContext executionContext,
70 string filePath,
71 string source,
73 IFileSystem fileSystem)
74 {
75 InputNode = inputNode;
76 OutputNode = outputNode;
77 ExecutionContext = executionContext;
78 FilePath = filePath;
79 Source = source;
80 Options = options;
81 FileSystem = fileSystem;
82 }
83
87 public IFileSystem FileSystem { get; }
88
92 public HtmlDocument InputDocument => InputNode.OwnerDocument;
93
97 public HtmlDocument OutputDocument => OutputNode.OwnerDocument;
98
110 HtmlNode inputNode,
111 HtmlNode outputNode,
112 BadExecutionContext? executionContext = null)
113 {
114 return new BadHtmlContext(
115 inputNode,
116 outputNode,
117 executionContext ?? ExecutionContext,
118 FilePath,
119 Source,
120 Options,
122 );
123 }
124
130 public BadSourcePosition CreateAttributePosition(HtmlAttribute attribute)
131 {
132 return new BadSourcePosition(FilePath, Source, attribute.ValueStartIndex, attribute.Value.Length);
133 }
134
140 {
141 return new BadSourcePosition(FilePath, Source, InputNode.InnerStartIndex, InputNode.InnerLength);
142 }
143
149 {
150 return new BadSourcePosition(FilePath, Source, InputNode.InnerStartIndex, InputNode.InnerLength);
151 }
152
158 private static IEnumerable<BadExpression> VisitAll(IEnumerable<BadExpression> expressions)
159 {
160 return expressions.SelectMany(expression => expression.GetDescendantsAndSelf());
161 }
162
171 {
172 try
173 {
175 BadExpression expression = parser.ParseExpression();
176
177 foreach (BadExpression expr in expression.GetDescendantsAndSelf())
178 {
180 FilePath,
181 Source,
182 pos.Index + expr.Position.Index,
183 expr.Position.Length
184 );
185 expr.SetPosition(newPosition);
186 }
187
188 return expression;
189 }
191 {
192 if (e.Position == null)
193 {
195 }
196
197 throw new BadSourceReaderException(
200 );
201 }
202 }
203
211 public BadExpression[] Parse(string code, BadSourcePosition pos)
212 {
213 try
214 {
215 BadExpression[] expressions = BadSourceParser.Parse(FilePath, code).ToArray();
216
217 foreach (BadExpression expression in VisitAll(expressions))
218 {
220 FilePath,
221 Source,
222 pos.Index + expression.Position.Index,
223 expression.Position.Length
224 );
225 expression.SetPosition(newPosition);
226 }
227
228 return expressions;
229 }
231 {
232 if (e.Position == null)
233 {
235 }
236
237 throw new BadSourceReaderException(
240 );
241 }
242 }
243
251 public BadObject Execute(IEnumerable<BadExpression> expressions, BadSourcePosition position)
252 {
253 try
254 {
255 BadObject result = ExecutionContext.ExecuteScript(expressions);
256
257 return result.Dereference();
258 }
259 catch (BadRuntimeException e)
260 {
261 throw new BadRuntimeException(e.Message, position);
262 }
263 }
264
274 {
275 try
276 {
277 BadObject result = ExecutionContext.ExecuteScript(expression);
278
279 // if (ExecutionContext.Scope.IsError)
280 // {
281 // throw new BadRuntimeErrorException(ExecutionContext.Scope.Error);
282 // }
283
284 return result.Dereference();
285 }
286 catch (BadRuntimeException e)
287 {
288 throw new BadRuntimeException(e.Message, position);
289 }
290 }
291
299 {
300 BadExpression[] expressions = Parse(code, pos);
301
302 return Execute(expressions, pos);
303 }
304
312 {
313 BadExpression expression = ParseSingle(code, pos);
314
315 return Execute(expression, pos);
316 }
317}
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.