Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/pygments/lexers/nimrod.py: 100%

26 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-07-01 06:54 +0000

1""" 

2 pygments.lexers.nimrod 

3 ~~~~~~~~~~~~~~~~~~~~~~ 

4 

5 Lexer for the Nim language (formerly known as Nimrod). 

6 

7 :copyright: Copyright 2006-2023 by the Pygments team, see AUTHORS. 

8 :license: BSD, see LICENSE for details. 

9""" 

10 

11import re 

12 

13from pygments.lexer import RegexLexer, include, default, bygroups 

14from pygments.token import Text, Comment, Operator, Keyword, Name, String, \ 

15 Number, Punctuation, Error 

16 

17__all__ = ['NimrodLexer'] 

18 

19 

20class NimrodLexer(RegexLexer): 

21 """ 

22 For Nim source code. 

23 

24 .. versionadded:: 1.5 

25 """ 

26 

27 name = 'Nimrod' 

28 url = 'http://nim-lang.org/' 

29 aliases = ['nimrod', 'nim'] 

30 filenames = ['*.nim', '*.nimrod'] 

31 mimetypes = ['text/x-nim'] 

32 

33 flags = re.MULTILINE | re.IGNORECASE 

34 

35 def underscorize(words): 

36 newWords = [] 

37 new = [] 

38 for word in words: 

39 for ch in word: 

40 new.append(ch) 

41 new.append("_?") 

42 newWords.append(''.join(new)) 

43 new = [] 

44 return "|".join(newWords) 

45 

46 keywords = [ 

47 'addr', 'and', 'as', 'asm', 'bind', 'block', 'break', 'case', 

48 'cast', 'concept', 'const', 'continue', 'converter', 'defer', 'discard', 

49 'distinct', 'div', 'do', 'elif', 'else', 'end', 'enum', 'except', 

50 'export', 'finally', 'for', 'if', 'in', 'yield', 'interface', 

51 'is', 'isnot', 'iterator', 'let', 'mixin', 'mod', 

52 'not', 'notin', 'object', 'of', 'or', 'out', 'ptr', 'raise', 

53 'ref', 'return', 'shl', 'shr', 'static', 'try', 

54 'tuple', 'type', 'using', 'when', 'while', 'xor' 

55 ] 

56 

57 keywordsPseudo = [ 

58 'nil', 'true', 'false' 

59 ] 

60 

61 opWords = [ 

62 'and', 'or', 'not', 'xor', 'shl', 'shr', 'div', 'mod', 'in', 

63 'notin', 'is', 'isnot' 

64 ] 

65 

66 types = [ 

67 'int', 'int8', 'int16', 'int32', 'int64', 'float', 'float32', 'float64', 

68 'bool', 'char', 'range', 'array', 'seq', 'set', 'string' 

69 ] 

70 

71 tokens = { 

72 'root': [ 

73 # Comments 

74 (r'##\[', String.Doc, 'doccomment'), 

75 (r'##.*$', String.Doc), 

76 (r'#\[', Comment.Multiline, 'comment'), 

77 (r'#.*$', Comment), 

78 

79 # Pragmas 

80 (r'\{\.', String.Other, 'pragma'), 

81 

82 # Operators 

83 (r'[*=><+\-/@$~&%!?|\\\[\]]', Operator), 

84 (r'\.\.|\.|,|\[\.|\.\]|\{\.|\.\}|\(\.|\.\)|\{|\}|\(|\)|:|\^|`|;', 

85 Punctuation), 

86 

87 # Case statement branch 

88 (r'(\n\s*)(of)(\s)', bygroups(Text.Whitespace, Keyword, 

89 Text.Whitespace), 'casebranch'), 

90 

91 # Strings 

92 (r'(?:[\w]+)"', String, 'rdqs'), 

93 (r'"""', String.Double, 'tdqs'), 

94 ('"', String, 'dqs'), 

95 

96 # Char 

97 ("'", String.Char, 'chars'), 

98 

99 # Keywords 

100 (r'(%s)\b' % underscorize(opWords), Operator.Word), 

101 (r'(proc|func|method|macro|template)(\s)(?![(\[\]])', 

102 bygroups(Keyword, Text.Whitespace), 'funcname'), 

103 (r'(%s)\b' % underscorize(keywords), Keyword), 

104 (r'(%s)\b' % underscorize(['from', 'import', 'include', 'export']), 

105 Keyword.Namespace), 

106 (r'(v_?a_?r)\b', Keyword.Declaration), 

107 (r'(%s)\b' % underscorize(types), Name.Builtin), 

108 (r'(%s)\b' % underscorize(keywordsPseudo), Keyword.Pseudo), 

109 

110 # Identifiers 

111 (r'\b((?![_\d])\w)(((?!_)\w)|(_(?!_)\w))*', Name), 

112 

113 # Numbers 

114 (r'[0-9][0-9_]*(?=([e.]|\'f(32|64)))', 

115 Number.Float, ('float-suffix', 'float-number')), 

116 (r'0x[a-f0-9][a-f0-9_]*', Number.Hex, 'int-suffix'), 

117 (r'0b[01][01_]*', Number.Bin, 'int-suffix'), 

118 (r'0o[0-7][0-7_]*', Number.Oct, 'int-suffix'), 

119 (r'[0-9][0-9_]*', Number.Integer, 'int-suffix'), 

120 

121 # Whitespace 

122 (r'\s+', Text.Whitespace), 

123 (r'.+$', Error), 

124 ], 

125 'chars': [ 

126 (r'\\([\\abcefnrtvl"\']|x[a-f0-9]{2}|[0-9]{1,3})', String.Escape), 

127 (r"'", String.Char, '#pop'), 

128 (r".", String.Char) 

129 ], 

130 'strings': [ 

131 (r'(?<!\$)\$(\d+|#|\w+)+', String.Interpol), 

132 (r'[^\\\'"$\n]+', String), 

133 # quotes, dollars and backslashes must be parsed one at a time 

134 (r'[\'"\\]', String), 

135 # unhandled string formatting sign 

136 (r'\$', String) 

137 # newlines are an error (use "nl" state) 

138 ], 

139 'doccomment': [ 

140 (r'[^\]#]+', String.Doc), 

141 (r'##\[', String.Doc, '#push'), 

142 (r'\]##', String.Doc, '#pop'), 

143 (r'[\]#]', String.Doc), 

144 ], 

145 'comment': [ 

146 (r'[^\]#]+', Comment.Multiline), 

147 (r'#\[', Comment.Multiline, '#push'), 

148 (r'\]#', Comment.Multiline, '#pop'), 

149 (r'[\]#]', Comment.Multiline), 

150 ], 

151 'dqs': [ 

152 (r'\\([\\abcefnrtvl"\']|\n|x[a-f0-9]{2}|[0-9]{1,3})', 

153 String.Escape), 

154 (r'"', String, '#pop'), 

155 include('strings') 

156 ], 

157 'rdqs': [ 

158 (r'"(?!")', String, '#pop'), 

159 (r'""', String.Escape), 

160 include('strings') 

161 ], 

162 'tdqs': [ 

163 (r'"""', String.Double, '#pop'), 

164 include('strings'), 

165 (r'\n', String.Double) 

166 ], 

167 'funcname': [ 

168 (r'((?![\d_])\w)(((?!_)\w)|(_(?!_)\w))*', Name.Function, '#pop'), 

169 (r'`.+`', Name.Function, '#pop') 

170 ], 

171 'nl': [ 

172 (r'\n', String) 

173 ], 

174 'float-number': [ 

175 (r'\.(?!\.)[0-9_]*[f]*', Number.Float), 

176 (r'e[+-]?[0-9][0-9_]*', Number.Float), 

177 default('#pop') 

178 ], 

179 'float-suffix': [ 

180 (r'\'f(32|64)', Number.Float), 

181 default('#pop') 

182 ], 

183 'int-suffix': [ 

184 (r'\'i(32|64)', Number.Integer.Long), 

185 (r'\'i(8|16)', Number.Integer), 

186 default('#pop') 

187 ], 

188 'casebranch': [ 

189 (r',', Punctuation), 

190 (r'[\n ]+', Text.Whitespace), 

191 (r':', Operator, '#pop'), 

192 (r'\w+|[^:]', Name.Label), 

193 ], 

194 'pragma': [ 

195 (r'[:,]', Text), 

196 (r'[\n ]+', Text.Whitespace), 

197 (r'\.\}', String.Other, '#pop'), 

198 (r'\w+|\W+|[^.}]', String.Other), 

199 ], 

200 }