diff pyrect/translator/c_translator.py @ 62:a05baa7dc7ba

modify I/O routine. use mmap. it's really faster than fgets ;-)
author Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
date Fri, 05 Nov 2010 01:37:35 +0900
parents 4ae288b37591
children e327e93aeb3a
line wrap: on
line diff
--- a/pyrect/translator/c_translator.py	Thu Nov 04 22:04:34 2010 +0900
+++ b/pyrect/translator/c_translator.py	Fri Nov 05 01:37:35 2010 +0900
@@ -19,6 +19,7 @@
         if fa == "DFA":
             self.cg = regexp.dfacg
         self.debug = False
+        self.eols = (Character('\0'), Character('\n'), Character('\r'))
         self.special_rule = (Range, BegLine, MBCharacter)
         self.trans_stmt = self._trans_stmt(self.emit)
 
@@ -143,24 +144,21 @@
         else:
             default = "reject"
 
-        any_ = None
-
         for input_ in transition.keys():
             if type(input_) in self.special_rule:
                 self.trans_stmt.emit(input_, self.state_name(transition.pop(input_)))
             elif type(input_) is AnyChar:
-                any_ = (input_, self.state_name(transition.pop(input_)))
-                default = None
+                default = self.state_name(transition.pop(input_))
 
         if cur_state in self.cg.accepts:
-            eol = Character('\0')
-            transition[eol] = "accept"
+            for eol in self.eols:
+                transition[eol] = "accept"
+        elif default != "reject":
+            for eol in self.eols:
+                transition[eol] = "reject"
 
         self.emit_switch(transition, default)
 
-        if any_:
-            self.trans_stmt.emit(any_[0], any_[1])
-
         self.emitd("}", 2)
 
     def emit_initialization(self):
@@ -196,7 +194,7 @@
             self._emit("/* %s */" % input_node.__repr__())
 
         def visit_Character(self, char):
-            self._emit("case %d: /* match %s */" % (char.char, chr(char.char)))
+            self._emit("case %d: /* match %s */" % (char.char, char))
             self._emit("  return %s(s);" % self.next)
 
         def visit_EndLine(self, endline):
@@ -231,11 +229,6 @@
                 self._emit("if ('%s' <= *s && *s <= '%s')" % (range.lower.char, range.upper.char))
                 self._emit("  return %s(s+1);" % self.next, 2)
 
-        def visit_AnyChar(self, anychar):
-            self._emit(r"if (*s != '\0')")
-            self._emit(   "return %s(SKIP(s));" % self.next, 2)
-            self._emit("return reject(s);")
-
 def test():
     import doctest
     doctest.testmod()