Back to index

enigmail  1.4.3
Public Member Functions | Public Attributes
ply.yacc.ParserReflect Class Reference

List of all members.

Public Member Functions

def __init__
def get_all
def validate_all
def signature
def validate_files
def get_start
def validate_start
def get_error_func
def validate_error_func
def get_tokens
def validate_tokens
def get_precedence
def validate_precedence
def get_pfunctions
def validate_pfunctions

Public Attributes

 pdict
 start
 error_func
 tokens
 files
 grammar
 error
 log
 prec
 preclist
 pfuncs

Detailed Description

Definition at line 2762 of file yacc.py.


Constructor & Destructor Documentation

def ply.yacc.ParserReflect.__init__ (   self,
  pdict,
  log = None 
)

Definition at line 2763 of file yacc.py.

02763 
02764     def __init__(self,pdict,log=None):
02765         self.pdict      = pdict
02766         self.start      = None
02767         self.error_func = None
02768         self.tokens     = None
02769         self.files      = {}
02770         self.grammar    = []
02771         self.error      = 0
02772 
02773         if log is None:
02774             self.log = PlyLogger(sys.stderr)
02775         else:
02776             self.log = log


Member Function Documentation

Definition at line 2778 of file yacc.py.

02778 
02779     def get_all(self):
02780         self.get_start()
02781         self.get_error_func()
02782         self.get_tokens()
02783         self.get_precedence()
02784         self.get_pfunctions()
        

Here is the call graph for this function:

Definition at line 2865 of file yacc.py.

02865 
02866     def get_error_func(self):
02867         self.error_func = self.pdict.get('p_error')

Here is the caller graph for this function:

Definition at line 2958 of file yacc.py.

02958 
02959     def get_pfunctions(self):
02960         p_functions = []
02961         for name, item in self.pdict.items():
02962             if name[:2] != 'p_': continue
02963             if name == 'p_error': continue
02964             if isinstance(item,(types.FunctionType,types.MethodType)):
02965                 line = func_code(item).co_firstlineno
02966                 file = func_code(item).co_filename
02967                 p_functions.append((line,file,name,item.__doc__))
02968 
02969         # Sort all of the actions by line number
02970         p_functions.sort()
02971         self.pfuncs = p_functions
02972 

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 2923 of file yacc.py.

02923 
02924     def get_precedence(self):
02925         self.prec = self.pdict.get("precedence",None)

Here is the caller graph for this function:

Definition at line 2855 of file yacc.py.

02855 
02856     def get_start(self):
02857         self.start = self.pdict.get('start')

Here is the caller graph for this function:

Definition at line 2889 of file yacc.py.

02889 
02890     def get_tokens(self):
02891         tokens = self.pdict.get("tokens",None)
02892         if not tokens:
02893             self.log.error("No token list is defined")
02894             self.error = 1
02895             return
02896 
02897         if not isinstance(tokens,(list, tuple)):
02898             self.log.error("tokens must be a list or tuple")
02899             self.error = 1
02900             return
02901         
02902         if not tokens:
02903             self.log.error("tokens is empty")
02904             self.error = 1
02905             return
02906 
02907         self.tokens = tokens

Here is the caller graph for this function:

Definition at line 2796 of file yacc.py.

02796 
02797     def signature(self):
02798         try:
02799             from hashlib import md5
02800         except ImportError:
02801             from md5 import md5
02802         try:
02803             sig = md5()
02804             if self.start:
02805                 sig.update(self.start.encode('latin-1'))
02806             if self.prec:
02807                 sig.update("".join(["".join(p) for p in self.prec]).encode('latin-1'))
02808             if self.tokens:
02809                 sig.update(" ".join(self.tokens).encode('latin-1'))
02810             for f in self.pfuncs:
02811                 if f[3]:
02812                     sig.update(f[3].encode('latin-1'))
02813         except (TypeError,ValueError):
02814             pass
02815         return sig.digest()

Definition at line 2786 of file yacc.py.

02786 
02787     def validate_all(self):
02788         self.validate_start()
02789         self.validate_error_func()
02790         self.validate_tokens()
02791         self.validate_precedence()
02792         self.validate_pfunctions()
02793         self.validate_files()
02794         return self.error

Here is the call graph for this function:

Definition at line 2869 of file yacc.py.

02869 
02870     def validate_error_func(self):
02871         if self.error_func:
02872             if isinstance(self.error_func,types.FunctionType):
02873                 ismethod = 0
02874             elif isinstance(self.error_func, types.MethodType):
02875                 ismethod = 1
02876             else:
02877                 self.log.error("'p_error' defined, but is not a function or method")
02878                 self.error = 1
02879                 return
02880 
02881             eline = func_code(self.error_func).co_firstlineno
02882             efile = func_code(self.error_func).co_filename
02883             self.files[efile] = 1
02884 
02885             if (func_code(self.error_func).co_argcount != 1+ismethod):
02886                 self.log.error("%s:%d: p_error() requires 1 argument",efile,eline)
02887                 self.error = 1

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 2827 of file yacc.py.

02827 
02828     def validate_files(self):
02829         # Match def p_funcname(
02830         fre = re.compile(r'\s*def\s+(p_[a-zA-Z_0-9]*)\(')
02831 
02832         for filename in self.files.keys():
02833             base,ext = os.path.splitext(filename)
02834             if ext != '.py': return 1          # No idea. Assume it's okay.
02835 
02836             try:
02837                 f = open(filename)
02838                 lines = f.readlines()
02839                 f.close()
02840             except IOError:
02841                 continue
02842 
02843             counthash = { }
02844             for linen,l in enumerate(lines):
02845                 linen += 1
02846                 m = fre.match(l)
02847                 if m:
02848                     name = m.group(1)
02849                     prev = counthash.get(name)
02850                     if not prev:
02851                         counthash[name] = linen
02852                     else:
02853                         self.log.warning("%s:%d: Function %s redefined. Previously defined on line %d", filename,linen,name,prev)

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 2974 of file yacc.py.

02974 
02975     def validate_pfunctions(self):
02976         grammar = []
02977         # Check for non-empty symbols
02978         if len(self.pfuncs) == 0:
02979             self.log.error("no rules of the form p_rulename are defined")
02980             self.error = 1
02981             return 
02982         
02983         for line, file, name, doc in self.pfuncs:
02984             func = self.pdict[name]
02985             if isinstance(func, types.MethodType):
02986                 reqargs = 2
02987             else:
02988                 reqargs = 1
02989             if func_code(func).co_argcount > reqargs:
02990                 self.log.error("%s:%d: Rule '%s' has too many arguments",file,line,func.__name__)
02991                 self.error = 1
02992             elif func_code(func).co_argcount < reqargs:
02993                 self.log.error("%s:%d: Rule '%s' requires an argument",file,line,func.__name__)
02994                 self.error = 1
02995             elif not func.__doc__:
02996                 self.log.warning("%s:%d: No documentation string specified in function '%s' (ignored)",file,line,func.__name__)
02997             else:
02998                 try:
02999                     parsed_g = parse_grammar(doc,file,line)
03000                     for g in parsed_g:
03001                         grammar.append((name, g))
03002                 except SyntaxError:
03003                     e = sys.exc_info()[1]
03004                     self.log.error(str(e))
03005                     self.error = 1
03006 
03007                 # Looks like a valid grammar rule
03008                 # Mark the file in which defined.
03009                 self.files[file] = 1
03010 
03011         # Secondary validation step that looks for p_ definitions that are not functions
03012         # or functions that look like they might be grammar rules.
03013 
03014         for n,v in self.pdict.items():
03015             if n[0:2] == 'p_' and isinstance(v, (types.FunctionType, types.MethodType)): continue
03016             if n[0:2] == 't_': continue
03017             if n[0:2] == 'p_' and n != 'p_error':
03018                 self.log.warning("'%s' not defined as a function", n)
03019             if ((isinstance(v,types.FunctionType) and func_code(v).co_argcount == 1) or
03020                 (isinstance(v,types.MethodType) and func_code(v).co_argcount == 2)):
03021                 try:
03022                     doc = v.__doc__.split(" ")
03023                     if doc[1] == ':':
03024                         self.log.warning("%s:%d: Possible grammar rule '%s' defined without p_ prefix",
03025                                          func_code(v).co_filename, func_code(v).co_firstlineno,n)
03026                 except Exception:
03027                     pass
03028 
03029         self.grammar = grammar
03030 
03031 # -----------------------------------------------------------------------------
03032 # yacc(module)
03033 #
03034 # Build a parser
03035 # -----------------------------------------------------------------------------

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 2927 of file yacc.py.

02927 
02928     def validate_precedence(self):
02929         preclist = []
02930         if self.prec:
02931             if not isinstance(self.prec,(list,tuple)):
02932                 self.log.error("precedence must be a list or tuple")
02933                 self.error = 1
02934                 return
02935             for level,p in enumerate(self.prec):
02936                 if not isinstance(p,(list,tuple)):
02937                     self.log.error("Bad precedence table")
02938                     self.error = 1
02939                     return
02940 
02941                 if len(p) < 2:
02942                     self.log.error("Malformed precedence entry %s. Must be (assoc, term, ..., term)",p)
02943                     self.error = 1
02944                     return
02945                 assoc = p[0]
02946                 if not isinstance(assoc,str):
02947                     self.log.error("precedence associativity must be a string")
02948                     self.error = 1
02949                     return
02950                 for term in p[1:]:
02951                     if not isinstance(term,str):
02952                         self.log.error("precedence items must be strings")
02953                         self.error = 1
02954                         return
02955                     preclist.append((term,assoc,level+1))
02956         self.preclist = preclist

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 2859 of file yacc.py.

02859 
02860     def validate_start(self):
02861         if self.start is not None:
02862             if not isinstance(self.start,str):
02863                 self.log.error("'start' must be a string")

Here is the caller graph for this function:

Definition at line 2909 of file yacc.py.

02909 
02910     def validate_tokens(self):
02911         # Validate the tokens.
02912         if 'error' in self.tokens:
02913             self.log.error("Illegal token name 'error'. Is a reserved word")
02914             self.error = 1
02915             return
02916 
02917         terminals = {}
02918         for n in self.tokens:
02919             if n in terminals:
02920                 self.log.warning("Token '%s' multiply defined", n)
02921             terminals[n] = 1

Here is the caller graph for this function:


Member Data Documentation

Definition at line 2770 of file yacc.py.

Definition at line 2766 of file yacc.py.

Definition at line 2768 of file yacc.py.

Definition at line 2769 of file yacc.py.

Definition at line 2773 of file yacc.py.

Definition at line 2764 of file yacc.py.

Definition at line 2970 of file yacc.py.

Definition at line 2924 of file yacc.py.

Definition at line 2955 of file yacc.py.

Definition at line 2765 of file yacc.py.

Definition at line 2767 of file yacc.py.


The documentation for this class was generated from the following file: