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

require 'yaml'
require_relative "../task_helper"

# YAML file is made by manually copying keywords from https://www.mathworks.com/help/matlab/referencelist.html?type=function&listtype=alpha&category=index&blocktype=&capability=
MATLAB_KEYWORDS_FILE = "./lib/rouge/lexers/matlab/keywords.rb"

namespace :builtins do
  task :matlab do
    MatlabBuiltins.process(MATLAB_KEYWORDS_FILE)
  end
end

class MatlabBuiltins < BuiltinsGenerator
  MATLAB_SYNTAX_FILE = File.join(__dir__, "matlab/builtins.yml")

  def fetch
    File.read(MATLAB_SYNTAX_FILE)
  end

  def parse
    ::YAML.load(@input).sort.uniq
  end

  def generate
    yield   "# -*- coding: utf-8 -*- #"
    yield   "# frozen_string_literal: true"
    yield   ""
    yield   "# DO NOT EDIT"
    yield   ""
    yield   "# This file is automatically generated by `rake builtins:matlab`."
    yield   "# See tasks/builtins/matlab.rake for more info."
    yield   ""
    yield   "module Rouge"
    yield   "  module Lexers"
    yield   "    class Matlab"
    yield   "      BUILTINS = Set.new #{@keywords.inspect}"
    yield   "    end"
    yield   "  end"
    yield   "end"
  end
end
