| |
- AttrToken
- ParseContext
- Tree
-
- ID
- List
- exceptions.Exception(exceptions.BaseException)
-
- ParseError
class AttrToken |
|
AttrToken: represents a named value encountered during parsing.
This is an internal class; it should not be used outside this module. |
|
Methods defined here:
- __init__(self, key)
|
class List(Tree) |
|
List: represents a list expression.
A list can contain positional entries and named values; it therefore
acts as both an array and a dict.
Array-style operations:
len(l)
l[int]
l[int:int]
l.append(tree)
Dict-style operations:
l.has_attr(key)
l.get_attr(key)
l.set_attr(key, tree)
(The keys in these operations must be strings.)
Positional (array) values and named (dict) values are separate. If l
has no positional values, len(l) is zero, no matter how many named
values it has. Contrariwise, l.get_attr() will never retrieve a
positional value.
List(val, val, ... key=val, key=val) -- constructor
Construct a List with the given named and/or positional values. All
values must be Trees. You can also construct a List from a Python
list or dict, using the forms List(*list) or List(**dict). |
|
Methods defined here:
- __contains__(self, it)
- __getitem__(self, key)
- __init__(self, *args, **attrs)
- __iter__(self)
- __len__(self)
- append(self, val)
- append(val) -> None
Add the Tree as the last positional entry.
- get_attr(self, key)
- get_attr(key) -> Tree
Retrieve the named Tree which has the given key. If there is
no entry with that key, returns None.
- has_attr(self, key)
- has_attr(key) -> bool
Returns whether there is an entry with the given key.
- serialize(self)
- set_attr(self, key, val)
- set_attr(key, val) -> None
Add the Tree val as a named entry, with the given key.
Methods inherited from Tree:
- __repr__(self)
- as_boolean(self)
- as_boolean() -> bool
Get the boolean value which this tree represents. The empty
string is considered false, as are '0', 'no', or 'false'. Or
really any string beginning with '0', 'n', 'N', 'f', or 'F'.
Anything else is true.
Raises ValueError if called on a List.
- as_float(self)
- as_float() -> float
Get the float value which this tree represents.
Raises ValueError if called on a List, or on a string which
is not interpretable as a float.
- as_integer(self)
- as_integer() -> int or long
Get the integer value which this tree represents.
Raises ValueError if called on a List, or on a string which
is not interpretable as an integer.
- as_string(self)
- as_string() -> str or unicode
Get the string which this tree represents. (The result is not
quoted or escaped; it is the underlying string value.)
Raises ValueError if called on a List.
|
class ParseContext |
|
ParseContext: represents the state of an ongoing parse() operation.
Parsing S-expressions is quite simple; we only need a stream of
characters and the ability to look ahead by one. (Or, if you like,
the ability to push one character back onto the stream.)
Fields:
fl -- a file-like object, from which characters are read.
nextch -- if a character has been pushed back, it is here;
if not, this is None.
Constructor:
ParseContext(fl) -- constructor |
|
Methods defined here:
- __init__(self, fl)
- close(self)
- close() -> None
Shut down the parser, and close the underlying stream.
- finalwhite(self)
- finalwhite() -> None
Ensure that there are no more expressions in the stream. Trailing
whitespace is ignored.
Raises ParseError on failure.
- parseattr(self)
- parseattr() -> Tree
Parse the value part of a named value. The stream must be after
the equals sign.
- parseid(self)
- parseid() -> ID or AttrToken
Parse an unquoted string expression. The stream must be at the
beginning of the expression.
- parselist(self)
- parselist() -> List
Parse a parenthesized list expression. The stream must be at
the beginning of the list contents, after the open parenthesis.
- parsestring(self)
- parsestring() -> ID
Parse an quoted string expression. The stream must be at the
beginning of the expression, before the initial quote.
- parsetree(self)
- parsetree() -> Tree or EndOfList or AttrToken
Parse one expression from the stream, and return the Tree that
represents it. Leading whitespace is ignored.
EndOfList indicates that a closing parenthesis has been
reached; an AttrToken instance indicates a named value such
as x=y. These are not valid expressions on their own; they can
only occur inside lists.
|
class Tree |
|
Tree: represents an S-expression.
This is a virtual base class. Do not instantiate it; instead use
the List or ID class. |
|
Methods defined here:
- __repr__(self)
- as_boolean(self)
- as_boolean() -> bool
Get the boolean value which this tree represents. The empty
string is considered false, as are '0', 'no', or 'false'. Or
really any string beginning with '0', 'n', 'N', 'f', or 'F'.
Anything else is true.
Raises ValueError if called on a List.
- as_float(self)
- as_float() -> float
Get the float value which this tree represents.
Raises ValueError if called on a List, or on a string which
is not interpretable as a float.
- as_integer(self)
- as_integer() -> int or long
Get the integer value which this tree represents.
Raises ValueError if called on a List, or on a string which
is not interpretable as an integer.
- as_string(self)
- as_string() -> str or unicode
Get the string which this tree represents. (The result is not
quoted or escaped; it is the underlying string value.)
Raises ValueError if called on a List.
- serialize(self)
- serialize() -> str or unicode
Convert this tree into its textual representation. Strings will
be quoted and escaped if necessary.
| |