Sha256: 174946dc31981ffe555e42dd2ba4ff178dfb334fa574ec53b145940bf17622ee

Contents?: true

Size: 1.46 KB

Versions: 174

Compression:

Stored size: 1.46 KB

Contents

#!/usr/bin/env ruby

require 'fileutils'
require 'antlr4-native'

grammar_file = ARGV.shift

# ANTLR does weird things if the grammar file isn't in the current working directory
temp_grammar_file = File.join(FileUtils.pwd(), File.basename(grammar_file))
FileUtils.cp(grammar_file, temp_grammar_file)

# generate parser
generator = Antlr4Native::Generator.new(
  grammar_files: [File.basename(temp_grammar_file)],
  output_dir: 'ext',
  parser_root_method: 'syntax'
)
generator.generate

# fix issues with generated parser
parser_source_file = File.join(*%w(ext express-parser express_parser.cpp))
parser_source_lines = File.read(parser_source_file).split(/\n/)

# - add ParserProxy tokens method, simple compensation for missing exposed BufferedTokenStream
i = parser_source_lines.index{|x| x == '  Object syntax() {'}
parser_source_lines[i + 6] += "\n" + <<~CPP.split(/\n/).map{|x| x != "" ? "  " + x : x}.join("\n") + "\n"
  Array getTokens() {
    Array a;

    std::vector<Token*> tokens = this -> tokens -> getTokens();

    for (auto &token : tokens) {
      a.push(token);
    }

    return a;
  }
CPP
i = parser_source_lines.index{|x| x == '    .define_method("syntax", &ParserProxy::syntax)'}
parser_source_lines[i] += "\n" + <<~CPP.split(/\n/).map{|x| x != "" ? "    " + x : x}.join("\n")
  .define_method("tokens", &ParserProxy::getTokens)
CPP

# write fixed parser file
File.write(parser_source_file, parser_source_lines.join("\n") + "\n")

# cleanup
FileUtils.rm(temp_grammar_file)

Version data entries

174 entries across 174 versions & 1 rubygems

Version Path
expressir-1.2.4 exe/generate-parser
expressir-1.2.4-x86_64-linux exe/generate-parser
expressir-1.2.4-x86_64-darwin exe/generate-parser
expressir-1.2.4-x86-mingw32 exe/generate-parser
expressir-1.2.4-x86-linux exe/generate-parser
expressir-1.2.4-x64-mingw32 exe/generate-parser
expressir-1.2.4-arm64-darwin exe/generate-parser
expressir-1.2.4-aarch64-linux exe/generate-parser
expressir-1.2.3 exe/generate-parser
expressir-1.2.3-x86_64-linux exe/generate-parser
expressir-1.2.3-x86_64-darwin exe/generate-parser
expressir-1.2.3-x86-mingw32 exe/generate-parser
expressir-1.2.3-x86-linux exe/generate-parser
expressir-1.2.3-x64-mingw32 exe/generate-parser
expressir-1.2.3-arm64-darwin exe/generate-parser
expressir-1.2.3-aarch64-linux exe/generate-parser
expressir-1.2.1 exe/generate-parser
expressir-1.2.1-x86_64-linux exe/generate-parser
expressir-1.2.1-x86_64-darwin exe/generate-parser
expressir-1.2.1-x86-mingw32 exe/generate-parser