BadScript 2
Loading...
Searching...
No Matches
BadWhileNodeTransformer.cs
Go to the documentation of this file.
7
8using HtmlAgilityPack;
9
10namespace BadHtml.Transformer;
11
16{
18 protected override bool CanTransform(BadHtmlContext context)
19 {
20 return context.InputNode.Name == "bs:while";
21 }
22
31 private static bool Evaluate(BadHtmlContext context, HtmlAttribute attribute, BadExpression[] expressions)
32 {
33 BadSourcePosition pos = context.CreateAttributePosition(attribute);
34 BadObject resultObj = context.Execute(expressions, pos);
35
36 if (resultObj is not IBadBoolean result)
37 {
39 context.ExecutionContext.Scope,
40 "Result of 'test' attribute in 'bs:if' node is not a boolean",
41 pos
42 );
43 }
44
45 return result.Value;
46 }
47
49 protected override void TransformNode(BadHtmlContext context)
50 {
51 HtmlAttribute? conditionAttribute = context.InputNode.Attributes["test"];
52
53 if (conditionAttribute == null)
54 {
56 context.ExecutionContext.Scope,
57 "Missing 'test' attribute in 'bs:if' node",
58 context.CreateOuterPosition()
59 );
60 }
61
62 if (string.IsNullOrEmpty(conditionAttribute.Value))
63 {
65 context.ExecutionContext.Scope,
66 "Empty 'test' attribute in 'bs:if' node",
67 context.CreateAttributePosition(conditionAttribute)
68 );
69 }
70
71 BadExpression[] expressions =
72 context.Parse(conditionAttribute.Value, context.CreateAttributePosition(conditionAttribute));
73
74 while (Evaluate(context, conditionAttribute, expressions))
75 {
76 using BadExecutionContext loopContext = new BadExecutionContext(
78 "bs:while",
79 context.ExecutionContext.Scope,
80 null,
81 BadScopeFlags.Breakable | BadScopeFlags.Continuable
82 )
83 );
84
85 foreach (HtmlNode? child in context.InputNode.ChildNodes)
86 {
87 BadHtmlContext childContext = context.CreateChild(child, context.OutputNode, loopContext);
88
89 Transform(childContext);
90 }
91 }
92 }
93}
Implements the Html Context for the Transformation Process.
BadSourcePosition CreateAttributePosition(HtmlAttribute attribute)
Creates the Source Position of the specified Attribute.
BadSourcePosition CreateOuterPosition()
Creates the Source Position of the current Input Nodes Outer Content.
BadHtmlContext CreateChild(HtmlNode inputNode, HtmlNode outputNode, BadExecutionContext? executionContext=null)
Creates a child context with the specified input, output node and optional execution context.
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.
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.
Base class of all Node transformers.
static void Transform(BadHtmlContext context)
Transforms the input node with one of the registered transformers.
Implements the 'bs:while' node transformer.
override bool CanTransform(BadHtmlContext context)
override void TransformNode(BadHtmlContext context)
static bool Evaluate(BadHtmlContext context, HtmlAttribute attribute, BadExpression[] expressions)
Evaluates the Expressions inside the attribute.
Describes a specific position inside a source file.
Base Implementation for all Expressions used inside the Script.
The Execution Context. Every execution of a script needs a context the script is running in....
BadScope Scope
The Root Scope of the Context.
BadScope CreateChild(string name, BadScope? caller, bool? useVisibility, BadScopeFlags flags=BadScopeFlags.RootScope)
Creates a subscope of the current scope.
Definition BadScope.cs:792
static BadRuntimeException Create(BadScope? scope, string message)
Creates a new BadScriptException.
The Base Class for all BadScript Objects.
Definition BadObject.cs:14
Implements the Interface for Native Boolean.
Definition IBadBoolean.cs:7
Implementations of Html Node Transformers that are used in the Transformation Process.
Contains Shared Data Structures and Functionality.
Contains the Expressions for the BadScript2 Language.
Contains the Error Objects for the BadScript2 Language.
Contains the Native Runtime Objects.
Definition BadBoolean.cs:6
Contains the Runtime Objects.
Definition BadArray.cs:10
Contains the Runtime Implementation.
BadScopeFlags
Defines Different Behaviours for the Current Scope.