Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Eike Welk

By: a guest on Apr 14th, 2008  |  syntax: Python  |  size: 3.25 KB  |  views: 55  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. # -*- coding: utf-8 -*-
  2. #Author: Eike Welk <eike@users.sourceforge.net>
  3. #This computer program has been placed in the public domain.
  4.  
  5. from pyparsing import ParseElementEnhance, Keyword, Word, Group, ZeroOrMore, \
  6.     OneOrMore, StringEnd, alphas, alphanums, ParseException, \
  7.     ParseFatalException, _ustr
  8.  
  9.  
  10. #Took code from pyparsing.Optional as a template
  11. class ErrStop(ParseElementEnhance):
  12.     """Parser that prevents backtracking.
  13.       The parser tries to match the given expression. If this expression does not match
  14.       the parser raises a ParseFatalException and parsing stops.
  15.       Otherwise, if the given expression matches, its parse results are returned and
  16.       the ErrStop has no effect on the parse results.
  17.    """
  18.     #TODO: implement setErrorAction( callableObject )
  19.     #TODO: implement setErrorMessage( errorMsgStr )
  20.     def __init__( self, expr ):
  21.         super(ErrStop,self).__init__( expr, savelist=False )
  22.         self.mayReturnEmpty = True
  23.  
  24.     def parseImpl( self, instring, loc, doActions=True ):
  25.         try:
  26.             loc, tokens = self.expr._parse( instring, loc, doActions, callPreParse=False )
  27.         except IndexError:
  28.             raise ParseFatalException(instring, loc, 'Index error: ', self.expr)
  29.         except ParseException, theError:
  30.             raise ParseFatalException(instring, theError.loc, theError.msg, self.expr)
  31.         return loc, tokens
  32.  
  33.     def __str__( self ):
  34.         if hasattr(self,"name"):
  35.             return self.name
  36.  
  37.         if self.strRepr is None:
  38.             self.strRepr = "[" + _ustr(self.expr) + "]"
  39.  
  40.         return self.strRepr
  41.  
  42.  
  43. #------- Define the Language -----------------------------------------------------
  44. # Some Basics
  45. identifier = Word(alphas+'_', alphanums+'_')
  46. attrNameList = Group(identifier + ZeroOrMore(',' + identifier))
  47. # The statements
  48. dataDef1 = Group(Keyword('data') + attrNameList + ':' + identifier + ';')
  49. dataDef2 = Group(Keyword('data') + ErrStop(attrNameList + ':' + identifier + ';'))
  50. foo1 = Group(Keyword('foo1') + ';')
  51. foo2 = Group(Keyword('foo2') + ';')
  52. # The top level parsers
  53. programPs1 = OneOrMore(dataDef1 | foo1 | foo2) + StringEnd()
  54. programPs2 = OneOrMore(dataDef2 | foo1 | foo2) + StringEnd()
  55.  
  56.  
  57. #------------ Test the parsers -----------------------------------------------------
  58. # example "programs"
  59. progGood = \
  60. 'foo1; data a, a1, b: Real; foo1;'
  61. progBad = \
  62. 'foo1; data a, a1 b: Real; foo1;' # missing ',' after char 15
  63.  
  64. print 'Test regular parser:'
  65. print programPs1.parseString(progGood)
  66. try:
  67.     print programPs1.parseString(progBad)
  68. except ParseException, theError:
  69.     print theError
  70.  
  71. print
  72. print 'Test parser with backtracking stop:'
  73. print programPs2.parseString(progGood)
  74. try:
  75.     print programPs2.parseString(progBad)
  76. except (ParseException, ParseFatalException), theError:
  77.     print theError
  78.  
  79.  
  80. # --------- The program should print the following lines: --------------------------
  81. #Test regular parser:
  82. #[['foo1', ';'], ['data', ['a', ',', 'a1', ',', 'b'], ':', 'Real', ';'], ['foo1', ';']]
  83. #Expected end of text (at char 6), (line:1, col:7)
  84. #
  85. #Test parser with backtracking stop:
  86. #[['foo1', ';'], ['data', ['a', ',', 'a1', ',', 'b'], ':', 'Real', ';'], ['foo1', ';']]
  87. #Expected ":" (at char 17), (line:1, col:18)