diff --git a/src/nquads_parser.yrl b/src/nquads_parser.yrl index 345799b..d0dd50b 100644 --- a/src/nquads_parser.yrl +++ b/src/nquads_parser.yrl @@ -1,7 +1,7 @@ %% Grammar for N-Quads as specified in http://www.w3.org/TR/2014/REC-n-quads-20140225/ -Nonterminals nquadsDoc nonEmptyNquadsDoc statement subject predicate object graphLabel literal eols. -Terminals iriref blank_node_label string_literal_quote langtag '^^' '.' eol. +Nonterminals nquadsDoc nonEmptyNquadsDoc statement subject predicate object graphLabel literal quoted_triple eols. +Terminals iriref blank_node_label string_literal_quote langtag '^^' '.' '<<' '>>' eol. Rootsymbol nquadsDoc. eols -> eols eol. @@ -25,10 +25,12 @@ statement -> subject predicate object '.' : { '$1', '$2', '$3' }. subject -> iriref : to_iri('$1'). subject -> blank_node_label : to_bnode('$1'). +subject -> quoted_triple : '$1'. predicate -> iriref : to_iri('$1'). object -> iriref : to_iri('$1'). object -> blank_node_label : to_bnode('$1'). object -> literal : '$1'. +object -> quoted_triple : '$1'. graphLabel -> iriref : to_iri('$1'). graphLabel -> blank_node_label : to_bnode('$1'). @@ -36,6 +38,7 @@ literal -> string_literal_quote '^^' iriref : to_literal('$1', {datatype, to_iri literal -> string_literal_quote langtag : to_literal('$1', {language, to_langtag('$2')}). literal -> string_literal_quote : to_literal('$1'). +quoted_triple -> '<<' subject predicate object '>>' : { '$2', '$3', '$4' }. Erlang code. diff --git a/test/acceptance/nquads_star_test.exs b/test/acceptance/nquads_star_test.exs new file mode 100644 index 0000000..7164e5e --- /dev/null +++ b/test/acceptance/nquads_star_test.exs @@ -0,0 +1,65 @@ +defmodule RDF.Star.NQuads.TestSuite do + @moduledoc """ + RDF-star N-Quads Test Suite. + + This runs the official RDF-star N-Triples Test Suite and variations of its + test files with added graph names against the `RDF.NQuads.Decoder`. + """ + + use ExUnit.Case, async: false + + import RDF.Sigils + + @ntriples_star_test_suite_path "rdf-star/nt/syntax" + @nquads_star_test_suite_path "rdf-star/nq/syntax" + @ntriples_star_test_suite Path.join(RDF.TestData.dir(), @ntriples_star_test_suite_path) + @nquads_star_test_suite Path.join(RDF.TestData.dir(), @nquads_star_test_suite_path) + + ExUnit.Case.register_attribute(__ENV__, :nt_test) + ExUnit.Case.register_attribute(__ENV__, :nq_test) + + @ntriples_star_test_suite + |> File.ls!() + |> Enum.filter(fn file -> Path.extname(file) == ".nt" end) + |> Enum.each(fn file -> + @nt_test file: Path.join(@ntriples_star_test_suite, file) + if file |> String.contains?("-bad-") do + test "Negative syntax test: #{file}", context do + assert {:error, _} = RDF.NQuads.read_file(context.registered.nt_test[:file]) + end + else + test "Positive syntax test: #{file}", context do + assert {:ok, %RDF.Dataset{}} = RDF.NQuads.read_file(context.registered.nt_test[:file]) + end + end + end) + + @nquads_star_test_suite + |> File.ls!() + |> Enum.filter(fn file -> Path.extname(file) == ".nq" end) + |> Enum.each(fn file -> + @nq_test file: Path.join(@nquads_star_test_suite, file) + test "Positive syntax test: #{file}", context do + nq_test_file = context.registered.nq_test[:file] + assert {:ok, %RDF.Dataset{} = dataset1} = RDF.NQuads.read_file(nq_test_file) + + nt_test_file = + nq_test_file + |> String.replace( + @nquads_star_test_suite_path <> "/nquads-", + @ntriples_star_test_suite_path <> "/ntriples-" + ) + |> String.replace(~r/\.nq$/, ".nt") + + assert {:ok, %RDF.Dataset{} = dataset2} = RDF.NQuads.read_file(nt_test_file) + + assert RDF.Dataset.graph_count(dataset1) == 1 + assert RDF.Dataset.graph_count(dataset2) == 1 + + assert %RDF.Graph{} = graph1 = RDF.Dataset.graph(dataset1, ~I) + assert %RDF.Graph{} = graph2 = RDF.Dataset.default_graph(dataset2) + # + assert RDF.Graph.change_name(graph1, nil) == graph2 + end + end) +end diff --git a/test/data/rdf-star/nq/syntax/nquads-star-bnode-1.nq b/test/data/rdf-star/nq/syntax/nquads-star-bnode-1.nq new file mode 100644 index 0000000..5848197 --- /dev/null +++ b/test/data/rdf-star/nq/syntax/nquads-star-bnode-1.nq @@ -0,0 +1,2 @@ +_:b0 . +<< _:b0 >> "ABC" . diff --git a/test/data/rdf-star/nq/syntax/nquads-star-bnode-2.nq b/test/data/rdf-star/nq/syntax/nquads-star-bnode-2.nq new file mode 100644 index 0000000..60829f6 --- /dev/null +++ b/test/data/rdf-star/nq/syntax/nquads-star-bnode-2.nq @@ -0,0 +1,2 @@ + _:b1 . +<< _:b1 >> "456"^^ . diff --git a/test/data/rdf-star/nq/syntax/nquads-star-nested-1.nq b/test/data/rdf-star/nq/syntax/nquads-star-nested-1.nq new file mode 100644 index 0000000..8c093dc --- /dev/null +++ b/test/data/rdf-star/nq/syntax/nquads-star-nested-1.nq @@ -0,0 +1,3 @@ + . +<< >> . +<< << >> >> "1"^^ . diff --git a/test/data/rdf-star/nq/syntax/nquads-star-nested-2.nq b/test/data/rdf-star/nq/syntax/nquads-star-nested-2.nq new file mode 100644 index 0000000..201d9e8 --- /dev/null +++ b/test/data/rdf-star/nq/syntax/nquads-star-nested-2.nq @@ -0,0 +1,3 @@ + . + << >> . +<< << >> >> . diff --git a/test/data/rdf-star/nq/syntax/nquads-star-syntax-1.nq b/test/data/rdf-star/nq/syntax/nquads-star-syntax-1.nq new file mode 100644 index 0000000..9ea5b8b --- /dev/null +++ b/test/data/rdf-star/nq/syntax/nquads-star-syntax-1.nq @@ -0,0 +1 @@ +<< >> . diff --git a/test/data/rdf-star/nq/syntax/nquads-star-syntax-2.nq b/test/data/rdf-star/nq/syntax/nquads-star-syntax-2.nq new file mode 100644 index 0000000..eb78f63 --- /dev/null +++ b/test/data/rdf-star/nq/syntax/nquads-star-syntax-2.nq @@ -0,0 +1 @@ + << >> . diff --git a/test/data/rdf-star/nq/syntax/nquads-star-syntax-3.nq b/test/data/rdf-star/nq/syntax/nquads-star-syntax-3.nq new file mode 100644 index 0000000..a643048 --- /dev/null +++ b/test/data/rdf-star/nq/syntax/nquads-star-syntax-3.nq @@ -0,0 +1 @@ +<< >> << >> . diff --git a/test/data/rdf-star/nq/syntax/nquads-star-syntax-4.nq b/test/data/rdf-star/nq/syntax/nquads-star-syntax-4.nq new file mode 100644 index 0000000..2f63565 --- /dev/null +++ b/test/data/rdf-star/nq/syntax/nquads-star-syntax-4.nq @@ -0,0 +1 @@ +<<>><<>>. diff --git a/test/data/rdf-star/nq/syntax/nquads-star-syntax-5.nq b/test/data/rdf-star/nq/syntax/nquads-star-syntax-5.nq new file mode 100644 index 0000000..bd28ee4 --- /dev/null +++ b/test/data/rdf-star/nq/syntax/nquads-star-syntax-5.nq @@ -0,0 +1 @@ +<<<<>><<>>>><<<<>><<>>>>.