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
"""
from __future__ import absolute_import
import cython
from .Transitions import TransitionMap
try:
......@@ -12,9 +13,10 @@ try:
except ImportError:
from sys import maxint
try:
if not cython.compiled:
try:
unichr
except NameError:
except NameError:
unichr = chr
LOWEST_PRIORITY = -maxint
......@@ -131,18 +133,14 @@ class FastMachine(object):
FastMachine is a deterministic machine represented in a way that
allows fast scanning.
"""
initial_states = None # {state_name:state}
states = None # [state] where state = {event:state, 'else':state, 'action':Action}
next_number = 1 # for debugging
new_state_template = {
def __init__(self):
self.initial_states = {} # {state_name:state}
self.states = [] # [state] where state = {event:state, 'else':state, 'action':Action}
self.next_number = 1 # for debugging
self.new_state_template = {
'': None, 'bol': None, 'eol': None, 'eof': None, 'else': None
}
def __init__(self):
self.initial_states = {}
self.states = []
def __del__(self):
for state in self.states:
state.clear()
......@@ -159,6 +157,7 @@ class FastMachine(object):
def make_initial_state(self, 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):
if type(event) is tuple:
code0, code1 = event
......@@ -210,9 +209,7 @@ class FastMachine(object):
if char_list:
ranges = self.chars_to_ranges(char_list)
ranges_to_state[ranges] = state
ranges_list = ranges_to_state.keys()
ranges_list.sort()
for ranges in ranges_list:
for ranges in sorted(ranges_to_state):
key = self.ranges_to_string(ranges)
state = ranges_to_state[ranges]
file.write(" %s --> State %d\n" % (key, state['number']))
......@@ -221,6 +218,7 @@ class FastMachine(object):
if state:
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):
char_list.sort()
i = 0
......
......@@ -84,6 +84,7 @@ def compile_cython_modules(profile=False, compile_more=False, cython_with_refnan
compiled_modules = [
"Cython.Plex.Scanners",
"Cython.Plex.Actions",
"Cython.Plex.Machines",
"Cython.Compiler.Scanning",
"Cython.Compiler.Visitor",
"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