Class ParserException

All Implemented Interfaces:
Serializable

public class ParserException extends OpenExpressionException
Parser Exception 解析异常

Thrown when an error occurs during expression parsing. Includes detailed position information for error reporting.

在表达式解析过程中发生错误时抛出。包含用于错误报告的详细位置信息。

Features | 主要功能:

  • Detailed error position (line, column) - 详细的错误位置(行、列)
  • Expression context with position indicator (^) - 表达式上下文和位置指示符
  • Typed error categories via ErrorType enum - 通过ErrorType枚举的类型化错误分类
  • Static factory methods for common parse errors - 常见解析错误的静态工厂方法

Usage Examples | 使用示例:

try {
    Parser.parse("1 ++ 2");
} catch (ParserException e) {
    int line = e.getLine();
    int col = e.getColumn();
    ParserException.ErrorType type = e.getErrorType();
}

// Create specific errors
throw ParserException.unexpectedToken("+", "1 ++ 2", 3);
throw ParserException.unterminatedString(5);

Security | 安全性:

  • Thread-safe: Yes, immutable after construction - 线程安全: 是,构造后不可变
  • Null-safe: Yes, null expression handled in formatting - 空值安全: 是,null表达式在格式化中处理
Since:
JDK 25, opencode-base-expression V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Details

    • ParserException

      public ParserException(String message)
      Create parser exception 创建解析异常
      Parameters:
      message - the error message | 错误消息
    • ParserException

      public ParserException(String message, int position)
      Create parser exception with position 创建带位置的解析异常
      Parameters:
      message - the error message | 错误消息
      position - the error position | 错误位置
    • ParserException

      public ParserException(String message, String expression, int position)
      Create parser exception with expression and position 创建带表达式和位置的解析异常
      Parameters:
      message - the error message | 错误消息
      expression - the expression | 表达式
      position - the error position | 错误位置
    • ParserException

      public ParserException(String message, String expression, int line, int column, ParserException.ErrorType errorType)
      Create parser exception with full details 创建带完整详情的解析异常
      Parameters:
      message - the error message | 错误消息
      expression - the expression | 表达式
      line - the line number | 行号
      column - the column number | 列号
      errorType - the error type | 错误类型
  • Method Details

    • getExpression

      public String getExpression()
      Get the expression 获取表达式
      Overrides:
      getExpression in class OpenExpressionException
      Returns:
      the expression | 表达式
    • getLine

      public int getLine()
      Get the line number 获取行号
      Returns:
      the line number | 行号
    • getColumn

      public int getColumn()
      Get the column number 获取列号
      Returns:
      the column number | 列号
    • getErrorType

      public ParserException.ErrorType getErrorType()
      Get the error type 获取错误类型
      Returns:
      the error type | 错误类型
    • unexpectedToken

      public static ParserException unexpectedToken(String token, int position)
      Create unexpected token error 创建意外词法单元错误
      Parameters:
      token - the unexpected token | 意外的词法单元
      position - the position | 位置
      Returns:
      the exception | 异常
    • unexpectedToken

      public static ParserException unexpectedToken(String token, String expression, int position)
      Create unexpected token error with expression 创建带表达式的意外词法单元错误
      Parameters:
      token - the unexpected token | 意外的词法单元
      expression - the expression | 表达式
      position - the position | 位置
      Returns:
      the exception | 异常
    • expectedToken

      public static ParserException expectedToken(String expected, String actual, int position)
      Create expected token error 创建期望词法单元错误
      Parameters:
      expected - the expected token | 期望的词法单元
      actual - the actual token | 实际的词法单元
      position - the position | 位置
      Returns:
      the exception | 异常
    • expectedToken

      public static ParserException expectedToken(String expected, String actual, String expression, int position)
      Create expected token error with expression 创建带表达式的期望词法单元错误
      Parameters:
      expected - the expected token | 期望的词法单元
      actual - the actual token | 实际的词法单元
      expression - the expression | 表达式
      position - the position | 位置
      Returns:
      the exception | 异常
    • unexpectedEnd

      public static ParserException unexpectedEnd(String expression)
      Create unexpected end of expression error 创建表达式意外结束错误
      Parameters:
      expression - the expression | 表达式
      Returns:
      the exception | 异常
    • invalidNumber

      public static ParserException invalidNumber(String value, int position)
      Create invalid number format error 创建无效数字格式错误
      Parameters:
      value - the invalid value | 无效的值
      position - the position | 位置
      Returns:
      the exception | 异常
    • unterminatedString

      public static ParserException unterminatedString(int position)
      Create unterminated string error 创建未终止字符串错误
      Parameters:
      position - the position | 位置
      Returns:
      the exception | 异常
    • invalidEscapeSequence

      public static ParserException invalidEscapeSequence(String sequence, int position)
      Create invalid escape sequence error 创建无效转义序列错误
      Parameters:
      sequence - the invalid sequence | 无效的序列
      position - the position | 位置
      Returns:
      the exception | 异常
    • unbalancedParentheses

      public static ParserException unbalancedParentheses(int position)
      Create unbalanced parentheses error 创建不平衡括号错误
      Parameters:
      position - the position | 位置
      Returns:
      the exception | 异常