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,10 +13,11 @@ try: ...@@ -12,10 +13,11 @@ try:
except ImportError: except ImportError:
from sys import maxint from sys import maxint
try: if not cython.compiled:
unichr try:
except NameError: unichr
unichr = chr except NameError:
unichr = chr
LOWEST_PRIORITY = -maxint LOWEST_PRIORITY = -maxint
...@@ -131,17 +133,13 @@ class FastMachine(object): ...@@ -131,17 +133,13 @@ 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}
states = None # [state] where state = {event:state, 'else':state, 'action':Action}
next_number = 1 # for debugging
new_state_template = {
'': None, 'bol': None, 'eol': None, 'eof': None, 'else': None
}
def __init__(self): def __init__(self):
self.initial_states = {} self.initial_states = {} # {state_name:state}
self.states = [] 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 __del__(self): def __del__(self):
for state in self.states: for state in self.states:
...@@ -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",
...@@ -236,24 +237,24 @@ def run_build(): ...@@ -236,24 +237,24 @@ def run_build():
The Cython language makes writing C extensions for the Python language as The Cython language makes writing C extensions for the Python language as
easy as Python itself. Cython is a source code translator based on Pyrex_, easy as Python itself. Cython is a source code translator based on Pyrex_,
but supports more cutting edge functionality and optimizations. but supports more cutting edge functionality and optimizations.
The Cython language is a superset of the Python language (almost all Python The Cython language is a superset of the Python language (almost all Python
code is also valid Cython code), but Cython additionally supports optional code is also valid Cython code), but Cython additionally supports optional
static typing to natively call C functions, operate with C++ classes and static typing to natively call C functions, operate with C++ classes and
declare fast C types on variables and class attributes. This allows the declare fast C types on variables and class attributes. This allows the
compiler to generate very efficient C code from Cython code. compiler to generate very efficient C code from Cython code.
This makes Cython the ideal language for writing glue code for external This makes Cython the ideal language for writing glue code for external
C/C++ libraries, and for fast C modules that speed up the execution of C/C++ libraries, and for fast C modules that speed up the execution of
Python code. Python code.
Note that for one-time builds, e.g. for CI/testing, on platforms that are not Note that for one-time builds, e.g. for CI/testing, on platforms that are not
covered by one of the wheel packages provided on PyPI *and* the pure Python wheel covered by one of the wheel packages provided on PyPI *and* the pure Python wheel
that we provide is not used, it is substantially faster than a full source build that we provide is not used, it is substantially faster than a full source build
to install an uncompiled (slower) version of Cython with:: to install an uncompiled (slower) version of Cython with::
pip install Cython --install-option="--no-cython-compile" pip install Cython --install-option="--no-cython-compile"
.. _Pyrex: https://www.cosc.canterbury.ac.nz/greg.ewing/python/Pyrex/ .. _Pyrex: https://www.cosc.canterbury.ac.nz/greg.ewing/python/Pyrex/
"""), """),
license='Apache', license='Apache',
......
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