codekingpro/portable-devtools
114k
1"""2 pygments.lexers.prolog3 ~~~~~~~~~~~~~~~~~~~~~~4 5 Lexers for Prolog and Prolog-like languages.6 7 :copyright: Copyright 2006-2024 by the Pygments team, see AUTHORS.8 :license: BSD, see LICENSE for details.9"""10 11import re12 13from pygments.lexer import RegexLexer, bygroups14from pygments.token import Text, Comment, Operator, Keyword, Name, String, \15 Number, Punctuation16 17__all__ = ['PrologLexer', 'LogtalkLexer']18 19 20class PrologLexer(RegexLexer):21 """22 Lexer for Prolog files.23 """24 name = 'Prolog'25 aliases = ['prolog']26 filenames = ['*.ecl', '*.prolog', '*.pro', '*.pl']27 mimetypes = ['text/x-prolog']28 url = 'https://en.wikipedia.org/wiki/Prolog'29 version_added = ''30 31 tokens = {32 'root': [33 (r'/\*', Comment.Multiline, 'nested-comment'),34 (r'%.*', Comment.Single),35 # character literal36 (r'0\'.', String.Char),37 (r'0b[01]+', Number.Bin),38 (r'0o[0-7]+', Number.Oct),39 (r'0x[0-9a-fA-F]+', Number.Hex),40 # literal with prepended base41 (r'\d\d?\'[a-zA-Z0-9]+', Number.Integer),42 (r'(\d+\.\d*|\d*\.\d+)([eE][+-]?[0-9]+)?', Number.Float),43 (r'\d+', Number.Integer),44 (r'[\[\](){}|.,;!]', Punctuation),45 (r':-|-->', Punctuation),46 (r'"(?:\\x[0-9a-fA-F]+\\|\\u[0-9a-fA-F]{4}|\\U[0-9a-fA-F]{8}|'47 r'\\[0-7]+\\|\\["\\abcefnrstv]|[^\\"])*"', String.Double),48 (r"'(?:''|[^'])*'", String.Atom), # quoted atom49 # Needs to not be followed by an atom.50 # (r'=(?=\s|[a-zA-Z\[])', Operator),51 (r'is\b', Operator),52 (r'(<|>|=<|>=|==|=:=|=|/|//|\*|\+|-)(?=\s|[a-zA-Z0-9\[])',53 Operator),54 (r'(mod|div|not)\b', Operator),55 (r'_', Keyword), # The don't-care variable56 (r'([a-z]+)(:)', bygroups(Name.Namespace, Punctuation)),57 (r'([a-z\u00c0-\u1fff\u3040-\ud7ff\ue000-\uffef]'58 r'[\w$\u00c0-\u1fff\u3040-\ud7ff\ue000-\uffef]*)'59 r'(\s*)(:-|-->)',60 bygroups(Name.Function, Text, Operator)), # function defn61 (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, Punctuation)),65 (r'[a-z\u00c0-\u1fff\u3040-\ud7ff\ue000-\uffef]'66 r'[\w$\u00c0-\u1fff\u3040-\ud7ff\ue000-\uffef]*',67 String.Atom), # atom, characters68 # This one includes !69 (r'[#&*+\-./:<=>?@\\^~\u00a1-\u00bf\u2010-\u303f]+',70 String.Atom), # atom, graphics71 (r'[A-Z_]\w*', Name.Variable),72 (r'\s+|[\u2000-\u200f\ufff0-\ufffe\uffef]', Text),73 ],74 'nested-comment': [75 (r'\*/', Comment.Multiline, '#pop'),76 (r'/\*', Comment.Multiline, '#push'),77 (r'[^*/]+', Comment.Multiline),78 (r'[*/]', Comment.Multiline),79 ],80 }81 82 def analyse_text(text):83 """Competes with IDL and Visual Prolog on *.pro"""84 if ':-' in text:85 # Visual Prolog also uses :-86 return 0.587 else:88 return 089 90 91class LogtalkLexer(RegexLexer):92 """93 For Logtalk source code.94 """95 96 name = 'Logtalk'97 url = 'http://logtalk.org/'98 aliases = ['logtalk']99 filenames = ['*.lgt', '*.logtalk']100 mimetypes = ['text/x-logtalk']101 version_added = '0.10'102 103 tokens = {104 'root': [105 # Directives106 (r'^\s*:-\s', Punctuation, 'directive'),107 # Comments108 (r'%.*?\n', Comment),109 (r'/\*(.|\n)*?\*/', Comment),110 # Whitespace111 (r'\n', Text),112 (r'\s+', Text),113 # Numbers114 (r"0'[\\]?.", Number),115 (r'0b[01]+', Number.Bin),116 (r'0o[0-7]+', Number.Oct),117 (r'0x[0-9a-fA-F]+', Number.Hex),118 (r'\d+\.?\d*((e|E)(\+|-)?\d+)?', Number),119 # Variables120 (r'([A-Z_][a-zA-Z0-9_]*)', Name.Variable),121 # Event handlers122 (r'(after|before)(?=[(])', Keyword),123 # Message forwarding handler124 (r'forward(?=[(])', Keyword),125 # Execution-context methods126 (r'(context|parameter|this|se(lf|nder))(?=[(])', Keyword),127 # Reflection128 (r'(current_predicate|predicate_property)(?=[(])', Keyword),129 # DCGs and term expansion130 (r'(expand_(goal|term)|(goal|term)_expansion|phrase)(?=[(])', Keyword),131 # Entity132 (r'(abolish|c(reate|urrent))_(object|protocol|category)(?=[(])', Keyword),133 (r'(object|protocol|category)_property(?=[(])', Keyword),134 # Entity relations135 (r'co(mplements_object|nforms_to_protocol)(?=[(])', Keyword),136 (r'extends_(object|protocol|category)(?=[(])', Keyword),137 (r'imp(lements_protocol|orts_category)(?=[(])', Keyword),138 (r'(instantiat|specializ)es_class(?=[(])', Keyword),139 # Events140 (r'(current_event|(abolish|define)_events)(?=[(])', Keyword),141 # Flags142 (r'(create|current|set)_logtalk_flag(?=[(])', Keyword),143 # Compiling, loading, and library paths144 (r'logtalk_(compile|l(ibrary_path|oad|oad_context)|make(_target_action)?)(?=[(])', Keyword),145 (r'\blogtalk_make\b', Keyword),146 # Database147 (r'(clause|retract(all)?)(?=[(])', Keyword),148 (r'a(bolish|ssert(a|z))(?=[(])', Keyword),149 # Control constructs150 (r'(ca(ll|tch)|throw)(?=[(])', Keyword),151 (r'(fa(il|lse)|true|(instantiation|system)_error)\b', Keyword),152 (r'(uninstantiation|type|domain|existence|permission|representation|evaluation|resource|syntax)_error(?=[(])', Keyword),153 # All solutions154 (r'((bag|set)of|f(ind|or)all)(?=[(])', Keyword),155 # Multi-threading predicates156 (r'threaded(_(ca(ll|ncel)|once|ignore|exit|peek|wait|notify))?(?=[(])', Keyword),157 # Engine predicates158 (r'threaded_engine(_(create|destroy|self|next|next_reified|yield|post|fetch))?(?=[(])', Keyword),159 # Term unification160 (r'(subsumes_term|unify_with_occurs_check)(?=[(])', Keyword),161 # Term creation and decomposition162 (r'(functor|arg|copy_term|numbervars|term_variables)(?=[(])', Keyword),163 # Evaluable functors164 (r'(div|rem|m(ax|in|od)|abs|sign)(?=[(])', Keyword),165 (r'float(_(integer|fractional)_part)?(?=[(])', Keyword),166 (r'(floor|t(an|runcate)|round|ceiling)(?=[(])', Keyword),167 # Other arithmetic functors168 (r'(cos|a(cos|sin|tan|tan2)|exp|log|s(in|qrt)|xor)(?=[(])', Keyword),169 # Term testing170 (r'(var|atom(ic)?|integer|float|c(allable|ompound)|n(onvar|umber)|ground|acyclic_term)(?=[(])', Keyword),171 # Term comparison172 (r'compare(?=[(])', Keyword),173 # Stream selection and control174 (r'(curren|se)t_(in|out)put(?=[(])', Keyword),175 (r'(open|close)(?=[(])', Keyword),176 (r'flush_output(?=[(])', Keyword),177 (r'(at_end_of_stream|flush_output)\b', Keyword),178 (r'(stream_property|at_end_of_stream|set_stream_position)(?=[(])', Keyword),179 # Character and byte input/output180 (r'(nl|(get|peek|put)_(byte|c(har|ode)))(?=[(])', Keyword),181 (r'\bnl\b', Keyword),182 # Term input/output183 (r'read(_term)?(?=[(])', Keyword),184 (r'write(q|_(canonical|term))?(?=[(])', Keyword),185 (r'(current_)?op(?=[(])', Keyword),186 (r'(current_)?char_conversion(?=[(])', Keyword),187 # Atomic term processing188 (r'atom_(length|c(hars|o(ncat|des)))(?=[(])', Keyword),189 (r'(char_code|sub_atom)(?=[(])', Keyword),190 (r'number_c(har|ode)s(?=[(])', Keyword),191 # Implementation defined hooks functions192 (r'(se|curren)t_prolog_flag(?=[(])', Keyword),193 (r'\bhalt\b', Keyword),194 (r'halt(?=[(])', Keyword),195 # Message sending operators196 (r'(::|:|\^\^)', Operator),197 # External call198 (r'[{}]', Keyword),199 # Logic and control200 (r'(ignore|once)(?=[(])', Keyword),201 (r'\brepeat\b', Keyword),202 # Sorting203 (r'(key)?sort(?=[(])', Keyword),204 # Bitwise functors205 (r'(>>|<<|/\\|\\\\|\\)', Operator),206 # Predicate aliases207 (r'\bas\b', Operator),208 # Arithmetic evaluation209 (r'\bis\b', Keyword),210 # Arithmetic comparison211 (r'(=:=|=\\=|<|=<|>=|>)', Operator),212 # Term creation and decomposition213 (r'=\.\.', Operator),214 # Term unification215 (r'(=|\\=)', Operator),216 # Term comparison217 (r'(==|\\==|@=<|@<|@>=|@>)', Operator),218 # Evaluable functors219 (r'(//|[-+*/])', Operator),220 (r'\b(e|pi|div|mod|rem)\b', Operator),221 # Other arithmetic functors222 (r'\b\*\*\b', Operator),223 # DCG rules224 (r'-->', Operator),225 # Control constructs226 (r'([!;]|->)', Operator),227 # Logic and control228 (r'\\+', Operator),229 # Mode operators230 (r'[?@]', Operator),231 # Existential quantifier232 (r'\^', Operator),233 # Punctuation234 (r'[()\[\],.|]', Text),235 # Atoms236 (r"[a-z][a-zA-Z0-9_]*", Text),237 (r"'", String, 'quoted_atom'),238 # Double-quoted terms239 (r'"', String, 'double_quoted_term'),240 ],241 242 'quoted_atom': [243 (r"''", String),244 (r"'", String, '#pop'),245 (r'\\([\\abfnrtv"\']|(x[a-fA-F0-9]+|[0-7]+)\\)', String.Escape),246 (r"[^\\'\n]+", String),247 (r'\\', String),248 ],249 250 'double_quoted_term': [251 (r'""', String),252 (r'"', String, '#pop'),253 (r'\\([\\abfnrtv"\']|(x[a-fA-F0-9]+|[0-7]+)\\)', String.Escape),254 (r'[^\\"\n]+', String),255 (r'\\', String),256 ],257 258 'directive': [259 # Conditional compilation directives260 (r'(el)?if(?=[(])', Keyword, 'root'),261 (r'(e(lse|ndif))(?=[.])', Keyword, 'root'),262 # Entity directives263 (r'(category|object|protocol)(?=[(])', Keyword, 'entityrelations'),264 (r'(end_(category|object|protocol))(?=[.])', Keyword, 'root'),265 # Predicate scope directives266 (r'(public|protected|private)(?=[(])', Keyword, 'root'),267 # Other directives268 (r'e(n(coding|sure_loaded)|xport)(?=[(])', Keyword, 'root'),269 (r'in(clude|itialization|fo)(?=[(])', Keyword, 'root'),270 (r'(built_in|dynamic|synchronized|threaded)(?=[.])', Keyword, 'root'),271 (r'(alias|d(ynamic|iscontiguous)|m(eta_(non_terminal|predicate)|ode|ultifile)|s(et_(logtalk|prolog)_flag|ynchronized))(?=[(])', Keyword, 'root'),272 (r'op(?=[(])', Keyword, 'root'),273 (r'(c(alls|oinductive)|module|reexport|use(s|_module))(?=[(])', Keyword, 'root'),274 (r'[a-z][a-zA-Z0-9_]*(?=[(])', Text, 'root'),275 (r'[a-z][a-zA-Z0-9_]*(?=[.])', Text, 'root'),276 ],277 278 'entityrelations': [279 (r'(complements|extends|i(nstantiates|mp(lements|orts))|specializes)(?=[(])', Keyword),280 # Numbers281 (r"0'[\\]?.", Number),282 (r'0b[01]+', Number.Bin),283 (r'0o[0-7]+', Number.Oct),284 (r'0x[0-9a-fA-F]+', Number.Hex),285 (r'\d+\.?\d*((e|E)(\+|-)?\d+)?', Number),286 # Variables287 (r'([A-Z_][a-zA-Z0-9_]*)', Name.Variable),288 # Atoms289 (r"[a-z][a-zA-Z0-9_]*", Text),290 (r"'", String, 'quoted_atom'),291 # Double-quoted terms292 (r'"', String, 'double_quoted_term'),293 # End of entity-opening directive294 (r'([)]\.)', Text, 'root'),295 # Scope operator296 (r'(::)', Operator),297 # Punctuation298 (r'[()\[\],.|]', Text),299 # Comments300 (r'%.*?\n', Comment),301 (r'/\*(.|\n)*?\*/', Comment),302 # Whitespace303 (r'\n', Text),304 (r'\s+', Text),305 ]306 }307 308 def analyse_text(text):309 if ':- object(' in text:310 return 1.0311 elif ':- protocol(' in text:312 return 1.0313 elif ':- category(' in text:314 return 1.0315 elif re.search(r'^:-\s[a-z]', text, re.M):316 return 0.9317 else:318 return 0.0319 