prolog.py 12 KB

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