diff --git a/lib/voxpop/non_terminal.ex b/lib/voxpop/non_terminal.ex index 8e7d2fb..1f26960 100644 --- a/lib/voxpop/non_terminal.ex +++ b/lib/voxpop/non_terminal.ex @@ -5,6 +5,12 @@ end defimpl Voxpop.Production, for: Voxpop.NonTerminal do def evaluate(non_terminal, registry) do - registry.rules |> Map.get(non_terminal.rule) |> Voxpop.Production.evaluate(registry) + value = Map.get(registry.rules, non_terminal.rule) + + if value do + Voxpop.Production.evaluate(value, registry) + else + raise "Invalid rule: `#{non_terminal.rule}`" + end end end diff --git a/mix.exs b/mix.exs index 29d1c6e..50ba596 100644 --- a/mix.exs +++ b/mix.exs @@ -7,9 +7,9 @@ defmodule Voxpop.Mixfile do elixir: "~> 1.2", build_embedded: Mix.env == :prod, start_permanent: Mix.env == :prod, - deps: deps, - package: package, - description: description + deps: deps(), + package: package(), + description: description() ] end diff --git a/test/voxpop_test.exs b/test/voxpop_test.exs index 9815702..c887478 100644 --- a/test/voxpop_test.exs +++ b/test/voxpop_test.exs @@ -21,4 +21,11 @@ defmodule VoxpopTest do concat_grammar = %Voxpop.Grammar.Definition{start: "{greeting} world", rules: %{greeting: "Hello"}} assert Voxpop.generate(concat_grammar) == "Hello world" end + + test "gracefully handles missing rules" do + concat_grammar = %Voxpop.Grammar.Definition{start: "{greeting} world", rules: %{greeting: "Hello {invalid}"}} + assert_raise RuntimeError, "Invalid rule: `invalid`", fn -> + Voxpop.generate(concat_grammar) + end + end end