BadScript 2
Loading...
Searching...
No Matches
BadExpressionValidatorContext.cs
Go to the documentation of this file.
1using System.Text;
2
7
9
13public readonly struct BadExpressionValidatorContext
14{
19 public override string ToString()
20 {
21 StringBuilder sb = new StringBuilder();
22
23 if (IsError)
24 {
25 sb.AppendLine("Validation failed:");
26 }
27 else if (HasWarnings)
28 {
29 sb.AppendLine("Validation succeeded with warnings:");
30 }
31 else
32 {
33 sb.AppendLine("Validation succeeded.");
34 }
35
36 foreach (BadExpressionValidatorMessage message in Messages)
37 {
38 sb.AppendLine(message.ToString());
39 }
40
41
42 return sb.ToString();
43 }
44
48 private readonly List<BadExpressionValidatorMessage> m_Messages = new List<BadExpressionValidatorMessage>();
49
53 public IReadOnlyList<BadExpressionValidatorMessage> Messages => m_Messages;
54
58 public bool IsError => m_Messages.Any(m => m.Type == BadExpressionValidatorMessageType.Error);
59 public bool HasWarnings => m_Messages.Any(m => m.Type == BadExpressionValidatorMessageType.Warning);
60
80
88 public void AddError(
89 string message,
90 BadExpression parentExpression,
91 BadExpression expression,
92 BadExpressionValidator validator)
93 {
94 m_Messages.Add(
96 message,
97 expression,
98 validator,
100 parentExpression
101 )
102 );
103 }
104
112 public void AddWarning(
113 string message,
114 BadExpression parentExpression,
115 BadExpression expression,
116 BadExpressionValidator validator)
117 {
118 m_Messages.Add(
120 message,
121 expression,
122 validator,
124 parentExpression
125 )
126 );
127 }
128
136 public void AddInfo(
137 string message,
138 BadExpression parentExpression,
139 BadExpression expression,
140 BadExpressionValidator validator)
141 {
142 m_Messages.Add(
144 message,
145 expression,
146 validator,
148 parentExpression
149 )
150 );
151 }
152
158 {
159 m_Validators.Add(validator);
160 }
161
166
173 {
174 foreach (BadExpression expr in expression.GetDescendantsAndSelf())
175 {
176 foreach (BadExpressionValidator? validator in m_Validators)
177 {
178 validator.Validate(this, expr);
179 }
180 }
181
182 return this;
183 }
184
190 public BadExpressionValidatorContext ValidateExpressions(IEnumerable<BadExpression> expressions)
191 {
193
194 foreach (BadExpression expression in expressions)
195 {
196 foreach (BadExpression expr in expression.GetDescendantsAndSelf())
197 {
198 foreach (BadExpressionValidator? validator in m_Validators)
199 {
200 validator.Validate(ctx, expr);
201 }
202 }
203 }
204
205 return ctx;
206 }
207
213 public static BadExpressionValidatorContext Validate(IEnumerable<BadExpression> expressions)
214 {
216
217 return result;
218 }
219
225 public static void ValidateOrThrow(IEnumerable<BadExpression> expressions)
226 {
227 BadExpressionValidatorContext result = Validate(expressions);
228
229 if (result.IsError)
230 {
231 throw new BadRuntimeException(result.ToString());
232 }
233 }
234}
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 Function Expressions for the BadScript2 Language.
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.