How to use the coconut.compiler.util.addspace function in coconut

To help you get started, we’ve selected a few coconut examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github evhub / coconut / coconut / compiler / grammar.py View on Github external
destructuring_stmt_ref = Optional(keyword("match").suppress()) + match + equals.suppress() + test_expr

    case_stmt = Forward()
    case_match = trace(
        Group(
            keyword("match").suppress() - match - Optional(keyword("if").suppress() - test) - full_suite,
        ),
    )
    case_stmt_ref = (
        keyword("case").suppress() + test - colon.suppress() - newline.suppress()
        - indent.suppress() - Group(OneOrMore(case_match))
        - dedent.suppress() - Optional(keyword("else").suppress() - suite)
    )

    exec_stmt = Forward()
    assert_stmt = addspace(keyword("assert") - testlist)
    if_stmt = condense(
        addspace(keyword("if") - condense(namedexpr_test - suite))
        - ZeroOrMore(addspace(keyword("elif") - condense(namedexpr_test - suite)))
        - Optional(else_stmt),
    )
    while_stmt = addspace(keyword("while") - condense(namedexpr_test - suite - Optional(else_stmt)))
    for_stmt = addspace(keyword("for") - assignlist - keyword("in") - condense(testlist - suite - Optional(else_stmt)))
    except_clause = attach(
        keyword("except").suppress() + (
            testlist_has_comma("list") | test("test")
        ) - Optional(keyword("as").suppress() - name),
        except_handle,
    )
    try_stmt = condense(
        keyword("try") - suite + (
            keyword("finally") - suite
github evhub / coconut / coconut / compiler / grammar.py View on Github external
)
    case_stmt_ref = (
        keyword("case").suppress() + test - colon.suppress() - newline.suppress()
        - indent.suppress() - Group(OneOrMore(case_match))
        - dedent.suppress() - Optional(keyword("else").suppress() - suite)
    )

    exec_stmt = Forward()
    assert_stmt = addspace(keyword("assert") - testlist)
    if_stmt = condense(
        addspace(keyword("if") - condense(namedexpr_test - suite))
        - ZeroOrMore(addspace(keyword("elif") - condense(namedexpr_test - suite)))
        - Optional(else_stmt),
    )
    while_stmt = addspace(keyword("while") - condense(namedexpr_test - suite - Optional(else_stmt)))
    for_stmt = addspace(keyword("for") - assignlist - keyword("in") - condense(testlist - suite - Optional(else_stmt)))
    except_clause = attach(
        keyword("except").suppress() + (
            testlist_has_comma("list") | test("test")
        ) - Optional(keyword("as").suppress() - name),
        except_handle,
    )
    try_stmt = condense(
        keyword("try") - suite + (
            keyword("finally") - suite
            | (
                OneOrMore(except_clause - suite) - Optional(keyword("except") - suite)
                | keyword("except") - suite
            ) - Optional(else_stmt) - Optional(keyword("finally") - suite)
        ),
    )
    exec_stmt_ref = keyword("exec").suppress() + lparen.suppress() + test + Optional(
github evhub / coconut / coconut / compiler / grammar.py View on Github external
from_import = (
        keyword("from").suppress()
        - condense(ZeroOrMore(unsafe_dot) + dotted_name | OneOrMore(unsafe_dot) | star)
        - keyword("import").suppress() - (from_import_names | Group(star))
    )
    import_stmt = Forward()
    import_stmt_ref = from_import | basic_import

    nonlocal_stmt = Forward()
    namelist = attach(
        maybeparens(lparen, itemlist(name, comma), rparen) - Optional(equals.suppress() - test_expr),
        namelist_handle,
    )
    global_stmt = addspace(keyword("global") - namelist)
    nonlocal_stmt_ref = addspace(keyword("nonlocal") - namelist)
    del_stmt = addspace(keyword("del") - simple_assignlist)

    matchlist_list = Group(Optional(tokenlist(match, comma)))
    matchlist_tuple = Group(
        Optional(
            match + OneOrMore(comma.suppress() + match) + Optional(comma.suppress())
            | match + comma.suppress(),
        ),
    )
    matchlist_star = (
        Optional(Group(OneOrMore(match + comma.suppress())))
        + star.suppress() + name
        + Optional(Group(OneOrMore(comma.suppress() + match)))
        + Optional(comma.suppress())
    )
    matchlist_data = (
        Optional(Group(OneOrMore(match + comma.suppress())), default=())
github evhub / coconut / coconut / compiler / grammar.py View on Github external
| namedexpr,
    )

    async_comp_for = Forward()
    classlist_ref = Optional(
        lparen.suppress() + rparen.suppress()
        | Group(
            condense(lparen + testlist + rparen)("tests")
            | function_call("args"),
        ),
    )
    class_suite = suite | attach(newline, class_suite_handle)
    classdef = condense(addspace(keyword("class") - name) - classlist - class_suite)
    comp_iter = Forward()
    base_comp_for = addspace(keyword("for") + assignlist + keyword("in") + test_item + Optional(comp_iter))
    async_comp_for_ref = addspace(keyword("async") + base_comp_for)
    comp_for <<= async_comp_for | base_comp_for
    comp_if = addspace(keyword("if") + test_no_cond + Optional(comp_iter))
    comp_iter <<= comp_for | comp_if

    complex_raise_stmt = Forward()
    pass_stmt = keyword("pass")
    break_stmt = keyword("break")
    continue_stmt = keyword("continue")
    return_stmt = addspace(keyword("return") - Optional(testlist))
    simple_raise_stmt = addspace(keyword("raise") + Optional(test))
    complex_raise_stmt_ref = keyword("raise").suppress() + test + keyword("from").suppress() - test
    raise_stmt = complex_raise_stmt | simple_raise_stmt
    flow_stmt = break_stmt | continue_stmt | return_stmt | raise_stmt | yield_expr

    dotted_as_name = Group(dotted_name - Optional(keyword("as").suppress() - name))
    import_as_name = Group(name - Optional(keyword("as").suppress() - name))
github evhub / coconut / coconut / compiler / grammar.py View on Github external
keyword("finally") - suite
            | (
                OneOrMore(except_clause - suite) - Optional(keyword("except") - suite)
                | keyword("except") - suite
            ) - Optional(else_stmt) - Optional(keyword("finally") - suite)
        ),
    )
    exec_stmt_ref = keyword("exec").suppress() + lparen.suppress() + test + Optional(
        comma.suppress() + test + Optional(
            comma.suppress() + test + Optional(
                comma.suppress(),
            ),
        ),
    ) + rparen.suppress()

    with_item = addspace(test - Optional(keyword("as") - name))
    with_item_list = Group(maybeparens(lparen, tokenlist(with_item, comma), rparen))
    with_stmt_ref = keyword("with").suppress() - with_item_list - suite
    with_stmt = Forward()

    return_typedef = Forward()
    name_funcdef = trace(condense(dotted_name + parameters))
    op_tfpdef = unsafe_typedef_default | condense(name + Optional(default))
    op_funcdef_arg = name | condense(lparen.suppress() + op_tfpdef + rparen.suppress())
    op_funcdef_name = unsafe_backtick.suppress() + dotted_name + unsafe_backtick.suppress()
    op_funcdef = trace(
        attach(
            Group(Optional(op_funcdef_arg))
            + op_funcdef_name
            + Group(Optional(op_funcdef_arg)),
            op_funcdef_handle,
        ),
github evhub / coconut / coconut / compiler / grammar.py View on Github external
attach(implicit_return + keyword("where").suppress() - where_suite, where_stmt_handle)
        | condense(implicit_return + newline)
    )
    math_funcdef_body = condense(ZeroOrMore(~(implicit_return_stmt + dedent) + stmt) - implicit_return_stmt)
    math_funcdef_suite = (
        attach(implicit_return_stmt, make_suite_handle)
        | condense(newline - indent - math_funcdef_body - dedent)
    )
    end_func_equals = return_typedef + equals.suppress() | fixto(equals, ":")
    math_funcdef = trace(
        attach(
            condense(addspace(keyword("def") + base_funcdef) + end_func_equals) - math_funcdef_suite,
            math_funcdef_handle,
        ),
    )
    math_match_funcdef = addspace(
        match_def_modifiers
        + trace(
            attach(
                base_match_funcdef
                + equals.suppress()
                - Optional(docstring)
                - (
                    attach(implicit_return_stmt, make_suite_handle)
                    | newline.suppress() - indent.suppress()
                    - Optional(docstring)
                    - attach(math_funcdef_body, make_suite_handle)
                    - dedent.suppress()
                ),
                join_match_funcdef,
            ),
        ),
github evhub / coconut / coconut / compiler / grammar.py View on Github external
attach(implicit_return_stmt, make_suite_handle)
                    | newline.suppress() - indent.suppress()
                    - Optional(docstring)
                    - attach(math_funcdef_body, make_suite_handle)
                    - dedent.suppress()
                ),
                join_match_funcdef,
            ),
        ),
    )

    async_stmt = Forward()
    async_stmt_ref = addspace(keyword("async") + (with_stmt | for_stmt))

    async_funcdef = keyword("async").suppress() + (funcdef | math_funcdef)
    async_match_funcdef = addspace(
        trace(
            # we don't suppress addpattern so its presence can be detected later
            keyword("match").suppress() + keyword("addpattern") + keyword("async").suppress()
            | keyword("addpattern") + keyword("match").suppress() + keyword("async").suppress()
            | keyword("match").suppress() + keyword("async").suppress() + Optional(keyword("addpattern"))
            | keyword("addpattern") + keyword("async").suppress() + Optional(keyword("match")).suppress()
            | keyword("async").suppress() + match_def_modifiers,
        ) + (def_match_funcdef | math_match_funcdef),
    )

    datadef = Forward()
    data_args = Group(
        Optional(
            lparen.suppress() + ZeroOrMore(
                Group(
                    # everything here must end with arg_comma
github evhub / coconut / coconut / compiler / grammar.py View on Github external
+ equals.suppress()
                - Optional(docstring)
                - (
                    attach(implicit_return_stmt, make_suite_handle)
                    | newline.suppress() - indent.suppress()
                    - Optional(docstring)
                    - attach(math_funcdef_body, make_suite_handle)
                    - dedent.suppress()
                ),
                join_match_funcdef,
            ),
        ),
    )

    async_stmt = Forward()
    async_stmt_ref = addspace(keyword("async") + (with_stmt | for_stmt))

    async_funcdef = keyword("async").suppress() + (funcdef | math_funcdef)
    async_match_funcdef = addspace(
        trace(
            # we don't suppress addpattern so its presence can be detected later
            keyword("match").suppress() + keyword("addpattern") + keyword("async").suppress()
            | keyword("addpattern") + keyword("match").suppress() + keyword("async").suppress()
            | keyword("match").suppress() + keyword("async").suppress() + Optional(keyword("addpattern"))
            | keyword("addpattern") + keyword("async").suppress() + Optional(keyword("match")).suppress()
            | keyword("async").suppress() + match_def_modifiers,
        ) + (def_match_funcdef | math_match_funcdef),
    )

    datadef = Forward()
    data_args = Group(
        Optional(
github evhub / coconut / coconut / compiler / grammar.py View on Github external
from_import_names = Group(maybeparens(lparen, tokenlist(import_as_name, comma), rparen))
    basic_import = keyword("import").suppress() - (import_names | Group(star))
    from_import = (
        keyword("from").suppress()
        - condense(ZeroOrMore(unsafe_dot) + dotted_name | OneOrMore(unsafe_dot) | star)
        - keyword("import").suppress() - (from_import_names | Group(star))
    )
    import_stmt = Forward()
    import_stmt_ref = from_import | basic_import

    nonlocal_stmt = Forward()
    namelist = attach(
        maybeparens(lparen, itemlist(name, comma), rparen) - Optional(equals.suppress() - test_expr),
        namelist_handle,
    )
    global_stmt = addspace(keyword("global") - namelist)
    nonlocal_stmt_ref = addspace(keyword("nonlocal") - namelist)
    del_stmt = addspace(keyword("del") - simple_assignlist)

    matchlist_list = Group(Optional(tokenlist(match, comma)))
    matchlist_tuple = Group(
        Optional(
            match + OneOrMore(comma.suppress() + match) + Optional(comma.suppress())
            | match + comma.suppress(),
        ),
    )
    matchlist_star = (
        Optional(Group(OneOrMore(match + comma.suppress())))
        + star.suppress() + name
        + Optional(Group(OneOrMore(comma.suppress() + match)))
        + Optional(comma.suppress())
    )
github evhub / coconut / coconut / compiler / grammar.py View on Github external
test,
        typedef_callable,
        typedef_atom_ref,
    )
    typedef_atom <<= _typedef_atom
    typedef_test <<= _typedef_test

    test <<= trace(
        typedef_callable
        | lambdef
        | addspace(test_item + Optional(keyword("if") + test_item + keyword("else") + test)),
    )
    test_no_cond <<= trace(lambdef_no_cond | test_item)

    namedexpr = Forward()
    namedexpr_ref = addspace(name + colon_eq + test)
    namedexpr_test <<= trace(
        test + ~colon_eq
        | namedexpr,
    )

    async_comp_for = Forward()
    classlist_ref = Optional(
        lparen.suppress() + rparen.suppress()
        | Group(
            condense(lparen + testlist + rparen)("tests")
            | function_call("args"),
        ),
    )
    class_suite = suite | attach(newline, class_suite_handle)
    classdef = condense(addspace(keyword("class") - name) - classlist - class_suite)
    comp_iter = Forward()