printers.py :  » Parser » SimpleParse » SimpleParse-2.1.1a2 » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Parser » SimpleParse 
SimpleParse » SimpleParse 2.1.1a2 » printers.py
"""Utility to print Python code for a given generator object's element tokens"""
import string
class _GeneratorFormatter:
  """Singleton Class to give a generator's element tokens as a source string

  Call this as:
    printers.asGenerator( generator ) to get a Python source string
    that tries to recreate the generator as a set of objectgenerator
    element token objects (as seen in simpleparsegrammar).
  """
  HEAD = """from simpleparse import generator
from simpleparse.objectgenerator import *
GENERATOR = generator.Generator ()

class Parser:
  '''Mix-in class for simpleparse.parser.Parser which uses this GENERATOR
  to build tagging tables.  You'll likely want to override __init__ to avoid
  building a new parser from a grammar (or subclass BaseParser instead of
  Parser)
  '''
  def buildTagger( self, name=None, processor = None ):
    '''Build the tag-table for parsing the EBNF for this parser'''
    return GENERATOR.buildParser( name, processor )

"""
  ITEM = """GENERATOR.addDefinition(
    %(name)s,
    %(element)s,
)
"""
  def __call__( self, generator ):
    temp = [self.HEAD]
    for name,element in map(None, generator.getNames(), generator.getRootObjects()):
      name = repr(name)
      element = self.reprObject(element,1)
      temp.append( self.ITEM%locals())
    return string.join( temp, "")
  def reprObject( self, obj, depth=0, indent='    ' ):
    """Return a recognisable version of an objectgenerator element token"""
    argTemplate = (indent*(depth+1))+"%s = %s,"
    temp = ["""%s("""%(obj.__class__.__name__)]
    for key,value in obj.__dict__.items():
      if key == 'children':
        childTemplate = (indent*(depth+2)) + '%s,'
        childTemp = ["["]
        for child in value:
          childTemp.append(childTemplate%self.reprObject(child,depth+2))
        childTemp.append( (indent*(depth+1))+']' )
        
        temp.append(
          argTemplate% (key, string.join(childTemp, '\n'))
        )
      else:
        temp.append( argTemplate%( key, repr(value)))
    temp.append( (indent*depth)+')')
    return string.join( temp,'\n')

asGenerator = _GeneratorFormatter()
asObject = asGenerator.reprObject

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.