Skip to content

Commit

Permalink
Only regenerate parser if grammar has changed (#158)
Browse files Browse the repository at this point in the history
Previously, `slime_parser.erl` was regenerated on each compilation,
which casued any file depending on it to be recompiled, even if nothing
changed.

This continues to generate the grammar each time, but if it doesn't
differ from what's on disk, skip regenerating the parser.
  • Loading branch information
sionide21 authored and doomspork committed Oct 31, 2019
1 parent 83854d3 commit 9ea4a7d
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions tasks/compile.peg.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,27 @@ defmodule Mix.Tasks.Compile.Peg do
:slime, :attr_list_delims, %{"[" => "]", "(" => ")", "{" => "}"}
)
grammar = EEx.eval_file("src/slime_parser.peg.eex", attr_list_delims: attr_list_delims)
File.write!("src/slime_parser.peg", grammar)
peg = "src/slime_parser.peg" |> Path.expand |> String.to_charlist

if contents_changed?("src/slime_parser.peg", grammar) do
compile_grammar("src/slime_parser.peg", grammar)
else
:ok
end
end

defp contents_changed?(file, expected) do
case File.read(file) do
{:ok, contents} ->
contents != expected

_ ->
true
end
end

defp compile_grammar(file, grammar) do
File.write!(file, grammar)
peg = file |> Path.expand |> String.to_charlist
case :neotoma.file(peg, transform_module: :slime_parser_transform) do
:ok -> :ok
{:error, reason} ->
Expand Down

0 comments on commit 9ea4a7d

Please sign in to comment.