Skip to content

Commit

Permalink
Update grammar to start with QueryUnit instead of query, and make Que…
Browse files Browse the repository at this point in the history
…ry optional, to allow for empty input.
  • Loading branch information
gkellogg committed Apr 3, 2013
1 parent f288075 commit d569776
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 19 deletions.
4 changes: 2 additions & 2 deletions Rakefile
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ task :meta => "lib/sparql/grammar/meta.rb"


file "lib/sparql/grammar/meta.rb" => "etc/sparql11.bnf" do |t| file "lib/sparql/grammar/meta.rb" => "etc/sparql11.bnf" do |t|
sh %{ sh %{
ebnf --ll1 Query --format rb \ ebnf --ll1 QueryUnit --format rb \
--mod-name SPARQL::Grammar::Meta \ --mod-name SPARQL::Grammar::Meta \
--output lib/sparql/grammar/meta.rb \ --output lib/sparql/grammar/meta.rb \
etc/sparql11.bnf etc/sparql11.bnf
Expand All @@ -71,7 +71,7 @@ end


file "etc/sparql11.ll1.sxp" => "etc/sparql11.bnf" do |t| file "etc/sparql11.ll1.sxp" => "etc/sparql11.bnf" do |t|
sh %{ sh %{
ebnf --ll1 Query --format sxp \ ebnf --ll1 QueryUnit --format sxp \
--output etc/sparql11.ll1.sxp \ --output etc/sparql11.ll1.sxp \
etc/sparql11.bnf etc/sparql11.bnf
} }
Expand Down
2 changes: 1 addition & 1 deletion etc/sparql11.bnf
Original file line number Original file line Diff line number Diff line change
@@ -1,6 +1,6 @@
# SPARQL 1.1 Grammar # SPARQL 1.1 Grammar


[1] QueryUnit ::= Query [1] QueryUnit ::= Query?
[2] Query ::= Prologue [2] Query ::= Prologue
( SelectQuery | ConstructQuery | DescribeQuery | AskQuery ) ( SelectQuery | ConstructQuery | DescribeQuery | AskQuery )
ValuesClause ValuesClause
Expand Down
7 changes: 4 additions & 3 deletions etc/sparql11.ll1.sxp
Original file line number Original file line Diff line number Diff line change
@@ -1,10 +1,11 @@
( (
(rule _empty "0" (first _eps) (seq)) (rule _empty "0" (first _eps) (seq))
(rule QueryUnit "1" (rule QueryUnit "1"
(first "ASK" "BASE" "CONSTRUCT" "DESCRIBE" "PREFIX" "SELECT")
(seq Query))
(rule Query "2"
(start #t) (start #t)
(first "ASK" "BASE" "CONSTRUCT" "DESCRIBE" "PREFIX" "SELECT" _eps)
(follow _eof)
(alt _empty Query))
(rule Query "2"
(first "ASK" "BASE" "CONSTRUCT" "DESCRIBE" "PREFIX" "SELECT") (first "ASK" "BASE" "CONSTRUCT" "DESCRIBE" "PREFIX" "SELECT")
(follow _eof) (follow _eof)
(seq Prologue _Query_1 ValuesClause)) (seq Prologue _Query_1 ValuesClause))
Expand Down
2 changes: 1 addition & 1 deletion etc/sparql11.sxp
Original file line number Original file line Diff line number Diff line change
@@ -1,6 +1,6 @@
( (
(rule _empty "0" (seq)) (rule _empty "0" (seq))
(rule QueryUnit "1" (seq Query)) (rule QueryUnit "1" (alt _empty Query))
(rule Query "2" (seq Prologue _Query_1 ValuesClause)) (rule Query "2" (seq Prologue _Query_1 ValuesClause))
(rule _Query_1 "2.1" (alt SelectQuery ConstructQuery DescribeQuery AskQuery)) (rule _Query_1 "2.1" (alt SelectQuery ConstructQuery DescribeQuery AskQuery))
(rule UpdateUnit "3" (seq Update)) (rule UpdateUnit "3" (seq Update))
Expand Down
15 changes: 13 additions & 2 deletions lib/sparql/grammar/meta.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,7 +1,7 @@
# This file is automatically generated by /Users/gregg/.rvm/gems/ruby-1.9.3-p392/bundler/gems/ebnf-cd313e5f46b4/bin/ebnf # This file is automatically generated by /Users/gregg/.rvm/gems/ruby-1.9.3-p392/bundler/gems/ebnf-92bb7ca4b9c0/bin/ebnf
# BRANCH derived from etc/sparql11.bnf # BRANCH derived from etc/sparql11.bnf
module SPARQL::Grammar::Meta module SPARQL::Grammar::Meta
START = :Query START = :QueryUnit


BRANCH = { BRANCH = {
:AdditiveExpression => { :AdditiveExpression => {
Expand Down Expand Up @@ -4327,6 +4327,14 @@ module SPARQL::Grammar::Meta
"PREFIX" => [:Prologue, :_Query_1, :ValuesClause], "PREFIX" => [:Prologue, :_Query_1, :ValuesClause],
"SELECT" => [:Prologue, :_Query_1, :ValuesClause], "SELECT" => [:Prologue, :_Query_1, :ValuesClause],
}, },
:QueryUnit => {
"ASK" => [:Query],
"BASE" => [:Query],
"CONSTRUCT" => [:Query],
"DESCRIBE" => [:Query],
"PREFIX" => [:Query],
"SELECT" => [:Query],
},
:_Query_1 => { :_Query_1 => {
"ASK" => [:AskQuery], "ASK" => [:AskQuery],
"CONSTRUCT" => [:ConstructQuery], "CONSTRUCT" => [:ConstructQuery],
Expand Down Expand Up @@ -13832,6 +13840,7 @@ module SPARQL::Grammar::Meta
"BASE", "BASE",
"PREFIX"], "PREFIX"],
:QueryUnit => [ :QueryUnit => [
:_eps,
"CONSTRUCT", "CONSTRUCT",
"DESCRIBE", "DESCRIBE",
"ASK", "ASK",
Expand Down Expand Up @@ -40459,6 +40468,8 @@ module SPARQL::Grammar::Meta
"GRAPH"], "GRAPH"],
:Query => [ :Query => [
:_eof], :_eof],
:QueryUnit => [
:_eof],
:_Query_1 => [ :_Query_1 => [
"VALUES", "VALUES",
:_eof], :_eof],
Expand Down
3 changes: 1 addition & 2 deletions lib/sparql/grammar/parser11.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -1043,8 +1043,7 @@ def to_s
def parse(prod = START) def parse(prod = START)
ll1_parse(@input, prod.to_sym, @options.merge(:branch => BRANCH, ll1_parse(@input, prod.to_sym, @options.merge(:branch => BRANCH,
:first => FIRST, :first => FIRST,
:follow => FOLLOW) :follow => FOLLOW))
) {}


# The last thing on the @prod_data stack is the result # The last thing on the @prod_data stack is the result
@result = case @result = case
Expand Down
18 changes: 10 additions & 8 deletions spec/grammar/parser_spec.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -705,10 +705,18 @@ def bgp_patterns
include ProductionRequirements include ProductionRequirements
include ProductionExamples include ProductionExamples


describe "when matching the [2] Query production rule" do describe "when matching the [1] QueryUnit production rule" do
with_production(:Query) do |production| with_production(:QueryUnit) do |production|
it_ignores_empty_input_using production it_ignores_empty_input_using production
given_it_generates(production, "SELECT * FROM <a> WHERE {?a ?b ?c}", %q((dataset (<a>) (bgp (triple ?a ?b ?c)))))
given_it_generates(production, "CONSTRUCT {?a ?b ?c} WHERE {?a ?b ?c FILTER (?a)}", %q((construct ((triple ?a ?b ?c)) (filter ?a (bgp (triple ?a ?b ?c))))))
given_it_generates(production, "DESCRIBE * FROM <a> WHERE {?a ?b ?c}", %q((describe () (dataset (<a>) (bgp (triple ?a ?b ?c))))))
given_it_generates(production, "ASK WHERE {GRAPH <a> {?a ?b ?c}}", %q((ask (graph <a> (bgp (triple ?a ?b ?c))))))
end
end


describe "when matching the [2] Query production rule" do
with_production(:Query) do |production|
{ {
"BASE <foo/> SELECT * WHERE { <a> <b> <c> }" => "BASE <foo/> SELECT * WHERE { <a> <b> <c> }" =>
%q((base <foo/> (bgp (triple <a> <b> <c>)))), %q((base <foo/> (bgp (triple <a> <b> <c>)))),
Expand Down Expand Up @@ -937,7 +945,6 @@ def bgp_patterns


describe "when matching the [13] DatasetClause production rule" do describe "when matching the [13] DatasetClause production rule" do
with_production(:DatasetClause) do |production| with_production(:DatasetClause) do |production|
it_ignores_empty_input_using production
given_it_generates(production, %q(FROM <http://example.org/foaf/aliceFoaf>), [:dataset, RDF::URI("http://example.org/foaf/aliceFoaf")]) given_it_generates(production, %q(FROM <http://example.org/foaf/aliceFoaf>), [:dataset, RDF::URI("http://example.org/foaf/aliceFoaf")])
given_it_generates(production, %q(FROM NAMED <http://example.org/foaf/aliceFoaf>), [:dataset, [:named, RDF::URI("http://example.org/foaf/aliceFoaf")]]) given_it_generates(production, %q(FROM NAMED <http://example.org/foaf/aliceFoaf>), [:dataset, [:named, RDF::URI("http://example.org/foaf/aliceFoaf")]])
end end
Expand All @@ -949,8 +956,6 @@ def bgp_patterns
# [16] SourceSelector # [16] SourceSelector
describe "when matching the [17] WhereClause production rule" do describe "when matching the [17] WhereClause production rule" do
with_production(:WhereClause) do |production| with_production(:WhereClause) do |production|
it_ignores_empty_input_using production

bgp_patterns.each_pair do |input, result| bgp_patterns.each_pair do |input, result|
given_it_generates(production, "WHERE {#{input}}", result, given_it_generates(production, "WHERE {#{input}}", result,
:prefixes => {nil => "http://example.com/", :rdf => RDF.to_uri.to_s}, :prefixes => {nil => "http://example.com/", :rdf => RDF.to_uri.to_s},
Expand Down Expand Up @@ -1238,7 +1243,6 @@ def bgp_patterns
# [69] Constraint ::= BrackettedExpression | BuiltInCall | FunctionCall # [69] Constraint ::= BrackettedExpression | BuiltInCall | FunctionCall
describe "when matching the [69] Constraint production rule" do describe "when matching the [69] Constraint production rule" do
with_production(:Constraint) do |production| with_production(:Constraint) do |production|
it_ignores_empty_input_using production
it_recognizes_bracketted_expression_using production it_recognizes_bracketted_expression_using production
it_recognizes_built_in_call_using production it_recognizes_built_in_call_using production
it_recognizes_function_using production it_recognizes_function_using production
Expand Down Expand Up @@ -1341,8 +1345,6 @@ def bgp_patterns


describe "when matching the [108] Var production rule" do describe "when matching the [108] Var production rule" do
with_production(:Var) do |production| with_production(:Var) do |production|
it_ignores_empty_input_using production

it "recognizes the VAR1 terminal" do it "recognizes the VAR1 terminal" do
it_recognizes_var1(production) it_recognizes_var1(production)
end end
Expand Down

0 comments on commit d569776

Please sign in to comment.