class Lemon < Formula
  desc "LALR(1) parser generator like yacc or bison"
  homepage "https://www.hwaci.com/sw/lemon/"
  url "https://tx97.net/pub/distfiles/lemon-1.69.tar.bz2"
  sha256 "bc7c1cae233b6af48f4b436ee900843106a15bdb1dc810bc463d8c6aad0dd916"

  bottle do
    sha256 "ba917fc3f6df28ea62f91bdd45cbe4c43dd8148c2573fc263cd1bcfcb7915a6e" => :mojave
    sha256 "b01253f24f206fb1a1e76656ec4c9f43d3132d8dcaaa60708d1478e0a3546a12" => :high_sierra
    sha256 "54fc18e48b3e9bfa4e14317158e3aef097d075bc3819d6424cb5b48a5845a7d8" => :sierra
    sha256 "9016fa354fa9f34abf2db3cdca2e13a6dd6223534ff9ea08001ddaff053fe446" => :el_capitan
    sha256 "e9b8328c8d905424be43404911bff1296c16fbdd83ecfeab7b51917f31c81ab7" => :yosemite
    sha256 "a8120db2de1708f3ecd4bddc5775f90cf5c39a55010a90d01b8cd5f58325560e" => :mavericks
    sha256 "dd245cd856b28f4d14a3f34e243b29b032becf0809208db66bb4c550e4789a83" => :mountain_lion
  end

  def install
    pkgshare.install "lempar.c"

    # patch the parser generator to look for the 'lempar.c' template file where we've installed it
    inreplace "lemon.c", / = pathsearch\([^)]*\);/, " = \"#{pkgshare}/lempar.c\";"

    system ENV.cc, "-o", "lemon", "lemon.c"
    bin.install "lemon"
  end

  test do
    (testpath/"gram.y").write <<~EOS
      %token_type {int}
      %left PLUS.
      %include {
        #include <iostream>
        #include "example1.h"
      }
      %syntax_error {
        std::cout << "Syntax error!" << std::endl;
      }
      program ::= expr(A).   { std::cout << "Result=" << A << std::endl; }
      expr(A) ::= expr(B) PLUS  expr(C).   { A = B + C; }
      expr(A) ::= INTEGER(B). { A = B; }
    EOS

    system "#{bin}/lemon", "gram.y"
    assert_predicate testpath/"gram.c", :exist?
  end
end
