pawn.py 8.0 KB

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