Sha256: 82b45ae8f3445c83f13ed002044945323628ad866fbd08206f35388799b78a92

Contents?: true

Size: 1.84 KB

Versions: 15

Compression:

Stored size: 1.84 KB

Contents

# -*- coding: utf-8 -*- #

module Rouge
  module Lexers
    class Matlab < RegexLexer
      title "MATLAB"
      desc "Matlab"
      tag 'matlab'
      aliases 'm'
      filenames '*.m'
      mimetypes 'text/x-matlab', 'application/x-matlab'

      def self.analyze_text(text)
        return 0.4 if text.match(/^\s*% /) # % comments are a dead giveaway
      end

      def self.keywords
        @keywords = Set.new %w(
          break case catch classdef continue else elseif end for function
          global if otherwise parfor persistent return spmd switch try while
        )
      end

      def self.builtins
        load Pathname.new(__FILE__).dirname.join('matlab/builtins.rb')
        self.builtins
      end

      state :root do
        rule /\s+/m, Text # Whitespace
        rule %r([{]%.*?%[}])m, Comment::Multiline
        rule /%.*$/, Comment::Single
        rule /([.][.][.])(.*?)$/ do
          groups(Keyword, Comment)
        end

        rule /^(!)(.*?)(?=%|$)/ do |m|
          token Keyword, m[1]
          delegate Shell, m[2]
        end


        rule /[a-zA-Z][_a-zA-Z0-9]*/m do |m|
          match = m[0]
          if self.class.keywords.include? match
            token Keyword
          elsif self.class.builtins.include? match
            token Name::Builtin
          else
            token Name
          end
        end

        rule %r{[(){};:,\/\\\]\[]}, Punctuation

        rule /~=|==|<<|>>|[-~+\/*%=<>&^|.@]/, Operator


        rule /(\d+\.\d*|\d*\.\d+)(e[+-]?[0-9]+)?/i, Num::Float
        rule /\d+e[+-]?[0-9]+/i, Num::Float
        rule /\d+L/, Num::Integer::Long
        rule /\d+/, Num::Integer

        rule /'(?=(.*'))/, Str::Single, :string
        rule /'/, Operator
      end

      state :string do
        rule /[^']+/, Str::Single
        rule /''/, Str::Escape
        rule /'/, Str::Single, :pop!
      end
    end
  end
end

Version data entries

15 entries across 14 versions & 2 rubygems

Version Path
tdiary-5.0.5 vendor/bundle/gems/tdiary-5.0.4/vendor/bundle/gems/rouge-2.0.7/lib/rouge/lexers/matlab.rb
tdiary-5.0.5 vendor/bundle/gems/rouge-2.0.7/lib/rouge/lexers/matlab.rb
rouge-2.1.0 lib/rouge/lexers/matlab.rb
tdiary-5.0.4 vendor/bundle/gems/rouge-2.0.7/lib/rouge/lexers/matlab.rb
rouge-2.0.7 lib/rouge/lexers/matlab.rb
rouge-2.0.6 lib/rouge/lexers/matlab.rb
rouge-2.0.5 lib/rouge/lexers/matlab.rb
rouge-2.0.3 lib/rouge/lexers/matlab.rb
rouge-2.0.2 lib/rouge/lexers/matlab.rb
rouge-2.0.1 lib/rouge/lexers/matlab.rb
rouge-2.0.0 lib/rouge/lexers/matlab.rb
rouge-1.11.1 lib/rouge/lexers/matlab.rb
rouge-1.11.0 lib/rouge/lexers/matlab.rb
rouge-1.10.1 lib/rouge/lexers/matlab.rb
rouge-1.10.0 lib/rouge/lexers/matlab.rb