def global_number_digits(): return Sequence( "+", ZeroOrMore(NonTerminal("phonedigit")), DIGIT(), ZeroOrMore(NonTerminal("phonedigit")) )
def local_number(): return Sequence( local_number_digits(), ZeroOrMore(NonTerminal("par")), context(), ZeroOrMore(NonTerminal("par")) )
def recurse(p: Parser) -> Union[DiagramItem, str, None]: # Transform parsers are just ignored if isinstance(p, _Transform): return recurse(p.children[0]) elif isinstance(p, Opt): # Opt parsers are straightforwardly wrapped into an Optional return Optional(recurse(p.parser)) elif isinstance(p, _Extract): # Extract is ignored return recurse(p.parser) # For list parsers, we create a sequence with the right separator # and sub-parser. elif isinstance(p, List): sep = recurse(p.sep) if p.sep else None child = recurse(p.parser) if p.empty_valid: return ZeroOrMore(child, repeat=sep) else: return OneOrMore(child, repeat=sep) # For defers, we just return the rule name elif isinstance(p, Defer): return p.rule_name # For tokens, we return either the quoted original string, or the DSL # name. elif isinstance(p, _Token): if p._original_string: return repr(p._original_string) else: return p.val.dsl_name children = [] for c in p.children: res = recurse(c) if res is not None: children.append(res) if isinstance(p, Or): if len(children) == 0: return None children = sorted(children, key=lambda c: isinstance(c, Skip)) return Choice(0, *children) elif isinstance(p, _Row): if len(children) == 0: return Skip() return Sequence(*children) else: return None
def domainlabel(): return Choice(0, alphanum(), Sequence( alphanum(), ZeroOrMore(Sequence(alphanum(), "-")), alphanum() ) )
def toplabel(): return Choice( 0, ALPHA(), Sequence( ALPHA(), ZeroOrMore(Choice(0, alphanum(), "-")), alphanum() ) )
def expr(self, children): if len(children) == 3: base, mi, ma = children if int(mi.value) == 0: if int(ma.value) == 1: return Optional(base) return ZeroOrMore(base, Comment(f'{mi.value}..{ma.value}')) else: return OneOrMore(base, Comment(f'{mi.value}..{ma.value}')) base, op = children if op.type != 'OP': return OneOrMore(base, Comment(f'{op.value} times')) if op.value == '+': return OneOrMore(base) elif op.value == '*': return ZeroOrMore(base) elif op.value == '?': return Optional(base) else: raise ValueError(f"Unsupported Operator {op!r}")
def generate_diagram(name, *modifiers): return NamedDiagram( name, Diagram( Sequence( name, ZeroOrMore( Sequence( copy(whitespace), Choice(0, *modifiers), ), skip=SKIP, ))))
def generate_sequence(name, *modifiers): if len(modifiers) == 1: return Sequence( name, Optional(Sequence(copy(whitespace), Choice(0, *modifiers)), skip=SKIP)) return Sequence( name, ZeroOrMore(Sequence( copy(whitespace), Choice(0, *modifiers), ), skip=SKIP), )
def local_number_digits(): return Sequence( ZeroOrMore(NonTerminal("phonedigit_hex")), Choice(0, HEXDIG(), "*", "#"), ZeroOrMore(NonTerminal("phonedigit_hex")) )
ZeroOrMore(Choice( 0, NonTerminal('literal'), Sequence( '[', Choice( 1, '[', Sequence( Optional(copy(whitespace), skip=SKIP), Choice( 0, generate_sequence( 'month', copy(padding), copy(month_repr), copy(case_sensitive), ), generate_sequence( 'weekday', copy(weekday_repr), copy(weekday_one_indexed), copy(case_sensitive), ), generate_sequence( 'week_number', copy(padding), copy(week_number_repr), ), generate_sequence( 'year', copy(padding), copy(year_repr), copy(year_base), copy(sign), ), generate_sequence( 'hour', copy(padding), copy(hour_repr), ), generate_sequence( 'period', copy(period_case), copy(case_sensitive), ), generate_sequence('subsecond', copy(subsecond_digits)), generate_sequence( 'offset_hour', copy(padding), copy(sign), ), generate_sequence( Choice( 0, 'day', 'ordinal', 'minute', 'second', 'offset_minute', 'offset_second', ), copy(padding), ), ), Optional(copy(whitespace), skip=SKIP), ']', )))), skip=SKIP))
def query(): return ZeroOrMore(Group(Choice(0, *pchar()), "pchar"), "/", "?")
def data(): return ZeroOrMore(uric())
def mediatype(): return Sequence(Optional(Sequence(type_(), "/", subtype())), ZeroOrMore(Sequence(";", parameter())))
def global_number(): return Sequence(global_number_digits(), ZeroOrMore(NonTerminal("par")))
def fragment(): return ZeroOrMore(Group(Choice(0, *pchar()), "pchar"), "/", "?")
def scheme(): return Sequence( ALPHA(), ZeroOrMore(HorizontalChoice(ALPHA(), DIGIT()), HorizontalChoice("+", "-", ".")))
def reg_name(): return ZeroOrMore( Choice(0, unreserved(), Sequence(*pct_encoded()), sub_delims()))
def segment(): return ZeroOrMore(Choice(0, *pchar()))
def domainname(): return Sequence( ZeroOrMore(Sequence(Group(domainlabel(), "domainlabel"), ".")), Group(toplabel(), "toplabel"), Optional(".") )
def userinfo(): return ZeroOrMore( Choice(0, Group(unreserved(), "unreserved"), Group(Sequence(*pct_encoded()), "pct-encoded"), Group(sub_delims(), "sub-delims"), ":"))