|
1 # -*- coding: utf-8 -*- |
|
2 """ |
|
3 pygments.lexers.actionscript |
|
4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|
5 |
|
6 Lexers for ActionScript and MXML. |
|
7 |
|
8 :copyright: Copyright 2006-2017 by the Pygments team, see AUTHORS. |
|
9 :license: BSD, see LICENSE for details. |
|
10 """ |
|
11 |
|
12 import re |
|
13 |
|
14 from pygments.lexer import RegexLexer, bygroups, using, this, words, default |
|
15 from pygments.token import Text, Comment, Operator, Keyword, Name, String, \ |
|
16 Number, Punctuation |
|
17 |
|
18 __all__ = ['ActionScriptLexer', 'ActionScript3Lexer', 'MxmlLexer'] |
|
19 |
|
20 |
|
21 class ActionScriptLexer(RegexLexer): |
|
22 """ |
|
23 For ActionScript source code. |
|
24 |
|
25 .. versionadded:: 0.9 |
|
26 """ |
|
27 |
|
28 name = 'ActionScript' |
|
29 aliases = ['as', 'actionscript'] |
|
30 filenames = ['*.as'] |
|
31 mimetypes = ['application/x-actionscript', 'text/x-actionscript', |
|
32 'text/actionscript'] |
|
33 |
|
34 flags = re.DOTALL |
|
35 tokens = { |
|
36 'root': [ |
|
37 (r'\s+', Text), |
|
38 (r'//.*?\n', Comment.Single), |
|
39 (r'/\*.*?\*/', Comment.Multiline), |
|
40 (r'/(\\\\|\\/|[^/\n])*/[gim]*', String.Regex), |
|
41 (r'[~^*!%&<>|+=:;,/?\\-]+', Operator), |
|
42 (r'[{}\[\]();.]+', Punctuation), |
|
43 (words(( |
|
44 'case', 'default', 'for', 'each', 'in', 'while', 'do', 'break', |
|
45 'return', 'continue', 'if', 'else', 'throw', 'try', 'catch', |
|
46 'var', 'with', 'new', 'typeof', 'arguments', 'instanceof', 'this', |
|
47 'switch'), suffix=r'\b'), |
|
48 Keyword), |
|
49 (words(( |
|
50 'class', 'public', 'final', 'internal', 'native', 'override', 'private', |
|
51 'protected', 'static', 'import', 'extends', 'implements', 'interface', |
|
52 'intrinsic', 'return', 'super', 'dynamic', 'function', 'const', 'get', |
|
53 'namespace', 'package', 'set'), suffix=r'\b'), |
|
54 Keyword.Declaration), |
|
55 (r'(true|false|null|NaN|Infinity|-Infinity|undefined|Void)\b', |
|
56 Keyword.Constant), |
|
57 (words(( |
|
58 'Accessibility', 'AccessibilityProperties', 'ActionScriptVersion', |
|
59 'ActivityEvent', 'AntiAliasType', 'ApplicationDomain', 'AsBroadcaster', 'Array', |
|
60 'AsyncErrorEvent', 'AVM1Movie', 'BevelFilter', 'Bitmap', 'BitmapData', |
|
61 'BitmapDataChannel', 'BitmapFilter', 'BitmapFilterQuality', 'BitmapFilterType', |
|
62 'BlendMode', 'BlurFilter', 'Boolean', 'ByteArray', 'Camera', 'Capabilities', 'CapsStyle', |
|
63 'Class', 'Color', 'ColorMatrixFilter', 'ColorTransform', 'ContextMenu', |
|
64 'ContextMenuBuiltInItems', 'ContextMenuEvent', 'ContextMenuItem', |
|
65 'ConvultionFilter', 'CSMSettings', 'DataEvent', 'Date', 'DefinitionError', |
|
66 'DeleteObjectSample', 'Dictionary', 'DisplacmentMapFilter', 'DisplayObject', |
|
67 'DisplacmentMapFilterMode', 'DisplayObjectContainer', 'DropShadowFilter', |
|
68 'Endian', 'EOFError', 'Error', 'ErrorEvent', 'EvalError', 'Event', 'EventDispatcher', |
|
69 'EventPhase', 'ExternalInterface', 'FileFilter', 'FileReference', |
|
70 'FileReferenceList', 'FocusDirection', 'FocusEvent', 'Font', 'FontStyle', 'FontType', |
|
71 'FrameLabel', 'FullScreenEvent', 'Function', 'GlowFilter', 'GradientBevelFilter', |
|
72 'GradientGlowFilter', 'GradientType', 'Graphics', 'GridFitType', 'HTTPStatusEvent', |
|
73 'IBitmapDrawable', 'ID3Info', 'IDataInput', 'IDataOutput', 'IDynamicPropertyOutput' |
|
74 'IDynamicPropertyWriter', 'IEventDispatcher', 'IExternalizable', |
|
75 'IllegalOperationError', 'IME', 'IMEConversionMode', 'IMEEvent', 'int', |
|
76 'InteractiveObject', 'InterpolationMethod', 'InvalidSWFError', 'InvokeEvent', |
|
77 'IOError', 'IOErrorEvent', 'JointStyle', 'Key', 'Keyboard', 'KeyboardEvent', 'KeyLocation', |
|
78 'LineScaleMode', 'Loader', 'LoaderContext', 'LoaderInfo', 'LoadVars', 'LocalConnection', |
|
79 'Locale', 'Math', 'Matrix', 'MemoryError', 'Microphone', 'MorphShape', 'Mouse', 'MouseEvent', |
|
80 'MovieClip', 'MovieClipLoader', 'Namespace', 'NetConnection', 'NetStatusEvent', |
|
81 'NetStream', 'NewObjectSample', 'Number', 'Object', 'ObjectEncoding', 'PixelSnapping', |
|
82 'Point', 'PrintJob', 'PrintJobOptions', 'PrintJobOrientation', 'ProgressEvent', 'Proxy', |
|
83 'QName', 'RangeError', 'Rectangle', 'ReferenceError', 'RegExp', 'Responder', 'Sample', |
|
84 'Scene', 'ScriptTimeoutError', 'Security', 'SecurityDomain', 'SecurityError', |
|
85 'SecurityErrorEvent', 'SecurityPanel', 'Selection', 'Shape', 'SharedObject', |
|
86 'SharedObjectFlushStatus', 'SimpleButton', 'Socket', 'Sound', 'SoundChannel', |
|
87 'SoundLoaderContext', 'SoundMixer', 'SoundTransform', 'SpreadMethod', 'Sprite', |
|
88 'StackFrame', 'StackOverflowError', 'Stage', 'StageAlign', 'StageDisplayState', |
|
89 'StageQuality', 'StageScaleMode', 'StaticText', 'StatusEvent', 'String', 'StyleSheet', |
|
90 'SWFVersion', 'SyncEvent', 'SyntaxError', 'System', 'TextColorType', 'TextField', |
|
91 'TextFieldAutoSize', 'TextFieldType', 'TextFormat', 'TextFormatAlign', |
|
92 'TextLineMetrics', 'TextRenderer', 'TextSnapshot', 'Timer', 'TimerEvent', 'Transform', |
|
93 'TypeError', 'uint', 'URIError', 'URLLoader', 'URLLoaderDataFormat', 'URLRequest', |
|
94 'URLRequestHeader', 'URLRequestMethod', 'URLStream', 'URLVariabeles', 'VerifyError', |
|
95 'Video', 'XML', 'XMLDocument', 'XMLList', 'XMLNode', 'XMLNodeType', 'XMLSocket', |
|
96 'XMLUI'), suffix=r'\b'), |
|
97 Name.Builtin), |
|
98 (words(( |
|
99 'decodeURI', 'decodeURIComponent', 'encodeURI', 'escape', 'eval', 'isFinite', 'isNaN', |
|
100 'isXMLName', 'clearInterval', 'fscommand', 'getTimer', 'getURL', 'getVersion', |
|
101 'parseFloat', 'parseInt', 'setInterval', 'trace', 'updateAfterEvent', |
|
102 'unescape'), suffix=r'\b'), |
|
103 Name.Function), |
|
104 (r'[$a-zA-Z_]\w*', Name.Other), |
|
105 (r'[0-9][0-9]*\.[0-9]+([eE][0-9]+)?[fd]?', Number.Float), |
|
106 (r'0x[0-9a-f]+', Number.Hex), |
|
107 (r'[0-9]+', Number.Integer), |
|
108 (r'"(\\\\|\\"|[^"])*"', String.Double), |
|
109 (r"'(\\\\|\\'|[^'])*'", String.Single), |
|
110 ] |
|
111 } |
|
112 |
|
113 |
|
114 class ActionScript3Lexer(RegexLexer): |
|
115 """ |
|
116 For ActionScript 3 source code. |
|
117 |
|
118 .. versionadded:: 0.11 |
|
119 """ |
|
120 |
|
121 name = 'ActionScript 3' |
|
122 aliases = ['as3', 'actionscript3'] |
|
123 filenames = ['*.as'] |
|
124 mimetypes = ['application/x-actionscript3', 'text/x-actionscript3', |
|
125 'text/actionscript3'] |
|
126 |
|
127 identifier = r'[$a-zA-Z_]\w*' |
|
128 typeidentifier = identifier + r'(?:\.<\w+>)?' |
|
129 |
|
130 flags = re.DOTALL | re.MULTILINE |
|
131 tokens = { |
|
132 'root': [ |
|
133 (r'\s+', Text), |
|
134 (r'(function\s+)(' + identifier + r')(\s*)(\()', |
|
135 bygroups(Keyword.Declaration, Name.Function, Text, Operator), |
|
136 'funcparams'), |
|
137 (r'(var|const)(\s+)(' + identifier + r')(\s*)(:)(\s*)(' + |
|
138 typeidentifier + r')', |
|
139 bygroups(Keyword.Declaration, Text, Name, Text, Punctuation, Text, |
|
140 Keyword.Type)), |
|
141 (r'(import|package)(\s+)((?:' + identifier + r'|\.)+)(\s*)', |
|
142 bygroups(Keyword, Text, Name.Namespace, Text)), |
|
143 (r'(new)(\s+)(' + typeidentifier + r')(\s*)(\()', |
|
144 bygroups(Keyword, Text, Keyword.Type, Text, Operator)), |
|
145 (r'//.*?\n', Comment.Single), |
|
146 (r'/\*.*?\*/', Comment.Multiline), |
|
147 (r'/(\\\\|\\/|[^\n])*/[gisx]*', String.Regex), |
|
148 (r'(\.)(' + identifier + r')', bygroups(Operator, Name.Attribute)), |
|
149 (r'(case|default|for|each|in|while|do|break|return|continue|if|else|' |
|
150 r'throw|try|catch|with|new|typeof|arguments|instanceof|this|' |
|
151 r'switch|import|include|as|is)\b', |
|
152 Keyword), |
|
153 (r'(class|public|final|internal|native|override|private|protected|' |
|
154 r'static|import|extends|implements|interface|intrinsic|return|super|' |
|
155 r'dynamic|function|const|get|namespace|package|set)\b', |
|
156 Keyword.Declaration), |
|
157 (r'(true|false|null|NaN|Infinity|-Infinity|undefined|void)\b', |
|
158 Keyword.Constant), |
|
159 (r'(decodeURI|decodeURIComponent|encodeURI|escape|eval|isFinite|isNaN|' |
|
160 r'isXMLName|clearInterval|fscommand|getTimer|getURL|getVersion|' |
|
161 r'isFinite|parseFloat|parseInt|setInterval|trace|updateAfterEvent|' |
|
162 r'unescape)\b', Name.Function), |
|
163 (identifier, Name), |
|
164 (r'[0-9][0-9]*\.[0-9]+([eE][0-9]+)?[fd]?', Number.Float), |
|
165 (r'0x[0-9a-f]+', Number.Hex), |
|
166 (r'[0-9]+', Number.Integer), |
|
167 (r'"(\\\\|\\"|[^"])*"', String.Double), |
|
168 (r"'(\\\\|\\'|[^'])*'", String.Single), |
|
169 (r'[~^*!%&<>|+=:;,/?\\{}\[\]().-]+', Operator), |
|
170 ], |
|
171 'funcparams': [ |
|
172 (r'\s+', Text), |
|
173 (r'(\s*)(\.\.\.)?(' + identifier + r')(\s*)(:)(\s*)(' + |
|
174 typeidentifier + r'|\*)(\s*)', |
|
175 bygroups(Text, Punctuation, Name, Text, Operator, Text, |
|
176 Keyword.Type, Text), 'defval'), |
|
177 (r'\)', Operator, 'type') |
|
178 ], |
|
179 'type': [ |
|
180 (r'(\s*)(:)(\s*)(' + typeidentifier + r'|\*)', |
|
181 bygroups(Text, Operator, Text, Keyword.Type), '#pop:2'), |
|
182 (r'\s+', Text, '#pop:2'), |
|
183 default('#pop:2') |
|
184 ], |
|
185 'defval': [ |
|
186 (r'(=)(\s*)([^(),]+)(\s*)(,?)', |
|
187 bygroups(Operator, Text, using(this), Text, Operator), '#pop'), |
|
188 (r',', Operator, '#pop'), |
|
189 default('#pop') |
|
190 ] |
|
191 } |
|
192 |
|
193 def analyse_text(text): |
|
194 if re.match(r'\w+\s*:\s*\w', text): |
|
195 return 0.3 |
|
196 return 0 |
|
197 |
|
198 |
|
199 class MxmlLexer(RegexLexer): |
|
200 """ |
|
201 For MXML markup. |
|
202 Nested AS3 in <script> tags is highlighted by the appropriate lexer. |
|
203 |
|
204 .. versionadded:: 1.1 |
|
205 """ |
|
206 flags = re.MULTILINE | re.DOTALL |
|
207 name = 'MXML' |
|
208 aliases = ['mxml'] |
|
209 filenames = ['*.mxml'] |
|
210 mimetimes = ['text/xml', 'application/xml'] |
|
211 |
|
212 tokens = { |
|
213 'root': [ |
|
214 ('[^<&]+', Text), |
|
215 (r'&\S*?;', Name.Entity), |
|
216 (r'(\<\!\[CDATA\[)(.*?)(\]\]\>)', |
|
217 bygroups(String, using(ActionScript3Lexer), String)), |
|
218 ('<!--', Comment, 'comment'), |
|
219 (r'<\?.*?\?>', Comment.Preproc), |
|
220 ('<![^>]*>', Comment.Preproc), |
|
221 (r'<\s*[\w:.-]+', Name.Tag, 'tag'), |
|
222 (r'<\s*/\s*[\w:.-]+\s*>', Name.Tag), |
|
223 ], |
|
224 'comment': [ |
|
225 ('[^-]+', Comment), |
|
226 ('-->', Comment, '#pop'), |
|
227 ('-', Comment), |
|
228 ], |
|
229 'tag': [ |
|
230 (r'\s+', Text), |
|
231 (r'[\w.:-]+\s*=', Name.Attribute, 'attr'), |
|
232 (r'/?\s*>', Name.Tag, '#pop'), |
|
233 ], |
|
234 'attr': [ |
|
235 (r'\s+', Text), |
|
236 ('".*?"', String, '#pop'), |
|
237 ("'.*?'", String, '#pop'), |
|
238 (r'[^\s>]+', String, '#pop'), |
|
239 ], |
|
240 } |