lib/rouge/lexers/python.rb in rouge-3.4.1 vs lib/rouge/lexers/python.rb in rouge-3.5.0

- old
+ new

@@ -10,11 +10,11 @@ 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)?)?/) + return true if text.shebang?(/pythonw?(?:[23](?:\.\d+)?)?/) end def self.keywords @keywords ||= %w( assert break continue del elif else except exec @@ -67,63 +67,63 @@ 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 + rule %r/\n+/m, Text + rule %r/^(:)(\s*)([ru]{,2}""".*?""")/mi do groups Punctuation, Text, Str::Doc end - rule /[^\S\n]+/, Text + rule %r/[^\S\n]+/, Text rule %r(#(.*)?\n?), Comment::Single - rule /[\[\]{}:(),;]/, Punctuation - rule /\\\n/, Text - rule /\\/, Text + rule %r/[\[\]{}:(),;]/, Punctuation + rule %r/\\\n/, Text + rule %r/\\/, Text - rule /(in|is|and|or|not)\b/, Operator::Word - rule /(<<|>>|\/\/|\*\*)=?/, Operator - rule /[-~+\/*%=<>&^|@]=?|!=/, Operator - rule /\.(?![0-9])/, Operator # so it doesn't match float literals + rule %r/(in|is|and|or|not)\b/, Operator::Word + rule %r/(<<|>>|\/\/|\*\*)=?/, Operator + rule %r/[-~+\/*%=<>&^|@]=?|!=/, Operator + rule %r/\.(?![0-9])/, Operator # so it doesn't match float literals - rule /(from)((?:\\\s|\s)+)(#{dotted_identifier})((?:\\\s|\s)+)(import)/ do + rule %r/(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 + rule %r/(import)(\s+)(#{dotted_identifier})/ do groups Keyword::Namespace, Text, Name::Namespace end - rule /(def)((?:\s|\\\s)+)/ do + rule %r/(def)((?:\s|\\\s)+)/ do groups Keyword, Text push :funcname end - rule /(class)((?:\s|\\\s)+)/ do + rule %r/(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 %r/`.*?`/, Str::Backtick + rule %r/(?:r|ur|ru)"""/i, Str, :raw_tdqs + rule %r/(?:r|ur|ru)'''/i, Str, :raw_tsqs + rule %r/(?:r|ur|ru)"/i, Str, :raw_dqs + rule %r/(?:r|ur|ru)'/i, Str, :raw_sqs + rule %r/u?"""/i, Str, :tdqs + rule %r/u?'''/i, Str, :tsqs + rule %r/u?"/i, Str, :dqs + rule %r/u?'/i, Str, :sqs - rule /@#{dotted_identifier}/i, Name::Decorator + rule %r/@#{dotted_identifier}/i, Name::Decorator # using negative lookbehind so we don't match property names - rule /(?<!\.)#{identifier}/ do |m| + rule %r/(?<!\.)#{identifier}/ do |m| if self.class.keywords.include? m[0] token Keyword elsif self.class.exceptions.include? m[0] token Name::Builtin elsif self.class.builtins.include? m[0] @@ -138,19 +138,19 @@ rule identifier, Name digits = /[0-9](_?[0-9])*/ decimal = /((#{digits})?\.#{digits}|#{digits}\.)/ exponent = /e[+-]?#{digits}/i - rule /#{decimal}(#{exponent})?j?/i, Num::Float - rule /#{digits}#{exponent}j?/i, Num::Float - rule /#{digits}j/i, Num::Float + rule %r/#{decimal}(#{exponent})?j?/i, Num::Float + rule %r/#{digits}#{exponent}j?/i, Num::Float + rule %r/#{digits}j/i, Num::Float - rule /0b(_?[0-1])+/i, Num::Bin - rule /0o(_?[0-7])+/i, Num::Oct - rule /0x(_?[a-f0-9])+/i, Num::Hex - rule /\d+L/, Num::Integer::Long - rule /([1-9](_?[0-9])*|0(_?0)*)/, Num::Integer + rule %r/0b(_?[0-1])+/i, Num::Bin + rule %r/0o(_?[0-7])+/i, Num::Oct + rule %r/0x(_?[a-f0-9])+/i, Num::Hex + rule %r/\d+L/, Num::Integer::Long + rule %r/([1-9](_?[0-9])*|0(_?0)*)/, Num::Integer end state :funcname do rule identifier, Name::Function, :pop! end @@ -158,79 +158,79 @@ state :classname do rule identifier, Name::Class, :pop! end state :raise do - rule /from\b/, Keyword - rule /raise\b/, Keyword - rule /yield\b/, Keyword - rule /\n/, Text, :pop! - rule /;/, Punctuation, :pop! + rule %r/from\b/, Keyword + rule %r/raise\b/, Keyword + rule %r/yield\b/, Keyword + rule %r/\n/, Text, :pop! + rule %r/;/, Punctuation, :pop! mixin :root end state :yield do mixin :raise end state :strings do - rule /%(\([a-z0-9_]+\))?[-#0 +]*([0-9]+|[*])?(\.([0-9]+|[*]))?/i, Str::Interpol + rule %r/%(\([a-z0-9_]+\))?[-#0 +]*([0-9]+|[*])?(\.([0-9]+|[*]))?/i, Str::Interpol end state :strings_double do - rule /[^\\"%\n]+/, Str + rule %r/[^\\"%\n]+/, Str mixin :strings end state :strings_single do - rule /[^\\'%\n]+/, Str + rule %r/[^\\'%\n]+/, Str mixin :strings end state :nl do - rule /\n/, Str + rule %r/\n/, Str end state :escape do rule %r(\\ ( [\\abfnrtv"'] | \n - | N{[a-zA-z][a-zA-Z ]+[a-zA-Z]} + | N{[a-zA-Z][a-zA-Z ]+[a-zA-Z]} | u[a-fA-F0-9]{4} | U[a-fA-F0-9]{8} | x[a-fA-F0-9]{2} | [0-7]{1,3} ) )x, Str::Escape end state :raw_escape do - rule /\\./, Str + rule %r/\\./, Str end state :dqs do - rule /"/, Str, :pop! + rule %r/"/, Str, :pop! mixin :escape mixin :strings_double end state :sqs do - rule /'/, Str, :pop! + rule %r/'/, Str, :pop! mixin :escape mixin :strings_single end state :tdqs do - rule /"""/, Str, :pop! - rule /"/, Str + rule %r/"""/, Str, :pop! + rule %r/"/, Str mixin :escape mixin :strings_double mixin :nl end state :tsqs do - rule /'''/, Str, :pop! - rule /'/, Str + rule %r/'''/, Str, :pop! + rule %r/'/, Str mixin :escape mixin :strings_single mixin :nl end