class documentation

class SExprParser(object):

View In Hierarchy

Class that implements an S-Expression parser.

Method ast Generate an abstract syntax tree (AST) from normalized input.
Method normalizeInput Parse an input string into a list of opening and closing parentheses, and atoms. Atoms include symbols, numbers and strings.
Class Variable errorExpression In case of an error this attribute contains the error expression.
def ast(self, input: SSymbolsList | str, topLevel: bool = True, allowBrackets: bool = False, parentSymbol: SSymbol | None = None) -> SSymbolsList:

Generate an abstract syntax tree (AST) from normalized input.

The result is a list of elements. Each element is either an atom, a string, a number, or again a list of elements.

Parameters
input:SSymbolsList | strEither a string or a list of SSymbol elements. A string would internally be parsed to a list of SSymbol elements before further processing.
topLevel:boolIndicating whether a parsed input is at the top level or a branch of a another AST.
allowBrackets:boolAllow "[" and "]" for opening and closing lists as well.
parentSymbol:SSymbol | NoneThe parent symbol of the current AST.
Returns
SSymbolsListA list that represents the abstract syntax tree.
Raises
ValueErrorIn case of a syntax error (usually missing opening or closing paranthesis).
def normalizeInput(self, input: str, allowBrackets: bool = False) -> SSymbolsList:

Parse an input string into a list of opening and closing parentheses, and atoms. Atoms include symbols, numbers and strings.

The results excludes all whitespaces. Also, special escape characters outside and inside of strings are handled. The escape character is backslash.

Parameters
input:strThe input string.
allowBrackets:boolAllow "[" and "]" for opening and closing lists as well.
Returns
SSymbolsListA list of paranthesis and atoms.
errorExpression: SSymbol =

In case of an error this attribute contains the error expression.