You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

213 lines
6.0 KiB

4 years ago
  1. # -*- coding: utf-8 -*-
  2. """
  3. pygments.token
  4. ~~~~~~~~~~~~~~
  5. Basic token types and the standard tokens.
  6. :copyright: Copyright 2006-2019 by the Pygments team, see AUTHORS.
  7. :license: BSD, see LICENSE for details.
  8. """
  9. class _TokenType(tuple):
  10. parent = None
  11. def split(self):
  12. buf = []
  13. node = self
  14. while node is not None:
  15. buf.append(node)
  16. node = node.parent
  17. buf.reverse()
  18. return buf
  19. def __init__(self, *args):
  20. # no need to call super.__init__
  21. self.subtypes = set()
  22. def __contains__(self, val):
  23. return self is val or (
  24. type(val) is self.__class__ and
  25. val[:len(self)] == self
  26. )
  27. def __getattr__(self, val):
  28. if not val or not val[0].isupper():
  29. return tuple.__getattribute__(self, val)
  30. new = _TokenType(self + (val,))
  31. setattr(self, val, new)
  32. self.subtypes.add(new)
  33. new.parent = self
  34. return new
  35. def __repr__(self):
  36. return 'Token' + (self and '.' or '') + '.'.join(self)
  37. def __copy__(self):
  38. # These instances are supposed to be singletons
  39. return self
  40. def __deepcopy__(self, memo):
  41. # These instances are supposed to be singletons
  42. return self
  43. Token = _TokenType()
  44. # Special token types
  45. Text = Token.Text
  46. Whitespace = Text.Whitespace
  47. Escape = Token.Escape
  48. Error = Token.Error
  49. # Text that doesn't belong to this lexer (e.g. HTML in PHP)
  50. Other = Token.Other
  51. # Common token types for source code
  52. Keyword = Token.Keyword
  53. Name = Token.Name
  54. Literal = Token.Literal
  55. String = Literal.String
  56. Number = Literal.Number
  57. Punctuation = Token.Punctuation
  58. Operator = Token.Operator
  59. Comment = Token.Comment
  60. # Generic types for non-source code
  61. Generic = Token.Generic
  62. # String and some others are not direct children of Token.
  63. # alias them:
  64. Token.Token = Token
  65. Token.String = String
  66. Token.Number = Number
  67. def is_token_subtype(ttype, other):
  68. """
  69. Return True if ``ttype`` is a subtype of ``other``.
  70. exists for backwards compatibility. use ``ttype in other`` now.
  71. """
  72. return ttype in other
  73. def string_to_tokentype(s):
  74. """
  75. Convert a string into a token type::
  76. >>> string_to_token('String.Double')
  77. Token.Literal.String.Double
  78. >>> string_to_token('Token.Literal.Number')
  79. Token.Literal.Number
  80. >>> string_to_token('')
  81. Token
  82. Tokens that are already tokens are returned unchanged:
  83. >>> string_to_token(String)
  84. Token.Literal.String
  85. """
  86. if isinstance(s, _TokenType):
  87. return s
  88. if not s:
  89. return Token
  90. node = Token
  91. for item in s.split('.'):
  92. node = getattr(node, item)
  93. return node
  94. # Map standard token types to short names, used in CSS class naming.
  95. # If you add a new item, please be sure to run this file to perform
  96. # a consistency check for duplicate values.
  97. STANDARD_TYPES = {
  98. Token: '',
  99. Text: '',
  100. Whitespace: 'w',
  101. Escape: 'esc',
  102. Error: 'err',
  103. Other: 'x',
  104. Keyword: 'k',
  105. Keyword.Constant: 'kc',
  106. Keyword.Declaration: 'kd',
  107. Keyword.Namespace: 'kn',
  108. Keyword.Pseudo: 'kp',
  109. Keyword.Reserved: 'kr',
  110. Keyword.Type: 'kt',
  111. Name: 'n',
  112. Name.Attribute: 'na',
  113. Name.Builtin: 'nb',
  114. Name.Builtin.Pseudo: 'bp',
  115. Name.Class: 'nc',
  116. Name.Constant: 'no',
  117. Name.Decorator: 'nd',
  118. Name.Entity: 'ni',
  119. Name.Exception: 'ne',
  120. Name.Function: 'nf',
  121. Name.Function.Magic: 'fm',
  122. Name.Property: 'py',
  123. Name.Label: 'nl',
  124. Name.Namespace: 'nn',
  125. Name.Other: 'nx',
  126. Name.Tag: 'nt',
  127. Name.Variable: 'nv',
  128. Name.Variable.Class: 'vc',
  129. Name.Variable.Global: 'vg',
  130. Name.Variable.Instance: 'vi',
  131. Name.Variable.Magic: 'vm',
  132. Literal: 'l',
  133. Literal.Date: 'ld',
  134. String: 's',
  135. String.Affix: 'sa',
  136. String.Backtick: 'sb',
  137. String.Char: 'sc',
  138. String.Delimiter: 'dl',
  139. String.Doc: 'sd',
  140. String.Double: 's2',
  141. String.Escape: 'se',
  142. String.Heredoc: 'sh',
  143. String.Interpol: 'si',
  144. String.Other: 'sx',
  145. String.Regex: 'sr',
  146. String.Single: 's1',
  147. String.Symbol: 'ss',
  148. Number: 'm',
  149. Number.Bin: 'mb',
  150. Number.Float: 'mf',
  151. Number.Hex: 'mh',
  152. Number.Integer: 'mi',
  153. Number.Integer.Long: 'il',
  154. Number.Oct: 'mo',
  155. Operator: 'o',
  156. Operator.Word: 'ow',
  157. Punctuation: 'p',
  158. Comment: 'c',
  159. Comment.Hashbang: 'ch',
  160. Comment.Multiline: 'cm',
  161. Comment.Preproc: 'cp',
  162. Comment.PreprocFile: 'cpf',
  163. Comment.Single: 'c1',
  164. Comment.Special: 'cs',
  165. Generic: 'g',
  166. Generic.Deleted: 'gd',
  167. Generic.Emph: 'ge',
  168. Generic.Error: 'gr',
  169. Generic.Heading: 'gh',
  170. Generic.Inserted: 'gi',
  171. Generic.Output: 'go',
  172. Generic.Prompt: 'gp',
  173. Generic.Strong: 'gs',
  174. Generic.Subheading: 'gu',
  175. Generic.Traceback: 'gt',
  176. }