Пример #1
0
    def _nonkey_extended():
        """parser for 'word (or word) (paren stuff)'.
        (or word) gives a synonym as a parenthetical within
        a word pattern.  Side effect is a new global synonym."""
        p = ((Pattern._nonkey() + c.paren(
            (next_word('or') + Pattern._nonkey()).treat(
                lib.snd).plus()).possibly()) +
             c.paren(Pattern._nonkey().plus()).many())

        def f(item):
            item1 = p.process(item)
            ((a, bs), cs) = item1.acc
            vals = [a.value] + [i.value for i in bs]
            c.synonym_add(vals)
            return c.update((a, cs), item1)

        return Parse(f)
Пример #2
0
def test_delimit():
    its = mk_item_stream('(hello){there}')
    its1 = pc.paren(pc.next_any_word()).process(its)
    #tok = its1.acc
    #print(tok)
    vs = [t.value for t in its1.acc]
    assert vs == ['hello']
    its2 = pc.brace(pc.next_any_word()).process(its1)
    assert its2.acc[0].value == 'there'
Пример #3
0
def annotated_var(prs):
    """
    Parser for annotated variable in parentheses.  
    Annotation is parsed with prs.
    Parser output is a var token 
    annotation is stored in attribute 'annotation' of var token.
    
    Sample input to parser:
        (x : A)
    """
    def trt(acc):
        v, ann = acc
        if len(ann) > 0:
            return c.copy_token(v, {'annotation': ann[0]})
        return v

    return c.paren(var() + colon_annotation(prs)).treat(trt)
Пример #4
0
def annotated_vars(prs):
    """Parser for list of annotated variables, parsing annotation with prs
    
    Missing annotation added as a metavariable.
    Variables within the parentheses are constrained to have the same type.
    
    Sample input:
        (x y z : A)
        (u v)
        
    Output is a list of annotated vars using attribute 'annotation'
    """
    def trt(acc):
        vs, ann = acc
        if len(ann) == 0:
            return vs
        assert (len(ann) == 1)
        return [c.copy_token(v, ann[0]) for v in vs]

    return c.paren(var().plus() + colon_annotation_or_meta()).treat(trt)
Пример #5
0
    def precedence_level():  #was paren_precedence_level
        """parser for the precedence level.
        
        sample input:
            'with precedence 10 and left associativity'
        output: (integer,assoc), where assoc in ['left','right','no']."""
        def _precedence_level():
            def f(raw):
                ((_, i), pos) = raw
                if len(pos) == 0:
                    l = 'no'
                else:
                    l = pos[0][0][1]
                return (int(i), l)

            return ((next_phrase('with precedence') + c.next_type('INTEGER')) +
                    (next_word('and') + lit('assoc') +
                     next_word('associtivity')).possibly()).treat(f)

        return Pattern._precedence_level() | c.paren(
            Pattern._precedence_level())
Пример #6
0
 def macro_inferring():
     return c.paren(next_word_inferring() + var().plus() +
                    opt_colon_sort_meta())
Пример #7
0
 def var_multisubsect_pattern():
     return ((Pattern._var() + next_value(',') + Pattern._var())
             | c.paren(Pattern._var() + next_value(',') + Pattern._var() +
                       opt_colon_type_meta()))
Пример #8
0
 def _var():
     """parser for a variable appearing in a pattern"""
     return var() | c.paren(var() + opt_colon_sort)
Пример #9
0
 def by_ref():
     return c.paren(next_word('by') + Proof.ref_item()).possibly()
Пример #10
0
 def required_arg_template_pat():
     return ((c.paren(var_or_atomics() + opt_colon_sort_meta()))
             | var_or_atomic())