Sha256: ca143066686ab0d1fe8de9bc74e6f718af1c84621719c48e938e6c611fc8f735

Contents?: true

Size: 1.48 KB

Versions: 82

Compression:

Stored size: 1.48 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("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] += <<~CPP.split("\n").map { |x| x == "" ? x : "  #{x}" }.join("\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, Return().keepAlive())' }
parser_source_lines[i] += <<~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

82 entries across 82 versions & 1 rubygems

Version Path
expressir-1.2.5-arm64-darwin exe/generate-parser
expressir-1.2.5-aarch64-linux exe/generate-parser