LISP S-expressions

Textual representation

Special characters are:

  • the parenthesis ( and ),
  • the double quote ",
  • the vertical bar |.

Symbols are represented by their name. Vertical bars | can be used to delimit names that contain blanks, special characters, non printable characters, non-ASCII characters, or can be confused as a number.

Numbers follow the syntax specified by the C function strtol() with base=0.

Strings are delimited by double quotes. All C string escapes are recognized. Non-printable ASCII characters must be escaped.

List are represented by an open parenthesis ( followed by the space separated list elements, followed by a closing parenthesis ).

When the cdr of the last pair is non zero, the closed parenthesis is preceded by a space, a dot ., a space, and the textual representation of the cdr. (This is only partially supported by Python bindings.)

Symbols

class djvu.sexpr.Symbol(str)
>>> Symbol('ham')
Symbol('ham')

S-expressions

class djvu.sexpr.Expression

Inheritance diagram:

Inheritance diagram of IntExpression, ListExpression, StringExpression, SymbolExpression

as_string(width=None, escape_unicode=True)

Return a string representation of the expression.

print_into(file, width=None, escape_unicode=True)

Print the expression into the file.

value

The “pythonic” value of the expression. Lisp lists as mapped to Python tuples.

lvalue

The “pythonic” value of the expression. Lisp lists as mapped to Python lists.

New in version 0.4.

class djvu.sexpr.IntExpression

IntExpression can represent any integer in range \left[-2^{29}, 2^{29}\right).

To create objects of this class, use the Expression constructor:

>>> x = Expression(42)
>>> x
Expression(42)
>>> type(x)
<class 'djvu.sexpr.IntExpression'>
>>> x.as_string()
'42'
>>> x.value
42
class djvu.sexpr.ListExpression

To create objects of this class, use the Expression constructor:

>>> x = Expression([4, 2])
>>> x
Expression([4, 2])
>>> type(x)
<class 'djvu.sexpr.ListExpression'>
>>> x.as_string()
'(4 2)'
>>> x.value
(4, 2)
>>> x.lvalue
[4, 2]
class djvu.sexpr.StringExpression

To create objects of this class, use the Expression constructor:

>>> x = Expression('eggs')
>>> x
Expression('eggs')
>>> type(x)
<class 'djvu.sexpr.StringExpression'>
>>> x.as_string()
'"eggs"'
>>> x.value
'eggs'
class djvu.sexpr.SymbolExpression

To create objects of this class, use the Expression constructor:

>>> x = Expression(Symbol('ham'))
>>> x
Expression(Symbol('ham'))
>>> type(x)
<class 'djvu.sexpr.SymbolExpression'>
>>> x.as_string()
'ham'
>>> x.value
Symbol('ham')

Varieties

djvu.sexpr.EMPTY_LIST

Empty list S-expression.

>>> EMPTY_LIST
Expression([])