ThirdParty/Pygments/pygments/token.py

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

eric ide

mercurial