Mercurial > paraspace
changeset 38:0766bd54c9d3
Formally parse path into parts in structpath.py
author | Thinker K.F. Li <thinker@codemud.net> |
---|---|
date | Wed, 15 Jun 2011 22:16:00 +0800 |
parents | ee8aeb299f10 |
children | aa05cc7ccd0d |
files | paraspace/structpath.py |
diffstat | 1 files changed, 80 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- a/paraspace/structpath.py Wed Jun 15 13:30:11 2011 +0800 +++ b/paraspace/structpath.py Wed Jun 15 22:16:00 2011 +0800 @@ -24,6 +24,10 @@ # objs = find_objs_path(ctx, '/car/wheels') # \endcode # +import re + +_reo_nest_chars = re.compile('[\'"\\[\\]/\\\\]') + class context(object): all_classes = None class_instances = None @@ -44,9 +48,83 @@ pass +STATE_START = 0 +STATE_PRED = 1 +STATE_STR_SQ = 2 +STATE_STR_DQ = 3 +STATE_BKSP = 4 + def _path_split(path): - parts = [p.strip() - for p in path.split('/')] + stk = [] + idx = 0 + left = 0 + state = STATE_START + parts = [] + while True: + mo = _reo_nest_chars.search(path, idx) + + if mo == None: + parts.append(path[left:]) + break + + ch = mo.group() + idx = mo.end() + if state == STATE_START: + if ch == '/': + parts.append(path[left:idx - 1]) + left = idx + elif ch == '[': + stk.append(state) + state = STATE_PRED + elif ch == '\'': + stk.append(state) + state = STATE_STR_SQ + elif ch == '"': + stk.append(state) + state = STATE_STR_DQ + pass + pass + elif state == STATE_STR_SQ: + if ch == '\'': + state = stk.pop() + pass + elif ch == '\\': + stk.append(state) + state = STATE_BKSP + bksp_idx = idx + pass + pass + elif state == STATE_STR_DQ: + if ch == '"': + state = stk.pop() + elif ch == '\\': + stk.append(state) + state = STATE_BKSP + bksp_idx = idx + pass + pass + elif state == STATE_PRED: + if ch == '[': + stk.append(state) + state = STATE_PRED + elif ch == '\'': + stk.append(state) + state = STATE_STR_SQ + elif ch == '"': + stk.append(state) + state = STATE_STR_DQ + elif ch == ']': + state = stk.pop() + pass + pass + elif state == STATE_BKSP: + if idx != (bksp_idx + 1): + idx = mo.start() + pass + state = stk.pop() + pass + pass + return parts