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

29 statements  

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

1""" 

2 pygments.lexers.prolog 

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

4 

5 Lexers for Prolog and Prolog-like languages. 

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, bygroups 

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

15 Number, Punctuation 

16 

17__all__ = ['PrologLexer', 'LogtalkLexer'] 

18 

19 

20class PrologLexer(RegexLexer): 

21 """ 

22 Lexer for Prolog files. 

23 """ 

24 name = 'Prolog' 

25 aliases = ['prolog'] 

26 filenames = ['*.ecl', '*.prolog', '*.pro', '*.pl'] 

27 mimetypes = ['text/x-prolog'] 

28 

29 tokens = { 

30 'root': [ 

31 (r'/\*', Comment.Multiline, 'nested-comment'), 

32 (r'%.*', Comment.Single), 

33 # character literal 

34 (r'0\'.', String.Char), 

35 (r'0b[01]+', Number.Bin), 

36 (r'0o[0-7]+', Number.Oct), 

37 (r'0x[0-9a-fA-F]+', Number.Hex), 

38 # literal with prepended base 

39 (r'\d\d?\'[a-zA-Z0-9]+', Number.Integer), 

40 (r'(\d+\.\d*|\d*\.\d+)([eE][+-]?[0-9]+)?', Number.Float), 

41 (r'\d+', Number.Integer), 

42 (r'[\[\](){}|.,;!]', Punctuation), 

43 (r':-|-->', Punctuation), 

44 (r'"(?:\\x[0-9a-fA-F]+\\|\\u[0-9a-fA-F]{4}|\\U[0-9a-fA-F]{8}|' 

45 r'\\[0-7]+\\|\\["\\abcefnrstv]|[^\\"])*"', String.Double), 

46 (r"'(?:''|[^'])*'", String.Atom), # quoted atom 

47 # Needs to not be followed by an atom. 

48 # (r'=(?=\s|[a-zA-Z\[])', Operator), 

49 (r'is\b', Operator), 

50 (r'(<|>|=<|>=|==|=:=|=|/|//|\*|\+|-)(?=\s|[a-zA-Z0-9\[])', 

51 Operator), 

52 (r'(mod|div|not)\b', Operator), 

53 (r'_', Keyword), # The don't-care variable 

54 (r'([a-z]+)(:)', bygroups(Name.Namespace, Punctuation)), 

55 (r'([a-z\u00c0-\u1fff\u3040-\ud7ff\ue000-\uffef]' 

56 r'[\w$\u00c0-\u1fff\u3040-\ud7ff\ue000-\uffef]*)' 

57 r'(\s*)(:-|-->)', 

58 bygroups(Name.Function, Text, Operator)), # function defn 

59 (r'([a-z\u00c0-\u1fff\u3040-\ud7ff\ue000-\uffef]' 

60 r'[\w$\u00c0-\u1fff\u3040-\ud7ff\ue000-\uffef]*)' 

61 r'(\s*)(\()', 

62 bygroups(Name.Function, Text, Punctuation)), 

63 (r'[a-z\u00c0-\u1fff\u3040-\ud7ff\ue000-\uffef]' 

64 r'[\w$\u00c0-\u1fff\u3040-\ud7ff\ue000-\uffef]*', 

65 String.Atom), # atom, characters 

66 # This one includes ! 

67 (r'[#&*+\-./:<=>?@\\^~\u00a1-\u00bf\u2010-\u303f]+', 

68 String.Atom), # atom, graphics 

69 (r'[A-Z_]\w*', Name.Variable), 

70 (r'\s+|[\u2000-\u200f\ufff0-\ufffe\uffef]', Text), 

71 ], 

72 'nested-comment': [ 

73 (r'\*/', Comment.Multiline, '#pop'), 

74 (r'/\*', Comment.Multiline, '#push'), 

75 (r'[^*/]+', Comment.Multiline), 

76 (r'[*/]', Comment.Multiline), 

77 ], 

78 } 

79 

80 def analyse_text(text): 

81 return ':-' in text 

82 

83 

84class LogtalkLexer(RegexLexer): 

85 """ 

86 For Logtalk source code. 

87 

88 .. versionadded:: 0.10 

89 """ 

90 

91 name = 'Logtalk' 

92 url = 'http://logtalk.org/' 

93 aliases = ['logtalk'] 

94 filenames = ['*.lgt', '*.logtalk'] 

95 mimetypes = ['text/x-logtalk'] 

96 

97 tokens = { 

98 'root': [ 

99 # Directives 

100 (r'^\s*:-\s', Punctuation, 'directive'), 

101 # Comments 

102 (r'%.*?\n', Comment), 

103 (r'/\*(.|\n)*?\*/', Comment), 

104 # Whitespace 

105 (r'\n', Text), 

106 (r'\s+', Text), 

107 # Numbers 

108 (r"0'[\\]?.", Number), 

109 (r'0b[01]+', Number.Bin), 

110 (r'0o[0-7]+', Number.Oct), 

111 (r'0x[0-9a-fA-F]+', Number.Hex), 

112 (r'\d+\.?\d*((e|E)(\+|-)?\d+)?', Number), 

113 # Variables 

114 (r'([A-Z_][a-zA-Z0-9_]*)', Name.Variable), 

115 # Event handlers 

116 (r'(after|before)(?=[(])', Keyword), 

117 # Message forwarding handler 

118 (r'forward(?=[(])', Keyword), 

119 # Execution-context methods 

120 (r'(context|parameter|this|se(lf|nder))(?=[(])', Keyword), 

121 # Reflection 

122 (r'(current_predicate|predicate_property)(?=[(])', Keyword), 

123 # DCGs and term expansion 

124 (r'(expand_(goal|term)|(goal|term)_expansion|phrase)(?=[(])', Keyword), 

125 # Entity 

126 (r'(abolish|c(reate|urrent))_(object|protocol|category)(?=[(])', Keyword), 

127 (r'(object|protocol|category)_property(?=[(])', Keyword), 

128 # Entity relations 

129 (r'co(mplements_object|nforms_to_protocol)(?=[(])', Keyword), 

130 (r'extends_(object|protocol|category)(?=[(])', Keyword), 

131 (r'imp(lements_protocol|orts_category)(?=[(])', Keyword), 

132 (r'(instantiat|specializ)es_class(?=[(])', Keyword), 

133 # Events 

134 (r'(current_event|(abolish|define)_events)(?=[(])', Keyword), 

135 # Flags 

136 (r'(create|current|set)_logtalk_flag(?=[(])', Keyword), 

137 # Compiling, loading, and library paths 

138 (r'logtalk_(compile|l(ibrary_path|oad|oad_context)|make(_target_action)?)(?=[(])', Keyword), 

139 (r'\blogtalk_make\b', Keyword), 

140 # Database 

141 (r'(clause|retract(all)?)(?=[(])', Keyword), 

142 (r'a(bolish|ssert(a|z))(?=[(])', Keyword), 

143 # Control constructs 

144 (r'(ca(ll|tch)|throw)(?=[(])', Keyword), 

145 (r'(fa(il|lse)|true|(instantiation|system)_error)\b', Keyword), 

146 (r'(type|domain|existence|permission|representation|evaluation|resource|syntax)_error(?=[(])', Keyword), 

147 # All solutions 

148 (r'((bag|set)of|f(ind|or)all)(?=[(])', Keyword), 

149 # Multi-threading predicates 

150 (r'threaded(_(ca(ll|ncel)|once|ignore|exit|peek|wait|notify))?(?=[(])', Keyword), 

151 # Engine predicates 

152 (r'threaded_engine(_(create|destroy|self|next|next_reified|yield|post|fetch))?(?=[(])', Keyword), 

153 # Term unification 

154 (r'(subsumes_term|unify_with_occurs_check)(?=[(])', Keyword), 

155 # Term creation and decomposition 

156 (r'(functor|arg|copy_term|numbervars|term_variables)(?=[(])', Keyword), 

157 # Evaluable functors 

158 (r'(div|rem|m(ax|in|od)|abs|sign)(?=[(])', Keyword), 

159 (r'float(_(integer|fractional)_part)?(?=[(])', Keyword), 

160 (r'(floor|t(an|runcate)|round|ceiling)(?=[(])', Keyword), 

161 # Other arithmetic functors 

162 (r'(cos|a(cos|sin|tan|tan2)|exp|log|s(in|qrt)|xor)(?=[(])', Keyword), 

163 # Term testing 

164 (r'(var|atom(ic)?|integer|float|c(allable|ompound)|n(onvar|umber)|ground|acyclic_term)(?=[(])', Keyword), 

165 # Term comparison 

166 (r'compare(?=[(])', Keyword), 

167 # Stream selection and control 

168 (r'(curren|se)t_(in|out)put(?=[(])', Keyword), 

169 (r'(open|close)(?=[(])', Keyword), 

170 (r'flush_output(?=[(])', Keyword), 

171 (r'(at_end_of_stream|flush_output)\b', Keyword), 

172 (r'(stream_property|at_end_of_stream|set_stream_position)(?=[(])', Keyword), 

173 # Character and byte input/output 

174 (r'(nl|(get|peek|put)_(byte|c(har|ode)))(?=[(])', Keyword), 

175 (r'\bnl\b', Keyword), 

176 # Term input/output 

177 (r'read(_term)?(?=[(])', Keyword), 

178 (r'write(q|_(canonical|term))?(?=[(])', Keyword), 

179 (r'(current_)?op(?=[(])', Keyword), 

180 (r'(current_)?char_conversion(?=[(])', Keyword), 

181 # Atomic term processing 

182 (r'atom_(length|c(hars|o(ncat|des)))(?=[(])', Keyword), 

183 (r'(char_code|sub_atom)(?=[(])', Keyword), 

184 (r'number_c(har|ode)s(?=[(])', Keyword), 

185 # Implementation defined hooks functions 

186 (r'(se|curren)t_prolog_flag(?=[(])', Keyword), 

187 (r'\bhalt\b', Keyword), 

188 (r'halt(?=[(])', Keyword), 

189 # Message sending operators 

190 (r'(::|:|\^\^)', Operator), 

191 # External call 

192 (r'[{}]', Keyword), 

193 # Logic and control 

194 (r'(ignore|once)(?=[(])', Keyword), 

195 (r'\brepeat\b', Keyword), 

196 # Sorting 

197 (r'(key)?sort(?=[(])', Keyword), 

198 # Bitwise functors 

199 (r'(>>|<<|/\\|\\\\|\\)', Operator), 

200 # Predicate aliases 

201 (r'\bas\b', Operator), 

202 # Arithmetic evaluation 

203 (r'\bis\b', Keyword), 

204 # Arithmetic comparison 

205 (r'(=:=|=\\=|<|=<|>=|>)', Operator), 

206 # Term creation and decomposition 

207 (r'=\.\.', Operator), 

208 # Term unification 

209 (r'(=|\\=)', Operator), 

210 # Term comparison 

211 (r'(==|\\==|@=<|@<|@>=|@>)', Operator), 

212 # Evaluable functors 

213 (r'(//|[-+*/])', Operator), 

214 (r'\b(e|pi|div|mod|rem)\b', Operator), 

215 # Other arithmetic functors 

216 (r'\b\*\*\b', Operator), 

217 # DCG rules 

218 (r'-->', Operator), 

219 # Control constructs 

220 (r'([!;]|->)', Operator), 

221 # Logic and control 

222 (r'\\+', Operator), 

223 # Mode operators 

224 (r'[?@]', Operator), 

225 # Existential quantifier 

226 (r'\^', Operator), 

227 # Strings 

228 (r'"(\\\\|\\[^\\]|[^"\\])*"', String), 

229 # Punctuation 

230 (r'[()\[\],.|]', Text), 

231 # Atoms 

232 (r"[a-z][a-zA-Z0-9_]*", Text), 

233 (r"'", String, 'quoted_atom'), 

234 ], 

235 

236 'quoted_atom': [ 

237 (r"''", String), 

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

239 (r'\\([\\abfnrtv"\']|(x[a-fA-F0-9]+|[0-7]+)\\)', String.Escape), 

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

241 (r'\\', String), 

242 ], 

243 

244 'directive': [ 

245 # Conditional compilation directives 

246 (r'(el)?if(?=[(])', Keyword, 'root'), 

247 (r'(e(lse|ndif))(?=[.])', Keyword, 'root'), 

248 # Entity directives 

249 (r'(category|object|protocol)(?=[(])', Keyword, 'entityrelations'), 

250 (r'(end_(category|object|protocol))(?=[.])', Keyword, 'root'), 

251 # Predicate scope directives 

252 (r'(public|protected|private)(?=[(])', Keyword, 'root'), 

253 # Other directives 

254 (r'e(n(coding|sure_loaded)|xport)(?=[(])', Keyword, 'root'), 

255 (r'in(clude|itialization|fo)(?=[(])', Keyword, 'root'), 

256 (r'(built_in|dynamic|synchronized|threaded)(?=[.])', Keyword, 'root'), 

257 (r'(alias|d(ynamic|iscontiguous)|m(eta_(non_terminal|predicate)|ode|ultifile)|s(et_(logtalk|prolog)_flag|ynchronized))(?=[(])', Keyword, 'root'), 

258 (r'op(?=[(])', Keyword, 'root'), 

259 (r'(c(alls|oinductive)|module|reexport|use(s|_module))(?=[(])', Keyword, 'root'), 

260 (r'[a-z][a-zA-Z0-9_]*(?=[(])', Text, 'root'), 

261 (r'[a-z][a-zA-Z0-9_]*(?=[.])', Text, 'root'), 

262 ], 

263 

264 'entityrelations': [ 

265 (r'(complements|extends|i(nstantiates|mp(lements|orts))|specializes)(?=[(])', Keyword), 

266 # Numbers 

267 (r"0'[\\]?.", Number), 

268 (r'0b[01]+', Number.Bin), 

269 (r'0o[0-7]+', Number.Oct), 

270 (r'0x[0-9a-fA-F]+', Number.Hex), 

271 (r'\d+\.?\d*((e|E)(\+|-)?\d+)?', Number), 

272 # Variables 

273 (r'([A-Z_][a-zA-Z0-9_]*)', Name.Variable), 

274 # Atoms 

275 (r"[a-z][a-zA-Z0-9_]*", Text), 

276 (r"'", String, 'quoted_atom'), 

277 # Strings 

278 (r'"(\\\\|\\[^\\]|[^"\\])*"', String), 

279 # End of entity-opening directive 

280 (r'([)]\.)', Text, 'root'), 

281 # Scope operator 

282 (r'(::)', Operator), 

283 # Punctuation 

284 (r'[()\[\],.|]', Text), 

285 # Comments 

286 (r'%.*?\n', Comment), 

287 (r'/\*(.|\n)*?\*/', Comment), 

288 # Whitespace 

289 (r'\n', Text), 

290 (r'\s+', Text), 

291 ] 

292 } 

293 

294 def analyse_text(text): 

295 if ':- object(' in text: 

296 return 1.0 

297 elif ':- protocol(' in text: 

298 return 1.0 

299 elif ':- category(' in text: 

300 return 1.0 

301 elif re.search(r'^:-\s[a-z]', text, re.M): 

302 return 0.9 

303 else: 

304 return 0.0