prolog.py 12 KB

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