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 (68 lines) | stat: -rw-r--r-- 1,792 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
# -*- coding: utf-8 -*-
# pylint: disable=too-many-lines
from __future__ import print_function
from __future__ import unicode_literals

import contextlib

from cmakelang import common


class MockEverything(object):
  """Dummy object which implements any interface by mocking all functions
     with an empty implementation that returns None"""

  # pylint: disable=unused-argument
  def _dummy(self, *_args, **_kwargs):
    return

  def __getattr__(self, _name):
    return self._dummy


class ParseContext(object):
  """Global context passed through every function in the parse stack."""

  def __init__(self, parse_db=None, lint_ctx=None, config=None):
    if parse_db is None:
      from cmakelang.parse.funs import get_parse_db
      parse_db = get_parse_db()
    self.parse_db = parse_db

    if lint_ctx is None:
      lint_ctx = MockEverything()
    self.lint_ctx = lint_ctx

    if config is None:
      from cmakelang import configuration
      config = configuration.Configuration()
    self.config = config

    # List of currently open parse nodes. Only used by nodes below
    # the statement level.
    self.argstack = []

  @contextlib.contextmanager
  def pusharg(self, node):
    self.argstack.append(node)
    yield None
    if not self.argstack:
      raise common.InternalError(
          "Unexpected empty argstack, expected {}".format(node))

    if self.argstack[-1] is not node:
      raise common.InternalError(
          "Unexpected node {} on argstack, expecting {}"
          .format(self.argstack[-1], node))

    self.argstack.pop(-1)


def parse(tokens, ctx=None):
  """
  digest tokens, then layout the digested blocks.
  """
  if ctx is None:
    ctx = ParseContext()
  from cmakelang.parse.body_nodes import BodyNode
  return BodyNode.consume(ctx, tokens)