Coverage for C:\leo.repo\leo-editor\leo\plugins\importers\tcl.py: 50%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

42 statements  

1#@+leo-ver=5-thin 

2#@+node:ekr.20170615153639.2: * @file ../plugins/importers/tcl.py 

3""" 

4The @auto importer for the tcl language. 

5 

6Created 2017/06/15 by the `importer;;` abbreviation. 

7""" 

8import re 

9from leo.core import leoGlobals as g 

10from leo.plugins.importers import linescanner 

11assert g 

12Importer = linescanner.Importer 

13Target = linescanner.Target 

14#@+others 

15#@+node:ekr.20170615153639.3: ** class Tcl_Importer 

16class Tcl_Importer(Importer): 

17 """The importer for the tcl lanuage.""" 

18 

19 def __init__(self, importCommands, **kwargs): 

20 """Tcl_Importer.__init__""" 

21 super().__init__( 

22 importCommands, 

23 language='tcl', 

24 state_class=Tcl_ScanState, 

25 strict=False, 

26 ) 

27 

28 #@+others 

29 #@+node:ekr.20170615155627.1: *3* tcl.starts_block 

30 starts_pattern = re.compile(r'\s*(proc)\s+') 

31 

32 def starts_block(self, i, lines, new_state, prev_state): 

33 """True if the line startswith proc outside any context.""" 

34 if prev_state.in_context(): 

35 return False 

36 line = lines[i] 

37 m = self.starts_pattern.match(line) 

38 return bool(m) 

39 #@+node:ekr.20170615153639.5: *3* tcl.clean_headline 

40 proc_pattern = re.compile(r'\s*proc\s+([\w$]+)') 

41 

42 def clean_headline(self, s, p=None): 

43 """Return a cleaned up headline s.""" 

44 m = re.match(self.proc_pattern, s) 

45 return 'proc ' + m.group(1) if m else s 

46 #@-others 

47#@+node:ekr.20170615153639.7: ** class class Tcl_ScanState 

48class Tcl_ScanState: 

49 """A class representing the state of the tcl line-oriented scan.""" 

50 

51 def __init__(self, d=None): 

52 """Tcl_ScanState.__init__""" 

53 if d: 

54 prev = d.get('prev') 

55 self.context = prev.context 

56 self.curlies = prev.curlies 

57 else: 

58 self.context = '' 

59 self.curlies = 0 

60 

61 def __repr__(self): 

62 """Tcl_ScanState.__repr__""" 

63 return "Tcl_ScanState context: %r curlies: %s" % ( 

64 self.context, self.curlies) 

65 

66 __str__ = __repr__ 

67 

68 #@+others 

69 #@+node:ekr.20170615160228.1: *3* tcl_state.in_context 

70 def in_context(self): 

71 """True if in a special context.""" 

72 return self.context # or self.curlies > 0 

73 

74 #@+node:ekr.20170615153639.8: *3* tcl_state.level 

75 def level(self): 

76 """Tcl_ScanState.level.""" 

77 return self.curlies 

78 #@+node:ekr.20170615153639.9: *3* tcl_state.update 

79 def update(self, data): 

80 """ 

81 Tcl_ScanState.update 

82 

83 Update the state using the 6-tuple returned by v2_scan_line. 

84 Return i = data[1] 

85 """ 

86 context, i, delta_c, delta_p, delta_s, bs_nl = data 

87 # All ScanState classes must have a context ivar. 

88 self.context = context 

89 self.curlies += delta_c 

90 return i 

91 #@-others 

92#@-others 

93importer_dict = { 

94 'func': Tcl_Importer.do_import(), 

95 'extensions': ['.tcl'], 

96} 

97#@@language python 

98#@@tabwidth -4 

99 

100 

101#@-leo