File: __init__.py

package info (click to toggle)
cmake-format 0.6.13-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,436 kB
  • sloc: python: 16,990; makefile: 14
file content (89 lines) | stat: -rw-r--r-- 2,087 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# pylint: disable=too-many-lines
"""
Statement parser functions
"""

import importlib
import logging

from cmakelang import lex
from cmakelang.parse.additional_nodes import ShellCommandNode
from cmakelang.parse.argument_nodes import (
    ConditionalGroupNode, StandardParser, StandardParser2
)
from cmakelang.parse.common import NodeType, KwargBreaker, TreeNode
from cmakelang.parse.util import (
    IMPLICIT_PARG_TYPES,
    WHITESPACE_TOKENS,
    get_first_semantic_token,
    get_normalized_kwarg,
    get_tag,
    should_break,
)
from cmakelang.parse.funs import standard_funs

logger = logging.getLogger(__name__)


def get_funtree(cmdspec):
  kwargs = {}

  for kwarg, subspec in cmdspec.kwargs.items():
    if kwarg in ("if", "elseif", "while"):
      subparser = ConditionalGroupNode.parse
    elif kwarg == "COMMAND":
      subparser = ShellCommandNode.parse
    elif isinstance(subspec, (standard_funs.CommandSpec)):
      subparser = StandardParser2(subspec, get_funtree(subspec))
    else:
      raise ValueError("Unexpected kwarg spec of type {}"
                       .format(type(subspec)))

    kwargs[kwarg] = subparser

  return kwargs


SUBMODULE_NAMES = [
    "add_executable",
    "add_library",
    "add_xxx",
    "deprecated",
    "break",
    "external_project",
    "fetch_content",
    "foreach",
    "file",
    "install",
    "list",
    "miscellaneous",
    "random",
    "set",
    "set_target_properties",
]


def get_parse_db():
  """
  Returns a dictionary mapping statement name to parse functor for that
  statement.
  """

  parse_db = {}

  for subname in SUBMODULE_NAMES:
    submodule = importlib.import_module("cmakelang.parse.funs." + subname)
    submodule.populate_db(parse_db)

  for key in (
      "if", "else", "elseif", "endif", "while", "endwhile"):
    parse_db[key] = ConditionalGroupNode.parse

  for key in ("function", "macro"):
    parse_db[key] = StandardParser("1+")

  for key in ("endfunction", "endmacro"):
    parse_db[key] = StandardParser("?")

  parse_db.update(get_funtree(standard_funs.get_fn_spec()))
  return parse_db