Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/pygments/lexers/pawn.py: 78%
40 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:16 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:16 +0000
1"""
2 pygments.lexers.pawn
3 ~~~~~~~~~~~~~~~~~~~~
5 Lexers for the Pawn languages.
7 :copyright: Copyright 2006-2023 by the Pygments team, see AUTHORS.
8 :license: BSD, see LICENSE for details.
9"""
11from pygments.lexer import RegexLexer
12from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
13 Number, Punctuation
14from pygments.util import get_bool_opt
16__all__ = ['SourcePawnLexer', 'PawnLexer']
19class SourcePawnLexer(RegexLexer):
20 """
21 For SourcePawn source code with preprocessor directives.
23 .. versionadded:: 1.6
24 """
25 name = 'SourcePawn'
26 aliases = ['sp']
27 filenames = ['*.sp']
28 mimetypes = ['text/x-sourcepawn']
30 #: optional Comment or Whitespace
31 _ws = r'(?:\s|//.*?\n|/\*.*?\*/)+'
32 #: only one /* */ style comment
33 _ws1 = r'\s*(?:/[*].*?[*]/\s*)*'
35 tokens = {
36 'root': [
37 # preprocessor directives: without whitespace
38 (r'^#if\s+0', Comment.Preproc, 'if0'),
39 ('^#', Comment.Preproc, 'macro'),
40 # or with whitespace
41 ('^' + _ws1 + r'#if\s+0', Comment.Preproc, 'if0'),
42 ('^' + _ws1 + '#', Comment.Preproc, 'macro'),
43 (r'\n', Text),
44 (r'\s+', Text),
45 (r'\\\n', Text), # line continuation
46 (r'/(\\\n)?/(\n|(.|\n)*?[^\\]\n)', Comment.Single),
47 (r'/(\\\n)?\*(.|\n)*?\*(\\\n)?/', Comment.Multiline),
48 (r'[{}]', Punctuation),
49 (r'L?"', String, 'string'),
50 (r"L?'(\\.|\\[0-7]{1,3}|\\x[a-fA-F0-9]{1,2}|[^\\\'\n])'", String.Char),
51 (r'(\d+\.\d*|\.\d+|\d+)[eE][+-]?\d+[LlUu]*', Number.Float),
52 (r'(\d+\.\d*|\.\d+|\d+[fF])[fF]?', Number.Float),
53 (r'0x[0-9a-fA-F]+[LlUu]*', Number.Hex),
54 (r'0[0-7]+[LlUu]*', Number.Oct),
55 (r'\d+[LlUu]*', Number.Integer),
56 (r'[~!%^&*+=|?:<>/-]', Operator),
57 (r'[()\[\],.;]', Punctuation),
58 (r'(case|const|continue|native|'
59 r'default|else|enum|for|if|new|operator|'
60 r'public|return|sizeof|static|decl|struct|switch)\b', Keyword),
61 (r'(bool|Float)\b', Keyword.Type),
62 (r'(true|false)\b', Keyword.Constant),
63 (r'[a-zA-Z_]\w*', Name),
64 ],
65 'string': [
66 (r'"', String, '#pop'),
67 (r'\\([\\abfnrtv"\']|x[a-fA-F0-9]{2,4}|[0-7]{1,3})', String.Escape),
68 (r'[^\\"\n]+', String), # all other characters
69 (r'\\\n', String), # line continuation
70 (r'\\', String), # stray backslash
71 ],
72 'macro': [
73 (r'[^/\n]+', Comment.Preproc),
74 (r'/\*(.|\n)*?\*/', Comment.Multiline),
75 (r'//.*?\n', Comment.Single, '#pop'),
76 (r'/', Comment.Preproc),
77 (r'(?<=\\)\n', Comment.Preproc),
78 (r'\n', Comment.Preproc, '#pop'),
79 ],
80 'if0': [
81 (r'^\s*#if.*?(?<!\\)\n', Comment.Preproc, '#push'),
82 (r'^\s*#endif.*?(?<!\\)\n', Comment.Preproc, '#pop'),
83 (r'.*?\n', Comment),
84 ]
85 }
87 SM_TYPES = {'Action', 'bool', 'Float', 'Plugin', 'String', 'any',
88 'AdminFlag', 'OverrideType', 'OverrideRule', 'ImmunityType',
89 'GroupId', 'AdminId', 'AdmAccessMode', 'AdminCachePart',
90 'CookieAccess', 'CookieMenu', 'CookieMenuAction', 'NetFlow',
91 'ConVarBounds', 'QueryCookie', 'ReplySource',
92 'ConVarQueryResult', 'ConVarQueryFinished', 'Function',
93 'Action', 'Identity', 'PluginStatus', 'PluginInfo', 'DBResult',
94 'DBBindType', 'DBPriority', 'PropType', 'PropFieldType',
95 'MoveType', 'RenderMode', 'RenderFx', 'EventHookMode',
96 'EventHook', 'FileType', 'FileTimeMode', 'PathType',
97 'ParamType', 'ExecType', 'DialogType', 'Handle', 'KvDataTypes',
98 'NominateResult', 'MapChange', 'MenuStyle', 'MenuAction',
99 'MenuSource', 'RegexError', 'SDKCallType', 'SDKLibrary',
100 'SDKFuncConfSource', 'SDKType', 'SDKPassMethod', 'RayType',
101 'TraceEntityFilter', 'ListenOverride', 'SortOrder', 'SortType',
102 'SortFunc2D', 'APLRes', 'FeatureType', 'FeatureStatus',
103 'SMCResult', 'SMCError', 'TFClassType', 'TFTeam', 'TFCond',
104 'TFResourceType', 'Timer', 'TopMenuAction', 'TopMenuObjectType',
105 'TopMenuPosition', 'TopMenuObject', 'UserMsg'}
107 def __init__(self, **options):
108 self.smhighlighting = get_bool_opt(options,
109 'sourcemod', True)
111 self._functions = set()
112 if self.smhighlighting:
113 from pygments.lexers._sourcemod_builtins import FUNCTIONS
114 self._functions.update(FUNCTIONS)
115 RegexLexer.__init__(self, **options)
117 def get_tokens_unprocessed(self, text):
118 for index, token, value in \
119 RegexLexer.get_tokens_unprocessed(self, text):
120 if token is Name:
121 if self.smhighlighting:
122 if value in self.SM_TYPES:
123 token = Keyword.Type
124 elif value in self._functions:
125 token = Name.Builtin
126 yield index, token, value
129class PawnLexer(RegexLexer):
130 """
131 For Pawn source code.
133 .. versionadded:: 2.0
134 """
136 name = 'Pawn'
137 aliases = ['pawn']
138 filenames = ['*.p', '*.pwn', '*.inc']
139 mimetypes = ['text/x-pawn']
141 #: optional Comment or Whitespace
142 _ws = r'(?:\s|//.*?\n|/[*][\w\W]*?[*]/)+'
143 #: only one /* */ style comment
144 _ws1 = r'\s*(?:/[*].*?[*]/\s*)*'
146 tokens = {
147 'root': [
148 # preprocessor directives: without whitespace
149 (r'^#if\s+0', Comment.Preproc, 'if0'),
150 ('^#', Comment.Preproc, 'macro'),
151 # or with whitespace
152 ('^' + _ws1 + r'#if\s+0', Comment.Preproc, 'if0'),
153 ('^' + _ws1 + '#', Comment.Preproc, 'macro'),
154 (r'\n', Text),
155 (r'\s+', Text),
156 (r'\\\n', Text), # line continuation
157 (r'/(\\\n)?/(\n|(.|\n)*?[^\\]\n)', Comment.Single),
158 (r'/(\\\n)?\*[\w\W]*?\*(\\\n)?/', Comment.Multiline),
159 (r'[{}]', Punctuation),
160 (r'L?"', String, 'string'),
161 (r"L?'(\\.|\\[0-7]{1,3}|\\x[a-fA-F0-9]{1,2}|[^\\\'\n])'", String.Char),
162 (r'(\d+\.\d*|\.\d+|\d+)[eE][+-]?\d+[LlUu]*', Number.Float),
163 (r'(\d+\.\d*|\.\d+|\d+[fF])[fF]?', Number.Float),
164 (r'0x[0-9a-fA-F]+[LlUu]*', Number.Hex),
165 (r'0[0-7]+[LlUu]*', Number.Oct),
166 (r'\d+[LlUu]*', Number.Integer),
167 (r'[~!%^&*+=|?:<>/-]', Operator),
168 (r'[()\[\],.;]', Punctuation),
169 (r'(switch|case|default|const|new|static|char|continue|break|'
170 r'if|else|for|while|do|operator|enum|'
171 r'public|return|sizeof|tagof|state|goto)\b', Keyword),
172 (r'(bool|Float)\b', Keyword.Type),
173 (r'(true|false)\b', Keyword.Constant),
174 (r'[a-zA-Z_]\w*', Name),
175 ],
176 'string': [
177 (r'"', String, '#pop'),
178 (r'\\([\\abfnrtv"\']|x[a-fA-F0-9]{2,4}|[0-7]{1,3})', String.Escape),
179 (r'[^\\"\n]+', String), # all other characters
180 (r'\\\n', String), # line continuation
181 (r'\\', String), # stray backslash
182 ],
183 'macro': [
184 (r'[^/\n]+', Comment.Preproc),
185 (r'/\*(.|\n)*?\*/', Comment.Multiline),
186 (r'//.*?\n', Comment.Single, '#pop'),
187 (r'/', Comment.Preproc),
188 (r'(?<=\\)\n', Comment.Preproc),
189 (r'\n', Comment.Preproc, '#pop'),
190 ],
191 'if0': [
192 (r'^\s*#if.*?(?<!\\)\n', Comment.Preproc, '#push'),
193 (r'^\s*#endif.*?(?<!\\)\n', Comment.Preproc, '#pop'),
194 (r'.*?\n', Comment),
195 ]
196 }
198 def analyse_text(text):
199 """This is basically C. There is a keyword which doesn't exist in C
200 though and is nearly unique to this language."""
201 if 'tagof' in text:
202 return 0.01