~ubuntu-branches/ubuntu/breezy/antlr/breezy

« back to all changes in this revision

Viewing changes to examples/python/asn1/asn1.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2005-06-29 16:11:22 UTC
  • mfrom: (0.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20050629161122-g81crc3z92p5xhsg
Tags: 2.7.5-6ubuntu4
Build depend on java-gcj-compat-dev, depend on java-gcj-compat.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#! /usr/bin/python -t
 
2
## --*- python -*--
 
3
 
 
4
import sys
 
5
import antlr
 
6
    
 
7
version = sys.version.split()[0]
 
8
if version < '2.2.1':
 
9
    False = 0
 
10
if version < '2.3':
 
11
    True = not False
 
12
 
 
13
class CharScanner(antlr.CharScanner):
 
14
   def __init__(self,*args):
 
15
      super(CharScanner,self).__init__(*args)
 
16
      self.altcomment = True
 
17
      self.state_with_syntax = False
 
18
 
 
19
   ### check whether a string contains a lower case char
 
20
   def haslowerchar(self,s):
 
21
      return (s.upper() != s)
 
22
 
 
23
   def handle_comment(self):
 
24
      la1 = self.LA(1)
 
25
      if not la1:
 
26
         self.throw_no_viable_alt_for_char(la1)
 
27
      elif la1 in '-':
 
28
         self.match("--")
 
29
      elif la1 in '\n':
 
30
         self.match('\n')
 
31
         self.newline()
 
32
      elif la1 in '\r': 
 
33
         self.match('\r')
 
34
         if self.LA(2) == '\n':
 
35
            self.match('\n')
 
36
         self.newline()
 
37
      elif la1 in u'\u000b' :
 
38
         self.match(u'\u000b')
 
39
         self.newline()
 
40
      elif la1 in u'\u000c':
 
41
         self.match('\u000c')
 
42
         self.newline()
 
43
      else:
 
44
         self.throw_no_viable_alt_for_char(la1)
 
45
 
 
46
 
 
47
   def throw_no_viable_alt_for_char(self,la1):
 
48
      raise antlr.NoViableAltForCharException(
 
49
         la1, 
 
50
         self.getFilename(), 
 
51
         self.getLine(), 
 
52
         self.getColumn()
 
53
         )
 
54
      
 
55
   def chr_ws_erase(self,string,*chars):
 
56
      return string
 
57
 
 
58
 
 
59
if __name__ == '__main__' :
 
60
   ### create my lexer ..
 
61
   ### print "reading from test.in .."
 
62
   Lexer = lexer.Lexer("test.in")
 
63
 
 
64
 
 
65
   token = Lexer.nextToken()
 
66
   while not token.isEOF():
 
67
      ### Is there a way to simplify this loop??
 
68
      ### this looks complicated to me. However, we can't simply
 
69
      ### return none to check  for EOF as we would like to know
 
70
      ### where  EOF appeared  (file, line, col etc). This would
 
71
      ### be lost. Or we could return NIL in case of EOF and, if
 
72
      ### we are really want to know more about EOF ask lexer to
 
73
      ### provide this information. But this would extend the
 
74
      ### lexer's interface. Another idea would be to return EOF
 
75
      ### by  exception, but EOF is actually not an exception at
 
76
      ### all.
 
77
      ### handle token
 
78
      print token
 
79
      token = Lexer.nextToken()
 
80
 
 
81