Ejemplo n.º 1
0
def parse(input):
    period = sometok("period")
    string = p.oneplus(sometok("string")) >> (lambda x: " ".join(x))
    number = sometok("number")

    title = string + p.skip(period) >> RecipeTitle
    ingredients_start = sometok("ingredients_start") + p.skip(period) >> IngredientStart

    dry_measure = p.maybe(sometok("measure_type")) + sometok("dry_measure")
    liquid_measure = sometok("liquid_measure")
    mix_measure = sometok("mix_measure")

    # is this valid ? 'g of butter', unit w/o initial_value
    ingredient = p.maybe(number) + p.maybe(dry_measure | liquid_measure | mix_measure) + string >> unarg(Ingredient)

    ingredients = p.many(ingredient)

    cooking_time = p.skip(sometok("cooking_time")) + (number >> unarg(CookingTime)) + p.skip(sometok("period"))

    oven_temp = p.skip(sometok("oven")) + p.many(number) + p.skip(sometok("oven_temp")) >> unarg(Oven)

    method_start = sometok("method_start") + p.skip(period)

    comment = p.skip(p.many(string | period))
    header = title + p.maybe(comment)

    instruction = (string + p.skip(period)) >> parse_instruction

    instructions = p.many(instruction)

    program = (method_start + instructions) >> unarg(MethodStart)

    serves = (sometok("serve") + number >> (lambda x: Serve("serve", x[1]))) + p.skip(period)

    ingredients_section = (ingredients_start + ingredients) >> unarg(IngredientSection)

    recipe = (
        header
        + p.maybe(ingredients_section)
        + p.maybe(cooking_time)
        + p.maybe(oven_temp)
        + p.maybe(program)
        + p.maybe(serves)
    ) >> RecipeNode

    main_parser = p.oneplus(recipe)
    return main_parser.parse(tokenize(input))
Ejemplo n.º 2
0
    def F(*xargs):
        (s,(f,x)) = xargs
        if s is None: 
            return x
        try:
            if type(s)==float and type(x)==float:
                ret = f[1](s,x)
            else:
                ret = f[0](s,x)
        except ZeroDivisionError:
            return None
        return ret
    res = reduce(F, args, z)
    return res

eval = unarg(eval_expr)

number = sometok('number') >> make_number
name   = sometok('name')   >> make_name
string = sometok('string') >> make_string
func   = sometok('func')   >> make_func

value  = number | name | string

makeop = lambda s, f: op(s) >> const(f)

add = makeop('+', (ets.add, operator.add))
sub = makeop('-', (ets.sub, operator.sub))
mul = makeop('*', (ets.mul, operator.mul))
div = makeop('/', (ets.div, operator.div))
Ejemplo n.º 3
0
        Spec('string',  r'"[^"]*"'), # '\"' escapes are ignored
    ]
    useless = ['comment', 'newline', 'space']
    t = make_tokenizer(specs)
    return [x for x in t(str) if x.type not in useless]

id = sometoks(['name', 'number', 'string'])
make_graph_attr = lambda args: DefAttrs(u'graph', [Attr(*args)])
make_edge = lambda x, xs, attrs: Edge([x] + xs, attrs)

node_id = memoize(id) # + maybe(port)
a_list = (
    id +
    maybe(op_('=') + id) +
    skip(maybe(op(',')))
    >> unarg(Attr))
attr_list = (
    many(op_('[') + many(a_list) + op_(']'))
    >> flatten)
attr_stmt = (
   (n('graph') | n('node') | n('edge')) +
   attr_list
   >> unarg(DefAttrs))
graph_attr = id + op_('=') + id >> make_graph_attr
node_stmt = node_id + attr_list >> unarg(Node)
# We use a fwd() because of circular definitions like (stmt_list -> stmt ->
# subgraph -> stmt_list)
subgraph = fwd()
edge_rhs = skip(op('->') | op('--')) + (subgraph | node_id)
edge_stmt = (
    (subgraph | node_id) +