3 # Parse S-expressions output by the Panel Editor
4 # (which is written in Scheme so it can't help writing S-expressions).
6 # See notes at end of file.
7 from warnings
import warnpy3k
8 warnpy3k("the panelparser module has been removed in Python 3.0", stacklevel
=2)
14 separators
= operators
+ whitespace
+ ';' + '"'
18 # Return a list of tokens (strings).
20 def tokenize_string(s
):
45 if s
[i
] in separators
: break
52 # Tokenize a whole file (given as file object, not as file name).
53 # Return a list of tokens (strings).
55 def tokenize_file(fp
):
60 tokens
= tokens
+ tokenize_string(line
)
64 # Exception raised by parse_exr.
66 syntax_error
= 'syntax error'
69 # Parse an S-expression.
70 # Input is a list of tokens as returned by tokenize_*().
71 # Return a pair (expr, tokens)
72 # where expr is a list representing the s-expression,
73 # and tokens contains the remaining tokens.
74 # May raise syntax_error.
76 def parse_expr(tokens
):
77 if (not tokens
) or tokens
[0] != '(':
78 raise syntax_error
, 'expected "("'
83 raise syntax_error
, 'missing ")"'
85 return expr
, tokens
[1:]
86 elif tokens
[0] == '(':
87 subexpr
, tokens
= parse_expr(tokens
)
90 expr
.append(tokens
[0])
94 # Parse a file (given as file object, not as file name).
95 # Return a list of parsed S-expressions found at the top level.
98 tokens
= tokenize_file(fp
)
101 expr
, tokens
= parse_expr(tokens
)
102 exprlist
.append(expr
)
109 # '(hip (hop hur-ray))'
111 # passed to tokenize_string() returns the token list
112 # ['(', 'hip', '(', 'hop', 'hur-ray', ')', ')']
114 # When this is passed to parse_expr() it returns the expression
115 # ['hip', ['hop', 'hur-ray']]
116 # plus an empty token list (because there are no tokens left.
118 # When a file containing the example is passed to parse_file() it returns
119 # a list whose only element is the output of parse_expr() above:
120 # [['hip', ['hop', 'hur-ray']]]
125 # Comments start with semicolon (;) and continue till the end of the line.
127 # Tokens are separated by whitespace, except the following characters
128 # always form a separate token (outside strings):
130 # Strings are enclosed in double quotes (") and backslash (\) is used
131 # as escape character in strings.