prolog.py 12 KB

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