def sort_canonical(keyword, stmts):
    """Sort all `stmts` in the canonical order defined by `keyword`.
    Return the sorted list.  The `stmt` list is not modified.
    If `keyword` does not have a canonical order, the list is returned
    as is.
    """

    try:
        (_arg_type, subspec) = stmt_map[keyword]
    except KeyError:
        return stmts
    res = []
    # keep the order of data definition statements and case
    keep = [s[0] for s in data_def_stmts] + ['case']
    for (kw, _spec) in flatten_spec(subspec):
        # keep comments before a statement together with that statement
        comments = []
        for s in stmts:
            if s.keyword == '_comment':
                comments.append(s)
            elif s.keyword == kw and kw not in keep:
                res.extend(comments)
                comments = []
                res.append(s)
            else:
                comments = []

    # then copy all other statements (extensions)
    res.extend([stmt for stmt in stmts if stmt not in res])
    return res
def sort_canonical ( keyword , stmts ) : try : ( _arg_type , subspec ) = stmt_map [ keyword ] except KeyError : return stmts res = [ ] keep = [ s [ 0 ] for s in data_def_stmts ] + [ 'case' ] for ( kw , _spec ) in flatten_spec ( subspec ) : comments = [ ] for s in stmts : if s . keyword == '_comment' : comments . append ( s ) elif s . keyword == kw and kw not in keep : res . extend ( comments ) comments = [ ] res . append ( s ) else : comments = [ ] res . extend ( [ stmt for stmt in stmts if stmt not in res ] ) return res
Sort all `stmts` in the canonical order defined by `keyword`.
    Return the sorted list.  The `stmt` list is not modified.
    If `keyword` does not have a canonical order, the list is returned
    as is.