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

12 statements  

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

1""" 

2 pygments.lexers.fantom 

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

4 

5 Lexer for the Fantom language. 

6 

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

8 :license: BSD, see LICENSE for details. 

9""" 

10 

11from string import Template 

12 

13from pygments.lexer import RegexLexer, include, bygroups, using, \ 

14 this, default, words 

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

16 Number, Punctuation, Literal, Whitespace 

17 

18__all__ = ['FantomLexer'] 

19 

20 

21class FantomLexer(RegexLexer): 

22 """ 

23 For Fantom source code. 

24 

25 .. versionadded:: 1.5 

26 """ 

27 name = 'Fantom' 

28 aliases = ['fan'] 

29 filenames = ['*.fan'] 

30 mimetypes = ['application/x-fantom'] 

31 

32 # often used regexes 

33 def s(str): 

34 return Template(str).substitute( 

35 dict( 

36 pod=r'[\"\w\.]+', 

37 eos=r'\n|;', 

38 id=r'[a-zA-Z_]\w*', 

39 # all chars which can be part of type definition. Starts with 

40 # either letter, or [ (maps), or | (funcs) 

41 type=r'(?:\[|[a-zA-Z_]|\|)[:\w\[\]|\->?]*?', 

42 ) 

43 ) 

44 

45 tokens = { 

46 'comments': [ 

47 (r'(?s)/\*.*?\*/', Comment.Multiline), # Multiline 

48 (r'//.*?$', Comment.Single), # Single line 

49 # TODO: highlight references in fandocs 

50 (r'\*\*.*?$', Comment.Special), # Fandoc 

51 (r'#.*$', Comment.Single) # Shell-style 

52 ], 

53 'literals': [ 

54 (r'\b-?[\d_]+(ns|ms|sec|min|hr|day)', Number), # Duration 

55 (r'\b-?[\d_]*\.[\d_]+(ns|ms|sec|min|hr|day)', Number), # Duration with dot 

56 (r'\b-?(\d+)?\.\d+(f|F|d|D)?', Number.Float), # Float/Decimal 

57 (r'\b-?0x[0-9a-fA-F_]+', Number.Hex), # Hex 

58 (r'\b-?[\d_]+', Number.Integer), # Int 

59 (r"'\\.'|'[^\\]'|'\\u[0-9a-f]{4}'", String.Char), # Char 

60 (r'"', Punctuation, 'insideStr'), # Opening quote 

61 (r'`', Punctuation, 'insideUri'), # Opening accent 

62 (r'\b(true|false|null)\b', Keyword.Constant), # Bool & null 

63 (r'(?:(\w+)(::))?(\w+)(<\|)(.*?)(\|>)', # DSL 

64 bygroups(Name.Namespace, Punctuation, Name.Class, 

65 Punctuation, String, Punctuation)), 

66 (r'(?:(\w+)(::))?(\w+)?(#)(\w+)?', # Type/slot literal 

67 bygroups(Name.Namespace, Punctuation, Name.Class, 

68 Punctuation, Name.Function)), 

69 (r'\[,\]', Literal), # Empty list 

70 (s(r'($type)(\[,\])'), # Typed empty list 

71 bygroups(using(this, state='inType'), Literal)), 

72 (r'\[:\]', Literal), # Empty Map 

73 (s(r'($type)(\[:\])'), 

74 bygroups(using(this, state='inType'), Literal)), 

75 ], 

76 'insideStr': [ 

77 (r'\\\\', String.Escape), # Escaped backslash 

78 (r'\\"', String.Escape), # Escaped " 

79 (r'\\`', String.Escape), # Escaped ` 

80 (r'\$\w+', String.Interpol), # Subst var 

81 (r'\$\{.*?\}', String.Interpol), # Subst expr 

82 (r'"', Punctuation, '#pop'), # Closing quot 

83 (r'.', String) # String content 

84 ], 

85 'insideUri': [ # TODO: remove copy/paste str/uri 

86 (r'\\\\', String.Escape), # Escaped backslash 

87 (r'\\"', String.Escape), # Escaped " 

88 (r'\\`', String.Escape), # Escaped ` 

89 (r'\$\w+', String.Interpol), # Subst var 

90 (r'\$\{.*?\}', String.Interpol), # Subst expr 

91 (r'`', Punctuation, '#pop'), # Closing tick 

92 (r'.', String.Backtick) # URI content 

93 ], 

94 'protectionKeywords': [ 

95 (r'\b(public|protected|private|internal)\b', Keyword), 

96 ], 

97 'typeKeywords': [ 

98 (r'\b(abstract|final|const|native|facet|enum)\b', Keyword), 

99 ], 

100 'methodKeywords': [ 

101 (r'\b(abstract|native|once|override|static|virtual|final)\b', 

102 Keyword), 

103 ], 

104 'fieldKeywords': [ 

105 (r'\b(abstract|const|final|native|override|static|virtual|' 

106 r'readonly)\b', Keyword) 

107 ], 

108 'otherKeywords': [ 

109 (words(( 

110 'try', 'catch', 'throw', 'finally', 'for', 'if', 'else', 'while', 

111 'as', 'is', 'isnot', 'switch', 'case', 'default', 'continue', 

112 'break', 'do', 'return', 'get', 'set'), prefix=r'\b', suffix=r'\b'), 

113 Keyword), 

114 (r'\b(it|this|super)\b', Name.Builtin.Pseudo), 

115 ], 

116 'operators': [ 

117 (r'\+\+|\-\-|\+|\-|\*|/|\|\||&&|<=>|<=|<|>=|>|=|!|\[|\]', Operator) 

118 ], 

119 'inType': [ 

120 (r'[\[\]|\->:?]', Punctuation), 

121 (s(r'$id'), Name.Class), 

122 default('#pop'), 

123 

124 ], 

125 'root': [ 

126 include('comments'), 

127 include('protectionKeywords'), 

128 include('typeKeywords'), 

129 include('methodKeywords'), 

130 include('fieldKeywords'), 

131 include('literals'), 

132 include('otherKeywords'), 

133 include('operators'), 

134 (r'using\b', Keyword.Namespace, 'using'), # Using stmt 

135 (r'@\w+', Name.Decorator, 'facet'), # Symbol 

136 (r'(class|mixin)(\s+)(\w+)', bygroups(Keyword, Whitespace, Name.Class), 

137 'inheritance'), # Inheritance list 

138 

139 # Type var := val 

140 (s(r'($type)([ \t]+)($id)(\s*)(:=)'), 

141 bygroups(using(this, state='inType'), Whitespace, 

142 Name.Variable, Whitespace, Operator)), 

143 

144 # var := val 

145 (s(r'($id)(\s*)(:=)'), 

146 bygroups(Name.Variable, Whitespace, Operator)), 

147 

148 # .someId( or ->someId( ### 

149 (s(r'(\.|(?:\->))($id)(\s*)(\()'), 

150 bygroups(Operator, Name.Function, Whitespace, Punctuation), 

151 'insideParen'), 

152 

153 # .someId or ->someId 

154 (s(r'(\.|(?:\->))($id)'), 

155 bygroups(Operator, Name.Function)), 

156 

157 # new makeXXX ( 

158 (r'(new)(\s+)(make\w*)(\s*)(\()', 

159 bygroups(Keyword, Whitespace, Name.Function, Whitespace, Punctuation), 

160 'insideMethodDeclArgs'), 

161 

162 # Type name ( 

163 (s(r'($type)([ \t]+)' # Return type and whitespace 

164 r'($id)(\s*)(\()'), # method name + open brace 

165 bygroups(using(this, state='inType'), Whitespace, 

166 Name.Function, Whitespace, Punctuation), 

167 'insideMethodDeclArgs'), 

168 

169 # ArgType argName, 

170 (s(r'($type)(\s+)($id)(\s*)(,)'), 

171 bygroups(using(this, state='inType'), Whitespace, Name.Variable, 

172 Whitespace, Punctuation)), 

173 

174 # ArgType argName) 

175 # Covered in 'insideParen' state 

176 

177 # ArgType argName -> ArgType| 

178 (s(r'($type)(\s+)($id)(\s*)(\->)(\s*)($type)(\|)'), 

179 bygroups(using(this, state='inType'), Whitespace, Name.Variable, 

180 Whitespace, Punctuation, Whitespace, using(this, state='inType'), 

181 Punctuation)), 

182 

183 # ArgType argName| 

184 (s(r'($type)(\s+)($id)(\s*)(\|)'), 

185 bygroups(using(this, state='inType'), Whitespace, Name.Variable, 

186 Whitespace, Punctuation)), 

187 

188 # Type var 

189 (s(r'($type)([ \t]+)($id)'), 

190 bygroups(using(this, state='inType'), Whitespace, 

191 Name.Variable)), 

192 

193 (r'\(', Punctuation, 'insideParen'), 

194 (r'\{', Punctuation, 'insideBrace'), 

195 (r'\s+', Whitespace), 

196 (r'.', Text) 

197 ], 

198 'insideParen': [ 

199 (r'\)', Punctuation, '#pop'), 

200 include('root'), 

201 ], 

202 'insideMethodDeclArgs': [ 

203 (r'\)', Punctuation, '#pop'), 

204 (s(r'($type)(\s+)($id)(\s*)(\))'), 

205 bygroups(using(this, state='inType'), Whitespace, Name.Variable, 

206 Whitespace, Punctuation), '#pop'), 

207 include('root'), 

208 ], 

209 'insideBrace': [ 

210 (r'\}', Punctuation, '#pop'), 

211 include('root'), 

212 ], 

213 'inheritance': [ 

214 (r'\s+', Whitespace), # Whitespace 

215 (r':|,', Punctuation), 

216 (r'(?:(\w+)(::))?(\w+)', 

217 bygroups(Name.Namespace, Punctuation, Name.Class)), 

218 (r'\{', Punctuation, '#pop') 

219 ], 

220 'using': [ 

221 (r'[ \t]+', Whitespace), # consume whitespaces 

222 (r'(\[)(\w+)(\])', 

223 bygroups(Punctuation, Comment.Special, Punctuation)), # ffi 

224 (r'(\")?([\w.]+)(\")?', 

225 bygroups(Punctuation, Name.Namespace, Punctuation)), # podname 

226 (r'::', Punctuation, 'usingClass'), 

227 default('#pop') 

228 ], 

229 'usingClass': [ 

230 (r'[ \t]+', Whitespace), # consume whitespaces 

231 (r'(as)(\s+)(\w+)', 

232 bygroups(Keyword.Declaration, Whitespace, Name.Class), '#pop:2'), 

233 (r'[\w$]+', Name.Class), 

234 default('#pop:2') # jump out to root state 

235 ], 

236 'facet': [ 

237 (r'\s+', Whitespace), 

238 (r'\{', Punctuation, 'facetFields'), 

239 default('#pop') 

240 ], 

241 'facetFields': [ 

242 include('comments'), 

243 include('literals'), 

244 include('operators'), 

245 (r'\s+', Whitespace), 

246 (r'(\s*)(\w+)(\s*)(=)', bygroups(Whitespace, Name, Whitespace, Operator)), 

247 (r'\}', Punctuation, '#pop'), 

248 (r'\s+', Whitespace), 

249 (r'.', Text) 

250 ], 

251 }