Exemple #1
0
def parse_foreach_standard(ctx, tokens, breakstack):
    """
  ::

    foreach(<loop_var> <items>)
      <commands>
    endforeach()
  """
    tree = ArgGroupNode()

    # If it is a whitespace token then put it directly in the parse tree at
    # the current depth
    while tokens and tokens[0].type in WHITESPACE_TOKENS:
        tree.children.append(tokens.pop(0))
        continue

    # Consume the loop variable and any attached comments
    ntokens = len(tokens)
    subtree = PositionalGroupNode.parse(ctx, tokens, 1, [], breakstack)
    assert len(tokens) < ntokens
    tree.children.append(subtree)

    # Consume the items of iteration
    ntokens = len(tokens)
    subtree = PositionalGroupNode.parse(ctx, tokens, '+', [], breakstack)
    assert len(tokens) < ntokens
    tree.children.append(subtree)

    return tree
Exemple #2
0
def parse_foreach_in(ctx, tokens, breakstack):
    """
  ::

    foreach(loop_var IN [LISTS [<lists>]] [ITEMS [<items>]])
  """
    tree = ArgGroupNode()

    # If it is a whitespace token then put it directly in the parse tree at
    # the current depth
    while tokens and tokens[0].type in WHITESPACE_TOKENS:
        tree.children.append(tokens.pop(0))
        continue

    # Consume the loop variable and any attached comments
    ntokens = len(tokens)
    pargs = PositionalGroupNode.parse(ctx, tokens, 2, ["IN"], breakstack)
    assert len(tokens) < ntokens
    tree.children.append(pargs)

    kwargs = {"LISTS": PositionalParser("+"), "ITEMS": PositionalParser("+")}
    sub_breakstack = breakstack + [KwargBreaker(list(kwargs.keys()))]

    while tokens:
        # Break if the next token belongs to a parent parser, i.e. if it
        # matches a keyword argument of something higher in the stack, or if
        # it closes a parent group.
        if should_break(tokens[0], breakstack):
            break

        # If it is a whitespace token then put it directly in the parse tree at
        # the current depth
        if tokens[0].type in WHITESPACE_TOKENS:
            tree.children.append(tokens.pop(0))
            continue

        # If it's a comment, then add it at the current depth
        if tokens[0].type in (lex.TokenType.COMMENT,
                              lex.TokenType.BRACKET_COMMENT):
            if not get_tag(tokens[0]) in ("sort", "sortable"):
                child = TreeNode(NodeType.COMMENT)
                tree.children.append(child)
                child.children.append(tokens.pop(0))
                continue

        ntokens = len(tokens)
        word = get_normalized_kwarg(tokens[0])
        subparser = kwargs.get(word, None)
        if subparser is not None:
            subtree = KeywordGroupNode.parse(ctx, tokens, word, subparser,
                                             sub_breakstack)
        else:
            logger.warning("Unexpected positional argument at %s",
                           tokens[0].location)
            subtree = PositionalGroupNode.parse(ctx, tokens, '+', [],
                                                sub_breakstack)

        assert len(tokens) < ntokens
        tree.children.append(subtree)
    return tree
Exemple #3
0
def parse_add_library_object(ctx, tokens, breakstack):
    """
  ::
    add_library(<name> OBJECT <src>...)

    :see: https://cmake.org/cmake/help/latest/command/add_library.html#object-libraries
  """
    tree = ArgGroupNode()

    # If it is a whitespace token then put it directly in the parse tree at
    # the current depth
    while tokens and tokens[0].type in WHITESPACE_TOKENS:
        tree.children.append(tokens.pop(0))
        continue

    ntokens = len(tokens)
    subtree = PositionalGroupNode.parse(ctx, tokens, 2, ["OBJECT"], breakstack)
    assert len(tokens) < ntokens
    tree.children.append(subtree)

    while tokens:
        # Break if the next token belongs to a parent parser, i.e. if it
        # matches a keyword argument of something higher in the stack, or if
        # it closes a parent group.
        if should_break(tokens[0], breakstack):
            break

        # If it is a whitespace token then put it directly in the parse tree at
        # the current depth
        if tokens[0].type in WHITESPACE_TOKENS:
            tree.children.append(tokens.pop(0))
            continue

        # If it's a comment, then add it at the current depth
        if tokens[0].type in (TokenType.COMMENT, TokenType.BRACKET_COMMENT):
            if not get_tag(tokens[0]) in ("sort", "sortable"):
                child = TreeNode(NodeType.COMMENT)
                tree.children.append(child)
                child.children.append(tokens.pop(0))
                continue

        ntokens = len(tokens)
        subtree = PositionalGroupNode.parse(ctx, tokens, '+', [], breakstack)
        assert len(tokens) < ntokens
        tree.children.append(subtree)
    return tree
Exemple #4
0
def parse_file_write(ctx, tokens, breakstack):
    """
  ::

    file(WRITE <filename> <content>...)
    file(APPEND <filename> <content>...)

  :see: https://cmake.org/cmake/help/v3.14/command/file.html#writing
  """
    tree = ArgGroupNode()
    consume_whitespace_and_comments(ctx, tokens, tree)
    tree.children.append(
        PositionalGroupNode.parse(ctx, tokens, 2, ["WRITE", "APPEND"],
                                  breakstack))
    consume_whitespace_and_comments(ctx, tokens, tree)
    tree.children.append(
        PositionalGroupNode.parse(ctx, tokens, '+', [], breakstack))

    return tree
Exemple #5
0
def parse_empty(ctx, tokens, breakstack):
    """
  ::

    break()
    continue()
    enable_testing()

  :see: https://cmake.org/cmake/help/latest/command/break.html
  :see: https://cmake.org/cmake/help/latest/command/continue.html
  :see: https://cmake.org/cmake/help/latest/command/enable_testing.html
  :see: https://cmake.org/cmake/help/latest/command/return.html
  """
    tree = ArgGroupNode()

    while tokens:
        # Break if the next token belongs to a parent parser, i.e. if it
        # matches a keyword argument of something higher in the stack, or if
        # it closes a parent group.
        if should_break(tokens[0], breakstack):
            break

        # If it is a whitespace token then put it directly in the parse tree at
        # the current depth
        if tokens[0].type in WHITESPACE_TOKENS:
            tree.children.append(tokens.pop(0))
            continue

        # If it's a comment, then add it at the current depth
        if tokens[0].type in (lex.TokenType.COMMENT,
                              lex.TokenType.BRACKET_COMMENT):
            child = TreeNode(NodeType.COMMENT)
            tree.children.append(child)
            child.children.append(tokens.pop(0))
            continue

        ctx.lint_ctx.record_lint("E1121", location=tokens[0].get_location())
        ntokens = len(tokens)
        subtree = PositionalGroupNode.parse(ctx, tokens, "+", [], breakstack)
        assert len(tokens) < ntokens
        tree.children.append(subtree)
    return tree
Exemple #6
0
def parse_add_library_standard(ctx, tokens, breakstack, sortable):
    """
  ::

    add_library(<name> [STATIC | SHARED | MODULE]
               [EXCLUDE_FROM_ALL]
               source1 [source2 ...])

  :see: https://cmake.org/cmake/help/v3.0/command/add_library.html
  """
    # pylint: disable=too-many-statements

    parsing_name = 1
    parsing_type = 2
    parsing_flag = 3
    parsing_sources = 4

    tree = ArgGroupNode()

    # If it is a whitespace token then put it directly in the parse tree at
    # the current depth
    while tokens and tokens[0].type in WHITESPACE_TOKENS:
        tree.children.append(tokens.pop(0))
        continue

    state_ = parsing_name
    parg_group = None
    src_group = None
    active_depth = tree

    flags = ("STATIC", "SHARED", "MODULE", "OBJECT")

    while tokens:
        # This parse function breakson the first right paren, since parenthetical
        # groups are not allowed. A parenthesis might exist in a filename, but
        # if so that filename should be quoted so it wont show up as a RIGHT_PAREN
        # token.
        if tokens[0].type is TokenType.RIGHT_PAREN:
            break

        # If it is a whitespace token then put it directly in the parse tree at
        # the current depth
        if tokens[0].type in WHITESPACE_TOKENS:
            active_depth.children.append(tokens.pop(0))
            continue

        # If it's a comment token not associated with an argument, then put it
        # directly into the parse tree at the current depth
        if tokens[0].type in (TokenType.COMMENT, TokenType.BRACKET_COMMENT):
            if state_ > parsing_name:
                if get_tag(tokens[0]) in ("unsort", "unsortable"):
                    sortable = False
                elif get_tag(tokens[0]) in ("sort", "sortable"):
                    sortable = True
            child = TreeNode(NodeType.COMMENT)
            active_depth.children.append(child)
            child.children.append(tokens.pop(0))
            continue

        if state_ is parsing_name:
            token = tokens.pop(0)
            parg_group = PositionalGroupNode()
            parg_group.spec = PositionalSpec("+")
            active_depth = parg_group
            tree.children.append(parg_group)
            child = TreeNode(NodeType.ARGUMENT)
            child.children.append(token)
            CommentNode.consume_trailing(ctx, tokens, child)
            parg_group.children.append(child)
            state_ += 1
        elif state_ is parsing_type:
            if get_normalized_kwarg(tokens[0]) in flags:
                token = tokens.pop(0)
                child = TreeNode(NodeType.FLAG)
                child.children.append(token)
                CommentNode.consume_trailing(ctx, tokens, child)
                parg_group.children.append(child)
            state_ += 1
        elif state_ is parsing_flag:
            if get_normalized_kwarg(tokens[0]) == "EXCLUDE_FROM_ALL":
                token = tokens.pop(0)
                child = TreeNode(NodeType.FLAG)
                child.children.append(token)
                CommentNode.consume_trailing(ctx, tokens, child)
                parg_group.children.append(child)
            state_ += 1
            src_group = PositionalGroupNode(sortable=sortable,
                                            tags=["file-list"])
            src_group.spec = PositionalSpec("+")
            active_depth = src_group
            tree.children.append(src_group)
        elif state_ is parsing_sources:
            token = tokens.pop(0)
            child = TreeNode(NodeType.ARGUMENT)
            child.children.append(token)
            CommentNode.consume_trailing(ctx, tokens, child)
            src_group.children.append(child)

            if only_comments_and_whitespace_remain(tokens, breakstack):
                active_depth = tree

    return tree
Exemple #7
0
def parse_add_custom_target(ctx, tokens, breakstack):
    """
  ::
    add_custom_target(Name [ALL] [command1 [args1...]]
                      [COMMAND command2 [args2...] ...]
                      [DEPENDS depend depend depend ... ]
                      [BYPRODUCTS [files...]]
                      [WORKING_DIRECTORY dir]
                      [COMMENT comment]
                      [JOB_POOL job_pool]
                      [VERBATIM] [USES_TERMINAL]
                      [COMMAND_EXPAND_LISTS]
                      [SOURCES src1 [src2...]])

  :see: https://cmake.org/cmake/help/latest/command/add_custom_target.html
  """
    kwargs = {
        "BYPRODUCTS": PositionalParser("+"),
        "COMMAND": ShellCommandNode.parse,
        "COMMENT": PositionalParser(1),
        "DEPENDS": PositionalParser("+"),
        "JOB_POOL": PositionalParser(1),
        "SOURCES": PositionalParser("+"),
        "WORKING_DIRECTORY": PositionalParser(1),
    }

    required_kwargs = {
        # Required by convention
        "COMMENT": "C0113"
    }

    flags = ("VERBATIM", "USES_TERMINAL", "COMMAND_EXPAND_LISTS")
    tree = ArgGroupNode()

    # If it is a whitespace token then put it directly in the parse tree at
    # the current depth
    while tokens and tokens[0].type in WHITESPACE_TOKENS:
        tree.children.append(tokens.pop(0))
        continue

    breaker = KwargBreaker(list(kwargs.keys()) + list(flags))
    child_breakstack = breakstack + [breaker]

    nametree = None
    state = "name"

    while tokens:
        # Break if the next token belongs to a parent parser, i.e. if it
        # matches a keyword argument of something higher in the stack, or if
        # it closes a parent group.
        if should_break(tokens[0], breakstack):
            break

        # If it is a whitespace token then put it directly in the parse tree at
        # the current depth
        if tokens[0].type in WHITESPACE_TOKENS:
            tree.children.append(tokens.pop(0))
            continue

        # If it's a comment, then add it at the current depth
        if tokens[0].type in (TokenType.COMMENT, TokenType.BRACKET_COMMENT):
            child = TreeNode(NodeType.COMMENT)
            tree.children.append(child)
            child.children.append(tokens.pop(0))
            continue

        ntokens = len(tokens)
        if state == "name":
            next_semantic = get_first_semantic_token(tokens[1:])
            if (next_semantic is not None
                    and get_normalized_kwarg(next_semantic) == "ALL"):
                npargs = 2
            else:
                npargs = 1

            nametree = PositionalGroupNode.parse(ctx, tokens, npargs, ["ALL"],
                                                 child_breakstack)
            assert len(tokens) < ntokens
            tree.children.append(nametree)
            state = "first-command"
            continue

        word = get_normalized_kwarg(tokens[0])
        if state == "first-command":
            if not (word in kwargs or word in flags):
                subtree = PositionalGroupNode.parse(ctx, tokens, '+', [],
                                                    child_breakstack)
                tree.children.append(subtree)
                assert len(tokens) < ntokens
            state = "kwargs"
            continue

        if word in flags:
            subtree = FlagGroupNode.parse(
                ctx, tokens, flags,
                breakstack + [KwargBreaker(list(kwargs.keys()))])
            assert len(tokens) < ntokens
            tree.children.append(subtree)
            continue

        if word in kwargs:
            required_kwargs.pop(word, None)
            subtree = KeywordGroupNode.parse(ctx, tokens, word, kwargs[word],
                                             child_breakstack)
            assert len(tokens) < ntokens
            tree.children.append(subtree)
            continue

        ctx.lint_ctx.record_lint("E1122", location=tokens[0].get_location())
        # logger.warning(
        #     "Unexpected positional argument %s at %s",
        #     tokens[0].spelling, tokens[0].location())
        subtree = PositionalGroupNode.parse(ctx, tokens, '+', [],
                                            child_breakstack)
        assert len(tokens) < ntokens
        tree.children.append(subtree)
        continue

    if required_kwargs:
        location = ()
        for token in tree.get_semantic_tokens():
            location = token.get_location()
            break

        missing_kwargs = sorted(
            (lintid, word) for word, lintid in sorted(required_kwargs.items()))
        for lintid, word in missing_kwargs:
            ctx.lint_ctx.record_lint(lintid, word, location=location)
    return tree