actionscript.py 11 KB

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