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 $"Invalid parameter type expression for parameter {attribute.Name} in 'bs:function' node",
84 context.CreateAttributePosition(attribute)
85 );
86 }
87
88 return expressions[0];
89 }
90
92 protected override void TransformNode(BadHtmlContext context)
93 {
94 HtmlAttribute? nameAttribute = context.InputNode.Attributes["name"];
95
96 if (nameAttribute == null)
97 {
99 "Missing 'name' attribute in 'bs:function' node",
100 context.CreateOuterPosition()
101 );
102 }
103
104 if (string.IsNullOrEmpty(nameAttribute.Value))
105 {
107 "Empty 'name' attribute in 'bs:function' node",
108 context.CreateAttributePosition(nameAttribute)
109 );
110 }
111
112 IEnumerable<HtmlAttribute> parameterAttributes =
113 context.InputNode.Attributes.Where(x => x.Name.StartsWith("param:"));
114
115 BadFunctionParameter[] parameters = parameterAttributes
116 .Select(x => new BadFunctionParameter(x.Name.Remove(0, "param:".Length),
117 IsOptional(x.Value),
118 IsNullChecked(x.Value),
119 IsRestArgs(x.Value),
120 GetParameterType(context, x)
121 )
122 )
123 .ToArray();
124
125 BadInteropFunction func = new BadInteropFunction(nameAttribute.Value,
126 (ctx, args) =>
127 InvokeFunction(nameAttribute.Value,
128 context,
129 parameters,
130 ctx,
131 args
132 ),
133 false,
135 parameters
136 );
137
138 context.ExecutionContext.Scope.DefineVariable(nameAttribute.Value,
139 func,
140 context.ExecutionContext.Scope,
141 new BadPropertyInfo(func.GetPrototype(), true)
142 );
143 }
144
154 private BadObject InvokeFunction(string name,
155 BadHtmlContext context,
156 BadFunctionParameter[] parameters,
157 BadExecutionContext caller,
158 BadObject[] arguments)
159 {
161 caller.Scope,
162 null,
163 BadScopeFlags.Returnable |
164 BadScopeFlags.AllowThrow |
165 BadScopeFlags.CaptureThrow
166 )
167 );
168
169 // ReSharper disable once UseObjectOrCollectionInitializer
170 HtmlDocument outputDocument = new HtmlDocument();
171 outputDocument.OptionUseIdAttribute = true;
172 outputDocument.LoadHtml("");
173
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(child,
184 outputDocument.DocumentNode,
185 ctx,
186 context.FilePath,
187 context.Source,
188 context.Options,
189 context.FileSystem
190 );
191 Transform(childContext);
192 }
193
194 return outputDocument.DocumentNode.InnerHtml;
195 }
196}
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:597
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:784
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.