Commit 447445a3 authored by Stefan Behnel's avatar Stefan Behnel
parent aa4273db
cdef class FastMachine:
cdef readonly dict initial_states
cdef readonly dict new_state_template
cdef readonly list states
cdef readonly Py_ssize_t next_number
...@@ -5,6 +5,7 @@ Classes for building NFAs and DFAs ...@@ -5,6 +5,7 @@ Classes for building NFAs and DFAs
""" """
from __future__ import absolute_import from __future__ import absolute_import
import cython
from .Transitions import TransitionMap from .Transitions import TransitionMap
try: try:
...@@ -12,9 +13,10 @@ try: ...@@ -12,9 +13,10 @@ try:
except ImportError: except ImportError:
from sys import maxint from sys import maxint
try: if not cython.compiled:
try:
unichr unichr
except NameError: except NameError:
unichr = chr unichr = chr
LOWEST_PRIORITY = -maxint LOWEST_PRIORITY = -maxint
...@@ -131,18 +133,14 @@ class FastMachine(object): ...@@ -131,18 +133,14 @@ class FastMachine(object):
FastMachine is a deterministic machine represented in a way that FastMachine is a deterministic machine represented in a way that
allows fast scanning. allows fast scanning.
""" """
initial_states = None # {state_name:state} def __init__(self):
states = None # [state] where state = {event:state, 'else':state, 'action':Action} self.initial_states = {} # {state_name:state}
next_number = 1 # for debugging self.states = [] # [state] where state = {event:state, 'else':state, 'action':Action}
self.next_number = 1 # for debugging
new_state_template = { self.new_state_template = {
'': None, 'bol': None, 'eol': None, 'eof': None, 'else': None '': None, 'bol': None, 'eol': None, 'eof': None, 'else': None
} }
def __init__(self):
self.initial_states = {}
self.states = []
def __del__(self): def __del__(self):
for state in self.states: for state in self.states:
state.clear() state.clear()
...@@ -159,6 +157,7 @@ class FastMachine(object): ...@@ -159,6 +157,7 @@ class FastMachine(object):
def make_initial_state(self, name, state): def make_initial_state(self, name, state):
self.initial_states[name] = state self.initial_states[name] = state
@cython.locals(code0=cython.long, code1=cython.long, maxint=cython.long, state=dict)
def add_transitions(self, state, event, new_state, maxint=maxint): def add_transitions(self, state, event, new_state, maxint=maxint):
if type(event) is tuple: if type(event) is tuple:
code0, code1 = event code0, code1 = event
...@@ -210,9 +209,7 @@ class FastMachine(object): ...@@ -210,9 +209,7 @@ class FastMachine(object):
if char_list: if char_list:
ranges = self.chars_to_ranges(char_list) ranges = self.chars_to_ranges(char_list)
ranges_to_state[ranges] = state ranges_to_state[ranges] = state
ranges_list = ranges_to_state.keys() for ranges in sorted(ranges_to_state):
ranges_list.sort()
for ranges in ranges_list:
key = self.ranges_to_string(ranges) key = self.ranges_to_string(ranges)
state = ranges_to_state[ranges] state = ranges_to_state[ranges]
file.write(" %s --> State %d\n" % (key, state['number'])) file.write(" %s --> State %d\n" % (key, state['number']))
...@@ -221,6 +218,7 @@ class FastMachine(object): ...@@ -221,6 +218,7 @@ class FastMachine(object):
if state: if state:
file.write(" %s --> State %d\n" % (key, state['number'])) file.write(" %s --> State %d\n" % (key, state['number']))
@cython.locals(char_list=list, i=cython.Py_ssize_t, n=cython.Py_ssize_t, c1=cython.long, c2=cython.long)
def chars_to_ranges(self, char_list): def chars_to_ranges(self, char_list):
char_list.sort() char_list.sort()
i = 0 i = 0
......
...@@ -84,6 +84,7 @@ def compile_cython_modules(profile=False, compile_more=False, cython_with_refnan ...@@ -84,6 +84,7 @@ def compile_cython_modules(profile=False, compile_more=False, cython_with_refnan
compiled_modules = [ compiled_modules = [
"Cython.Plex.Scanners", "Cython.Plex.Scanners",
"Cython.Plex.Actions", "Cython.Plex.Actions",
"Cython.Plex.Machines",
"Cython.Compiler.Scanning", "Cython.Compiler.Scanning",
"Cython.Compiler.Visitor", "Cython.Compiler.Visitor",
"Cython.Compiler.FlowControl", "Cython.Compiler.FlowControl",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment