actionscript.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. """
  2. pygments.lexers.actionscript
  3. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. Lexers for ActionScript and MXML.
  5. :copyright: Copyright 2006-2024 by the Pygments team, see AUTHORS.
  6. :license: BSD, see LICENSE for details.
  7. """
  8. import re
  9. from pygments.lexer import RegexLexer, bygroups, using, this, words, default
  10. from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
  11. Number, Punctuation, Whitespace
  12. __all__ = ['ActionScriptLexer', 'ActionScript3Lexer', 'MxmlLexer']
  13. class ActionScriptLexer(RegexLexer):
  14. """
  15. For ActionScript source code.
  16. """
  17. name = 'ActionScript'
  18. aliases = ['actionscript', 'as']
  19. filenames = ['*.as']
  20. mimetypes = ['application/x-actionscript', 'text/x-actionscript',
  21. 'text/actionscript']
  22. url = 'https://en.wikipedia.org/wiki/ActionScript'
  23. version_added = '0.9'
  24. flags = re.DOTALL
  25. tokens = {
  26. 'root': [
  27. (r'\s+', Whitespace),
  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. def analyse_text(text):
  103. """This is only used to disambiguate between ActionScript and
  104. ActionScript3. We return 0 here; the ActionScript3 lexer will match
  105. AS3 variable definitions and that will hopefully suffice."""
  106. return 0
  107. class ActionScript3Lexer(RegexLexer):
  108. """
  109. For ActionScript 3 source code.
  110. """
  111. name = 'ActionScript 3'
  112. url = 'https://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/index.html'
  113. aliases = ['actionscript3', 'as3']
  114. filenames = ['*.as']
  115. mimetypes = ['application/x-actionscript3', 'text/x-actionscript3',
  116. 'text/actionscript3']
  117. version_added = '0.11'
  118. identifier = r'[$a-zA-Z_]\w*'
  119. typeidentifier = identifier + r'(?:\.<\w+>)?'
  120. flags = re.DOTALL | re.MULTILINE
  121. tokens = {
  122. 'root': [
  123. (r'\s+', Whitespace),
  124. (r'(function\s+)(' + identifier + r')(\s*)(\()',
  125. bygroups(Keyword.Declaration, Name.Function, Text, Operator),
  126. 'funcparams'),
  127. (r'(var|const)(\s+)(' + identifier + r')(\s*)(:)(\s*)(' +
  128. typeidentifier + r')',
  129. bygroups(Keyword.Declaration, Whitespace, Name, Whitespace, Punctuation, Whitespace,
  130. Keyword.Type)),
  131. (r'(import|package)(\s+)((?:' + identifier + r'|\.)+)(\s*)',
  132. bygroups(Keyword, Whitespace, Name.Namespace, Whitespace)),
  133. (r'(new)(\s+)(' + typeidentifier + r')(\s*)(\()',
  134. bygroups(Keyword, Whitespace, Keyword.Type, Whitespace, Operator)),
  135. (r'//.*?\n', Comment.Single),
  136. (r'/\*.*?\*/', Comment.Multiline),
  137. (r'/(\\\\|\\[^\\]|[^\\\n])*/[gisx]*', String.Regex),
  138. (r'(\.)(' + identifier + r')', bygroups(Operator, Name.Attribute)),
  139. (r'(case|default|for|each|in|while|do|break|return|continue|if|else|'
  140. r'throw|try|catch|with|new|typeof|arguments|instanceof|this|'
  141. r'switch|import|include|as|is)\b',
  142. Keyword),
  143. (r'(class|public|final|internal|native|override|private|protected|'
  144. r'static|import|extends|implements|interface|intrinsic|return|super|'
  145. r'dynamic|function|const|get|namespace|package|set)\b',
  146. Keyword.Declaration),
  147. (r'(true|false|null|NaN|Infinity|-Infinity|undefined|void)\b',
  148. Keyword.Constant),
  149. (r'(decodeURI|decodeURIComponent|encodeURI|escape|eval|isFinite|isNaN|'
  150. r'isXMLName|clearInterval|fscommand|getTimer|getURL|getVersion|'
  151. r'isFinite|parseFloat|parseInt|setInterval|trace|updateAfterEvent|'
  152. r'unescape)\b', Name.Function),
  153. (identifier, Name),
  154. (r'[0-9][0-9]*\.[0-9]+([eE][0-9]+)?[fd]?', Number.Float),
  155. (r'0x[0-9a-f]+', Number.Hex),
  156. (r'[0-9]+', Number.Integer),
  157. (r'"(\\\\|\\[^\\]|[^"\\])*"', String.Double),
  158. (r"'(\\\\|\\[^\\]|[^'\\])*'", String.Single),
  159. (r'[~^*!%&<>|+=:;,/?\\{}\[\]().-]+', Operator),
  160. ],
  161. 'funcparams': [
  162. (r'\s+', Whitespace),
  163. (r'(\s*)(\.\.\.)?(' + identifier + r')(\s*)(:)(\s*)(' +
  164. typeidentifier + r'|\*)(\s*)',
  165. bygroups(Whitespace, Punctuation, Name, Whitespace, Operator, Whitespace,
  166. Keyword.Type, Whitespace), 'defval'),
  167. (r'\)', Operator, 'type')
  168. ],
  169. 'type': [
  170. (r'(\s*)(:)(\s*)(' + typeidentifier + r'|\*)',
  171. bygroups(Whitespace, Operator, Whitespace, Keyword.Type), '#pop:2'),
  172. (r'\s+', Text, '#pop:2'),
  173. default('#pop:2')
  174. ],
  175. 'defval': [
  176. (r'(=)(\s*)([^(),]+)(\s*)(,?)',
  177. bygroups(Operator, Whitespace, using(this), Whitespace, Operator), '#pop'),
  178. (r',', Operator, '#pop'),
  179. default('#pop')
  180. ]
  181. }
  182. def analyse_text(text):
  183. if re.match(r'\w+\s*:\s*\w', text):
  184. return 0.3
  185. return 0
  186. class MxmlLexer(RegexLexer):
  187. """
  188. For MXML markup.
  189. Nested AS3 in <script> tags is highlighted by the appropriate lexer.
  190. """
  191. flags = re.MULTILINE | re.DOTALL
  192. name = 'MXML'
  193. aliases = ['mxml']
  194. filenames = ['*.mxml']
  195. url = 'https://en.wikipedia.org/wiki/MXML'
  196. version_added = '1.1'
  197. tokens = {
  198. 'root': [
  199. ('[^<&]+', Text),
  200. (r'&\S*?;', Name.Entity),
  201. (r'(\<\!\[CDATA\[)(.*?)(\]\]\>)',
  202. bygroups(String, using(ActionScript3Lexer), String)),
  203. ('<!--', Comment, 'comment'),
  204. (r'<\?.*?\?>', Comment.Preproc),
  205. ('<![^>]*>', Comment.Preproc),
  206. (r'<\s*[\w:.-]+', Name.Tag, 'tag'),
  207. (r'<\s*/\s*[\w:.-]+\s*>', Name.Tag),
  208. ],
  209. 'comment': [
  210. ('[^-]+', Comment),
  211. ('-->', Comment, '#pop'),
  212. ('-', Comment),
  213. ],
  214. 'tag': [
  215. (r'\s+', Whitespace),
  216. (r'[\w.:-]+\s*=', Name.Attribute, 'attr'),
  217. (r'/?\s*>', Name.Tag, '#pop'),
  218. ],
  219. 'attr': [
  220. (r'\s+', Whitespace),
  221. ('".*?"', String, '#pop'),
  222. ("'.*?'", String, '#pop'),
  223. (r'[^\s>]+', String, '#pop'),
  224. ],
  225. }