ยปCore Development>Code coverage>Lib/plat-irix6/panelparser.py

Python code coverage for Lib/plat-irix6/panelparser.py

#countcontent
1n/a# Module 'parser'
2n/a#
3n/a# Parse S-expressions output by the Panel Editor
4n/a# (which is written in Scheme so it can't help writing S-expressions).
5n/a#
6n/a# See notes at end of file.
7n/afrom warnings import warnpy3k
8n/awarnpy3k("the panelparser module has been removed in Python 3.0", stacklevel=2)
9n/adel warnpy3k
10n/a
11n/a
12n/awhitespace = ' \t\n'
13n/aoperators = '()\''
14n/aseparators = operators + whitespace + ';' + '"'
15n/a
16n/a
17n/a# Tokenize a string.
18n/a# Return a list of tokens (strings).
19n/a#
20n/adef tokenize_string(s):
21n/a tokens = []
22n/a while s:
23n/a c = s[:1]
24n/a if c in whitespace:
25n/a s = s[1:]
26n/a elif c == ';':
27n/a s = ''
28n/a elif c == '"':
29n/a n = len(s)
30n/a i = 1
31n/a while i < n:
32n/a c = s[i]
33n/a i = i+1
34n/a if c == '"': break
35n/a if c == '\\': i = i+1
36n/a tokens.append(s[:i])
37n/a s = s[i:]
38n/a elif c in operators:
39n/a tokens.append(c)
40n/a s = s[1:]
41n/a else:
42n/a n = len(s)
43n/a i = 1
44n/a while i < n:
45n/a if s[i] in separators: break
46n/a i = i+1
47n/a tokens.append(s[:i])
48n/a s = s[i:]
49n/a return tokens
50n/a
51n/a
52n/a# Tokenize a whole file (given as file object, not as file name).
53n/a# Return a list of tokens (strings).
54n/a#
55n/adef tokenize_file(fp):
56n/a tokens = []
57n/a while 1:
58n/a line = fp.readline()
59n/a if not line: break
60n/a tokens = tokens + tokenize_string(line)
61n/a return tokens
62n/a
63n/a
64n/a# Exception raised by parse_exr.
65n/a#
66n/asyntax_error = 'syntax error'
67n/a
68n/a
69n/a# Parse an S-expression.
70n/a# Input is a list of tokens as returned by tokenize_*().
71n/a# Return a pair (expr, tokens)
72n/a# where expr is a list representing the s-expression,
73n/a# and tokens contains the remaining tokens.
74n/a# May raise syntax_error.
75n/a#
76n/adef parse_expr(tokens):
77n/a if (not tokens) or tokens[0] != '(':
78n/a raise syntax_error, 'expected "("'
79n/a tokens = tokens[1:]
80n/a expr = []
81n/a while 1:
82n/a if not tokens:
83n/a raise syntax_error, 'missing ")"'
84n/a if tokens[0] == ')':
85n/a return expr, tokens[1:]
86n/a elif tokens[0] == '(':
87n/a subexpr, tokens = parse_expr(tokens)
88n/a expr.append(subexpr)
89n/a else:
90n/a expr.append(tokens[0])
91n/a tokens = tokens[1:]
92n/a
93n/a
94n/a# Parse a file (given as file object, not as file name).
95n/a# Return a list of parsed S-expressions found at the top level.
96n/a#
97n/adef parse_file(fp):
98n/a tokens = tokenize_file(fp)
99n/a exprlist = []
100n/a while tokens:
101n/a expr, tokens = parse_expr(tokens)
102n/a exprlist.append(expr)
103n/a return exprlist
104n/a
105n/a
106n/a# EXAMPLE:
107n/a#
108n/a# The input
109n/a# '(hip (hop hur-ray))'
110n/a#
111n/a# passed to tokenize_string() returns the token list
112n/a# ['(', 'hip', '(', 'hop', 'hur-ray', ')', ')']
113n/a#
114n/a# When this is passed to parse_expr() it returns the expression
115n/a# ['hip', ['hop', 'hur-ray']]
116n/a# plus an empty token list (because there are no tokens left.
117n/a#
118n/a# When a file containing the example is passed to parse_file() it returns
119n/a# a list whose only element is the output of parse_expr() above:
120n/a# [['hip', ['hop', 'hur-ray']]]
121n/a
122n/a
123n/a# TOKENIZING:
124n/a#
125n/a# Comments start with semicolon (;) and continue till the end of the line.
126n/a#
127n/a# Tokens are separated by whitespace, except the following characters
128n/a# always form a separate token (outside strings):
129n/a# ( ) '
130n/a# Strings are enclosed in double quotes (") and backslash (\) is used
131n/a# as escape character in strings.