Kaydet (Commit) 47478878 authored tarafından Guido van Rossum's avatar Guido van Rossum

New batch from Fred

üst 3a1fbb4c
This diff is collapsed.
This diff is collapsed.
"""Object-oriented interface to the parser module. """Object-oriented interface to the parser module.
This module exports three classes which together provide an interface This module exports four classes which together provide an interface
to the parser module. Together, the three classes represent two ways to the parser module. Together, the three classes represent two ways
to create parsed representations of Python source and the two starting to create parsed representations of Python source and the two starting
data types (source text and tuple representations). Each class data types (source text and tuple representations). Each class
provides interfaces which are identical other than the constructors. provides interfaces which are identical other than the constructors.
The constructors are described in detail in the documentation for each The constructors are described in detail in the documentation for each
class and the remaining, shared portion of the interface is documented class and the remaining, shared portion of the interface is documented
below. Briefly, the three classes provided are: below. Briefly, the classes provided are:
AST AST
Defines the primary interface to the AST objects and supports creation Defines the primary interface to the AST objects and supports creation
...@@ -23,6 +23,9 @@ FileSuiteAST ...@@ -23,6 +23,9 @@ FileSuiteAST
Convenience subclass of the `SuiteAST' class; loads source text of the Convenience subclass of the `SuiteAST' class; loads source text of the
suite from an external file. suite from an external file.
Common Methods
--------------
Aside from the constructors, several methods are provided to allow Aside from the constructors, several methods are provided to allow
access to the various interpretations of the parse tree and to check access to the various interpretations of the parse tree and to check
conditions of the construct represented by the parse tree. conditions of the construct represented by the parse tree.
...@@ -68,8 +71,8 @@ class AST: ...@@ -68,8 +71,8 @@ class AST:
This base class provides all of the query methods for subclass This base class provides all of the query methods for subclass
objects defined in this module. objects defined in this module.
""" """
_p = __import__('parser') # import internally to avoid import parser # import internally to avoid
# namespace pollution at the _p = parser # namespace pollution at the
# top level # top level
_text = None _text = None
_code = None _code = None
...@@ -84,7 +87,8 @@ class AST: ...@@ -84,7 +87,8 @@ class AST:
The tuple tree to convert. The tuple tree to convert.
The tuple-tree may represent either an expression or a suite; the The tuple-tree may represent either an expression or a suite; the
type will be determined automatically. type will be determined automatically. Line number information may
optionally be present for any subset of the terminal tokens.
""" """
if type(tuple) is not type(()): if type(tuple) is not type(()):
raise TypeError, 'Base AST class requires tuple parameter.' raise TypeError, 'Base AST class requires tuple parameter.'
...@@ -93,11 +97,24 @@ class AST: ...@@ -93,11 +97,24 @@ class AST:
self._ast = self._p.tuple2ast(tuple) self._ast = self._p.tuple2ast(tuple)
self._type = (self._p.isexpr(self._ast) and 'expression') or 'suite' self._type = (self._p.isexpr(self._ast) and 'expression') or 'suite'
def tuple(self): def list(self, line_info = 0):
"""Returns a fresh list representing the parse tree.
line_info
If true, includes line number information for terminal tokens in
the output data structure,
"""
return self._p.ast2list(self._ast, line_info)
def tuple(self, line_info = 0):
"""Returns the tuple representing the parse tree. """Returns the tuple representing the parse tree.
line_info
If true, includes line number information for terminal tokens in
the output data structure,
""" """
if self._tupl is None: if self._tupl is None:
self._tupl = self._p.ast2tuple(self._ast) self._tupl = self._p.ast2tuple(self._ast, line_info)
return self._tupl return self._tupl
def code(self): def code(self):
......
# Non-terminal symbols of Python grammar (from "graminit.h") #! /usr/bin/env python
#
# Non-terminal symbols of Python grammar (from "graminit.h")
#
# This file is automatically generated; please don't muck it up!
#
# To update the symbols in this file, 'cd' to the top directory of
# the python source tree after building the interpreter and run:
#
# PYTHONPATH=Lib:Modules ./python Lib/symbol.py
#
# (this path allows the import of string.py, token.py, and regexmodule.so
# for a site with no installation in place)
#--start constants--
single_input = 256 single_input = 256
file_input = 257 file_input = 257
eval_input = 258 eval_input = 258
...@@ -23,39 +36,40 @@ raise_stmt = 275 ...@@ -23,39 +36,40 @@ raise_stmt = 275
import_stmt = 276 import_stmt = 276
dotted_name = 277 dotted_name = 277
global_stmt = 278 global_stmt = 278
access_stmt = 279 exec_stmt = 279
accesstype = 280 compound_stmt = 280
exec_stmt = 281 if_stmt = 281
compound_stmt = 282 while_stmt = 282
if_stmt = 283 for_stmt = 283
while_stmt = 284 try_stmt = 284
for_stmt = 285 except_clause = 285
try_stmt = 286 suite = 286
except_clause = 287 test = 287
suite = 288 and_test = 288
test = 289 not_test = 289
and_test = 290 comparison = 290
not_test = 291 comp_op = 291
comparison = 292 expr = 292
comp_op = 293 xor_expr = 293
expr = 294 and_expr = 294
xor_expr = 295 shift_expr = 295
and_expr = 296 arith_expr = 296
shift_expr = 297 term = 297
arith_expr = 298 factor = 298
term = 299 power = 299
factor = 300 atom = 300
power = 301 lambdef = 301
atom = 302 trailer = 302
lambdef = 303 subscriptlist = 303
trailer = 304 subscript = 304
subscript = 305 sliceop = 305
exprlist = 306 exprlist = 306
testlist = 307 testlist = 307
dictmaker = 308 dictmaker = 308
classdef = 309 classdef = 309
arglist = 310 arglist = 310
argument = 311 argument = 311
#--end constants--
names = dir() names = dir()
sym_name = {} sym_name = {}
...@@ -63,3 +77,17 @@ for name in names: ...@@ -63,3 +77,17 @@ for name in names:
number = eval(name) number = eval(name)
if type(number) is type(0): if type(number) is type(0):
sym_name[number] = name sym_name[number] = name
def main():
import sys
import token
if len(sys.argv) == 1:
sys.argv = sys.argv + ["Include/graminit.h", "Lib/symbol.py"]
token.main()
if __name__ == "__main__":
main()
#
# end of file
# Tokens (from "token.h") #! /usr/bin/env python
#
# Tokens (from "token.h")
#
# This file is automatically generated; please don't muck it up!
#
# To update the symbols in this file, 'cd' to the top directory of
# the python source tree after building the interpreter and run:
#
# PYTHONPATH=./Lib ./python Lib/token.py
#
# (this path allows the import of string.py and regexmodule.so
# for a site with no installation in place)
#--start constants--
ENDMARKER = 0 ENDMARKER = 0
NAME = 1 NAME = 1
NUMBER = 2 NUMBER = 2
...@@ -39,6 +52,9 @@ RIGHTSHIFT = 35 ...@@ -39,6 +52,9 @@ RIGHTSHIFT = 35
DOUBLESTAR = 36 DOUBLESTAR = 36
OP = 37 OP = 37
ERRORTOKEN = 38 ERRORTOKEN = 38
N_TOKENS = 39
NT_OFFSET = 256
#--end constants--
names = dir() names = dir()
tok_name = {} tok_name = {}
...@@ -47,9 +63,6 @@ for name in names: ...@@ -47,9 +63,6 @@ for name in names:
if type(number) is type(0): if type(number) is type(0):
tok_name[number] = name tok_name[number] = name
N_TOKENS = 39 # Number of tokens including ERRORTOKEN
NT_OFFSET = 256 # Start of non-terminal symbols
def ISTERMINAL(x): def ISTERMINAL(x):
return x < NT_OFFSET return x < NT_OFFSET
...@@ -58,3 +71,61 @@ def ISNONTERMINAL(x): ...@@ -58,3 +71,61 @@ def ISNONTERMINAL(x):
def ISEOF(x): def ISEOF(x):
return x == ENDMARKER return x == ENDMARKER
def main():
import regex
import string
import sys
args = sys.argv[1:]
inFileName = args and args[0] or "Include/token.h"
outFileName = "Lib/token.py"
if len(args) > 1:
outFileName = args[1]
try:
fp = open(inFileName)
except IOError, err:
sys.stdout.write("I/O error: %s\n" % str(err))
sys.exit(1)
lines = string.splitfields(fp.read(), "\n")
fp.close()
re = regex.compile(
"#define[ \t][ \t]*\([A-Z][A-Z_]*\)[ \t][ \t]*\([0-9][0-9]*\)",
regex.casefold)
tokens = {}
for line in lines:
if re.match(line) > -1:
name, val = re.group(1, 2)
val = string.atoi(val)
tokens[val] = name # reverse so we can sort them...
keys = tokens.keys()
keys.sort()
# load the output skeleton from the target:
try:
fp = open(outFileName)
except IOError, err:
sys.stderr.write("I/O error: %s\n" % str(err))
sys.exit(2)
format = string.splitfields(fp.read(), "\n")
fp.close()
try:
start = format.index("#--start constants--") + 1
end = format.index("#--end constants--")
except ValueError:
sys.stderr.write("target does not contain format markers")
sys.exit(3)
lines = []
for val in keys:
lines.append("%s = %d" % (tokens[val], val))
format[start:end] = lines
try:
fp = open(outFileName, 'w')
except IOError, err:
sys.stderr.write("I/O error: %s\n" % str(err))
sys.exit(4)
fp.write(string.joinfields(format, "\n"))
fp.close()
if __name__ == "__main__":
main()
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment