Error handling and reporting

class trlc.errors.Message_Handler(brief=False, detailed_info=True)

Bases: object

Universal message handler

All messages from TRLC are processed by this class. If you want to write a tool that emits additional messages then it would be a really good idea to also use this class. Do not use your own print statements.

If the location comes from the location attribute of Node then you also get context provided for free.

Attribute brief:

When true displays as much context as possible

Type:

Boolean

Attribute warnings:

Number of system or user warnings raised

Type:

int

Attribute errors:

Number of system or user errors raised

Type:

int

Attribute supressed:

Number of messages supressed by policy

Type:

int

error(location, message, explanation=None, fatal=True, user=False)

Create an error message

For example:

mh.error(my_expr.location, "potato")

Might generate this output:

x = 5 + 2
      ^ foo.check:5: error: potato
Parameters:
  • location (Location) – where to attach the message

  • message (str) – the message to print

  • fatal (bool) – should we raise an exception in addition to printing the error?

  • user (bool) – if set print “check error:” instead of “error:”

Raises:

TRLC_Error – if fatal is true

warning(location, message, explanation=None, user=False)

Create a warning message

Parameters:
  • location (Location) – where to attach the message

  • message (str) – the message to print

  • user (bool) – if set print “check warning:” instead of “warning:”

class trlc.errors.Location(file_name, line_no=None, col_no=None)

Bases: object

Reference to a source or virtual location

Any message raised by the Message_Handler will be attached to a given location. This location can be real (i.e. something in a file) or virtual (i.e. a builtin function).

Attribute file_name:

the name of the file or virtual location

Type:

str

Attribute line_no:

an optional line number, starting at 1

Type:

int

Attribute col_no:

an optional column number, starting at 1

Type:

int:

get_end_location()

Get location point to the end of this location

When we generate a location for a longer sequence then this function gets the “end” of it:

for example here
    ^^^^^^^ this is the whole range
    ^ file/line/col points here
          ^ file/line/col of end_location points here
Returns:

a pointer to the last character in a location

Return type:

Location

to_string(include_column=True)

Return a nice string representation

The style is the gcc-style file:line:column format. Note that the filename is stripped of its path in order to make the final message smaller.

Parameters:

include_column (bool) – If set, include the column location (if there is one)

Returns:

a formatted location

Return type:

str

class trlc.errors.TRLC_Error(location, kind, message)

Bases: Exception

The universal exception that TRLC raises if something goes wrong

Attribute location:

Where the issue originates from

Type:

Location

Attribute kind:

The kind of problem (e.g. lex error, error, warning, etc.)

Type:

str

Attribute message:

Description of the problem

Type:

str