lib/tasks/antlr4.rake
def embed_grammar(grammar)
    require 'parse_tree_visualizer/popen'

    grammar_dir = Rails.root.join('lib', 'antlr4', 'grammars', grammar).to_s
    lexer = ::ParseTreeVisualizer::Popen.popen(%W(find . -name *Lexer.cs), grammar_dir)
    parser = ::ParseTreeVisualizer::Popen.popen(%W(find . -name *Parser.cs), grammar_dir)

    if lexer.blank? or parser.blank? or lexer[0].blank? or parser[0].blank?
      raise 'Error: Lexer or Parser cs file not found'
    end

    lexer = lexer[0].gsub(/\A\.\//, '')
    parser = parser[0].gsub(/\A\.\//, '')

    lexer = lexer[0, lexer.length - '.cs'.length - 1]
    parser = parser[0, parser.length - '.cs'.length - 1]

    modifying_src = File.read(ORG_PROGRAM_CS_PATH)
    modifying_src.gsub!(/CUSTOM_LEXER/, lexer)
    modifying_src.gsub!(/CUSTOM_PARSER/, parser)

    File.open(PROGRAM_CS_PATH, 'w') do |out|
      out << modifying_src
    end
  end
# Replace lexer/parser type string to specified one
Replace lexer/parser type string to specified one
Replace lexer / parser type string to specified one
def embed_grammar(grammar)
    require 'parse_tree_visualizer/popen'
    grammar_dir = Rails.root.join('lib', 'antlr4', 'grammars', grammar).to_s
    lexer = ::ParseTreeVisualizer::Popen.popen(%W(find . -name *Lexer.cs), grammar_dir)
    parser = ::ParseTreeVisualizer::Popen.popen(%W(find . -name *Parser.cs), grammar_dir)
    if lexer.blank? or parser.blank? or lexer[0].blank? or parser[0].blank?
      raise 'Error: Lexer or Parser cs file not found'
    end
    lexer = lexer[0].gsub(/\A\.\//, '')
    parser = parser[0].gsub(/\A\.\//, '')
    lexer = lexer[0, lexer.length - '.cs'.length - 1]
    parser = parser[0, parser.length - '.cs'.length - 1]
    modifying_src = File.read(ORG_PROGRAM_CS_PATH)
    modifying_src.gsub!(/CUSTOM_LEXER/, lexer)
    modifying_src.gsub!(/CUSTOM_PARSER/, parser)
    File.open(PROGRAM_CS_PATH, 'w') do |out|
      out << modifying_src
    end
  end
def embed_grammar ( grammar ) require 'parse_tree_visualizer/popen' grammar_dir = Rails . root . join ( 'lib' , 'antlr4' , 'grammars' , grammar ) . to_s lexer = :: ParseTreeVisualizer :: Popen . popen ( %W( find . -name *Lexer.cs ) , grammar_dir ) parser = :: ParseTreeVisualizer :: Popen . popen ( %W( find . -name *Parser.cs ) , grammar_dir ) if lexer . blank? or parser . blank? or lexer [ 0 ] . blank? or parser [ 0 ] . blank? raise 'Error: Lexer or Parser cs file not found' end lexer = lexer [ 0 ] . gsub ( / \A \. \/ / , '' ) parser = parser [ 0 ] . gsub ( / \A \. \/ / , '' ) lexer = lexer [ 0 , lexer . length - '.cs' . length - 1 ] parser = parser [ 0 , parser . length - '.cs' . length - 1 ] modifying_src = File . read ( ORG_PROGRAM_CS_PATH ) modifying_src . gsub! ( / CUSTOM_LEXER / , lexer ) modifying_src . gsub! ( / CUSTOM_PARSER / , parser ) File . open ( PROGRAM_CS_PATH , 'w' ) do | out | out << modifying_src end end
Replace lexer/parser type string to specified one
Replace lexer / parser type string to specified one
grammar