Mercurial > hg > Members > shinya > pyrect
changeset 84:f5c4193913a1
add table-lookup option.
author | Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Thu, 11 Nov 2010 01:59:02 +0900 |
parents | 68cefeb3bee1 |
children | b34a900a3a0b |
files | pyrect/jitgrep.py pyrect/translator/grep_translator.py pyrect/translator/template/grep.c |
diffstat | 3 files changed, 25 insertions(+), 10 deletions(-) [+] |
line wrap: on
line diff
--- a/pyrect/jitgrep.py Wed Nov 10 22:48:33 2010 +0900 +++ b/pyrect/jitgrep.py Thu Nov 11 01:59:02 2010 +0900 @@ -28,6 +28,7 @@ psr.add_option("--time", action="store_true", dest="time", default=False, help="Print compile/matching time.") psr.add_option("--thread", action="store", type="string", dest="thread", default="0", metavar="FILE", help="number of thread.") psr.add_option("--disable-booster", action="store_true", dest="no_boost", default=False, help="disable boosetr (default: use booster).") + psr.add_option("--enable-table-lookup", action="store_true", dest="table_lookup", default=False, help="use table-lookup in first-state's transition.") psr.add_option("--filter", action="store", type="string", dest="filter", default="", help="chose filtering-algorithm bmh(default), quick, or none.") psr.add_option("--debug", action="store_true", dest="debug", default=False, help="Dump commands, not evalute matching (except interactive mode).") psr.add_option("--label", action="store_true", dest="label", default=False, help="label implimentation in C.") @@ -77,6 +78,7 @@ grept = GREPTranslator(reg) if opts.filter: grept.filter = opts.filter grept.skip_boost = not opts.no_boost + grept.table_lookup = opts.table_lookup grept.thread_line = int(opts.thread) grept.bufsize = bufsize
--- a/pyrect/translator/grep_translator.py Wed Nov 10 22:48:33 2010 +0900 +++ b/pyrect/translator/grep_translator.py Thu Nov 11 01:59:02 2010 +0900 @@ -30,6 +30,7 @@ self.filter_only = False self.filter_prefix = False self.skip_boost = True + self.table_lookup = False self.start = "matcher" self.interface = "UCHARP beg, UCHARP buf, UCHARP end" self.args = "beg, buf, end" @@ -85,18 +86,29 @@ else: self.skip_boost = False - self.emiti("void (*first_state_tbl[256])(UCHARP, UCHARP, UCHARP) = {") - tbl = ["booster"] * 256 - for c, n in self.cg.map[self.cg.start].iteritems(): - if type(c) != Character: continue - else: tbl[c.char] = self.state_name(n) - tbl[0] = tbl[10] = tbl[13] = "reject" - self.emit(", ".join(tbl)) - self.emitd("};", 2) + 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(): @@ -266,8 +278,8 @@ self.emitd("}", 2) return - if cur_state == self.cg.start: - self.emit( "return first_state_tbl[*buf++](%s);" % self.args) + if cur_state == self.cg.start and self.table_lookup: + self.emit( "return start_table[*buf++](%s);" % self.args) self.emitd("}", 2) return