| | | 1 | | using Elsa.Dsl.ElsaScript.Ast; |
| | | 2 | | using Elsa.Dsl.ElsaScript.Contracts; |
| | | 3 | | using Parlot; |
| | | 4 | | using Parlot.Fluent; |
| | | 5 | | |
| | | 6 | | namespace Elsa.Dsl.ElsaScript.Parser; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// ElsaScript parser using Parlot for robust parsing. |
| | | 10 | | /// </summary> |
| | | 11 | | public class ElsaScriptParser : IElsaScriptParser |
| | | 12 | | { |
| | | 13 | | private static readonly Parser<ProgramNode> ProgramParser; |
| | | 14 | | |
| | | 15 | | static ElsaScriptParser() |
| | | 16 | | { |
| | | 17 | | // Keywords |
| | 2 | 18 | | var useKeyword = Terms.Text("use"); |
| | 2 | 19 | | var workflowKeyword = Terms.Text("workflow"); |
| | 2 | 20 | | var expressionsKeyword = Terms.Text("expressions"); |
| | 2 | 21 | | var listenKeyword = Terms.Text("listen"); |
| | 2 | 22 | | var varKeyword = Terms.Text("var"); |
| | 2 | 23 | | var constKeyword = Terms.Text("const"); |
| | 2 | 24 | | var forKeyword = Terms.Text("for"); |
| | 2 | 25 | | var foreachKeyword = Terms.Text("foreach"); |
| | 2 | 26 | | var inKeyword = Terms.Text("in"); |
| | 2 | 27 | | var toKeyword = Terms.Text("to"); |
| | 2 | 28 | | var throughKeyword = Terms.Text("through"); |
| | 2 | 29 | | var stepKeyword = Terms.Text("step"); |
| | 2 | 30 | | var flowchartKeyword = Terms.Text("flowchart"); |
| | 2 | 31 | | var entryKeyword = Terms.Text("entry"); |
| | | 32 | | |
| | | 33 | | // Basic tokens |
| | 2 | 34 | | var identifier = Terms.Identifier(); |
| | 2 | 35 | | var stringLiteral = Terms.String(); |
| | 2 | 36 | | var integerLiteral = Terms.Integer(); |
| | 2 | 37 | | var decimalLiteral = Terms.Decimal(); |
| | | 38 | | |
| | | 39 | | // Punctuation |
| | 2 | 40 | | var semicolon = Terms.Char(';'); |
| | 2 | 41 | | var comma = Terms.Char(','); |
| | 2 | 42 | | var colon = Terms.Char(':'); |
| | 2 | 43 | | var leftParen = Terms.Char('('); |
| | 2 | 44 | | var rightParen = Terms.Char(')'); |
| | 2 | 45 | | var leftBrace = Terms.Char('{'); |
| | 2 | 46 | | var rightBrace = Terms.Char('}'); |
| | 2 | 47 | | var leftBracket = Terms.Char('['); |
| | 2 | 48 | | var rightBracket = Terms.Char(']'); |
| | 2 | 49 | | var dot = Terms.Char('.'); |
| | 2 | 50 | | var arrow = Terms.Text("=>"); |
| | 2 | 51 | | var rightArrow = Terms.Text("->"); |
| | 2 | 52 | | var equals = Terms.Char('='); |
| | | 53 | | |
| | | 54 | | // Deferred parsers for recursive structures |
| | 2 | 55 | | var expression = Deferred<ExpressionNode>(); |
| | 2 | 56 | | var statement = Deferred<StatementNode>(); |
| | | 57 | | |
| | | 58 | | // Expression parsers |
| | 2 | 59 | | var booleanLiteral = Terms.Text("true").Or(Terms.Text("false")) |
| | 4 | 60 | | .Then<ExpressionNode>(x => new LiteralNode { Value = x.ToString() == "true" }); |
| | | 61 | | |
| | 2 | 62 | | var numberLiteral = decimalLiteral |
| | 20 | 63 | | .Then<ExpressionNode>(x => new LiteralNode { Value = x }); |
| | | 64 | | |
| | 2 | 65 | | var intLiteral = integerLiteral |
| | 2 | 66 | | .Then<ExpressionNode>(x => new LiteralNode { Value = (long)x }); |
| | | 67 | | |
| | 2 | 68 | | var stringExpr = stringLiteral |
| | 31 | 69 | | .Then<ExpressionNode>(x => new LiteralNode { Value = x.ToString() }); |
| | | 70 | | |
| | 2 | 71 | | var identifierExpr = identifier |
| | 4 | 72 | | .Then<ExpressionNode>(x => new IdentifierNode { Name = x.ToString() }); |
| | | 73 | | |
| | | 74 | | // Array literal: [expr, expr, ...] |
| | 2 | 75 | | var commaSeparatedExpression = expression.And(ZeroOrOne(comma)).Then(x => x.Item1); |
| | 2 | 76 | | var arrayLiteral = Between(leftBracket, ZeroOrMany(commaSeparatedExpression), rightBracket) |
| | 2 | 77 | | .Then<ExpressionNode>(elements => new ArrayLiteralNode { Elements = elements.ToList() }); |
| | | 78 | | |
| | | 79 | | // Elsa expression: lang => <raw text until matching )> |
| | | 80 | | // We need to capture raw text after => up to the closing parenthesis |
| | | 81 | | // This supports nested parentheses by counting depth |
| | | 82 | | // Use a custom scanner-based parser wrapped in RawExpressionParser |
| | 2 | 83 | | var rawExpressionText = new RawExpressionParser(); |
| | | 84 | | |
| | 2 | 85 | | var elsaExpressionWithLang = identifier |
| | 2 | 86 | | .And(arrow) |
| | 2 | 87 | | .And(rawExpressionText) |
| | 10 | 88 | | .Then<ExpressionNode>(x => new ElsaExpressionNode |
| | 10 | 89 | | { |
| | 10 | 90 | | Language = x.Item1.ToString(), |
| | 10 | 91 | | Expression = x.Item3.ToString().Trim() |
| | 10 | 92 | | }); |
| | | 93 | | |
| | 2 | 94 | | var elsaExpressionWithoutLang = arrow |
| | 2 | 95 | | .And(rawExpressionText) |
| | 2 | 96 | | .Then<ExpressionNode>(x => new ElsaExpressionNode |
| | 2 | 97 | | { |
| | 2 | 98 | | Language = null, |
| | 2 | 99 | | Expression = x.Item2.ToString().Trim() |
| | 2 | 100 | | }); |
| | | 101 | | |
| | 2 | 102 | | var elsaExpression = elsaExpressionWithLang.Or(elsaExpressionWithoutLang); |
| | | 103 | | |
| | | 104 | | // Expression priority: try most specific first |
| | 2 | 105 | | expression.Parser = elsaExpression |
| | 2 | 106 | | .Or(arrayLiteral) |
| | 2 | 107 | | .Or(booleanLiteral) |
| | 2 | 108 | | .Or(numberLiteral) |
| | 2 | 109 | | .Or(intLiteral) |
| | 2 | 110 | | .Or(stringExpr) |
| | 2 | 111 | | .Or(identifierExpr); |
| | | 112 | | |
| | | 113 | | // Argument parser: name: value or just value |
| | 2 | 114 | | var namedArgument = identifier |
| | 2 | 115 | | .And(colon) |
| | 2 | 116 | | .And(expression) |
| | 3 | 117 | | .Then(x => new ArgumentNode { Name = x.Item1.ToString(), Value = x.Item3 }); |
| | | 118 | | |
| | 2 | 119 | | var positionalArgument = expression |
| | 28 | 120 | | .Then(x => new ArgumentNode { Value = x }); |
| | | 121 | | |
| | 2 | 122 | | var argument = namedArgument.Or(positionalArgument); |
| | | 123 | | |
| | 29 | 124 | | var commaSeparatedArgument = argument.And(ZeroOrOne(comma)).Then(x => x.Item1); |
| | 2 | 125 | | var arguments = ZeroOrMany(commaSeparatedArgument); |
| | | 126 | | |
| | | 127 | | // Activity invocation: ActivityName(args) - with or without arguments |
| | 2 | 128 | | var activityInvocationWithArgs = identifier |
| | 2 | 129 | | .And(leftParen) |
| | 2 | 130 | | .And(arguments) |
| | 2 | 131 | | .And(rightParen) |
| | 33 | 132 | | .Then(x => new ActivityInvocationNode |
| | 33 | 133 | | { |
| | 33 | 134 | | ActivityName = x.Item1.ToString(), |
| | 33 | 135 | | Arguments = x.Item3.ToList() |
| | 33 | 136 | | }); |
| | | 137 | | |
| | 2 | 138 | | var activityInvocationNoArgs = identifier |
| | 2 | 139 | | .And(leftParen) |
| | 2 | 140 | | .And(rightParen) |
| | 2 | 141 | | .Then(x => new ActivityInvocationNode |
| | 2 | 142 | | { |
| | 2 | 143 | | ActivityName = x.Item1.ToString(), |
| | 2 | 144 | | Arguments = [] |
| | 2 | 145 | | }); |
| | | 146 | | |
| | 2 | 147 | | var activityInvocation = activityInvocationWithArgs.Or(activityInvocationNoArgs); |
| | | 148 | | |
| | | 149 | | // Variable declaration: var/const name = expr |
| | 2 | 150 | | var variableKindParser = varKeyword.Or(constKeyword); |
| | | 151 | | |
| | 2 | 152 | | var variableDeclaration = variableKindParser |
| | 2 | 153 | | .And(identifier) |
| | 2 | 154 | | .And(equals) |
| | 2 | 155 | | .And(expression) |
| | 2 | 156 | | .Then<StatementNode>(x => |
| | 2 | 157 | | { |
| | 2 | 158 | | // x is a flat tuple (kind, identifier, equals, expression) |
| | 8 | 159 | | var kind = x.Item1.ToString() switch |
| | 8 | 160 | | { |
| | 6 | 161 | | "var" => VariableKind.Var, |
| | 2 | 162 | | "const" => VariableKind.Const, |
| | 0 | 163 | | _ => VariableKind.Var |
| | 8 | 164 | | }; |
| | 2 | 165 | | |
| | 8 | 166 | | return new VariableDeclarationNode |
| | 8 | 167 | | { |
| | 8 | 168 | | Kind = kind, |
| | 8 | 169 | | Name = x.Item2.ToString(), |
| | 8 | 170 | | Value = x.Item4 |
| | 8 | 171 | | }; |
| | 2 | 172 | | }); |
| | | 173 | | |
| | | 174 | | // Listen statement: listen ActivityName(args) |
| | 2 | 175 | | var listenStatement = listenKeyword |
| | 2 | 176 | | .And(activityInvocation) |
| | 5 | 177 | | .Then<StatementNode>(x => new ListenNode { Activity = x.Item2 }); |
| | | 178 | | |
| | | 179 | | // Statement: variable declaration, listen, or activity invocation |
| | 2 | 180 | | var activityStatement = activityInvocation |
| | 26 | 181 | | .Then<StatementNode>(x => x); |
| | | 182 | | |
| | | 183 | | // Declare deferred for loop, foreach, and flowchart parsers |
| | 2 | 184 | | var forStatement = Deferred<StatementNode>(); |
| | 2 | 185 | | var foreachStatement = Deferred<StatementNode>(); |
| | 2 | 186 | | var flowchartStatement = Deferred<StatementNode>(); |
| | | 187 | | |
| | 2 | 188 | | statement.Parser = variableDeclaration |
| | 2 | 189 | | .Or(listenStatement) |
| | 2 | 190 | | .Or(forStatement) |
| | 2 | 191 | | .Or(foreachStatement) |
| | 2 | 192 | | .Or(flowchartStatement) |
| | 2 | 193 | | .Or(activityStatement); |
| | | 194 | | |
| | | 195 | | // Statement with optional semicolon |
| | 47 | 196 | | var statementWithSemicolon = statement.And(ZeroOrOne(semicolon)).Then(x => x.Item1); |
| | | 197 | | |
| | | 198 | | // For loop statement: for (var i = 0 to 10 step 1) { body } or for (i = 0 to 10) statement |
| | | 199 | | // Must be defined after statementWithSemicolon |
| | 2 | 200 | | var rangeOperator = toKeyword.Or(throughKeyword); |
| | | 201 | | |
| | | 202 | | // For body can be either a block or a single statement |
| | 2 | 203 | | var forBlockBody = Between(leftBrace, ZeroOrMany(statementWithSemicolon), rightBrace) |
| | 6 | 204 | | .Then(statements => (StatementNode)(statements.Count == 1 |
| | 6 | 205 | | ? statements.First() |
| | 6 | 206 | | : new BlockNode { Statements = statements.ToList() })); |
| | 2 | 207 | | var forSingleStatementBody = statement; |
| | 2 | 208 | | var forBody = forBlockBody.Or(forSingleStatementBody); |
| | | 209 | | |
| | | 210 | | // For header with optional var: (var i = start to/through end step stepValue) |
| | | 211 | | // or (i = start to/through end step stepValue) |
| | | 212 | | // Step clause is optional |
| | 2 | 213 | | var optionalVarKeyword = ZeroOrOne(varKeyword); |
| | 6 | 214 | | var optionalStepClause = ZeroOrOne(stepKeyword.And(expression).Then(x => x.Item2)); |
| | | 215 | | |
| | 2 | 216 | | var forHeader = Between(leftParen, |
| | 2 | 217 | | optionalVarKeyword |
| | 2 | 218 | | .And(identifier) |
| | 2 | 219 | | .And(equals) |
| | 2 | 220 | | .And(expression) |
| | 2 | 221 | | .And(rangeOperator) |
| | 2 | 222 | | .And(expression) |
| | 2 | 223 | | .And(optionalStepClause) |
| | 4 | 224 | | .Then(x => ( |
| | 4 | 225 | | HasVar: x.Item1 != null, |
| | 4 | 226 | | VarName: x.Item2.ToString(), |
| | 4 | 227 | | Start: x.Item4, |
| | 4 | 228 | | RangeOp: x.Item5.ToString(), |
| | 4 | 229 | | End: x.Item6, |
| | 4 | 230 | | Step: x.Item7 |
| | 4 | 231 | | )), |
| | 2 | 232 | | rightParen); |
| | | 233 | | |
| | 2 | 234 | | var forStatementParser = forKeyword |
| | 2 | 235 | | .And(forHeader) |
| | 2 | 236 | | .And(forBody) |
| | 2 | 237 | | .Then<StatementNode>(result => |
| | 2 | 238 | | { |
| | 4 | 239 | | var header = result.Item2; |
| | 4 | 240 | | var body = result.Item3; |
| | 2 | 241 | | |
| | 2 | 242 | | // Default step to 1 if not specified |
| | 4 | 243 | | var stepExpr = header.Step ?? new LiteralNode { Value = 1 }; |
| | 2 | 244 | | |
| | 4 | 245 | | return new ForNode |
| | 4 | 246 | | { |
| | 4 | 247 | | DeclaresVariable = header.HasVar, |
| | 4 | 248 | | VariableName = header.VarName, |
| | 4 | 249 | | Start = header.Start, |
| | 4 | 250 | | End = header.End, |
| | 4 | 251 | | Step = stepExpr, |
| | 4 | 252 | | IsInclusive = header.RangeOp == "through", |
| | 4 | 253 | | Body = body |
| | 4 | 254 | | }; |
| | 2 | 255 | | }); |
| | | 256 | | |
| | 2 | 257 | | forStatement.Parser = forStatementParser; |
| | | 258 | | |
| | | 259 | | // ForEach statement: foreach (var item in collection) { body } or foreach (item in collection) statement |
| | | 260 | | // Must be defined after statementWithSemicolon |
| | | 261 | | // ForEach body can be either a block or a single statement |
| | 2 | 262 | | var foreachBlockBody = Between(leftBrace, ZeroOrMany(statementWithSemicolon), rightBrace) |
| | 2 | 263 | | .Then(statements => (StatementNode)(statements.Count == 1 |
| | 2 | 264 | | ? statements.First() |
| | 2 | 265 | | : new BlockNode { Statements = statements.ToList() })); |
| | 2 | 266 | | var foreachSingleStatementBody = statement; |
| | 2 | 267 | | var foreachBody = foreachBlockBody.Or(foreachSingleStatementBody); |
| | | 268 | | |
| | | 269 | | // ForEach header with optional var: (var item in collection) or (item in collection) |
| | 2 | 270 | | var foreachOptionalVarKeyword = ZeroOrOne(varKeyword); |
| | | 271 | | |
| | 2 | 272 | | var foreachHeader = Between(leftParen, |
| | 2 | 273 | | foreachOptionalVarKeyword |
| | 2 | 274 | | .And(identifier) |
| | 2 | 275 | | .And(inKeyword) |
| | 2 | 276 | | .And(expression) |
| | 0 | 277 | | .Then(x => ( |
| | 0 | 278 | | HasVar: x.Item1 != null, |
| | 0 | 279 | | VarName: x.Item2.ToString(), |
| | 0 | 280 | | Collection: x.Item4 |
| | 0 | 281 | | )), |
| | 2 | 282 | | rightParen); |
| | | 283 | | |
| | 2 | 284 | | var foreachStatementParser = foreachKeyword |
| | 2 | 285 | | .And(foreachHeader) |
| | 2 | 286 | | .And(foreachBody) |
| | 2 | 287 | | .Then<StatementNode>(result => |
| | 2 | 288 | | { |
| | 0 | 289 | | var header = result.Item2; |
| | 0 | 290 | | var body = result.Item3; |
| | 2 | 291 | | |
| | 0 | 292 | | return new ForEachNode |
| | 0 | 293 | | { |
| | 0 | 294 | | DeclaresVariable = header.HasVar, |
| | 0 | 295 | | VariableName = header.VarName, |
| | 0 | 296 | | Collection = header.Collection, |
| | 0 | 297 | | Body = body |
| | 0 | 298 | | }; |
| | 2 | 299 | | }); |
| | | 300 | | |
| | 2 | 301 | | foreachStatement.Parser = foreachStatementParser; |
| | | 302 | | |
| | | 303 | | // Flowchart statement: flowchart { [variables] [nodes] [connections] [entry] } |
| | | 304 | | // Node declaration: label: statement; |
| | | 305 | | // Entry declaration: entry label; |
| | | 306 | | // Connection declaration: source -> target; or source.Outcome -> target; |
| | | 307 | | |
| | | 308 | | // Flowchart body element can be: |
| | | 309 | | // 1. Variable declaration |
| | | 310 | | // 2. Node declaration (label: statement) |
| | | 311 | | // 3. Entry declaration (entry label) |
| | | 312 | | // 4. Connection declaration (source -> target or source.Outcome -> target) |
| | | 313 | | |
| | | 314 | | // Node declaration: label: activityInvocation; or label: { block } |
| | | 315 | | // Note: We use activityInvocation directly (not statement) to avoid circular dependency |
| | | 316 | | // since statement includes flowchart which would include node declarations |
| | 2 | 317 | | var nodeBlock = Between(leftBrace, ZeroOrMany(statementWithSemicolon), rightBrace) |
| | 4 | 318 | | .Then<StatementNode>(statements => statements.Count == 1 |
| | 4 | 319 | | ? statements.First() |
| | 4 | 320 | | : new BlockNode { Statements = statements.ToList() }); |
| | | 321 | | |
| | 6 | 322 | | var nodeActivityStatement = activityInvocation.Then<StatementNode>(s => s); |
| | | 323 | | |
| | 2 | 324 | | var nodeDeclaration = identifier |
| | 2 | 325 | | .And(colon) |
| | 2 | 326 | | .And(nodeBlock.Or(nodeActivityStatement)) |
| | 2 | 327 | | .And(ZeroOrOne(semicolon)) |
| | 8 | 328 | | .Then(x => new LabeledActivityNode |
| | 8 | 329 | | { |
| | 8 | 330 | | Label = x.Item1.ToString(), |
| | 8 | 331 | | Activity = x.Item3 |
| | 8 | 332 | | }); |
| | | 333 | | |
| | | 334 | | // Entry declaration: entry label; |
| | 2 | 335 | | var entryDeclaration = entryKeyword |
| | 2 | 336 | | .And(identifier) |
| | 2 | 337 | | .And(ZeroOrOne(semicolon)) |
| | 6 | 338 | | .Then(x => x.Item2.ToString()); |
| | | 339 | | |
| | | 340 | | // Connection declaration: source -> target; or source.Outcome -> target; |
| | | 341 | | // Source can be: identifier or identifier.identifier (with outcome) |
| | 2 | 342 | | var optionalOutcome = ZeroOrOne(dot.And(identifier).Then(x => x.Item2.ToString())); |
| | | 343 | | |
| | 2 | 344 | | var connectionSource = identifier |
| | 2 | 345 | | .And(optionalOutcome) |
| | 4 | 346 | | .Then(x => ( |
| | 4 | 347 | | SourceLabel: x.Item1.ToString(), |
| | 4 | 348 | | Outcome: x.Item2 |
| | 4 | 349 | | )); |
| | | 350 | | |
| | 2 | 351 | | var connectionTarget = identifier; |
| | | 352 | | |
| | 2 | 353 | | var connectionDeclaration = connectionSource |
| | 2 | 354 | | .And(rightArrow) |
| | 2 | 355 | | .And(connectionTarget) |
| | 2 | 356 | | .And(ZeroOrOne(semicolon)) |
| | 4 | 357 | | .Then(x => new ConnectionNode |
| | 4 | 358 | | { |
| | 4 | 359 | | Source = x.Item1.SourceLabel, |
| | 4 | 360 | | Outcome = x.Item1.Outcome, |
| | 4 | 361 | | Target = x.Item3.ToString() |
| | 4 | 362 | | }); |
| | | 363 | | |
| | | 364 | | // Flowchart body element type - try each parser in order |
| | 2 | 365 | | var flowchartBodyElement = variableDeclaration.Then<object>(v => v) |
| | 4 | 366 | | .Or(entryDeclaration.Then<object>(e => e)) |
| | 6 | 367 | | .Or(nodeDeclaration.Then<object>(n => n)) |
| | 4 | 368 | | .Or(connectionDeclaration.Then<object>(c => c)); |
| | | 369 | | |
| | 2 | 370 | | var flowchartBody = Between(leftBrace, ZeroOrMany(flowchartBodyElement), rightBrace); |
| | | 371 | | |
| | 2 | 372 | | var flowchartStatementParser = flowchartKeyword |
| | 2 | 373 | | .And(flowchartBody) |
| | 2 | 374 | | .Then<StatementNode>(result => |
| | 2 | 375 | | { |
| | 6 | 376 | | var bodyElements = result.Item2; |
| | 2 | 377 | | |
| | 6 | 378 | | var variables = new List<VariableDeclarationNode>(); |
| | 6 | 379 | | var nodes = new List<LabeledActivityNode>(); |
| | 6 | 380 | | var connections = new List<ConnectionNode>(); |
| | 6 | 381 | | string? entryPoint = null; |
| | 2 | 382 | | |
| | 36 | 383 | | foreach (var element in bodyElements) |
| | 2 | 384 | | { |
| | 12 | 385 | | if (element is VariableDeclarationNode varDecl) |
| | 0 | 386 | | variables.Add(varDecl); |
| | 12 | 387 | | else if (element is LabeledActivityNode node) |
| | 6 | 388 | | nodes.Add(node); |
| | 6 | 389 | | else if (element is ConnectionNode conn) |
| | 2 | 390 | | connections.Add(conn); |
| | 4 | 391 | | else if (element is string entry) |
| | 4 | 392 | | entryPoint = entry; |
| | 2 | 393 | | } |
| | 2 | 394 | | |
| | 6 | 395 | | return new FlowchartNode |
| | 6 | 396 | | { |
| | 6 | 397 | | Variables = variables, |
| | 6 | 398 | | Activities = nodes, |
| | 6 | 399 | | Connections = connections, |
| | 6 | 400 | | EntryPoint = entryPoint |
| | 6 | 401 | | }; |
| | 2 | 402 | | }); |
| | | 403 | | |
| | 2 | 404 | | flowchartStatement.Parser = flowchartStatementParser; |
| | | 405 | | |
| | | 406 | | // Use statement: use Namespace; or use expressions lang; |
| | 2 | 407 | | var namespaceUse = identifier |
| | 2 | 408 | | .And(ZeroOrMany(dot.And(identifier))) |
| | 2 | 409 | | .Then(x => |
| | 2 | 410 | | { |
| | 4 | 411 | | var ns = x.Item1.ToString(); |
| | 24 | 412 | | foreach (var part in x.Item2) |
| | 2 | 413 | | { |
| | 8 | 414 | | ns += "." + part.Item2.ToString(); |
| | 2 | 415 | | } |
| | 4 | 416 | | return new UseNode { Type = UseType.Namespace, Value = ns }; |
| | 2 | 417 | | }); |
| | | 418 | | |
| | 2 | 419 | | var expressionUse = expressionsKeyword |
| | 2 | 420 | | .And(identifier) |
| | 17 | 421 | | .Then(x => new UseNode { Type = UseType.Expressions, Value = x.Item2.ToString() }); |
| | | 422 | | |
| | 2 | 423 | | var useStatement = useKeyword |
| | 2 | 424 | | .And(expressionUse.Or(namespaceUse)) |
| | 2 | 425 | | .And(ZeroOrOne(semicolon)) |
| | 21 | 426 | | .Then(x => x.Item2); |
| | | 427 | | |
| | | 428 | | // Workflow metadata: name: value |
| | 2 | 429 | | var metadataEntry = identifier |
| | 2 | 430 | | .And(colon) |
| | 2 | 431 | | .And(expression) |
| | 14 | 432 | | .Then(x => (Name: x.Item1.ToString(), Value: EvaluateConstantExpressionStatic(x.Item3))); |
| | | 433 | | |
| | 14 | 434 | | var commaSeparatedMetadata = metadataEntry.And(ZeroOrOne(comma)).Then(x => x.Item1); |
| | 2 | 435 | | var metadataList = ZeroOrMany(commaSeparatedMetadata); |
| | | 436 | | |
| | | 437 | | // Workflow declaration: workflow Identifier [(metadata)] { [use statements] [statements] } |
| | 2 | 438 | | var workflowMetadata = Between(leftParen, metadataList, rightParen); |
| | | 439 | | |
| | | 440 | | // Workflow body can contain use statements and regular statements |
| | 2 | 441 | | var workflowUseStatement = useStatement; |
| | | 442 | | |
| | 2 | 443 | | var workflowBodyElement = Deferred<object>(); |
| | 2 | 444 | | workflowBodyElement.Parser = workflowUseStatement |
| | 2 | 445 | | .Then<object>(u => u) |
| | 36 | 446 | | .Or(statementWithSemicolon.Then<object>(s => s)); |
| | | 447 | | |
| | 2 | 448 | | var workflowBody = Between(leftBrace, ZeroOrMany(workflowBodyElement), rightBrace); |
| | | 449 | | |
| | 2 | 450 | | var workflowWithMetadata = workflowKeyword |
| | 2 | 451 | | .And(identifier) |
| | 2 | 452 | | .And(workflowMetadata) |
| | 4 | 453 | | .Then(x => (WorkflowId: x.Item2.ToString(), Metadata: x.Item3)); |
| | | 454 | | |
| | 2 | 455 | | var workflowWithoutMetadata = workflowKeyword |
| | 2 | 456 | | .And(identifier) |
| | 22 | 457 | | .Then(x => (WorkflowId: x.Item2.ToString(), Metadata: (IReadOnlyList<(string Name, object Value)>?)null)); |
| | | 458 | | |
| | 2 | 459 | | var workflowHeader = workflowWithMetadata.Or(workflowWithoutMetadata); |
| | | 460 | | |
| | 2 | 461 | | var workflowDeclaration = workflowHeader |
| | 2 | 462 | | .And(workflowBody) |
| | 2 | 463 | | .Then(x => |
| | 2 | 464 | | { |
| | 22 | 465 | | var header = x.Item1; |
| | 22 | 466 | | var bodyElements = x.Item2; |
| | 2 | 467 | | |
| | 22 | 468 | | var metadataDict = new Dictionary<string, object>(); |
| | 22 | 469 | | if (header.Metadata != null) |
| | 2 | 470 | | { |
| | 28 | 471 | | foreach (var entry in header.Metadata) |
| | 2 | 472 | | { |
| | 12 | 473 | | metadataDict[entry.Name] = entry.Value; |
| | 2 | 474 | | } |
| | 2 | 475 | | } |
| | 2 | 476 | | |
| | 2 | 477 | | // Separate use statements from regular statements in body |
| | 22 | 478 | | var workflowUses = new List<UseNode>(); |
| | 22 | 479 | | var statements = new List<StatementNode>(); |
| | 2 | 480 | | |
| | 116 | 481 | | foreach (var element in bodyElements) |
| | 2 | 482 | | { |
| | 36 | 483 | | if (element is UseNode useNode) |
| | 2 | 484 | | workflowUses.Add(useNode); |
| | 34 | 485 | | else if (element is StatementNode stmt) |
| | 34 | 486 | | statements.Add(stmt); |
| | 2 | 487 | | } |
| | 2 | 488 | | |
| | 22 | 489 | | return new WorkflowNode |
| | 22 | 490 | | { |
| | 22 | 491 | | Id = header.WorkflowId, |
| | 22 | 492 | | Metadata = metadataDict, |
| | 22 | 493 | | UseStatements = workflowUses, |
| | 22 | 494 | | Body = statements |
| | 22 | 495 | | }; |
| | 2 | 496 | | }); |
| | | 497 | | |
| | | 498 | | // Program with single workflow: [global use statements] [workflow declaration] |
| | 2 | 499 | | var programWithWorkflow = ZeroOrMany(useStatement) |
| | 2 | 500 | | .And(workflowDeclaration) |
| | 2 | 501 | | .Then(x => |
| | 2 | 502 | | { |
| | 39 | 503 | | var globalUses = x.Item1.Select(u => (UseNode)u).ToList(); |
| | 22 | 504 | | var workflow = x.Item2; |
| | 2 | 505 | | |
| | 22 | 506 | | return new ProgramNode |
| | 22 | 507 | | { |
| | 22 | 508 | | GlobalUseStatements = globalUses, |
| | 22 | 509 | | Workflows = new List<WorkflowNode> { workflow } |
| | 22 | 510 | | }; |
| | 2 | 511 | | }); |
| | | 512 | | |
| | | 513 | | // Fallback: raw statements without workflow keyword (backward compatibility) |
| | | 514 | | // Only match if there are actual statements (OneOrMany) |
| | 2 | 515 | | var programWithStatements = ZeroOrMany(useStatement) |
| | 2 | 516 | | .And(OneOrMany(statementWithSemicolon)) |
| | 2 | 517 | | .Then(x => |
| | 2 | 518 | | { |
| | 2 | 519 | | var globalUses = x.Item1.Select(u => (UseNode)u).ToList(); |
| | 2 | 520 | | var statements = x.Item2.ToList(); |
| | 2 | 521 | | |
| | 2 | 522 | | return new ProgramNode |
| | 2 | 523 | | { |
| | 2 | 524 | | GlobalUseStatements = globalUses, |
| | 2 | 525 | | Workflows = new List<WorkflowNode> |
| | 2 | 526 | | { |
| | 2 | 527 | | new WorkflowNode |
| | 2 | 528 | | { |
| | 2 | 529 | | Id = "DefaultWorkflow", |
| | 2 | 530 | | UseStatements = new List<UseNode>(), |
| | 2 | 531 | | Body = statements |
| | 2 | 532 | | } |
| | 2 | 533 | | } |
| | 2 | 534 | | }; |
| | 2 | 535 | | }); |
| | | 536 | | |
| | 2 | 537 | | var programParser = programWithWorkflow.Or(programWithStatements); |
| | | 538 | | |
| | 2 | 539 | | ProgramParser = programParser; |
| | 2 | 540 | | } |
| | | 541 | | |
| | | 542 | | /// <inheritdoc /> |
| | | 543 | | public ProgramNode Parse(string source) |
| | | 544 | | { |
| | 24 | 545 | | if (!ProgramParser.TryParse(source, out var result, out var error)) |
| | | 546 | | { |
| | 0 | 547 | | var errorMessage = error != null |
| | 0 | 548 | | ? $"{error.Message} at {error.Position}" |
| | 0 | 549 | | : "Unknown parse error"; |
| | 0 | 550 | | throw new ParseException($"Failed to parse ElsaScript: {errorMessage}"); |
| | | 551 | | } |
| | 24 | 552 | | return result; |
| | | 553 | | } |
| | | 554 | | |
| | | 555 | | /// <summary> |
| | | 556 | | /// Static helper to evaluate constant expressions during parsing. |
| | | 557 | | /// </summary> |
| | | 558 | | private static object EvaluateConstantExpressionStatic(ExpressionNode exprNode) |
| | | 559 | | { |
| | 12 | 560 | | return exprNode switch |
| | 12 | 561 | | { |
| | 12 | 562 | | LiteralNode literal => literal.Value ?? string.Empty, |
| | 0 | 563 | | IdentifierNode identifier => identifier.Name, |
| | 0 | 564 | | _ => string.Empty |
| | 12 | 565 | | }; |
| | | 566 | | } |
| | | 567 | | } |
| | | 568 | | |
| | | 569 | | /// <summary> |
| | | 570 | | /// Exception thrown when parsing fails. |
| | | 571 | | /// </summary> |
| | | 572 | | public class ParseException : Exception |
| | | 573 | | { |
| | | 574 | | public ParseException(string message) : base(message) |
| | | 575 | | { |
| | | 576 | | } |
| | | 577 | | } |
| | | 578 | | |
| | | 579 | | /// <summary> |
| | | 580 | | /// Custom parser that captures raw text after => until the matching closing parenthesis. |
| | | 581 | | /// Supports nested parentheses. |
| | | 582 | | /// </summary> |
| | | 583 | | internal sealed class RawExpressionParser : Parser<TextSpan> |
| | | 584 | | { |
| | | 585 | | public override bool Parse(ParseContext context, ref ParseResult<TextSpan> result) |
| | | 586 | | { |
| | | 587 | | context.EnterParser(this); |
| | | 588 | | |
| | | 589 | | var scanner = context.Scanner; |
| | | 590 | | var start = scanner.Cursor.Offset; |
| | | 591 | | var depth = 0; |
| | | 592 | | |
| | | 593 | | while (!scanner.Cursor.Eof) |
| | | 594 | | { |
| | | 595 | | var ch = scanner.Cursor.Current; |
| | | 596 | | |
| | | 597 | | if (ch == '(') |
| | | 598 | | { |
| | | 599 | | depth++; |
| | | 600 | | scanner.Cursor.Advance(); |
| | | 601 | | } |
| | | 602 | | else if (ch == ')') |
| | | 603 | | { |
| | | 604 | | if (depth == 0) |
| | | 605 | | { |
| | | 606 | | // This is the closing paren for the activity invocation |
| | | 607 | | break; |
| | | 608 | | } |
| | | 609 | | depth--; |
| | | 610 | | scanner.Cursor.Advance(); |
| | | 611 | | } |
| | | 612 | | else |
| | | 613 | | { |
| | | 614 | | scanner.Cursor.Advance(); |
| | | 615 | | } |
| | | 616 | | } |
| | | 617 | | |
| | | 618 | | var length = scanner.Cursor.Offset - start; |
| | | 619 | | if (length == 0) |
| | | 620 | | { |
| | | 621 | | context.ExitParser(this); |
| | | 622 | | return false; |
| | | 623 | | } |
| | | 624 | | |
| | | 625 | | var text = new TextSpan(scanner.Buffer, start, length); |
| | | 626 | | result.Set(start, scanner.Cursor.Offset, text); |
| | | 627 | | context.ExitParser(this); |
| | | 628 | | return true; |
| | | 629 | | } |
| | | 630 | | } |