lib/rouge/lexers/ruby.rb in rouge-0.4.0 vs lib/rouge/lexers/ruby.rb in rouge-0.5.0

- old
+ new

@@ -18,45 +18,45 @@ # symbols rule %r( : # initial : @{0,2} # optional ivar, for :@foo and :@@foo [a-z_]\w*[!?]? # the symbol - )xi, 'Literal.String.Symbol' + )xi, Str::Symbol # special symbols rule %r(:(?:\*\*|[-+]@|[/\%&\|^`~]|\[\]=?|<<|>>|<=?>|<=?|===?)), - 'Literal.String.Symbol' + Str::Symbol - rule /:'(\\\\|\\'|[^'])*'/, 'Literal.String.Symbol' - rule /\b[a-z_]\w*?:\s+/, 'Literal.String.Symbol' - rule /'(\\\\|\\'|[^'])*'/, 'Literal.String.Single' - rule /:"/, 'Literal.String.Symbol', :simple_sym - rule /"/, 'Literal.String.Double', :simple_string - rule /(?<!\.)`/, 'Literal.String.Backtick', :simple_backtick + rule /:'(\\\\|\\'|[^'])*'/, Str::Symbol + rule /\b[a-z_]\w*?:\s+/, Str::Symbol + rule /'(\\\\|\\'|[^'])*'/, Str::Single + rule /:"/, Str::Symbol, :simple_sym + rule /"/, Str::Double, :simple_string + rule /(?<!\.)`/, Str::Backtick, :simple_backtick # %-style delimiters # %(abc), %[abc], %<abc>, %.abc., %r.abc., etc delimiter_map = { '{' => '}', '[' => ']', '(' => ')', '<' => '>' } rule /%([rqswQWxiI])?([^\w\s])/ do |m| open = Regexp.escape(m[2]) close = Regexp.escape(delimiter_map[m[2]] || m[2]) interp = /[rQWxI]/ === m[1] - toktype = 'Literal.String.Other' + toktype = Str::Other debug { " open: #{open.inspect}" } debug { " close: #{close.inspect}" } # regexes if m[1] == 'r' - toktype = 'Literal.String.Regex' + toktype = Str::Regex push :regex_flags end token toktype push do - rule /\\[##{open}#{close}\\]/, 'Literal.String.Escape' + rule /\\[##{open}#{close}\\]/, Str::Escape # nesting rules only with asymmetric delimiters if open != close rule /#{open}/ do token toktype push @@ -75,17 +75,17 @@ end end end state :regex_flags do - rule /[mixounse]*/, 'Literal.String.Regex', :pop! + rule /[mixounse]*/, Str::Regex, :pop! end # double-quoted string and symbol - [[:string, 'Literal.String.Double', '"'], - [:sym, 'Literal.String.Symbol', '"'], - [:backtick, 'Literal.String.Backtick', '`']].each do |name, tok, fin| + [[:string, Str::Double, '"'], + [:sym, Str::Symbol, '"'], + [:backtick, Str::Backtick, '`']].each do |name, tok, fin| state :"simple_#{name}" do mixin :string_intp_escaped rule /[^\\#{fin}#]+/m, tok rule /[\\#]/, tok rule /#{fin}/, tok, :pop! @@ -103,11 +103,11 @@ attr_accessor attr catch throw private module_function public protected true false nil __FILE__ __LINE__ ) builtins_g = %w( - Array Float Integer String __id__ __send__ abort ancestors + Array Float Integer Str __id__ __send__ abort ancestors at_exit autoload binding callcc caller catch chomp chop class_eval class_variables clone const_defined\? const_get const_missing const_set constants display dup eval exec exit extend fail fork format freeze getc gets global_variables gsub hash id included_modules inspect instance_eval instance_method @@ -136,93 +136,98 @@ push :expr_start @heredoc_queue = [] end state :root do - rule /\n\s*/m, 'Text', :expr_start - rule /\s+/, 'Text' # NB: NOT /m - rule /#.*$/, 'Comment.Single' + rule /\n\s*/m, Text, :expr_start + rule /\s+/, Text # NB: NOT /m + rule /#.*$/, Comment::Single - rule %r(=begin\b.*?end\b)m, 'Comment.Multiline' - rule /(?:#{keywords.join('|')})\b/, 'Keyword', :expr_start - rule /(?:#{keywords_pseudo.join('|')})\b/, 'Keyword.Pseudo', :expr_start + rule %r(=begin\b.*?end\b)m, Comment::Multiline + rule /(?:#{keywords.join('|')})\b/, Keyword, :expr_start + rule /(?:#{keywords_pseudo.join('|')})\b/, Keyword::Pseudo, :expr_start rule %r( (module) (\s+) ([a-zA-Z_][a-zA-Z0-9_]*(::[a-zA-Z_][a-zA-Z0-9_]*)*) )x do - group 'Keyword' - group 'Text' - group 'Name.Namespace' + groups Keyword, Text, Name::Namespace end - rule /def\s+/, 'Keyword', :funcname - rule /class\s+/, 'Keyword', :classname + rule /(def\b)(\s*)/ do + groups Keyword, Text + push :funcname + end - rule /(?:#{builtins_q.join('|')})\?/, 'Name.Builtin', :expr_start - rule /(?:#{builtins_b.join('|')})!/, 'Name.Builtin', :expr_start + rule /(class\b)(\s*)/ do + groups Keyword, Text + push :classname + end + + rule /(?:#{builtins_q.join('|')})\?/, Name::Builtin, :expr_start + rule /(?:#{builtins_b.join('|')})!/, Name::Builtin, :expr_start rule /(?<!\.)(?:#{builtins_g.join('|')})\b/, - 'Name.Builtin', :method_call + Name::Builtin, :method_call - rule /__END__/, 'Comment.Preproc', :end_part + rule /__END__/, Comment::Preproc, :end_part - rule /0_?[0-7]+(?:_[0-7]+)*/, 'Literal.Number.Oct' - rule /0x[0-9A-Fa-f]+(?:_[0-9A-Fa-f]+)*/, 'Literal.Number.Hex' - rule /0b[01]+(?:_[01]+)*/, 'Literal.Number.Bin' - rule /[\d]+(?:_\d+)*/, 'Literal.Number.Integer' + rule /0_?[0-7]+(?:_[0-7]+)*/, Num::Oct + rule /0x[0-9A-Fa-f]+(?:_[0-9A-Fa-f]+)*/, Num::Hex + rule /0b[01]+(?:_[01]+)*/, Num::Bin + rule /[\d]+(?:_\d+)*/, Num::Integer # names - rule /@@[a-z_]\w*/i, 'Name.Variable.Class' - rule /@[a-z_]\w*/i, 'Name.Variable.Instance' - rule /\$\w+/, 'Name.Variable.Global' - rule %r(\$[!@&`'+~=/\\,;.<>_*\$?:"]), 'Name.Variable.Global' - rule /\$-[0adFiIlpvw]/, 'Name.Variable.Global' - rule /::/, 'Operator' + rule /@@[a-z_]\w*/i, Name::Variable::Class + rule /@[a-z_]\w*/i, Name::Variable::Instance + rule /\$\w+/, Name::Variable::Global + rule %r(\$[!@&`'+~=/\\,;.<>_*\$?:"]), Name::Variable::Global + rule /\$-[0adFiIlpvw]/, Name::Variable::Global + rule /::/, Operator mixin :strings # char operator. ?x evaulates to "x", unless there's a digit # beforehand like x>=0?n[x]:"" rule %r( \?(\\[MC]-)* # modifiers (\\([\\abefnrstv\#"']|x[a-fA-F0-9]{1,2}|[0-7]{1,3})|\S) (?!\w) - )x, 'Literal.String.Char' + )x, Str::Char mixin :has_heredocs - rule /[A-Z][a-zA-Z0-9_]+/, 'Name.Constant', :method_call + rule /[A-Z][a-zA-Z0-9_]+/, Name::Constant, :method_call rule /(\.|::)([a-z_]\w*[!?]?|[*%&^`~+-\/\[<>=])/, - 'Name.Function', :expr_start - rule /[a-zA-Z_]\w*[?!]/, 'Name', :expr_start - rule /[a-zA-Z_]\w*/, 'Name', :method_call + Name::Function, :expr_start + rule /[a-zA-Z_]\w*[?!]/, Name, :expr_start + rule /[a-zA-Z_]\w*/, Name, :method_call rule /\[|\]|\*\*|<<?|>>?|>=|<=|<=>|=~|={3}|!~|&&?|\|\||\.{1,3}/, - 'Operator', :expr_start - rule /[-+\/*%=<>&!^|~]=?/, 'Operator', :expr_start - rule %r<[({,?:\\;/]>, 'Punctuation', :expr_start - rule %r<[)}]>, 'Punctuation' + Operator, :expr_start + rule /[-+\/*%=<>&!^|~]=?/, Operator, :expr_start + rule %r<[({,?:\\;/]>, Punctuation, :expr_start + rule %r<[)}]>, Punctuation end state :has_heredocs do rule /(?<!\w)(<<-?)(["`']?)([a-zA-Z_]\w*)(\2)/ do |m| - token 'Operator', m[1] - token 'Name.Constant', "#{m[2]}#{m[3]}#{m[4]}" + token Operator, m[1] + token Name::Constant, "#{m[2]}#{m[3]}#{m[4]}" @heredoc_queue << [m[1] == '<<-', m[3]] push :heredoc_queue unless state? :heredoc_queue end rule /(<<-?)(["'])(\2)/ do |m| - token 'Operator', m[1] - token 'Name.Constant', "#{m[2]}#{m[3]}#{m[4]}" + token Operator, m[1] + token Name::Constant, "#{m[2]}#{m[3]}#{m[4]}" @heredoc_queue << [m[1] == '<<-', ''] push :heredoc_queue unless state? :heredoc_queue end end state :heredoc_queue do rule /(?=\n)/ do - pop!; push :resolve_heredocs + goto :resolve_heredocs end mixin :root end @@ -231,132 +236,124 @@ rule /(\n)([^#\\\n]*)$/ do |m| tolerant, heredoc_name = @heredoc_queue.first check = tolerant ? m[2].strip : m[2].rstrip - group 'Literal.String.Heredoc' + group Str::Heredoc # check if we found the end of the heredoc if check == heredoc_name - group 'Name.Constant' + group Name::Constant @heredoc_queue.shift # if there's no more, we're done looking. pop! if @heredoc_queue.empty? else - group 'Literal.String.Heredoc' + group Str::Heredoc end end - rule /[#\\\n]/, 'Literal.String.Heredoc' - rule /[^#\\\n]+/, 'Literal.String.Heredoc' + rule /[#\\\n]/, Str::Heredoc + rule /[^#\\\n]+/, Str::Heredoc end state :funcname do - rule /\s+/, 'Text' - rule /\(/, 'Punctuation', :defexpr + rule /\s+/, Text + rule /\(/, Punctuation, :defexpr rule %r( (?:([a-zA-Z_][\w_]*)(\.))? ( [a-zA-Z_][\w_]*[!?]? | \*\*? | [-+]@? | [/%&\|^`~] | \[\]=? | - << | >> | <=?> | >=? | ===? + <<? | >>? | <=>? | >= | ===? ) )x do |m| debug { "matches: #{[m[0], m[1], m[2], m[3]].inspect}" } - group 'Name.Class' - group 'Operator' - group 'Name.Function' + groups Name::Class, Operator, Name::Function pop! end rule(//) { pop! } end state :classname do - rule /\s+/, 'Text' - rule /\(/, 'Punctuation', :defexpr + rule /\s+/, Text + rule /\(/, Punctuation, :defexpr # class << expr - rule /<</, 'Operator', :pop! - rule /[A-Z_]\w*/, 'Name.Class' + rule /<</, Operator, :pop! + rule /[A-Z_]\w*/, Name::Class rule(//) { pop! } end state :defexpr do rule /(\))(\.|::)?/ do - group 'Punctuation' - group 'Operator' + groups Punctuation, Operator pop! end - rule /\(/, 'Operator', :defexpr + rule /\(/, Operator, :defexpr mixin :root end state :in_interp do - rule /}/, 'Literal.String.Interpol', :pop! + rule /}/, Str::Interpol, :pop! mixin :root end state :string_intp do - rule /\#{/, 'Literal.String.Interpol', :in_interp - rule /#(@@?|\$)[a-z_]\w*/i, 'Literal.String.Interpol' + rule /\#{/, Str::Interpol, :in_interp + rule /#(@@?|\$)[a-z_]\w*/i, Str::Interpol end state :string_intp_escaped do mixin :string_intp rule /\\([\\abefnrstv#"']|x[a-fA-F0-9]{1,2}|[0-7]{1,3})/, - 'Literal.String.Escape' - rule /\\./, 'Literal.String.Escape' + Str::Escape + rule /\\./, Str::Escape end state :method_call do rule %r((\s+)(/)(?=\S|\s*/)) do - group 'Text' - group 'Literal.String.Regex' - pop! - push :slash_regex + groups Text, Str::Regex + goto :slash_regex end rule(%r((?=\s*/))) { pop! } - rule(//) { pop!; push :expr_start } + rule(//) { goto :expr_start } end state :expr_start do rule %r((\s*)(/)) do - group 'Text' - group 'Literal.String.Regex' - pop! - push :slash_regex + groups Text, Str::Regex + goto :slash_regex end # special case for using a single space. Ruby demands that # these be in a single line, otherwise it would make no sense. rule /(\s*)(%[rqswQWxiI]? \S* )/ do - group 'Text' - group 'Literal.String.Other' + groups Text, Str::Other pop! end rule(//) { pop! } end state :slash_regex do mixin :string_intp - rule %r(\\\\), 'Literal.String.Regex' - rule %r(\\/), 'Literal.String.Regex' - rule %r([\\#]), 'Literal.String.Regex' - rule %r([^\\/#]+)m, 'Literal.String.Regex' + rule %r(\\\\), Str::Regex + rule %r(\\/), Str::Regex + rule %r([\\#]), Str::Regex + rule %r([^\\/#]+)m, Str::Regex rule %r(/) do - token 'Literal.String.Regex' - pop!; push :regex_flags + token Str::Regex + goto :regex_flags end end state :end_part do - # eat up the rest of the stream as Comment.Preproc - rule /.+/m, 'Comment.Preproc', :pop! + # eat up the rest of the stream as Comment::Preproc + rule /.+/m, Comment::Preproc, :pop! end end end end