Version: dev

expression_parse_error

Stable error · Expressions

Tier: stable · Kind: error · Topic: Expressions

JuliaTypeScriptPythonRustGo
ExpressionParseErrorExpressionParseErrorExpressionParseErrorExpressionParseErrorExpressionParseError

Julia

struct ExpressionParseError <: EarthSciASTError
ExpressionParseError(msg::String, pos::Int64)
ExpressionParseError(msg, pos)
ExpressionParseError(msg, pos)

Thrown when an expression string cannot be parsed by parse_expression or parse_equation .

  • msg: human-readable diagnostic.
  • pos: 0-based CHARACTER offset into the source where parsing failed (matching the pos field of the TypeScript reference implementation).

Source

TypeScript

class ExpressionParseError extends EsmDiagnosticError
new ExpressionParseError(message: string, pos: number): ExpressionParseError

Thrown when an expression string cannot be parsed.

Source

Example (expressions)

// Expressions are plain JSON trees; build one directly ...
const built: Expr = { op: '*', args: ['k', { op: '-', args: ['T_wall', 'T_air'] }] }

// ... or parse the text form, which is the inverse of `toAscii`.
const parsed = parseExpression('k * (T_wall - T_air)')
assert.deepEqual(parsed, built)
assert.deepEqual([...freeVariables(parsed)].sort(), ['T_air', 'T_wall', 'k'])
assert.equal(contains(parsed, 'T_wall'), true)

// Substitution is single-pass and returns a new tree.
const bound = substitute(parsed, { T_wall: 300, k: 0.05 })
assert.equal(toAscii(bound), '0.05 * (300 - T_air)')
assert.deepEqual([...freeVariables(bound)], ['T_air'])
assert.equal(toAscii(simplify(substitute(parsed, { T_wall: 300, T_air: 300 }))), '0')

const eq = parseEquation('D(T_air) = k * (T_wall - T_air)')
assert.deepEqual(eq.rhs, built)

assert.throws(() => parseExpression('k * (T_wall'), ExpressionParseError)

From docs/api/examples/typescript/expressions.ts

Python

class ExpressionParseError(EarthSciAstError, ValueError)
ExpressionParseError(message: str, pos: int)

Raised when an expression string cannot be parsed.

Subclasses ValueError as well as EarthSciAstError , matching the convention of ParseError, so callers that except ValueError around an authoring call still catch it.

:ivar pos: 0-based character offset into the source where parsing failed.

Source

Example (expression_parse_error)

try:
    earthsci_ast.parse_expression("k * (T_wall - ")
except earthsci_ast.ExpressionParseError as exc:
    print("parse error:", exc)
else:
    raise AssertionError("an unbalanced parenthesis should not parse")

From docs/api/examples/python/expressions.py

Rust

pub struct ExpressionParseError {
    pub message: String,
    pub pos: usize,
}

Returned when an expression string cannot be parsed.

Source · Rust docs

Go

type ExpressionParseError struct {
	Message string
	Pos     int
}

func (e *ExpressionParseError) Error() string

ExpressionParseError reports that an expression string could not be parsed. Pos is a 0-based RUNE offset into the source (not a byte offset — names such as ∂u_∂z and ∇phi are legal identifiers). Callers should match it with errors.As.

Methods

func (e *ExpressionParseError) Error() string

Source · Go docs