scanner module

Read the circuit definition file and translate the characters into symbols.

Used in the Logic Simulator project to read the characters in the definition file and translate them into symbols that are usable by the parser.

class scanner.Scanner(path: str, names: names.Names, errors: exceptions.Errors)

Bases: object

Read circuit definition file and translate the characters into symbols.

Once supplied with the path to a valid definition file, the scanner translates the sequence of characters in the definition file into symbols that the parser can use. It also skips over comments and irrelevant formatting characters, such as spaces and line breaks.

Parameters
  • path (str) – path to the circuit definition file.

  • names (Names) – instance of the names.Names() class.

BLOCK_COMMENT_IDENTIFIERS = ('/*', '*/')
class EOF

Bases: object

Symbol indicating the end of file.

LINE_COMMENT_IDENTIFIER = '//'
TREAT_INVALID_CHAR_AS_ERROR = True
get_line_by_lineno(lineno: int) str

Get the content of a line using the line number.

get_line_by_pos(pos: int) str

Get the content of a line a given position is on.

get_lineno_colno(pos: int) Tuple[int, int]

Get the line and column numbers of a position in file.

The position is zero-indexed. It is allowed to be one greater than the length of the file, which denotes end of file.

get_symbol() Optional[scanner.Symbol]

Translate the next sequence of characters into a symbol.

If the end of file is reached, None will be returned instead of a Symbol instance.

property pointer: Tuple[int, int, int]

Pointer position, line number, and column number.

property pointer_colno: int

Column number of the pointer.

It is zero-indexed and protected. It can be one larger than the length of the line if the pointer is on the last line, which denotes the end of file is reached.

property pointer_lineno: int

Line number of the pointer.

It is zero-indexed and protected.

property pointer_pos: Union[int, Type[scanner.Scanner.EOF]]

Pointer position in the file.

It is zero-indexed and protected. If the pointer is at end of file, Scanner.EOF will be returned.

class scanner.Symbol(symbol_type: Union[symbol_types.ReservedSymbolType, symbol_types.ExternalSymbolType], symbol_id: int, lineno: Optional[int] = None, colno: Optional[int] = None)

Bases: object

Encapsulate a symbol and store its properties.

Parameters
  • symbol_type (Union[ReservedSymbolType, ExternalSymbolType]) – The type of the symbol

  • symbol_id (int) – ID of the symbol, from the Names instance

  • lineno – The line number of the symbol string in file (optional)

  • colno – The column number of the start of the symbol string in file (optional)