summaryrefslogtreecommitdiffstats
path: root/Demo/parser
diff options
context:
space:
mode:
Diffstat (limited to 'Demo/parser')
-rw-r--r--Demo/parser/example.py146
-rw-r--r--Demo/parser/source.py18
-rwxr-xr-xDemo/parser/test_parser.py36
3 files changed, 100 insertions, 100 deletions
diff --git a/Demo/parser/example.py b/Demo/parser/example.py
index 363f5bb..821cef0 100644
--- a/Demo/parser/example.py
+++ b/Demo/parser/example.py
@@ -14,7 +14,7 @@ def get_docs(fileName):
"""Retrieve information from the parse tree of a source file.
fileName
- Name of the file to read Python source code from.
+ Name of the file to read Python source code from.
"""
source = open(fileName).read()
import os
@@ -30,86 +30,86 @@ class SuiteInfoBase:
_name = ''
def __init__(self, tree = None):
- self._class_info = {}
- self._function_info = {}
- if tree:
- self._extract_info(tree)
+ self._class_info = {}
+ self._function_info = {}
+ if tree:
+ self._extract_info(tree)
def _extract_info(self, tree):
- # extract docstring
- if len(tree) == 2:
- found, vars = match(DOCSTRING_STMT_PATTERN[1], tree[1])
- else:
- found, vars = match(DOCSTRING_STMT_PATTERN, tree[3])
- if found:
- self._docstring = eval(vars['docstring'])
- # discover inner definitions
- for node in tree[1:]:
- found, vars = match(COMPOUND_STMT_PATTERN, node)
- if found:
- cstmt = vars['compound']
- if cstmt[0] == symbol.funcdef:
- name = cstmt[2][1]
- self._function_info[name] = FunctionInfo(cstmt)
- elif cstmt[0] == symbol.classdef:
- name = cstmt[2][1]
- self._class_info[name] = ClassInfo(cstmt)
+ # extract docstring
+ if len(tree) == 2:
+ found, vars = match(DOCSTRING_STMT_PATTERN[1], tree[1])
+ else:
+ found, vars = match(DOCSTRING_STMT_PATTERN, tree[3])
+ if found:
+ self._docstring = eval(vars['docstring'])
+ # discover inner definitions
+ for node in tree[1:]:
+ found, vars = match(COMPOUND_STMT_PATTERN, node)
+ if found:
+ cstmt = vars['compound']
+ if cstmt[0] == symbol.funcdef:
+ name = cstmt[2][1]
+ self._function_info[name] = FunctionInfo(cstmt)
+ elif cstmt[0] == symbol.classdef:
+ name = cstmt[2][1]
+ self._class_info[name] = ClassInfo(cstmt)
def get_docstring(self):
- return self._docstring
+ return self._docstring
def get_name(self):
- return self._name
+ return self._name
def get_class_names(self):
- return self._class_info.keys()
+ return self._class_info.keys()
def get_class_info(self, name):
- return self._class_info[name]
+ return self._class_info[name]
def __getitem__(self, name):
- try:
- return self._class_info[name]
- except KeyError:
- return self._function_info[name]
+ try:
+ return self._class_info[name]
+ except KeyError:
+ return self._function_info[name]
class SuiteFuncInfo:
# Mixin class providing access to function names and info.
def get_function_names(self):
- return self._function_info.keys()
+ return self._function_info.keys()
def get_function_info(self, name):
- return self._function_info[name]
+ return self._function_info[name]
class FunctionInfo(SuiteInfoBase, SuiteFuncInfo):
def __init__(self, tree = None):
- self._name = tree[2][1]
- SuiteInfoBase.__init__(self, tree and tree[-1] or None)
+ self._name = tree[2][1]
+ SuiteInfoBase.__init__(self, tree and tree[-1] or None)
class ClassInfo(SuiteInfoBase):
def __init__(self, tree = None):
- self._name = tree[2][1]
- SuiteInfoBase.__init__(self, tree and tree[-1] or None)
+ self._name = tree[2][1]
+ SuiteInfoBase.__init__(self, tree and tree[-1] or None)
def get_method_names(self):
- return self._function_info.keys()
+ return self._function_info.keys()
def get_method_info(self, name):
- return self._function_info[name]
+ return self._function_info[name]
class ModuleInfo(SuiteInfoBase, SuiteFuncInfo):
def __init__(self, tree = None, name = "<string>"):
- self._name = name
- SuiteInfoBase.__init__(self, tree)
- if tree:
- found, vars = match(DOCSTRING_STMT_PATTERN, tree[1])
- if found:
- self._docstring = vars["docstring"]
+ self._name = name
+ SuiteInfoBase.__init__(self, tree)
+ if tree:
+ found, vars = match(DOCSTRING_STMT_PATTERN, tree[1])
+ if found:
+ self._docstring = vars["docstring"]
from types import ListType, TupleType
@@ -118,14 +118,14 @@ def match(pattern, data, vars=None):
"""Match `data' to `pattern', with variable extraction.
pattern
- Pattern to match against, possibly containing variables.
+ Pattern to match against, possibly containing variables.
data
- Data to be checked and against which variables are extracted.
+ Data to be checked and against which variables are extracted.
vars
- Dictionary of variables which have already been found. If not
- provided, an empty dictionary is created.
+ Dictionary of variables which have already been found. If not
+ provided, an empty dictionary is created.
The `pattern' value may contain variables of the form ['varname'] which
are allowed to match anything. The value that is matched is returned as
@@ -138,18 +138,18 @@ def match(pattern, data, vars=None):
values.
"""
if vars is None:
- vars = {}
- if type(pattern) is ListType: # 'variables' are ['varname']
- vars[pattern[0]] = data
- return 1, vars
+ vars = {}
+ if type(pattern) is ListType: # 'variables' are ['varname']
+ vars[pattern[0]] = data
+ return 1, vars
if type(pattern) is not TupleType:
- return (pattern == data), vars
+ return (pattern == data), vars
if len(data) != len(pattern):
- return 0, vars
+ return 0, vars
for pattern, data in map(None, pattern, data):
- same, vars = match(pattern, data, vars)
- if not same:
- break
+ same, vars = match(pattern, data, vars)
+ if not same:
+ break
return same, vars
@@ -172,21 +172,21 @@ DOCSTRING_STMT_PATTERN = (
(symbol.small_stmt,
(symbol.expr_stmt,
(symbol.testlist,
- (symbol.test,
- (symbol.and_test,
- (symbol.not_test,
- (symbol.comparison,
- (symbol.expr,
- (symbol.xor_expr,
- (symbol.and_expr,
- (symbol.shift_expr,
- (symbol.arith_expr,
- (symbol.term,
- (symbol.factor,
- (symbol.power,
- (symbol.atom,
- (token.STRING, ['docstring'])
- )))))))))))))))),
+ (symbol.test,
+ (symbol.and_test,
+ (symbol.not_test,
+ (symbol.comparison,
+ (symbol.expr,
+ (symbol.xor_expr,
+ (symbol.and_expr,
+ (symbol.shift_expr,
+ (symbol.arith_expr,
+ (symbol.term,
+ (symbol.factor,
+ (symbol.power,
+ (symbol.atom,
+ (token.STRING, ['docstring'])
+ )))))))))))))))),
(token.NEWLINE, '')
))
diff --git a/Demo/parser/source.py b/Demo/parser/source.py
index b1690a5..b900628 100644
--- a/Demo/parser/source.py
+++ b/Demo/parser/source.py
@@ -9,18 +9,18 @@ class Simple:
"This class does very little."
def method(self):
- "This method does almost nothing."
- return 1
+ "This method does almost nothing."
+ return 1
class Nested:
- "This is a nested class."
+ "This is a nested class."
- def nested_method(self):
- "Method of Nested class."
- def nested_function():
- "Function in method of Nested class."
- pass
- return nested_function
+ def nested_method(self):
+ "Method of Nested class."
+ def nested_function():
+ "Function in method of Nested class."
+ pass
+ return nested_function
def function():
"This function lives at the module level."
diff --git a/Demo/parser/test_parser.py b/Demo/parser/test_parser.py
index f91592f..3f02a96 100755
--- a/Demo/parser/test_parser.py
+++ b/Demo/parser/test_parser.py
@@ -11,24 +11,24 @@ def testChunk(t, fileName):
global _numFailed
print '----', fileName,
try:
- ast = parser.suite(t)
- tup = parser.ast2tuple(ast)
- # this discards the first AST; a huge memory savings when running
- # against a large source file like Tkinter.py.
- ast = None
- new = parser.tuple2ast(tup)
+ ast = parser.suite(t)
+ tup = parser.ast2tuple(ast)
+ # this discards the first AST; a huge memory savings when running
+ # against a large source file like Tkinter.py.
+ ast = None
+ new = parser.tuple2ast(tup)
except parser.ParserError, err:
- print
- print 'parser module raised exception on input file', fileName + ':'
- traceback.print_exc()
- _numFailed = _numFailed + 1
+ print
+ print 'parser module raised exception on input file', fileName + ':'
+ traceback.print_exc()
+ _numFailed = _numFailed + 1
else:
- if tup != parser.ast2tuple(new):
- print
- print 'parser module failed on input file', fileName
- _numFailed = _numFailed + 1
- else:
- print 'o.k.'
+ if tup != parser.ast2tuple(new):
+ print
+ print 'parser module failed on input file', fileName
+ _numFailed = _numFailed + 1
+ else:
+ print 'o.k.'
def testFile(fileName):
t = open(fileName).read()
@@ -38,8 +38,8 @@ def test():
import sys
args = sys.argv[1:]
if not args:
- import glob
- args = glob.glob("*.py")
+ import glob
+ args = glob.glob("*.py")
map(testFile, args)
sys.exit(_numFailed != 0)