• R/O
  • HTTP
  • SSH
  • HTTPS

Commit

Tags
No Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

generic text markup tools


Commit MetaInfo

Revisióna108d1a6634b0764d75500470ed232ab98be1698 (tree)
Tiempo2013-04-04 20:04:40
Autorhylom <hylom@hylo...>
Commiterhylom

Log Message

rewriting textparser...

Cambiar Resumen

Diferencia incremental

--- a/textparser.py
+++ b/textparser.py
@@ -3,71 +3,53 @@
33
44 import re
55
6+def CreateMode(lexi, mode_name):
7+ return Mode(lexi, mode_name)
68
7-class Mode(dict):
8- def __init__(self, modes, defs):
9- self.update(defs)
10- self.modes = modes
119
12- def check_to_exit(self, text):
13- rex = self.get("end", False)
10+class ModeBase(dict):
11+ def __init__(self, lexi, mode_name):
12+ self.lexi = lexi
13+ modes = lexi.get("modes")
14+ self.update(modes[mode_name])
15+
16+ def match(self, key, text):
17+ rex = self.get(key, False)
1418 if rex and re.search(rex, text):
1519 return True
1620 else:
1721 return False
1822
19- def exit(self):
20- if "onFinished" in self:
21- m = self["onFinished"]
22- if "insert" in m:
23- self._write(m["insert"])
24- if "replace" in m:
25- text = m["replace"]
26- if len(self.mode_stack) == 0:
27- return text
28- mode = self.mode_stack[-1]
23+ def has_element(self, key):
24+ return (key in self)
2925
26+ def write(self, text):
27+ self.writer.write(text)
3028
31- def apply(self, text):
32- if self._check_to_exit(mode, text):
33- if "onFinished" in self:
34- m = self["onFinished"]
35- if "insert" in m:
36- self._write(m["insert"])
37- if "replace" in m:
38- text = m["replace"]
39- if len(self.mode_stack) == 0:
40- return text
41- mode = self.mode_stack[-1]
29+class Mode(dict):
30+ def __init__(self, lexi, writer, defs):
31+ ModeBase.__init__(self, lexi, writer)
32+ self.update(defs)
4233
43- next_mode = self._transition_check(mode, text)
44- if next_mode:
45- self._transition(next_mode)
46- mode = next_mode
47- if "onStart" in self.modeOf(mode):
48- m = self.modeOf(mode)["onStart"]
49- if "insert" in m:
50- self._write(m["insert"])
34+ def mode_end_check(self, text):
35+ return self.match("end", text)
5136
52- rules = self.rulesOf(mode)
53- for key in self._sorted_keys(rules):
54- rule = rules[key]
55- if rule.get('pass', False):
56- continue
57- if 'regexp' in rule:
58- rex = re.compile(rule['regexp'])
59- match = rex.search(text)
60- if match:
61- sub_rule = rule.get('apply', False)
62- if sub_rule:
63- text = self._apply_rules(sub_rule, text)
64- text = self._do_action(mode, text, rule, rex, match)
65- if not rule.get('continue', True):
66- break
6737
68- return text
38+class ModeStack(object):
39+ def __init__(self):
40+ self.stack = []
41+
42+ def push(self, mode):
43+ self.stack.append(mode)
6944
45+ def pop(self):
46+ return self.stack.pop()
7047
48+ def current(self):
49+ try:
50+ return self.mode_stack[-1]
51+ except IndexError:
52+ return False
7153
7254 class Store(dict):
7355 def __init__(self):
@@ -109,17 +91,18 @@ class Parser(object):
10991 @return TextWriter object
11092 """
11193 self.lexi = lexi
112- self.mode_stack = ["global", ]
94+# self.mode_stack = ["global", ]
95+ self.mode_stack = ModeStack()
96+ mode_global = CreateMode(self.lexi, "global")
97+ self.mode_stack.push(mode_global)
98+
11399 self.store = Store()
114100 self.functions = {
115101 # "getImageGeom": getImageGeom,
116102 }
117103
118- def currentMode(self):
119- try:
120- return self.mode_stack[-1]
121- except IndexError:
122- return False
104+ def current_mode(self):
105+ return self.mode_stack.current()
123106
124107 def markup(self, iter_in, stream_out):
125108 """read from iter_in and output to stream_out
@@ -225,7 +208,7 @@ class Parser(object):
225208 text = self._expand_variable(mode, text)
226209 return text
227210
228- def _apply_rules2(self, mode, text):
211+ def _apply_rules(self, mode, text):
229212 # check global rule
230213 gi = self.lexi.get("globalIdentifier", False)
231214 if gi:
@@ -234,13 +217,17 @@ class Parser(object):
234217 self.store.save_global(m_gvi.group(1), m_gvi.group(2))
235218 return ''
236219
237- # extends
238- if "extends" in self.modeOf(mode):
239- pass
240-
241- if mode.check_to_exit(mode, text):
242- mode.exit()
220+ if self._mode_end_check(mode, text):
243221 self._mode_exit()
222+ if "onFinished" in self.modeOf(mode):
223+ m = self.modeOf(mode)["onFinished"]
224+ if "insert" in m:
225+ self._write(m["insert"])
226+ if "replace" in m:
227+ text = m["replace"]
228+ if len(self.mode_stack) == 0:
229+ return text
230+ mode = self.mode_stack[-1]
244231
245232 next_mode = self._transition_check(mode, text)
246233 if next_mode:
@@ -269,8 +256,7 @@ class Parser(object):
269256
270257 return text
271258
272-
273- def _apply_rules(self, mode, text):
259+ def _apply_rules2(self, text):
274260 # check global rule
275261 gi = self.lexi.get("globalIdentifier", False)
276262 if gi:
@@ -279,26 +265,24 @@ class Parser(object):
279265 self.store.save_global(m_gvi.group(1), m_gvi.group(2))
280266 return ''
281267
282- if self._mode_end_check(mode, text):
283- self._mode_exit()
284- if "onFinished" in self.modeOf(mode):
285- m = self.modeOf(mode)["onFinished"]
268+ mode = self.current_mode()
269+ if mode.mode_end_check(text):
270+ if mode.has_element("onFinished"):
271+ m = mode.get("onFinished")
286272 if "insert" in m:
287273 self._write(m["insert"])
288274 if "replace" in m:
289275 text = m["replace"]
290- if len(self.mode_stack) == 0:
291- return text
292- mode = self.mode_stack[-1]
293276
294- next_mode = self._transition_check(mode, text)
295- if next_mode:
296- self._transition(next_mode)
297- mode = next_mode
298- if "onStart" in self.modeOf(mode):
299- m = self.modeOf(mode)["onStart"]
300- if "insert" in m:
301- self._write(m["insert"])
277+ self.mode_stack.pop()
278+ if len(self.mode_stack) > 0:
279+ # start next mode
280+ if self.current().hasElement("onStart"):
281+ m = self.current()["onStart"]
282+ if "insert" in m:
283+ self._write(m["insert"])
284+ text = self._apply_rules2(text)
285+ return text
302286
303287 rules = self.rulesOf(mode)
304288 for key in self._sorted_keys(rules):
@@ -319,5 +303,7 @@ class Parser(object):
319303 return text
320304
321305
306+
307+
322308 def getImageGeom(context, filename):
323309 return [('width', str(128)), ('height', str(256))]