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-2017 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.Single),
  26. (r'/\*', Comment.Multiline, 'nested-comment'),
  27. (r'%.*', Comment.Single),
  28. # character literal
  29. (r'0\'.', String.Char),
  30. (r'0b[01]+', Number.Bin),
  31. (r'0o[0-7]+', Number.Oct),
  32. (r'0x[0-9a-fA-F]+', Number.Hex),
  33. # literal with prepended base
  34. (r'\d\d?\'[a-zA-Z0-9]+', Number.Integer),
  35. (r'(\d+\.\d*|\d*\.\d+)([eE][+-]?[0-9]+)?', Number.Float),
  36. (r'\d+', Number.Integer),
  37. (r'[\[\](){}|.,;!]', Punctuation),
  38. (r':-|-->', Punctuation),
  39. (r'"(?:\\x[0-9a-fA-F]+\\|\\u[0-9a-fA-F]{4}|\\U[0-9a-fA-F]{8}|'
  40. r'\\[0-7]+\\|\\["\nabcefnrstv]|[^\\"])*"', String.Double),
  41. (r"'(?:''|[^'])*'", String.Atom), # quoted atom
  42. # Needs to not be followed by an atom.
  43. # (r'=(?=\s|[a-zA-Z\[])', Operator),
  44. (r'is\b', Operator),
  45. (r'(<|>|=<|>=|==|=:=|=|/|//|\*|\+|-)(?=\s|[a-zA-Z0-9\[])',
  46. Operator),
  47. (r'(mod|div|not)\b', Operator),
  48. (r'_', Keyword), # The don't-care variable
  49. (r'([a-z]+)(:)', bygroups(Name.Namespace, Punctuation)),
  50. (u'([a-z\u00c0-\u1fff\u3040-\ud7ff\ue000-\uffef]'
  51. u'[\w$\u00c0-\u1fff\u3040-\ud7ff\ue000-\uffef]*)'
  52. u'(\\s*)(:-|-->)',
  53. bygroups(Name.Function, Text, Operator)), # function defn
  54. (u'([a-z\u00c0-\u1fff\u3040-\ud7ff\ue000-\uffef]'
  55. u'[\w$\u00c0-\u1fff\u3040-\ud7ff\ue000-\uffef]*)'
  56. u'(\\s*)(\\()',
  57. bygroups(Name.Function, Text, Punctuation)),
  58. (u'[a-z\u00c0-\u1fff\u3040-\ud7ff\ue000-\uffef]'
  59. u'[\w$\u00c0-\u1fff\u3040-\ud7ff\ue000-\uffef]*',
  60. String.Atom), # atom, characters
  61. # This one includes !
  62. (u'[#&*+\\-./:<=>?@\\\\^~\u00a1-\u00bf\u2010-\u303f]+',
  63. String.Atom), # atom, graphics
  64. (r'[A-Z_]\w*', Name.Variable),
  65. (u'\\s+|[\u2000-\u200f\ufff0-\ufffe\uffef]', Text),
  66. ],
  67. 'nested-comment': [
  68. (r'\*/', Comment.Multiline, '#pop'),
  69. (r'/\*', Comment.Multiline, '#push'),
  70. (r'[^*/]+', Comment.Multiline),
  71. (r'[*/]', Comment.Multiline),
  72. ],
  73. }
  74. def analyse_text(text):
  75. return ':-' in text
  76. class LogtalkLexer(RegexLexer):
  77. """
  78. For `Logtalk <http://logtalk.org/>`_ source code.
  79. .. versionadded:: 0.10
  80. """
  81. name = 'Logtalk'
  82. aliases = ['logtalk']
  83. filenames = ['*.lgt', '*.logtalk']
  84. mimetypes = ['text/x-logtalk']
  85. tokens = {
  86. 'root': [
  87. # Directives
  88. (r'^\s*:-\s', Punctuation, 'directive'),
  89. # Comments
  90. (r'%.*?\n', Comment),
  91. (r'/\*(.|\n)*?\*/', Comment),
  92. # Whitespace
  93. (r'\n', Text),
  94. (r'\s+', Text),
  95. # Numbers
  96. (r"0'.", Number),
  97. (r'0b[01]+', Number.Bin),
  98. (r'0o[0-7]+', Number.Oct),
  99. (r'0x[0-9a-fA-F]+', Number.Hex),
  100. (r'\d+\.?\d*((e|E)(\+|-)?\d+)?', Number),
  101. # Variables
  102. (r'([A-Z_]\w*)', Name.Variable),
  103. # Event handlers
  104. (r'(after|before)(?=[(])', Keyword),
  105. # Message forwarding handler
  106. (r'forward(?=[(])', Keyword),
  107. # Execution-context methods
  108. (r'(parameter|this|se(lf|nder))(?=[(])', Keyword),
  109. # Reflection
  110. (r'(current_predicate|predicate_property)(?=[(])', Keyword),
  111. # DCGs and term expansion
  112. (r'(expand_(goal|term)|(goal|term)_expansion|phrase)(?=[(])', Keyword),
  113. # Entity
  114. (r'(abolish|c(reate|urrent))_(object|protocol|category)(?=[(])', Keyword),
  115. (r'(object|protocol|category)_property(?=[(])', Keyword),
  116. # Entity relations
  117. (r'co(mplements_object|nforms_to_protocol)(?=[(])', Keyword),
  118. (r'extends_(object|protocol|category)(?=[(])', Keyword),
  119. (r'imp(lements_protocol|orts_category)(?=[(])', Keyword),
  120. (r'(instantiat|specializ)es_class(?=[(])', Keyword),
  121. # Events
  122. (r'(current_event|(abolish|define)_events)(?=[(])', Keyword),
  123. # Flags
  124. (r'(current|set)_logtalk_flag(?=[(])', Keyword),
  125. # Compiling, loading, and library paths
  126. (r'logtalk_(compile|l(ibrary_path|oad|oad_context)|make)(?=[(])', Keyword),
  127. (r'\blogtalk_make\b', Keyword),
  128. # Database
  129. (r'(clause|retract(all)?)(?=[(])', Keyword),
  130. (r'a(bolish|ssert(a|z))(?=[(])', Keyword),
  131. # Control constructs
  132. (r'(ca(ll|tch)|throw)(?=[(])', Keyword),
  133. (r'(fa(il|lse)|true)\b', Keyword),
  134. # All solutions
  135. (r'((bag|set)of|f(ind|or)all)(?=[(])', Keyword),
  136. # Multi-threading meta-predicates
  137. (r'threaded(_(call|once|ignore|exit|peek|wait|notify))?(?=[(])', Keyword),
  138. # Term unification
  139. (r'(subsumes_term|unify_with_occurs_check)(?=[(])', Keyword),
  140. # Term creation and decomposition
  141. (r'(functor|arg|copy_term|numbervars|term_variables)(?=[(])', Keyword),
  142. # Evaluable functors
  143. (r'(div|rem|m(ax|in|od)|abs|sign)(?=[(])', Keyword),
  144. (r'float(_(integer|fractional)_part)?(?=[(])', Keyword),
  145. (r'(floor|t(an|runcate)|round|ceiling)(?=[(])', Keyword),
  146. # Other arithmetic functors
  147. (r'(cos|a(cos|sin|tan|tan2)|exp|log|s(in|qrt)|xor)(?=[(])', Keyword),
  148. # Term testing
  149. (r'(var|atom(ic)?|integer|float|c(allable|ompound)|n(onvar|umber)|'
  150. r'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. # Ponctuation
  216. (r'[()\[\],.|]', Text),
  217. # Atoms
  218. (r"[a-z]\w*", 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)|'
  242. r'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]\w*(?=[(])', Text, 'root'),
  246. (r'[a-z]\w*[.]', 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_]\w*)', Name.Variable),
  258. # Atoms
  259. (r"[a-z]\w*", 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. # Ponctuation
  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('^:-\s[a-z]', text, re.M):
  285. return 0.9
  286. else:
  287. return 0.0