Sha256: 57a0c3b232dd9445044aaf18e78a0a0dfbb60641833efd13b9c708d9b35b6c23

Contents?: true

Size: 1.95 KB

Versions: 5

Compression:

Stored size: 1.95 KB

Contents

# -*- coding: utf-8 -*- #
# frozen_string_literal: true

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.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 File.join(__dir__, 'matlab/builtins.rb')
        self.builtins
      end

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

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


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


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

        rule %r/'(?=(.*'))/, Str::Single, :chararray
        rule %r/"(?=(.*"))/, Str::Double, :string
        rule %r/'/, Operator
      end

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

      state :string do
        rule %r/[^"]+/, Str::Double
        rule %r/""/, Str::Escape
        rule %r/"/, Str::Double, :pop!
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rouge-3.8.0 lib/rouge/lexers/matlab.rb
rouge-3.7.0 lib/rouge/lexers/matlab.rb
rouge-3.6.0 lib/rouge/lexers/matlab.rb
rouge-3.5.1 lib/rouge/lexers/matlab.rb
rouge-3.5.0 lib/rouge/lexers/matlab.rb