swiprolog_lexer.py (3419B)
1 """ 2 pygments.lexers.prolog 3 ~~~~~~~~~~~~~~~~~~~~~~ 4 5 Lexers for Prolog and Prolog-like languages. 6 7 :copyright: Copyright 2006-2025 by the Pygments team, see AUTHORS. 8 :license: BSD, see LICENSE for details. 9 """ 10 11 import re 12 13 from pygments.lexer import RegexLexer, bygroups 14 from pygments.token import Text, Comment, Operator, Keyword, Name, String, \ 15 Number, Punctuation 16 17 __all__ = ['SWIPrologLexer'] 18 19 T_ATOM = Name.Constant 20 T_BACKTICK = String.Char 21 22 class SWIPrologLexer(RegexLexer): 23 """ 24 Lexer for Prolog files. 25 """ 26 name = 'Prolog' 27 aliases = ['prolog'] 28 filenames = ['*.ecl', '*.prolog', '*.pro', '*.pl'] 29 mimetypes = ['text/x-prolog'] 30 url = 'https://en.wikipedia.org/wiki/Prolog' 31 version_added = '' 32 33 tokens = { 34 'root': [ 35 (r'/\*', Comment.Multiline, 'nested-comment'), 36 (r'%.*', Comment.Single), 37 # character literal 38 (r'0\'.', String.Char), 39 (r'0b[01]+', Number.Bin), 40 (r'0o[0-7]+', Number.Oct), 41 (r'0x[0-9a-fA-F]+', Number.Hex), 42 # literal with prepended base 43 (r'\d\d?\'[a-zA-Z0-9]+', Number.Integer), 44 (r'(\d+\.\d*|\d*\.\d+)([eE][+-]?[0-9]+)?', Number.Float), 45 (r'\d+', Number.Integer), 46 (r'[\[\](){}|.,;!]', Punctuation), 47 (r':-|-->', Punctuation), 48 (r'"(?:\\x[0-9a-fA-F]+\\|\\u[0-9a-fA-F]{4}|\\U[0-9a-fA-F]{8}|' 49 r'\\[0-7]+\\|\\["\\abcefnrstv]|[^\\"])*"', String.Double), 50 (r'`(?:\\x[0-9a-fA-F]+\\|\\u[0-9a-fA-F]{4}|\\U[0-9a-fA-F]{8}|' 51 r'\\[0-7]+\\|\\[`\\abcefnrstv]|[^\\`])*`', T_BACKTICK), 52 (r"'(?:''|[^'])*'", T_ATOM), # quoted atom 53 # Needs to not be followed by an atom. 54 # (r'=(?=\s|[a-zA-Z\[])', Operator), 55 (r'is\b', Operator), 56 (r'(<|>|=<|>=|==|=:=|=|/|//|\*|\+|-|~)(?=\s|[a-zA-Z0-9\[])', 57 Operator), 58 (r'(mod|div|not|will_be)\b', Operator), 59 (r'_', Keyword), # The don't-care variable 60 (r'([a-z]+)(:)', bygroups(Name.Namespace, Punctuation)), 61 (r'([a-z\u00c0-\u1fff\u3040-\ud7ff\ue000-\uffef]' 62 r'[\w$\u00c0-\u1fff\u3040-\ud7ff\ue000-\uffef]*)' 63 r'(\s*)(:-|-->)', 64 bygroups(Name.Function, Text, Operator)), # function defn 65 (r'([a-z\u00c0-\u1fff\u3040-\ud7ff\ue000-\uffef]' 66 r'[\w$\u00c0-\u1fff\u3040-\ud7ff\ue000-\uffef]*)' 67 r'(\s*)(\()', 68 bygroups(Name.Function, Text, Punctuation)), 69 (r'[a-z\u00c0-\u1fff\u3040-\ud7ff\ue000-\uffef]' 70 r'[\w$\u00c0-\u1fff\u3040-\ud7ff\ue000-\uffef]*', 71 T_ATOM), # atom, characters 72 # This one includes ! 73 (r'[#&*+\-./:<=>?@\\^~\u00a1-\u00bf\u2010-\u303f]+', 74 T_ATOM), # atom, graphics 75 (r'[A-Z_]\w*', Name.Variable), 76 (r'\s+|[\u2000-\u200f\ufff0-\ufffe\uffef]', Text), 77 ], 78 'nested-comment': [ 79 (r'\*/', Comment.Multiline, '#pop'), 80 (r'/\*', Comment.Multiline, '#push'), 81 (r'[^*/]+', Comment.Multiline), 82 (r'[*/]', Comment.Multiline), 83 ], 84 } 85 86 def analyse_text(text): 87 """Competes with IDL and Visual Prolog on *.pro""" 88 if ':-' in text: 89 # Visual Prolog also uses :- 90 return 0.5 91 else: 92 return 0