Mercurial > hg > Members > shinya > pyrect
changeset 85:b34a900a3a0b
modify table-lookup option.
author | Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Thu, 11 Nov 2010 10:17:11 +0900 |
parents | f5c4193913a1 |
children | 20a1c25d1fc9 d23f12ce0369 |
files | pyrect/translator/grep_translator.py pyrect/translator/template/grep.c |
diffstat | 2 files changed, 17 insertions(+), 26 deletions(-) [+] |
line wrap: on
line diff
--- a/pyrect/translator/grep_translator.py Thu Nov 11 01:59:02 2010 +0900 +++ b/pyrect/translator/grep_translator.py Thu Nov 11 10:17:11 2010 +0900 @@ -86,29 +86,9 @@ else: self.skip_boost = False - if self.table_lookup: - self.emit_table() - else: - self.emit("#define __TABLE_INIT__", 2) - grepsource = open(self.BASE_DIR + "/template/grep.c") self.emit(grepsource.read()) - def emit_table(self): - trans = self.cg.map[self.cg.start] - default = self.state_name(trans.pop(AnyChar())) - if self.skip_boost: default = "booster" - for eol in self.eols: - trans[eol] = "reject" - self.emit("#define __TABLE_INIT__ table_init();") - self.emit("void (*start_table[256])(UCHARP, UCHARP, UCHARP);") - self.emiti("void table_init() {") - self.emit( "int i;") - self.emit( "for (i = 0; i < 255; start_table[i++] = %s);" % default) - for c, n in trans.iteritems(): - self.emit("start_table[%d] = %s; /* %s */" % (c.char, self.state_name(n), c)) - self.emitd("}", 2) - def emit_bmh_filter(self, key): l = len(key) def emit_next(): @@ -278,20 +258,32 @@ self.emitd("}", 2) return - if cur_state == self.cg.start and self.table_lookup: - self.emit( "return start_table[*buf++](%s);" % self.args) + if transition.has_key(AnyChar()): + default = self.state_name(transition.pop(AnyChar())) + else: + default = self.state_name(self.cg.start) + + if self.table_lookup and (cur_state == self.cg.start or \ + self.state_name(cur_state) == default): + if self.skip_boost and default == self.state_name(self.cg.start): + default = "booster" + tbl = [default] * 256 + for eol in self.eols: + tbl[eol.char] = "reject" + for c, n in transition.iteritems(): + tbl[c.char] = self.state_name(n) + self.emit( "static void (*%s_table[256])(UCHARP, UCHARP, UCHARP) = {%s};" + % (self.state_name(cur_state), ", ".join(tbl))) + self.emit( "return %s_table[*buf++](%s);" % (self.state_name(cur_state), self.args)) self.emitd("}", 2) return - default = self.state_name(self.cg.start) for eol in self.eols: transition[eol] = "reject" 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: - default = self.state_name(transition.pop(input_)) self.emit_switch(transition, default)