BadScript 2
Loading...
Searching...
No Matches
BadExpressionValidatorContext.cs
Go to the documentation of this file.
1using System.Text;
2
6
8
12public readonly struct BadExpressionValidatorContext
13{
18 public override string ToString()
19 {
20 StringBuilder sb = new StringBuilder();
21
22 if (IsError)
23 {
24 sb.AppendLine("Validation failed:");
25 }
26 else if (HasWarnings)
27 {
28 sb.AppendLine("Validation succeeded with warnings:");
29 }
30 else
31 {
32 sb.AppendLine("Validation succeeded.");
33 }
34
35 foreach (BadExpressionValidatorMessage message in Messages)
36 {
37 sb.AppendLine(message.ToString());
38 }
39
40 return sb.ToString();
41 }
42
46 private readonly List<BadExpressionValidatorMessage> m_Messages = new List<BadExpressionValidatorMessage>();
47
51 public IReadOnlyList<BadExpressionValidatorMessage> Messages => m_Messages;
52
56 public bool IsError => m_Messages.Any(m => m.Type == BadExpressionValidatorMessageType.Error);
57
58 public bool HasWarnings => m_Messages.Any(m => m.Type == BadExpressionValidatorMessageType.Warning);
59
79
87 public void AddError(string message,
88 BadExpression parentExpression,
89 BadExpression expression,
90 BadExpressionValidator validator)
91 {
93 expression,
94 validator,
96 parentExpression
97 )
98 );
99 }
100
108 public void AddWarning(string message,
109 BadExpression parentExpression,
110 BadExpression expression,
111 BadExpressionValidator validator)
112 {
114 expression,
115 validator,
117 parentExpression
118 )
119 );
120 }
121
129 public void AddInfo(string message,
130 BadExpression parentExpression,
131 BadExpression expression,
132 BadExpressionValidator validator)
133 {
135 expression,
136 validator,
138 parentExpression
139 )
140 );
141 }
142
148 {
149 m_Validators.Add(validator);
150 }
151
156
163 {
164 foreach (BadExpression expr in expression.GetDescendantsAndSelf())
165 {
166 foreach (BadExpressionValidator? validator in m_Validators)
167 {
168 validator.Validate(this, expr);
169 }
170 }
171
172 return this;
173 }
174
180 public BadExpressionValidatorContext ValidateExpressions(IEnumerable<BadExpression> expressions)
181 {
183
184 foreach (BadExpression expression in expressions)
185 {
186 foreach (BadExpression expr in expression.GetDescendantsAndSelf())
187 {
188 foreach (BadExpressionValidator? validator in m_Validators)
189 {
190 validator.Validate(ctx, expr);
191 }
192 }
193 }
194
195 return ctx;
196 }
197
203 public static BadExpressionValidatorContext Validate(IEnumerable<BadExpression> expressions)
204 {
206
207 return result;
208 }
209
215 public static void ValidateOrThrow(IEnumerable<BadExpression> expressions)
216 {
217 BadExpressionValidatorContext result = Validate(expressions);
218
219 if (result.IsError)
220 {
221 throw new BadRuntimeException(result.ToString());
222 }
223 }
224}
Base Implementation for all Expressions used inside the Script.
IEnumerable< BadExpression > GetDescendantsAndSelf()
Returns all Descendants of the Expression and the Expression itself.
void Validate(BadExpressionValidatorContext context, BadExpression expr)
Validates the given expression.
Checks if there are any constant expressions in the if branches conditions.
Checks if there are any expressions in the for block.
Checks if there are any expressions in the if branches block.
Checks if there are any expressions in the lock block.
Checks if there are any expressions in the using block.
Contains the Expressions for the BadScript2 Language.
Contains the Expression Validators for the BadScript2 Language.
Contains the Comparison Operators for the BadScript2 Language.
BadExpressionValidatorMessageType
Defines the type of a BadExpressionValidatorMessage.
Contains the Error Objects for the BadScript2 Language.
readonly List< BadExpressionValidatorMessage > m_Messages
The messages generated by the validators.
readonly List< BadExpressionValidator > m_Validators
The validators used by this context.
BadExpressionValidatorContext ValidateExpressions(BadExpression expression)
Validates the given expression.
void AddWarning(string message, BadExpression parentExpression, BadExpression expression, BadExpressionValidator validator)
Adds a warning message to the context.
void AddError(string message, BadExpression parentExpression, BadExpression expression, BadExpressionValidator validator)
Adds an error message to the context.
BadExpressionValidatorContext ValidateExpressions(IEnumerable< BadExpression > expressions)
Validates the given expressions.
static void ValidateOrThrow(IEnumerable< BadExpression > expressions)
Validates the given expressions and throws an exception if there are any errors.
bool IsError
Indicates whether there are any messages of type Error.
static BadExpressionValidatorContext Validate(IEnumerable< BadExpression > expressions)
Validates the given expressions.
override string ToString()
Returns a string representation of the validation results.
void AddValidator(BadExpressionValidator validator)
Adds a validator to the context.
IReadOnlyList< BadExpressionValidatorMessage > Messages
The messages generated by the validators.
void AddInfo(string message, BadExpression parentExpression, BadExpression expression, BadExpressionValidator validator)
Adds an info message to the context.