Debian Codegraph @

pypy/interpreter/pyparser/automata.py

raw symbols SCIP
1 # ______________________________________________________________________
2 """Module automata
3
4 THIS FILE WAS COPIED FROM pypy/module/parser/pytokenize.py AND ADAPTED
5 TO BE ANNOTABLE (Mainly made the DFA's __init__ accept two lists
6 instead of a unique nested one)
7
8 $Id: automata.py,v 1.2 2003/10/02 17:37:17 jriehl Exp $
9 """
10 # ______________________________________________________________________
11 # Module level definitions
12
13 # PYPY Modification: removed the EMPTY class as it's not needed here
14
15
16 # PYPY Modification: DEFAULT is a singleton, used only in the pre-RPython
17 # dicts (see pytokenize.py). Then DFA.__init__() turns these dicts into
18 # more compact strings.
19 DEFAULT = object()
20
21 # PYPY Modification : removed all automata functions (any, maybe,
22 # newArcPair, etc.)
23
24 ERROR_STATE = chr(255)
25
26 # NB: all non-ascii bytes (>= 128) will be turned into 128
27 NON_ASCII = chr(128)
28
29
30 class DFA:
31 # ____________________________________________________________
32 def __init__(self, states, accepts, start = 0):
33 """ NOT_RPYTHON """
34 assert len(states) < 255 # no support for huge amounts of states
35 # construct string for looking up state transitions
36 string_states = [] * len(states)
37 # compute maximum
38 maximum = 0
39 for state in states:
40 for key in state:
41 if key == DEFAULT:
42 continue
43 ordkey = ord(key)
44 if ordkey > 128:
45 raise ValueError("DFA does not support matching of specific non-ASCII character %r. Use NON_ASCII instead" % key)
46 maximum = max(ordkey, maximum)
47 self.max_char = maximum + 1
48
49 defaults = []
50 for i, state in enumerate(states):
51 default = ERROR_STATE
52 if DEFAULT in state:
53 default = chr(state[DEFAULT])
54 defaults.append(default)
55 string_state = [default] * self.max_char
56 for key, value in state.iteritems():
57 if key == DEFAULT:
58 continue
59 assert len(key) == 1
60 assert ord(key) < self.max_char
61 string_state[ord(key)] = chr(value)
62 string_states.extend(string_state)
63 self.states = "".join(string_states)
64 self.defaults = "".join(defaults)
65 self.accepts = accepts
66 self.start = start
67
68 # ____________________________________________________________
69
70 def _next_state(self, item, crntState):
71 if ord(item) >= self.max_char:
72 return self.defaults[crntState]
73 else:
74 return self.states[crntState * self.max_char + ord(item)]
75
76 def recognize(self, inVec, pos = 0):
77 crntState = self.start
78 lastAccept = False
79 i = pos
80 for i in range(pos, len(inVec)):
81 item = inVec[i]
82 if ord(item) > 0x80:
83 item = NON_ASCII
84 accept = self.accepts[crntState]
85 crntState = self._next_state(item, crntState)
86 if crntState != ERROR_STATE:
87 pass
88 elif accept:
89 return i
90 elif lastAccept:
91 # This is now needed b/c of exception cases where there are
92 # transitions to dead states
93 return i - 1
94 else:
95 return -1
96 crntState = ord(crntState)
97 lastAccept = accept
98 # if self.states[crntState][1]:
99 if self.accepts[crntState]:
100 return i + 1
101 elif lastAccept:
102 return i
103 else:
104 return -1
105
106 # ______________________________________________________________________
107
108 class NonGreedyDFA (DFA):
109
110 def recognize(self, inVec, pos = 0):
111 crntState = self.start
112 i = pos
113 for i in range(pos, len(inVec)):
114 item = inVec[i]
115 if ord(item) > 0x80:
116 item = NON_ASCII
117 accept = self.accepts[crntState]
118 if accept:
119 return i
120 crntState = self._next_state(item, crntState)
121 if crntState == ERROR_STATE:
122 return -1
123 crntState = ord(crntState)
124 i += 1
125 if self.accepts[crntState]:
126 return i
127 else:
128 return -1
129
130 # ______________________________________________________________________
131 # End of automata.py