elsa-workflows/elsa-core · error · ParseException

Failed to parse ElsaScript

Error message

Failed to parse ElsaScript: {errorMessage}

What it means

ElsaScriptParser wraps all parse failures from ProgramParser.TryParse in a single ParseException whose message includes the underlying error and character position. It is the top-level parse gate: any syntax error in an ElsaScript source surfaces this way, with the root cause embedded in the message text.

Solutions

  1. Read the embedded '{error.Message} at {position}' portion and fix the syntax at that exact character position
  2. Validate braces, parentheses, and statement separators around the reported position
  3. Compare the failing syntax with a known-good ElsaScript example of the same construct
  4. Parse incrementally: comment out blocks and re-parse to isolate the offending region

Example fix

// before (missing colon after label)
flowchart
  Start WriteLine("hi")
// after
flowchart
  Start: WriteLine("hi")
Defensive patterns

Strategy: try-catch

Try / catch

try { var program = ElsaScriptParser.Parse(source); }
catch (ParseException ex)
{
    // ex.Message contains 'Failed to parse ElsaScript: <msg> at <position>'
    var at = ex.Message.LastIndexOf(" at ");
    var pos = at >= 0 ? ex.Message[(at + 4)..] : "unknown";
    logger.LogError(ex, "ElsaScript syntax error near position {Pos}", pos);
    throw;
}

Prevention

When it happens

Trigger: Calling the parser (e.g. via ElsaScriptCompiler or the ElsaScript language provider) with source that violates ElsaScript grammar — unbalanced braces, missing separators, invalid activity syntax, stray characters — at any position reported in the message.

Common situations: Hand-editing workflow scripts and introducing typos; paste from docs with mismatched syntax versions; generating scripts programmatically with malformed output; wrong quoting/escaping in string literals.

Understand the failure class

Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.

Related errors


AI-assisted analysis of elsa-workflows/elsa-core@fe9217bdfa (2026-09-13). Data as JSON: /api/errors/1df503688f022eb4. Report an issue: GitHub.

Appendix: source

Thrown at src/modules/Elsa.Dsl.ElsaScript/Parser/ElsaScriptParser.cs:547

                        }
                    }
                };
            });

        var programParser = programWithWorkflow.Or(programWithStatements);

        ProgramParser = programParser;
    }

    /// <inheritdoc />
    public ProgramNode Parse(string source)
    {
        if (!ProgramParser.TryParse(source, out var result, out var error))
        {
            var errorMessage = error != null 
                ? $"{error.Message} at {error.Position}" 
                : "Unknown parse error";
            throw new ParseException($"Failed to parse ElsaScript: {errorMessage}");
        }
        return result;
    }

    /// <summary>
    /// Static helper to evaluate constant expressions during parsing.
    /// </summary>
    private static object EvaluateConstantExpressionStatic(ExpressionNode exprNode)
    {
        return exprNode switch
        {
            LiteralNode literal => literal.Value ?? string.Empty,
            IdentifierNode identifier => identifier.Name,
            _ => string.Empty
        };
    }
}

View on GitHub (pinned to fe9217bdfa)