actionscript.py 11 KB

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