# -*- coding: utf-8 -*- # module Rouge module Lexers class Python < RegexLexer title "Python" desc "The Python programming language (python.org)" tag 'python' aliases 'py' filenames '*.py', '*.pyw', '*.sc', 'SConstruct', 'SConscript', '*.tac' mimetypes 'text/x-python', 'application/x-python' def self.detect?(text) return true if text.shebang?(/pythonw?(3|2(\.\d)?)?/) end def self.keywords @keywords ||= %w( assert break continue del elif else except exec finally for global if lambda pass print raise return try while yield as with from import yield ) end def self.builtins @builtins ||= %w( __import__ abs all any apply basestring bin bool buffer bytearray bytes callable chr classmethod cmp coerce compile complex delattr dict dir divmod enumerate eval execfile exit file filter float frozenset getattr globals hasattr hash hex id input int intern isinstance issubclass iter len list locals long map max min next object oct open ord pow property range raw_input reduce reload repr reversed round set setattr slice sorted staticmethod str sum super tuple type unichr unicode vars xrange zip ) end def self.builtins_pseudo @builtins_pseudo ||= %w(self None Ellipsis NotImplemented False True) end def self.exceptions @exceptions ||= %w( ArithmeticError AssertionError AttributeError BaseException DeprecationWarning EOFError EnvironmentError Exception FloatingPointError FutureWarning GeneratorExit IOError ImportError ImportWarning IndentationError IndexError KeyError KeyboardInterrupt LookupError MemoryError NameError NotImplemented NotImplementedError OSError OverflowError OverflowWarning PendingDeprecationWarning ReferenceError RuntimeError RuntimeWarning StandardError StopIteration SyntaxError SyntaxWarning SystemError SystemExit TabError TypeError UnboundLocalError UnicodeDecodeError UnicodeEncodeError UnicodeError UnicodeTranslateError UnicodeWarning UserWarning ValueError VMSError Warning WindowsError ZeroDivisionError ) end identifier = /[a-z_][a-z0-9_]*/i dotted_identifier = /[a-z_.][a-z0-9_.]*/i state :root do rule /\n+/m, Text rule /^(:)(\s*)([ru]{,2}""".*?""")/mi do groups Punctuation, Text, Str::Doc end rule /[^\S\n]+/, Text rule /#.*$/, Comment rule /[\[\]{}:(),;]/, Punctuation rule /\\\n/, Text rule /\\/, Text rule /(in|is|and|or|not)\b/, Operator::Word rule /!=|==|<<|>>|[-~+\/*%=<>&^|.]/, Operator rule /(from)((?:\\\s|\s)+)(#{dotted_identifier})((?:\\\s|\s)+)(import)/ do groups Keyword::Namespace, Text, Name::Namespace, Text, Keyword::Namespace end rule /(import)(\s+)(#{dotted_identifier})/ do groups Keyword::Namespace, Text, Name::Namespace end rule /(def)((?:\s|\\\s)+)/ do groups Keyword, Text push :funcname end rule /(class)((?:\s|\\\s)+)/ do groups Keyword, Text push :classname end # TODO: not in python 3 rule /`.*?`/, Str::Backtick rule /(?:r|ur|ru)"""/i, Str, :raw_tdqs rule /(?:r|ur|ru)'''/i, Str, :raw_tsqs rule /(?:r|ur|ru)"/i, Str, :raw_dqs rule /(?:r|ur|ru)'/i, Str, :raw_sqs rule /u?"""/i, Str, :tdqs rule /u?'''/i, Str, :tsqs rule /u?"/i, Str, :dqs rule /u?'/i, Str, :sqs rule /@#{dotted_identifier}/i, Name::Decorator # using negative lookbehind so we don't match property names rule /(?