Skip to content

syntax

tuprolog.solve.exception.error.syntax

Syntax errors: some text being read is not valid Prolog.

ISO Prolog's syntax_error error, thrown as error(syntax_error, Extra), means that a sequence of characters being read does not conform to Prolog's grammar: e.g. reading f( by read/1, or converting it to a term by term_to_atom/2. Errors of Python code parsing text (see tuprolog.core.parsing) are tuprolog.errors.ParsingErrors instead, not logic errors.

The error is a SyntaxError (the JVM class: import it explicitly, as it shadows Python's built-in name). Build errors with syntax_error, syntax_error_while_parsing_term, or syntax_error_while_parsing_clauses; error_detector points at the offending character of a text.

Examples:

>>> from tuprolog.solve import signature
>>> from tuprolog.solve.exception.error import *
>>> from tuprolog.solve.prolog import prolog_solver
>>> context = prolog_solver().current_context
>>> syntax_error_while_parsing_term(context, "f(", 1, 3, "unexpected end").error_struct
error(syntax_error, 'Syntax error at 1:3 while parsing `f(`: unexpected end')
See Also

2P-Kt's documentation on errors and exceptions.

syntax_error

syntax_error(
    context: ExecutionContext, message: str
) -> SyntaxError

Creates a syntax error with a message, without positional information.

Parameters:

Name Type Description Default
context ExecutionContext

the execution context in which the error occurs (e.g. a request's context).

required
message str

the error message.

required

Returns:

Type Description
SyntaxError

The error, thrown as error(syntax_error, Message).

Examples:

>>> from tuprolog.solve import signature
>>> from tuprolog.solve.exception.error import *
>>> from tuprolog.solve.prolog import prolog_solver
>>> context = prolog_solver().current_context
>>> syntax_error(context, "missing dot").message
'Syntax error: missing dot'

syntax_error_while_parsing_term

syntax_error_while_parsing_term(
    context: ExecutionContext,
    input: str,
    row: int,
    column: int,
    message: str,
) -> SyntaxError

Creates the syntax error for some text failing to parse as a term.

Parameters:

Name Type Description Default
context ExecutionContext

the execution context in which the error occurs (e.g. a request's context).

required
input str

the text being parsed.

required
row int

the (1-based) line where parsing failed.

required
column int

the (1-based) column where parsing failed.

required
message str

the detail message.

required

Returns:

Type Description
SyntaxError

The error, thrown as error(syntax_error, Message).

Examples:

>>> from tuprolog.solve import signature
>>> from tuprolog.solve.exception.error import *
>>> from tuprolog.solve.prolog import prolog_solver
>>> context = prolog_solver().current_context
>>> syntax_error_while_parsing_term(context, "f(", 1, 3, "unexpected end").message
'Syntax error at 1:3 while parsing `f(`: unexpected end'

syntax_error_while_parsing_clauses

syntax_error_while_parsing_clauses(
    context: ExecutionContext,
    input: str,
    index: int,
    row: int,
    column: int,
    message: str,
) -> SyntaxError

Creates the syntax error for a clause of some text failing to parse.

The message shows the offending lines, with a caret (^) under the offending column (see error_detector).

Parameters:

Name Type Description Default
context ExecutionContext

the execution context in which the error occurs (e.g. a request's context).

required
input str

the text being parsed, e.g. a theory.

required
index int

the index of the clause failing to parse.

required
row int

the (1-based) line where parsing failed.

required
column int

the (1-based) column where parsing failed.

required
message str

the detail message.

required

Returns:

Type Description
SyntaxError

The error, thrown as error(syntax_error, Message).

error_detector

error_detector(
    text: str,
    line: int,
    column: int,
    message: str | None = None,
) -> str

Renders a line of a text with a caret (^) under a column, for error messages.

The previous line, if any, is shown as well, for context.

Parameters:

Name Type Description Default
text str

the text.

required
line int

the (1-based) line to show.

required
column int

the (1-based) column to point at.

required
message str | None

a message to append after the caret, if any.

None

Returns:

Type Description
str

The rendered lines, each prefixed by its number.

Examples:

>>> from tuprolog.solve.exception.error.syntax import error_detector
>>> print(error_detector("foo(bar baz)", 1, 9, "unexpected"))
1: foo(bar baz)
           ^ unexpected