-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
117 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
defmodule Joy.Parsec do | ||
import NimbleParsec | ||
|
||
func = ascii_string([?0..?9, ?a..?z, ?!, ?^, ?%, ?*, ?+, ?-, ?/, ?<..??], min: 1) | ||
|
||
whitespace = repeat(ascii_char([?\n, ?\t, ?\s])) | ||
|
||
quotation = | ||
ignore(string("[")) | ||
|> ignore(whitespace) | ||
|> repeat(parsec(:element)) | ||
|> ignore(string("]")) | ||
|> wrap() | ||
|
||
defcombinatorp( | ||
:element, | ||
choice([func, quotation]) |> ignore(whitespace), | ||
inline: true, | ||
export_metadata: true | ||
) | ||
|
||
defparsec(:parse!, ignore(whitespace) |> repeat(parsec(:element)), inline: true, export_metadata: true) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
%{ | ||
"eliver": {:hex, :eliver, "2.0.3", "ddfb3a9e77028298dd8cb9d91bfe16a39db67a1f43830bb8f1c3ad085a9c96fe", [:mix], [{:enquirer, "~> 0.1.0", [hex: :enquirer, repo: "hexpm", optional: false]}], "hexpm", "bd2434db0bb850f2b304e77b29565646411235fadff89ad023787e6f52ce1e87"}, | ||
"enquirer": {:hex, :enquirer, "0.1.0", "96ca1a330c919652db4baed6cf572d8c9957a8a305365c54490262c337996a18", [:mix], [], "hexpm", "59445208ad96171f10b2d21bc35032f6f7a49aa4cfd5d13d18f93730c49140bb"}, | ||
"nimble_parsec": {:hex, :nimble_parsec, "1.2.0", "b44d75e2a6542dcb6acf5d71c32c74ca88960421b6874777f79153bbbbd7dccc", [:mix], [], "hexpm", "52b2871a7515a5ac49b00f214e4165a40724cf99798d8e4a65e4fd64ebd002c1"}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
defmodule Joy.ParsecTest do | ||
alias Joy.Parsec | ||
use ExUnit.Case | ||
|
||
test "parses various scenarios successfully" do | ||
# {:ok, acc, rest, context, line, offset} | ||
assert Parsec.parse!("") == {:ok, [], "", %{}, {1, 0}, 0} | ||
assert Parsec.parse!(" ") == {:ok, [], "", %{}, {1, 0}, 1} | ||
assert Parsec.parse!(" ") == {:ok, [], "", %{}, {1, 0}, 3} | ||
assert Parsec.parse!("[]") == {:ok, [[]], "", %{}, {1, 0}, 2} | ||
assert Parsec.parse!(" []") == {:ok, [[]], "", %{}, {1, 0}, 3} | ||
assert Parsec.parse!("[] ") == {:ok, [[]], "", %{}, {1, 0}, 3} | ||
assert Parsec.parse!(" [] ") == {:ok, [[]], "", %{}, {1, 0}, 4} | ||
assert Parsec.parse!("[ ]") == {:ok, [[]], "", %{}, {1, 0}, 3} | ||
assert Parsec.parse!("[ ]") == {:ok, [[]], "", %{}, {1, 0}, 4} | ||
assert Parsec.parse!("[ ]") == {:ok, [[]], "", %{}, {1, 0}, 5} | ||
assert Parsec.parse!("a") == {:ok, ["a"], "", %{}, {1, 0}, 1} | ||
assert Parsec.parse!(" a") == {:ok, ["a"], "", %{}, {1, 0}, 2} | ||
assert Parsec.parse!("a ") == {:ok, ["a"], "", %{}, {1, 0}, 2} | ||
assert Parsec.parse!(" a ") == {:ok, ["a"], "", %{}, {1, 0}, 3} | ||
assert Parsec.parse!(" a ") == {:ok, ["a"], "", %{}, {1, 0}, 4} | ||
assert Parsec.parse!(" a") == {:ok, ["a"], "", %{}, {1, 0}, 4} | ||
assert Parsec.parse!("[ a]") == {:ok, [["a"]], "", %{}, {1, 0}, 4} | ||
assert Parsec.parse!("[a ]") == {:ok, [["a"]], "", %{}, {1, 0}, 6} | ||
assert Parsec.parse!("[ a ]") == {:ok, [["a"]], "", %{}, {1, 0}, 6} | ||
assert Parsec.parse!("[ a ]") == {:ok, [["a"]], "", %{}, {1, 0}, 6} | ||
assert Parsec.parse!("[ a]") == {:ok, [["a"]], "", %{}, {1, 0}, 6} | ||
assert Parsec.parse!(" [a]") == {:ok, [["a"]], "", %{}, {1, 0}, 4} | ||
assert Parsec.parse!("[a] ") == {:ok, [["a"]], "", %{}, {1, 0}, 4} | ||
assert Parsec.parse!(" [a] ") == {:ok, [["a"]], "", %{}, {1, 0}, 5} | ||
assert Parsec.parse!(" [ a ]") == {:ok, [["a"]], "", %{}, {1, 0}, 6} | ||
assert Parsec.parse!("[ a ] ") == {:ok, [["a"]], "", %{}, {1, 0}, 6} | ||
assert Parsec.parse!(" [ a ] ") == {:ok, [["a"]], "", %{}, {1, 0}, 7} | ||
assert Parsec.parse!("[ a a]") == {:ok, [["a", "a"]], "", %{}, {1, 0}, 6} | ||
assert Parsec.parse!("[a a ]") == {:ok, [["a", "a"]], "", %{}, {1, 0}, 6} | ||
assert Parsec.parse!("[ a a ]") == {:ok, [["a", "a"]], "", %{}, {1, 0}, 7} | ||
assert Parsec.parse!("[ a []]") == {:ok, [["a", []]], "", %{}, {1, 0}, 7} | ||
assert Parsec.parse!("[a [] ]") == {:ok, [["a", []]], "", %{}, {1, 0}, 7} | ||
assert Parsec.parse!("[ a [] ]") == {:ok, [["a", []]], "", %{}, {1, 0}, 8} | ||
assert Parsec.parse!("[ a [a]]") == {:ok, [["a", ["a"]]], "", %{}, {1, 0}, 8} | ||
assert Parsec.parse!("[a [a] ]") == {:ok, [["a", ["a"]]], "", %{}, {1, 0}, 8} | ||
assert Parsec.parse!("[ a [a] ]") == {:ok, [["a", ["a"]]], "", %{}, {1, 0}, 9} | ||
assert Parsec.parse!("[ [] a]") == {:ok, [[[], "a"]], "", %{}, {1, 0}, 7} | ||
assert Parsec.parse!("[[] a ]") == {:ok, [[[], "a"]], "", %{}, {1, 0}, 7} | ||
assert Parsec.parse!("[ [] a ]") == {:ok, [[[], "a"]], "", %{}, {1, 0}, 8} | ||
assert Parsec.parse!("[ [a] []]") == {:ok, [[["a"], []]], "", %{}, {1, 0}, 9} | ||
assert Parsec.parse!("[[a] [] ]") == {:ok, [[["a"], []]], "", %{}, {1, 0}, 9} | ||
assert Parsec.parse!("[ [a] [] ]") == {:ok, [[["a"], []]], "", %{}, {1, 0}, 10} | ||
|
||
multiline = """ | ||
a | ||
a | ||
[ | ||
a | ||
] | ||
""" | ||
|
||
assert Parsec.parse!(multiline) == {:ok, ["a", "a", ["a"]], "", %{}, {7, 13}, 13} | ||
|
||
multiline = """ | ||
[ | ||
a | ||
[] | ||
] | ||
""" | ||
|
||
assert Parsec.parse!(multiline) == {:ok, [["a", []]], "", %{}, {5, 9}, 9} | ||
|
||
multiline = """ | ||
[ | ||
a | ||
[ | ||
a | ||
[ | ||
a | ||
] | ||
] | ||
] | ||
""" | ||
|
||
assert Parsec.parse!(multiline) == {:ok, [["a", ["a", ["a"]]]], "", %{}, {10, 106}, 106} | ||
end | ||
|
||
test "parses and fails in some cases" do | ||
# TODO fail if rest is not empty | ||
assert Parsec.parse!("[") == {:ok, [], "[", %{}, {1, 0}, 0} | ||
end | ||
end |