pawn.py 7.8 KB

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