prolog.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. """
  2. pygments.lexers.prolog
  3. ~~~~~~~~~~~~~~~~~~~~~~
  4. Lexers for Prolog and Prolog-like languages.
  5. :copyright: Copyright 2006-2024 by the Pygments team, see AUTHORS.
  6. :license: BSD, see LICENSE for details.
  7. """
  8. import re
  9. from pygments.lexer import RegexLexer, bygroups
  10. from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
  11. Number, Punctuation
  12. __all__ = ['PrologLexer', 'LogtalkLexer']
  13. class PrologLexer(RegexLexer):
  14. """
  15. Lexer for Prolog files.
  16. """
  17. name = 'Prolog'
  18. aliases = ['prolog']
  19. filenames = ['*.ecl', '*.prolog', '*.pro', '*.pl']
  20. mimetypes = ['text/x-prolog']
  21. url = 'https://en.wikipedia.org/wiki/Prolog'
  22. version_added = ''
  23. tokens = {
  24. 'root': [
  25. (r'/\*', Comment.Multiline, 'nested-comment'),
  26. (r'%.*', Comment.Single),
  27. # character literal
  28. (r'0\'.', String.Char),
  29. (r'0b[01]+', Number.Bin),
  30. (r'0o[0-7]+', Number.Oct),
  31. (r'0x[0-9a-fA-F]+', Number.Hex),
  32. # literal with prepended base
  33. (r'\d\d?\'[a-zA-Z0-9]+', Number.Integer),
  34. (r'(\d+\.\d*|\d*\.\d+)([eE][+-]?[0-9]+)?', Number.Float),
  35. (r'\d+', Number.Integer),
  36. (r'[\[\](){}|.,;!]', Punctuation),
  37. (r':-|-->', Punctuation),
  38. (r'"(?:\\x[0-9a-fA-F]+\\|\\u[0-9a-fA-F]{4}|\\U[0-9a-fA-F]{8}|'
  39. r'\\[0-7]+\\|\\["\\abcefnrstv]|[^\\"])*"', String.Double),
  40. (r"'(?:''|[^'])*'", String.Atom), # quoted atom
  41. # Needs to not be followed by an atom.
  42. # (r'=(?=\s|[a-zA-Z\[])', Operator),
  43. (r'is\b', Operator),
  44. (r'(<|>|=<|>=|==|=:=|=|/|//|\*|\+|-)(?=\s|[a-zA-Z0-9\[])',
  45. Operator),
  46. (r'(mod|div|not)\b', Operator),
  47. (r'_', Keyword), # The don't-care variable
  48. (r'([a-z]+)(:)', bygroups(Name.Namespace, Punctuation)),
  49. (r'([a-z\u00c0-\u1fff\u3040-\ud7ff\ue000-\uffef]'
  50. r'[\w$\u00c0-\u1fff\u3040-\ud7ff\ue000-\uffef]*)'
  51. r'(\s*)(:-|-->)',
  52. bygroups(Name.Function, Text, Operator)), # function defn
  53. (r'([a-z\u00c0-\u1fff\u3040-\ud7ff\ue000-\uffef]'
  54. r'[\w$\u00c0-\u1fff\u3040-\ud7ff\ue000-\uffef]*)'
  55. r'(\s*)(\()',
  56. bygroups(Name.Function, Text, Punctuation)),
  57. (r'[a-z\u00c0-\u1fff\u3040-\ud7ff\ue000-\uffef]'
  58. r'[\w$\u00c0-\u1fff\u3040-\ud7ff\ue000-\uffef]*',
  59. String.Atom), # atom, characters
  60. # This one includes !
  61. (r'[#&*+\-./:<=>?@\\^~\u00a1-\u00bf\u2010-\u303f]+',
  62. String.Atom), # atom, graphics
  63. (r'[A-Z_]\w*', Name.Variable),
  64. (r'\s+|[\u2000-\u200f\ufff0-\ufffe\uffef]', Text),
  65. ],
  66. 'nested-comment': [
  67. (r'\*/', Comment.Multiline, '#pop'),
  68. (r'/\*', Comment.Multiline, '#push'),
  69. (r'[^*/]+', Comment.Multiline),
  70. (r'[*/]', Comment.Multiline),
  71. ],
  72. }
  73. def analyse_text(text):
  74. """Competes with IDL and Visual Prolog on *.pro"""
  75. if ':-' in text:
  76. # Visual Prolog also uses :-
  77. return 0.5
  78. else:
  79. return 0
  80. class LogtalkLexer(RegexLexer):
  81. """
  82. For Logtalk source code.
  83. """
  84. name = 'Logtalk'
  85. url = 'http://logtalk.org/'
  86. aliases = ['logtalk']
  87. filenames = ['*.lgt', '*.logtalk']
  88. mimetypes = ['text/x-logtalk']
  89. version_added = '0.10'
  90. tokens = {
  91. 'root': [
  92. # Directives
  93. (r'^\s*:-\s', Punctuation, 'directive'),
  94. # Comments
  95. (r'%.*?\n', Comment),
  96. (r'/\*(.|\n)*?\*/', Comment),
  97. # Whitespace
  98. (r'\n', Text),
  99. (r'\s+', Text),
  100. # Numbers
  101. (r"0'[\\]?.", Number),
  102. (r'0b[01]+', Number.Bin),
  103. (r'0o[0-7]+', Number.Oct),
  104. (r'0x[0-9a-fA-F]+', Number.Hex),
  105. (r'\d+\.?\d*((e|E)(\+|-)?\d+)?', Number),
  106. # Variables
  107. (r'([A-Z_][a-zA-Z0-9_]*)', Name.Variable),
  108. # Event handlers
  109. (r'(after|before)(?=[(])', Keyword),
  110. # Message forwarding handler
  111. (r'forward(?=[(])', Keyword),
  112. # Execution-context methods
  113. (r'(context|parameter|this|se(lf|nder))(?=[(])', Keyword),
  114. # Reflection
  115. (r'(current_predicate|predicate_property)(?=[(])', Keyword),
  116. # DCGs and term expansion
  117. (r'(expand_(goal|term)|(goal|term)_expansion|phrase)(?=[(])', Keyword),
  118. # Entity
  119. (r'(abolish|c(reate|urrent))_(object|protocol|category)(?=[(])', Keyword),
  120. (r'(object|protocol|category)_property(?=[(])', Keyword),
  121. # Entity relations
  122. (r'co(mplements_object|nforms_to_protocol)(?=[(])', Keyword),
  123. (r'extends_(object|protocol|category)(?=[(])', Keyword),
  124. (r'imp(lements_protocol|orts_category)(?=[(])', Keyword),
  125. (r'(instantiat|specializ)es_class(?=[(])', Keyword),
  126. # Events
  127. (r'(current_event|(abolish|define)_events)(?=[(])', Keyword),
  128. # Flags
  129. (r'(create|current|set)_logtalk_flag(?=[(])', Keyword),
  130. # Compiling, loading, and library paths
  131. (r'logtalk_(compile|l(ibrary_path|oad|oad_context)|make(_target_action)?)(?=[(])', Keyword),
  132. (r'\blogtalk_make\b', Keyword),
  133. # Database
  134. (r'(clause|retract(all)?)(?=[(])', Keyword),
  135. (r'a(bolish|ssert(a|z))(?=[(])', Keyword),
  136. # Control constructs
  137. (r'(ca(ll|tch)|throw)(?=[(])', Keyword),
  138. (r'(fa(il|lse)|true|(instantiation|system)_error)\b', Keyword),
  139. (r'(uninstantiation|type|domain|existence|permission|representation|evaluation|resource|syntax)_error(?=[(])', Keyword),
  140. # All solutions
  141. (r'((bag|set)of|f(ind|or)all)(?=[(])', Keyword),
  142. # Multi-threading predicates
  143. (r'threaded(_(ca(ll|ncel)|once|ignore|exit|peek|wait|notify))?(?=[(])', Keyword),
  144. # Engine predicates
  145. (r'threaded_engine(_(create|destroy|self|next|next_reified|yield|post|fetch))?(?=[(])', Keyword),
  146. # Term unification
  147. (r'(subsumes_term|unify_with_occurs_check)(?=[(])', Keyword),
  148. # Term creation and decomposition
  149. (r'(functor|arg|copy_term|numbervars|term_variables)(?=[(])', Keyword),
  150. # Evaluable functors
  151. (r'(div|rem|m(ax|in|od)|abs|sign)(?=[(])', Keyword),
  152. (r'float(_(integer|fractional)_part)?(?=[(])', Keyword),
  153. (r'(floor|t(an|runcate)|round|ceiling)(?=[(])', Keyword),
  154. # Other arithmetic functors
  155. (r'(cos|a(cos|sin|tan|tan2)|exp|log|s(in|qrt)|xor)(?=[(])', Keyword),
  156. # Term testing
  157. (r'(var|atom(ic)?|integer|float|c(allable|ompound)|n(onvar|umber)|ground|acyclic_term)(?=[(])', Keyword),
  158. # Term comparison
  159. (r'compare(?=[(])', Keyword),
  160. # Stream selection and control
  161. (r'(curren|se)t_(in|out)put(?=[(])', Keyword),
  162. (r'(open|close)(?=[(])', Keyword),
  163. (r'flush_output(?=[(])', Keyword),
  164. (r'(at_end_of_stream|flush_output)\b', Keyword),
  165. (r'(stream_property|at_end_of_stream|set_stream_position)(?=[(])', Keyword),
  166. # Character and byte input/output
  167. (r'(nl|(get|peek|put)_(byte|c(har|ode)))(?=[(])', Keyword),
  168. (r'\bnl\b', Keyword),
  169. # Term input/output
  170. (r'read(_term)?(?=[(])', Keyword),
  171. (r'write(q|_(canonical|term))?(?=[(])', Keyword),
  172. (r'(current_)?op(?=[(])', Keyword),
  173. (r'(current_)?char_conversion(?=[(])', Keyword),
  174. # Atomic term processing
  175. (r'atom_(length|c(hars|o(ncat|des)))(?=[(])', Keyword),
  176. (r'(char_code|sub_atom)(?=[(])', Keyword),
  177. (r'number_c(har|ode)s(?=[(])', Keyword),
  178. # Implementation defined hooks functions
  179. (r'(se|curren)t_prolog_flag(?=[(])', Keyword),
  180. (r'\bhalt\b', Keyword),
  181. (r'halt(?=[(])', Keyword),
  182. # Message sending operators
  183. (r'(::|:|\^\^)', Operator),
  184. # External call
  185. (r'[{}]', Keyword),
  186. # Logic and control
  187. (r'(ignore|once)(?=[(])', Keyword),
  188. (r'\brepeat\b', Keyword),
  189. # Sorting
  190. (r'(key)?sort(?=[(])', Keyword),
  191. # Bitwise functors
  192. (r'(>>|<<|/\\|\\\\|\\)', Operator),
  193. # Predicate aliases
  194. (r'\bas\b', Operator),
  195. # Arithmetic evaluation
  196. (r'\bis\b', Keyword),
  197. # Arithmetic comparison
  198. (r'(=:=|=\\=|<|=<|>=|>)', Operator),
  199. # Term creation and decomposition
  200. (r'=\.\.', Operator),
  201. # Term unification
  202. (r'(=|\\=)', Operator),
  203. # Term comparison
  204. (r'(==|\\==|@=<|@<|@>=|@>)', Operator),
  205. # Evaluable functors
  206. (r'(//|[-+*/])', Operator),
  207. (r'\b(e|pi|div|mod|rem)\b', Operator),
  208. # Other arithmetic functors
  209. (r'\b\*\*\b', Operator),
  210. # DCG rules
  211. (r'-->', Operator),
  212. # Control constructs
  213. (r'([!;]|->)', Operator),
  214. # Logic and control
  215. (r'\\+', Operator),
  216. # Mode operators
  217. (r'[?@]', Operator),
  218. # Existential quantifier
  219. (r'\^', Operator),
  220. # Punctuation
  221. (r'[()\[\],.|]', Text),
  222. # Atoms
  223. (r"[a-z][a-zA-Z0-9_]*", Text),
  224. (r"'", String, 'quoted_atom'),
  225. # Double-quoted terms
  226. (r'"', String, 'double_quoted_term'),
  227. ],
  228. 'quoted_atom': [
  229. (r"''", String),
  230. (r"'", String, '#pop'),
  231. (r'\\([\\abfnrtv"\']|(x[a-fA-F0-9]+|[0-7]+)\\)', String.Escape),
  232. (r"[^\\'\n]+", String),
  233. (r'\\', String),
  234. ],
  235. 'double_quoted_term': [
  236. (r'""', String),
  237. (r'"', String, '#pop'),
  238. (r'\\([\\abfnrtv"\']|(x[a-fA-F0-9]+|[0-7]+)\\)', String.Escape),
  239. (r'[^\\"\n]+', String),
  240. (r'\\', String),
  241. ],
  242. 'directive': [
  243. # Conditional compilation directives
  244. (r'(el)?if(?=[(])', Keyword, 'root'),
  245. (r'(e(lse|ndif))(?=[.])', Keyword, 'root'),
  246. # Entity directives
  247. (r'(category|object|protocol)(?=[(])', Keyword, 'entityrelations'),
  248. (r'(end_(category|object|protocol))(?=[.])', Keyword, 'root'),
  249. # Predicate scope directives
  250. (r'(public|protected|private)(?=[(])', Keyword, 'root'),
  251. # Other directives
  252. (r'e(n(coding|sure_loaded)|xport)(?=[(])', Keyword, 'root'),
  253. (r'in(clude|itialization|fo)(?=[(])', Keyword, 'root'),
  254. (r'(built_in|dynamic|synchronized|threaded)(?=[.])', Keyword, 'root'),
  255. (r'(alias|d(ynamic|iscontiguous)|m(eta_(non_terminal|predicate)|ode|ultifile)|s(et_(logtalk|prolog)_flag|ynchronized))(?=[(])', Keyword, 'root'),
  256. (r'op(?=[(])', Keyword, 'root'),
  257. (r'(c(alls|oinductive)|module|reexport|use(s|_module))(?=[(])', Keyword, 'root'),
  258. (r'[a-z][a-zA-Z0-9_]*(?=[(])', Text, 'root'),
  259. (r'[a-z][a-zA-Z0-9_]*(?=[.])', Text, 'root'),
  260. ],
  261. 'entityrelations': [
  262. (r'(complements|extends|i(nstantiates|mp(lements|orts))|specializes)(?=[(])', Keyword),
  263. # Numbers
  264. (r"0'[\\]?.", Number),
  265. (r'0b[01]+', Number.Bin),
  266. (r'0o[0-7]+', Number.Oct),
  267. (r'0x[0-9a-fA-F]+', Number.Hex),
  268. (r'\d+\.?\d*((e|E)(\+|-)?\d+)?', Number),
  269. # Variables
  270. (r'([A-Z_][a-zA-Z0-9_]*)', Name.Variable),
  271. # Atoms
  272. (r"[a-z][a-zA-Z0-9_]*", Text),
  273. (r"'", String, 'quoted_atom'),
  274. # Double-quoted terms
  275. (r'"', String, 'double_quoted_term'),
  276. # End of entity-opening directive
  277. (r'([)]\.)', Text, 'root'),
  278. # Scope operator
  279. (r'(::)', Operator),
  280. # Punctuation
  281. (r'[()\[\],.|]', Text),
  282. # Comments
  283. (r'%.*?\n', Comment),
  284. (r'/\*(.|\n)*?\*/', Comment),
  285. # Whitespace
  286. (r'\n', Text),
  287. (r'\s+', Text),
  288. ]
  289. }
  290. def analyse_text(text):
  291. if ':- object(' in text:
  292. return 1.0
  293. elif ':- protocol(' in text:
  294. return 1.0
  295. elif ':- category(' in text:
  296. return 1.0
  297. elif re.search(r'^:-\s[a-z]', text, re.M):
  298. return 0.9
  299. else:
  300. return 0.0