summary refs log tree commit diff stats
path: root/compiler/depends.nim
blob: d0a1139ef844de43c367976845d04cc9ab532837 (plain) (blame)
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
#
#
#           The Nim Compiler
#        (c) Copyright 2012 Andreas Rumpf
#
#    See the file "copying.txt", included in this
#    distribution, for details about the copyright.
#

# This module implements a dependency file generator.

import
  os, options, ast, astalgo, msgs, ropes, idents, passes, modulepaths

from modulegraphs import ModuleGraph

type
  TGen = object of TPassContext
    module: PSym
    config: ConfigRef
    graph: ModuleGraph
  PGen = ref TGen

  Backend = ref object of RootRef
    dotGraph: Rope

proc addDependencyAux(b: Backend; importing, imported: string) =
  addf(b.dotGraph, "$1 -> \"$2\";$n", [rope(importing), rope(imported)])
  # s1 -> s2_4[label="[0-9]"];

proc addDotDependency(c: PPassContext, n: PNode): PNode =
  result = n
  let g = PGen(c)
  let b = Backend(g.graph.backend)
  case n.kind
  of nkImportStmt:
    for i in countup(0, sonsLen(n) - 1):
      var imported = getModuleName(g.config, n.sons[i])
      addDependencyAux(b, g.module.name.s, imported)
  of nkFromStmt, nkImportExceptStmt:
    var imported = getModuleName(g.config, n.sons[0])
    addDependencyAux(b, g.module.name.s, imported)
  of nkStmtList, nkBlockStmt, nkStmtListExpr, nkBlockExpr:
    for i in countup(0, sonsLen(n) - 1): discard addDotDependency(c, n.sons[i])
  else:
    discard

proc generateDot*(graph: ModuleGraph; project: string) =
  let b = Backend(graph.backend)
  discard writeRope("digraph $1 {$n$2}$n" % [
      rope(changeFileExt(extractFilename(project), "")), b.dotGraph],
            changeFileExt(project, "dot"))

proc myOpen(graph: ModuleGraph; module: PSym): PPassContext =
  var g: PGen
  new(g)
  g.module = module
  g.config = graph.config
  g.graph = graph
  if graph.backend == nil:
    graph.backend = Backend(dotGraph: nil)
  result = g

const gendependPass* = makePass(open = myOpen, process = addDotDependency)
span class="n">i, 0) except: pass else: for chunk in ansi.text_with_fg_bg_attr(line): if isinstance(chunk, tuple): self.set_fg_bg_attr(*chunk) else: self.addstr(chunk) def move(self, narg=None, **kw): direction = Direction(kw) if direction.horizontal(): self.startx = direction.move( direction=direction.right(), override=narg, maximum=self.max_width, current=self.startx, pagesize=self.wid, offset=-self.wid + 1) if direction.vertical(): if self.source_is_stream: self._get_line(self.scroll_begin + self.hei * 2) self.scroll_begin = direction.move( direction=direction.down(), override=narg, maximum=len(self.lines), current=self.scroll_begin, pagesize=self.hei, offset=-self.hei + 1) def press(self, key): self.env.keymanager.use_context(self.embedded and 'embedded_pager' or 'pager') self.env.key_append(key) kbuf = self.env.keybuffer cmd = kbuf.command if kbuf.failure: kbuf.clear() return elif not cmd: return self.env.cmd = cmd if cmd.function: try: cmd.function(CommandArgs.from_widget(self)) except Exception as error: self.fm.notify(error) if kbuf.done: kbuf.clear() else: kbuf.clear() def set_source(self, source, strip=False): if self.source and self.source_is_stream: self.source.close() self.max_width = 0 if isinstance(source, str): self.source_is_stream = False self.lines = source.splitlines() if self.lines: self.max_width = max(len(line) for line in self.lines) elif hasattr(source, '__getitem__'): self.source_is_stream = False self.lines = source if self.lines: self.max_width = max(len(line) for line in source) elif hasattr(source, 'readline'): self.source_is_stream = True self.lines = [] else: self.source = None self.source_is_stream = False return False self.markup = 'ansi' if not self.source_is_stream and strip: self.lines = map(lambda x: x.strip(), self.lines) self.source = source return True def click(self, event): n = event.ctrl() and 1 or 3 direction = event.mouse_wheel_direction() if direction: self.move(down=direction * n) return True def _get_line(self, n, attempt_to_read=True): assert isinstance(n, int), n try: return self.lines[n] except (KeyError, IndexError): if attempt_to_read and self.source_is_stream: try: for l in self.source: if len(l) > self.max_width: self.max_width = len(l) self.lines.append(l) if len(self.lines) > n: break except (UnicodeError, IOError): pass return self._get_line(n, attempt_to_read=False) return "" def _generate_lines(self, starty, startx): i = starty if not self.source: raise StopIteration while True: try: line = self._get_line(i).expandtabs(4) if self.markup is 'ansi': line = ansi.char_slice(line, startx, self.wid + startx) \ + ansi.reset else: line = line[startx:self.wid + startx] yield line.rstrip() except IndexError: raise StopIteration i += 1