1"""
2 pygments.lexers.vip
3 ~~~~~~~~~~~~~~~~~~~
4
5 Lexers for Visual Prolog & Grammar files.
6
7 :copyright: Copyright 2006-2025 by the Pygments team, see AUTHORS.
8 :license: BSD, see LICENSE for details.
9"""
10
11import re
12
13from pygments.lexer import RegexLexer, inherit, words, include
14from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
15 Number, Punctuation, Whitespace
16
17__all__ = ['VisualPrologLexer', 'VisualPrologGrammarLexer']
18
19
20class VisualPrologBaseLexer(RegexLexer):
21 minorendkw = ('try', 'foreach', 'if')
22 minorkwexp = ('and', 'catch', 'do', 'else', 'elseif', 'erroneous', 'externally', 'failure', 'finally', 'foreach', 'if', 'or', 'orelse', 'otherwise', 'then',
23 'try', 'div', 'mod', 'rem', 'quot')
24 dockw = ('short', 'detail', 'end', 'withdomain')
25 tokens = {
26 'root': [
27 (r'\s+', Whitespace),
28 (words(minorendkw, prefix=r'\bend\s+', suffix=r'\b'), Keyword.Minor),
29 (r'end', Keyword),
30 (words(minorkwexp, suffix=r'\b'), Keyword.Minor),
31 (r'0[xo][\da-fA-F_]+', Number),
32 (r'((\d[\d_]*)?\.)?\d[\d_]*([eE][\-+]?\d+)?', Number),
33 (r'_\w*', Name.Variable.Anonymous),
34 (r'[A-Z]\w*', Name.Variable),
35 (r'@\w+', Name.Variable),
36 (r'[a-z]\w*', Name),
37 (r'/\*', Comment, 'comment'),
38 (r'\%', Comment, 'commentline'),
39 (r'"', String.Symbol, 'string'),
40 (r'\'', String.Symbol, 'stringsingle'),
41 (r'@"', String.Symbol, 'atstring'),
42 (r'[\-+*^/!?<>=~:]+', Operator),
43 (r'[$,.[\]|(){}\\]+', Punctuation),
44 (r'.', Text),
45 ],
46 'commentdoc': [
47 (words(dockw, prefix=r'@', suffix=r'\b'), Comment.Preproc),
48 (r'@', Comment),
49 ],
50 'commentline': [
51 include('commentdoc'),
52 (r'[^@\n]+', Comment),
53 (r'$', Comment, '#pop'),
54 ],
55 'comment': [
56 include('commentdoc'),
57 (r'[^@*/]+', Comment),
58 (r'/\*', Comment, '#push'),
59 (r'\*/', Comment, '#pop'),
60 (r'[*/]', Comment),
61 ],
62 'stringescape': [
63 (r'\\u[0-9a-fA-F]{4}', String.Escape),
64 (r'\\[\'"ntr\\]', String.Escape),
65 ],
66 'stringsingle': [
67 include('stringescape'),
68 (r'\'', String.Symbol, '#pop'),
69 (r'[^\'\\\n]+', String),
70 (r'\n', String.Escape.Error, '#pop'),
71 ],
72 'string': [
73 include('stringescape'),
74 (r'"', String.Symbol, '#pop'),
75 (r'[^"\\\n]+', String),
76 (r'\n', String.Escape.Error, '#pop'),
77 ],
78 'atstring': [
79 (r'""', String.Escape),
80 (r'"', String.Symbol, '#pop'),
81 (r'[^"]+', String),
82 ]
83 }
84
85
86class VisualPrologLexer(VisualPrologBaseLexer):
87 """Lexer for VisualProlog
88 """
89 name = 'Visual Prolog'
90 url = 'https://www.visual-prolog.com/'
91 aliases = ['visualprolog']
92 filenames = ['*.pro', '*.cl', '*.i', '*.pack', '*.ph']
93 version_added = '2.17'
94
95 majorkw = ('goal', 'namespace', 'interface', 'class', 'implement', 'where', 'open', 'inherits', 'supports', 'resolve',
96 'delegate', 'monitor', 'constants', 'domains', 'predicates', 'constructors', 'properties', 'clauses', 'facts')
97 minorkw = ('align', 'anyflow', 'as', 'bitsize', 'determ', 'digits', 'erroneous', 'externally', 'failure', 'from',
98 'guard', 'multi', 'nondeterm', 'or', 'orelse', 'otherwise', 'procedure', 'resolve', 'single', 'suspending')
99 directivekw = ('bininclude', 'else', 'elseif', 'endif', 'error', 'export', 'externally', 'from', 'grammargenerate',
100 'grammarinclude', 'if', 'include', 'message', 'options', 'orrequires', 'requires', 'stringinclude', 'then')
101 tokens = {
102 'root': [
103 (words(minorkw, suffix=r'\b'), Keyword.Minor),
104 (words(majorkw, suffix=r'\b'), Keyword),
105 (words(directivekw, prefix='#', suffix=r'\b'), Keyword.Directive),
106 inherit
107 ]
108 }
109
110 def analyse_text(text):
111 """Competes with IDL and Prolog on *.pro; div. lisps on*.cl and SwigLexer on *.i"""
112 # These are *really* good indicators (and not conflicting with the other languages)
113 # end-scope first on line e.g. 'end implement'
114 # section keyword alone on line e.g. 'clauses'
115 if re.search(r'^\s*(end\s+(interface|class|implement)|(clauses|predicates|domains|facts|constants|properties)\s*$)', text):
116 return 0.98
117 else:
118 return 0
119
120
121class VisualPrologGrammarLexer(VisualPrologBaseLexer):
122 """Lexer for VisualProlog grammar
123 """
124
125 name = 'Visual Prolog Grammar'
126 url = 'https://www.visual-prolog.com/'
127 aliases = ['visualprologgrammar']
128 filenames = ['*.vipgrm']
129 version_added = '2.17'
130
131 majorkw = ('open', 'namespace', 'grammar', 'nonterminals',
132 'startsymbols', 'terminals', 'rules', 'precedence')
133 directivekw = ('bininclude', 'stringinclude')
134 tokens = {
135 'root': [
136 (words(majorkw, suffix=r'\b'), Keyword),
137 (words(directivekw, prefix='#', suffix=r'\b'), Keyword.Directive),
138 inherit
139 ]
140 }
141
142 def analyse_text(text):
143 """No competditors (currently)"""
144 # These are *really* good indicators
145 # end-scope first on line e.g. 'end grammar'
146 # section keyword alone on line e.g. 'rules'
147 if re.search(r'^\s*(end\s+grammar|(nonterminals|startsymbols|terminals|rules|precedence)\s*$)', text):
148 return 0.98
149 else:
150 return 0