def sdf_bond(): fst = yield P.string('\n') >> P.times(P.one_of(' 0123456789'), 3) fst = int(''.join(fst)) snd = yield P.times(P.one_of(' 0123456789'), 3) snd = int(''.join(snd)) bond_type = yield seperator >> num_i yield P.times(seperator >> num_i, 3) return SdfBond(fst, snd, bond_type)
def sdf_header(): mol_num = yield transparent >> num_i atom_num = yield P.string('\n\n\n') >> P.times(P.one_of(' 0123456789'), 3) atom_num = int(''.join(atom_num)) bond_num = yield P.times(P.one_of(' 0123456789'), 3) bond_num = int(''.join(bond_num)) yield P.times(seperator >> num_i, 5) yield seperator >> P.string("V2000") return SdfHeader(mol_num, atom_num, bond_num)
def __init__(self, header): header_fields = defaultdict(str) for line in header.splitlines(): field, label = line[:60], trim_whitespace(line[60:]) header_fields[label] += field self.header_fields = dict(header_fields) self.version, self.type, satellite = p.Parser( p.joint( n_ANY(9).parsecmap(float), p.compose(n_ANY(11), p.one_of('MON')), p.compose(n_ANY(19), n_ANY(1)))).parse( self.header_fields['RINEX VERSION / TYPE'])
def fn(): "Parse IBIS keyword." yield regex(r"^\[", re.MULTILINE) wordlets = yield sepBy1(name_only, one_of( " _")) # ``name`` gobbles up trailing space, which we don't want. yield string("]") yield ignore # So that ``keyword`` functions as a lexeme. res = ("_".join(wordlets) ) # Canonicalize to: "<wordlet1>_<wordlet2>_...". if kywrd: # assert res.lower() == kywrd.lower(), f"Expecting: {kywrd}; got: {res}." # Does not work! if res.lower() == kywrd.lower(): return res else: return fail.desc(f"Expecting: {kywrd}; got: {res}.") return res
def num_f(): sgn = yield P.many(P.one_of('-')) tmp_1 = yield P.many(P.digit()) tmp_2 = yield P.string('.') >> P.many(P.digit()) return float(''.join(sgn + ['0'] + tmp_1 + ['.'] + tmp_2 + ['0']))
def white(): yield P.one_of(' \t\r\n')
rule_type_def = particle >> possible_types @parsec.generate def multi_day_rule(): dates = yield multiple_dates rule_type = yield rule_type_def return { "days": flatten_multiple_dates(dates), "rule": rule_type_translations[rule_type] } # All rules rule = single_day_rule ^ multi_day_rule all_rules = parsec.sepEndBy1(rule, parsec.one_of("、。")) # === COMBINE EVERYTHING === # pattern_suffix = parsec.one_of("と。") pattern_name = parsec.string("全日運転") ^ parsec.string("土曜・休日") pattern_def = pattern_name << pattern_suffix pattern_translate = { "全日運転": "毎日", "土曜・休日": "土曜・休日", } @parsec.generate def parse_untenbi(): raw_pattern = yield (pattern_def ^ parsec.string(""))
@generate("remainder of line") def rest_line(): "Parse remainder of line." chars = yield (many(none_of("\n\r")) << ignore ) # So that we still function as a lexeme. return "".join(chars) skip_line = lexeme(rest_line).result('(Skipped.)') name_only = regex(r"[_a-zA-Z0-9/\.()#-]+") name = word(name_only) symbol = lexeme(regex(r"[a-zA-Z_][^\s()\[\]]*")) true = lexeme(string("True")).result(True) false = lexeme(string("False")).result(False) quoted_string = lexeme(regex(r'"[^"]*"')) fail = one_of("") skip_keyword = (skip_line >> many(none_of("[") >> skip_line)).result( '(Skipped.)') # Skip over everything until the next keyword begins. IBIS_num_suf = { 'T': 'e12', 'k': 'e3', 'n': 'e-9', 'G': 'e9', 'm': 'e-3', 'p': 'e-12', 'M': 'e6', 'u': 'e-6', 'f': 'e-15', }
def parse_symbol(): """Parse a symbol as a non-digit followed by any characters.""" symbol_chars = parsec.letter() | parsec.one_of('+-/*?!') first = yield symbol_chars rest = yield parsec.many(symbol_chars | parsec.digit()) return wtypes.Symbol(first + ''.join(rest))