def __setitem__(self, k, v): if isinstance(k, (slice, int)): Log.error("not supported") if v is None: v = NO_RESULTS if is_forward(self.type): self.tokens[0][k] = v return for i, tok in enumerate(self.tokens): if isinstance(tok, ParseResults): if tok.name == k: self.tokens[i] = v v = NO_RESULTS # ERASE ALL OTHERS elif isinstance(tok.type, Group): continue elif is_forward(tok.type) and isinstance( tok.tokens[0].type, Group): continue elif tok.name: continue tok.__setitem__(k, NO_RESULTS) # ERASE ALL CHILDREN if v is not NO_RESULTS: tokens = self.tokens if is_forward(self.type): tokens = tokens[0].tokens if isinstance(v, ParseResults): tokens.append(Annotation(k, v.start, v.end, v.tokens)) else: tokens.append(Annotation(k, -1, 0, listwrap(v)))
def to_union_call(tokens): unions = tokens["union"] if unions.type.parser_name == "unordered sql": output = scrub(unions) # REMOVE THE Group() else: unions = list(unions) sources = scrub([unions[i] for i in range(0, len(unions), 2)]) operators = [ "_".join(listwrap(scrub(unions[i]))) for i in range(1, len(unions), 2) ] acc = sources[-1] last_union = None for op, so in reversed(list(zip(operators, sources))): if op == last_union: acc[op] = [so] + acc[op] else: acc = {op: [so, acc]} last_union = op if not tokens["orderby"] and not tokens["offset"] and not tokens["limit"]: return acc else: output = {"from": acc} output["orderby"] = tokens["orderby"] output["offset"] = tokens["offset"] output["limit"] = tokens["limit"] return output
def causes(self): if self._causes is None: self._causes = list(sorted( listwrap(self.unsorted_cause), key=lambda e: -e.loc if isinstance(e, ParseException) else 0, )) return self._causes
def to_expression_call(tokens): over = tokens["over"] within = tokens["within"] if over or within: return expr = ParseResults( tokens.type, tokens.start, tokens.end, listwrap(tokens["value"]) ) return expr
def to_join_call(tokens): op = " ".join(listwrap(scrub(tokens["op"]))) if tokens["join"]["name"]: output = {op: { "name": tokens["join"]["name"], "value": tokens["join"]["value"], }} else: output = {op: tokens["join"]} output["on"] = tokens["on"] output["using"] = tokens["using"] return output
def to_bracket(tokens): acc = [] for e in listwrap(tokens["body"].value()): if isinstance(e, SingleCharLiteral): acc.append(e.parser_config.match) elif isinstance(e, Char): acc.extend(e.parser_config.include) else: Log.error("programmer error") if tokens["negate"]: return Char(exclude=acc) else: return Char(acc)
def to_union_call(tokens): unions = list(tokens["union"]) if len(unions) == 1: output = scrub(unions[0].tokens) # REMOVE THE Group() else: sources = scrub([unions[i] for i in range(0, len(unions), 2)]) operators = [ "_".join(listwrap(scrub(unions[i]))) for i in range(1, len(unions), 2) ] op = operators[0] if any(o != op for o in operators): raise Exception("Expecting no mixing of UNION with UNION ALL") if not tokens["orderby"] and not tokens["offset"] and not tokens[ "limit"]: return {op: sources} else: output = {"from": {op: sources}} output["orderby"] = tokens["orderby"] output["offset"] = tokens["offset"] output["limit"] = tokens["limit"] return output
def __init__(self, expr, default=None): Many.__init__(self, expr, stopOn=None, min_match=0, max_match=1) self.set_config(defaultValue=listwrap(default))
| ahead | not_ahead | behind | not_behind | non_capture | named | group ) more = (term + Optional(repetition)).addParseAction(repeat) sequence = OneOrMore(more).addParseAction(lambda t: And(t)) regex << ( delimitedList(sequence, separator="|") .set_token_name("value") .addParseAction(lambda t: MatchFirst(listwrap(t.value())).streamline()) .streamline() ) def srange(expr): pattern = brackets.parseString(expr).value() chars = set() def drill(e): if isinstance(e, Literal): chars.add(e.parser_config.match) elif isinstance(e, Char): chars.update(c for c in e.parser_config.include) elif isinstance(e, MatchFirst): for ee in e.exprs:
| esc_char | word_edge | brackets | ahead | not_ahead | behind | not_behind | non_capture | named | group) more = (term + Optional(repetition)).addParseAction(repeat) sequence = OneOrMore(more).addParseAction(lambda t: And(t)) regex << (delimitedList( sequence, separator="|").set_token_name("value").addParseAction( lambda t: MatchFirst(listwrap(t.value())).streamline()).streamline()) def srange(expr): pattern = brackets.parseString(expr).value() chars = set() def drill(e): if isinstance(e, Literal): chars.add(e.parser_config.match) elif isinstance(e, Char): chars.update(c for c in e.parser_config.include) elif isinstance(e, MatchFirst): for ee in e.exprs: drill(ee) elif isinstance(e, And):