BadScript 2
Loading...
Searching...
No Matches
BadFunctionNodeTransformer.cs
Go to the documentation of this file.
1using System.Collections.Generic;
2using System.Linq;
3
11
12using HtmlAgilityPack;
13
14namespace BadHtml.Transformer;
15
20{
22 protected override bool CanTransform(BadHtmlContext context)
23 {
24 return context.InputNode.Name == "bs:function";
25 }
26
32 private static bool IsOptional(string value)
33 {
34 return value.EndsWith("?") || value.EndsWith("?!");
35 }
36
42 private static bool IsNullChecked(string value)
43 {
44 return value.EndsWith("!") || value.EndsWith("!?");
45 }
46
52 private static bool IsRestArgs(string value)
53 {
54 return value.EndsWith("*");
55 }
56
64 private static BadExpression? GetParameterType(BadHtmlContext context, HtmlAttribute attribute)
65 {
66 string name = attribute.Value;
67
68 while (name.EndsWith("*") || name.EndsWith("?") || name.EndsWith("!"))
69 {
70 name = name.Remove(name.Length - 1);
71 }
72
73 if (string.IsNullOrEmpty(name))
74 {
75 return null;
76 }
77
78 BadExpression[] expressions = context.Parse(name + ';', context.CreateAttributePosition(attribute));
79
80 if (expressions.Length != 1)
81 {
83 context.ExecutionContext.Scope,
84 $"Invalid parameter type expression for parameter {attribute.Name} in 'bs:function' node",
85 context.CreateAttributePosition(attribute)
86 );
87 }
88
89 return expressions[0];
90 }
91
93 protected override void TransformNode(BadHtmlContext context)
94 {
95 HtmlAttribute? nameAttribute = context.InputNode.Attributes["name"];
96
97 if (nameAttribute == null)
98 {
100 context.ExecutionContext.Scope,
101 "Missing 'name' attribute in 'bs:function' node",
102 context.CreateOuterPosition()
103 );
104 }
105
106 if (string.IsNullOrEmpty(nameAttribute.Value))
107 {
109 context.ExecutionContext.Scope,
110 "Empty 'name' attribute in 'bs:function' node",
111 context.CreateAttributePosition(nameAttribute)
112 );
113 }
114
115 IEnumerable<HtmlAttribute> parameterAttributes =
116 context.InputNode.Attributes.Where(x => x.Name.StartsWith("param:"));
117
118 BadFunctionParameter[] parameters = parameterAttributes.Select(
119 x => new BadFunctionParameter(
120 x.Name.Remove(0, "param:".Length),
121 IsOptional(x.Value),
122 IsNullChecked(x.Value),
123 IsRestArgs(x.Value),
124 GetParameterType(context, x)
125 )
126 )
127 .ToArray();
128
130 nameAttribute.Value,
131 (ctx, args) => InvokeFunction(nameAttribute.Value, context, parameters, ctx, args),
132 false,
134 parameters
135 );
137 nameAttribute.Value,
138 func,
139 context.ExecutionContext.Scope,
140 new BadPropertyInfo(func.GetPrototype(), true)
141 );
142 }
143
154 string name,
155 BadHtmlContext context,
156 BadFunctionParameter[] parameters,
157 BadExecutionContext caller,
158 BadObject[] arguments)
159 {
162 ToString(),
163 caller.Scope,
164 null,
165 BadScopeFlags.Returnable | BadScopeFlags.AllowThrow | BadScopeFlags.CaptureThrow
166 )
167 );
168
169 // ReSharper disable once UseObjectOrCollectionInitializer
170 HtmlDocument outputDocument = new HtmlDocument();
171 outputDocument.OptionUseIdAttribute = true;
172 outputDocument.LoadHtml("");
174 BadFunction.GetHeader(name, BadNativeClassBuilder.GetNative("string"), parameters),
175 parameters,
176 ctx,
177 arguments,
178 context.CreateOuterPosition()
179 );
180
181 foreach (HtmlNode? child in context.InputNode.ChildNodes)
182 {
183 BadHtmlContext childContext = new BadHtmlContext(
184 child,
185 outputDocument.DocumentNode,
186 ctx,
187 context.FilePath,
188 context.Source,
189 context.Options,
190 context.FileSystem
191 );
192 Transform(childContext);
193 }
194
195 return outputDocument.DocumentNode.InnerHtml;
196 }
197}
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.
readonly HtmlNode InputNode
The Current Input Node.
readonly BadExecutionContext ExecutionContext
The Execution Context that is used to evaluate badscript code.
BadExpression[] Parse(string code, BadSourcePosition pos)
Parses the specified code and returns the expressions with their positions set to the specified posit...
Base class of all Node transformers.
static void Transform(BadHtmlContext context)
Transforms the input node with one of the registered transformers.
Implements a BadScript Function Node Transformer.
override void TransformNode(BadHtmlContext context)
static bool IsRestArgs(string value)
Returns true if the specified parameter is the rest argument.
override bool CanTransform(BadHtmlContext context)
static bool IsNullChecked(string value)
Returns true if the specified parameter is null checked.
static ? BadExpression GetParameterType(BadHtmlContext context, HtmlAttribute attribute)
Returns the Parameter Type for the specified attribute.
BadObject InvokeFunction(string name, BadHtmlContext context, BadFunctionParameter[] parameters, BadExecutionContext caller, BadObject[] arguments)
Invokes the specified function.
static bool IsOptional(string value)
Returns true if the specified parameter is optional.
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
void DefineVariable(string name, BadObject value, BadScope? caller=null, BadPropertyInfo? info=null, BadObject[]? attributes=null)
Defines a new Variable in the current scope.
Definition BadScope.cs:929
static BadRuntimeException Create(BadScope? scope, string message)
Creates a new BadScriptException.
Interop Function taking an array of arguments.
The Base Class for all BadScript Objects.
Definition BadObject.cs:14
Stores Meta Information about a Property.
Implements a function that can be called from the script.
static void ApplyParameters(string funcStr, BadFunctionParameter[] parameters, BadExecutionContext context, BadObject[] args, BadSourcePosition? position=null)
Applies the function arguments to the context of the function.
string GetHeader()
Returns the Header of the function.
override BadClassPrototype GetPrototype()
Helper Class that Builds a Native Class from a Prototype.
static BadClassPrototype GetNative(string name)
Returns a Native Class Prototype for the given Native Type.
Implementations of Html Node Transformers that are used in the Transformation Process.
Contains the Expressions for the BadScript2 Language.
Contains the Error Objects for the BadScript2 Language.
Contains the Interop Function Classes for the BadScript2 Language.
Contains Runtime Function Objects.
Contains the Runtime Objects.
Definition BadArray.cs:10
Contains the Runtime Implementation.
BadScopeFlags
Defines Different Behaviours for the Current Scope.