vim-rana-local/packages/wakatime/packages/pygments/token.py

214 lines
6.0 KiB
Python
Raw Normal View History

2013-09-22 23:22:11 +00:00
# -*- coding: utf-8 -*-
"""
pygments.token
~~~~~~~~~~~~~~
Basic token types and the standard tokens.
2017-02-14 07:32:53 +00:00
:copyright: Copyright 2006-2017 by the Pygments team, see AUTHORS.
2013-09-22 23:22:11 +00:00
:license: BSD, see LICENSE for details.
"""
2017-02-14 07:32:53 +00:00
2013-09-22 23:22:11 +00:00
class _TokenType(tuple):
parent = None
def split(self):
buf = []
node = self
while node is not None:
buf.append(node)
node = node.parent
buf.reverse()
return buf
def __init__(self, *args):
# no need to call super.__init__
self.subtypes = set()
def __contains__(self, val):
return self is val or (
type(val) is self.__class__ and
val[:len(self)] == self
)
def __getattr__(self, val):
if not val or not val[0].isupper():
return tuple.__getattribute__(self, val)
new = _TokenType(self + (val,))
setattr(self, val, new)
self.subtypes.add(new)
new.parent = self
return new
def __repr__(self):
return 'Token' + (self and '.' or '') + '.'.join(self)
2016-06-13 14:48:11 +00:00
def __copy__(self):
# These instances are supposed to be singletons
return self
def __deepcopy__(self, memo):
# These instances are supposed to be singletons
return self
2013-09-22 23:22:11 +00:00
2017-02-14 07:32:53 +00:00
Token = _TokenType()
2013-09-22 23:22:11 +00:00
# Special token types
2017-02-14 07:32:53 +00:00
Text = Token.Text
Whitespace = Text.Whitespace
Escape = Token.Escape
Error = Token.Error
2013-09-22 23:22:11 +00:00
# Text that doesn't belong to this lexer (e.g. HTML in PHP)
2017-02-14 07:32:53 +00:00
Other = Token.Other
2013-09-22 23:22:11 +00:00
# Common token types for source code
2017-02-14 07:32:53 +00:00
Keyword = Token.Keyword
Name = Token.Name
Literal = Token.Literal
String = Literal.String
Number = Literal.Number
2013-09-22 23:22:11 +00:00
Punctuation = Token.Punctuation
2017-02-14 07:32:53 +00:00
Operator = Token.Operator
Comment = Token.Comment
2013-09-22 23:22:11 +00:00
# Generic types for non-source code
2017-02-14 07:32:53 +00:00
Generic = Token.Generic
2013-09-22 23:22:11 +00:00
2017-02-14 07:32:53 +00:00
# String and some others are not direct children of Token.
2013-09-22 23:22:11 +00:00
# alias them:
Token.Token = Token
Token.String = String
Token.Number = Number
def is_token_subtype(ttype, other):
"""
Return True if ``ttype`` is a subtype of ``other``.
exists for backwards compatibility. use ``ttype in other`` now.
"""
return ttype in other
def string_to_tokentype(s):
"""
Convert a string into a token type::
>>> string_to_token('String.Double')
Token.Literal.String.Double
>>> string_to_token('Token.Literal.Number')
Token.Literal.Number
>>> string_to_token('')
Token
Tokens that are already tokens are returned unchanged:
>>> string_to_token(String)
Token.Literal.String
"""
if isinstance(s, _TokenType):
return s
if not s:
return Token
node = Token
for item in s.split('.'):
node = getattr(node, item)
return node
# Map standard token types to short names, used in CSS class naming.
# If you add a new item, please be sure to run this file to perform
# a consistency check for duplicate values.
STANDARD_TYPES = {
Token: '',
Text: '',
Whitespace: 'w',
2014-12-01 06:19:45 +00:00
Escape: 'esc',
2013-09-22 23:22:11 +00:00
Error: 'err',
Other: 'x',
Keyword: 'k',
Keyword.Constant: 'kc',
Keyword.Declaration: 'kd',
Keyword.Namespace: 'kn',
Keyword.Pseudo: 'kp',
Keyword.Reserved: 'kr',
Keyword.Type: 'kt',
Name: 'n',
Name.Attribute: 'na',
Name.Builtin: 'nb',
Name.Builtin.Pseudo: 'bp',
Name.Class: 'nc',
Name.Constant: 'no',
Name.Decorator: 'nd',
Name.Entity: 'ni',
Name.Exception: 'ne',
Name.Function: 'nf',
2017-02-14 07:32:53 +00:00
Name.Function.Magic: 'fm',
2013-09-22 23:22:11 +00:00
Name.Property: 'py',
Name.Label: 'nl',
Name.Namespace: 'nn',
Name.Other: 'nx',
Name.Tag: 'nt',
Name.Variable: 'nv',
Name.Variable.Class: 'vc',
Name.Variable.Global: 'vg',
Name.Variable.Instance: 'vi',
2017-02-14 07:32:53 +00:00
Name.Variable.Magic: 'vm',
2013-09-22 23:22:11 +00:00
Literal: 'l',
Literal.Date: 'ld',
String: 's',
2017-02-14 07:32:53 +00:00
String.Affix: 'sa',
2013-09-22 23:22:11 +00:00
String.Backtick: 'sb',
String.Char: 'sc',
2017-02-14 07:32:53 +00:00
String.Delimiter: 'dl',
2013-09-22 23:22:11 +00:00
String.Doc: 'sd',
String.Double: 's2',
String.Escape: 'se',
String.Heredoc: 'sh',
String.Interpol: 'si',
String.Other: 'sx',
String.Regex: 'sr',
String.Single: 's1',
String.Symbol: 'ss',
Number: 'm',
2014-12-01 06:19:45 +00:00
Number.Bin: 'mb',
2013-09-22 23:22:11 +00:00
Number.Float: 'mf',
Number.Hex: 'mh',
Number.Integer: 'mi',
Number.Integer.Long: 'il',
Number.Oct: 'mo',
Operator: 'o',
Operator.Word: 'ow',
Punctuation: 'p',
Comment: 'c',
2016-06-13 14:48:11 +00:00
Comment.Hashbang: 'ch',
2013-09-22 23:22:11 +00:00
Comment.Multiline: 'cm',
Comment.Preproc: 'cp',
2016-06-13 14:48:11 +00:00
Comment.PreprocFile: 'cpf',
2013-09-22 23:22:11 +00:00
Comment.Single: 'c1',
Comment.Special: 'cs',
Generic: 'g',
Generic.Deleted: 'gd',
Generic.Emph: 'ge',
Generic.Error: 'gr',
Generic.Heading: 'gh',
Generic.Inserted: 'gi',
Generic.Output: 'go',
Generic.Prompt: 'gp',
Generic.Strong: 'gs',
Generic.Subheading: 'gu',
Generic.Traceback: 'gt',
}