Skip to content

Commit

Permalink
Partial work on parsing Relation literals
Browse files Browse the repository at this point in the history
  • Loading branch information
d11wtq committed Nov 5, 2011
1 parent 0460460 commit a74c730
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 6 deletions.
3 changes: 3 additions & 0 deletions Gemfile
Expand Up @@ -2,3 +2,6 @@ source "http://rubygems.org"

# Specify your gem's dependencies in veritas-tutorial-d.gemspec
gemspec

# Transitional arrangement until Veritas 0.0.7 is released
gem "veritas", :git => "git://github.com/dkubb/veritas"
10 changes: 7 additions & 3 deletions lib/veritas-td/parser.rb
Expand Up @@ -47,14 +47,18 @@ class Parser < Parslet::Parser
table_dum
end

# Attributes
rule(:attribute_ref) { (match["A-Za-z_"] >> match["a-zA-Z0-9_"].repeat).as(:attribute_ref) }

# Tuples
rule(:tuple) { (ci_str("TUPLE") >> padded("{") >> padded("}")).as(:tuple) }
rule(:tuple) { (ci_str("TUPLE") >> padded("{") >> tuple_component.repeat >> padded("}")).as(:tuple) }
rule(:tuple_component) { attribute_ref.as(:name) >> wsp >> expr.as(:value) }

# Complex expressions
rule(:expr) { parenthesized(expr) | padded(unary_expr | binary_expr | scalar) }
rule(:expr) { parenthesized(expr) | padded(relation | unary_expr | binary_expr | scalar) }

# Full user input (currently single expressions only)
rule(:prog) { noop | relation | expr }
rule(:prog) { noop | expr }

# Top-level element is any possible expression
root(:prog)
Expand Down
34 changes: 32 additions & 2 deletions lib/veritas-td/transform.rb
Expand Up @@ -31,10 +31,40 @@ class Transform < Parslet::Transform

# RELATION { .. }
rule(:relation => simple(:empty)) { Veritas::Relation.new([], []) }
rule(:relation => subtree(:tuples)) { Veritas::Relation.new([], [tuples]) }
rule(:relation => subtree(:tuples)) do |dict|
Veritas::Relation.new(tuple_header(dict[:tuples]), tuple_set(dict[:tuples]))
end

# TUPLE { .. }
rule(:tuple => subtree(:components)) { [] }
rule(:tuple => simple(:empty)) { [] }
rule(:tuple => subtree(:components)) { components }
rule(:attribute_ref => simple(:name)) { name.to_sym }

# FIXME: Create factory classes
class << self
private

def tuple_header(tuples)
tuples.first.collect do |attribute|
[attribute[:name], attribute_type(attribute[:value])]
end
end

def attribute_type(value)
case value
when Fixnum, Integer
Integer
else
Object # ?
end
end

def tuple_set(tuples)
tuples.collect do |tuple|
tuple.collect { |attribute| attribute[:value] }
end
end
end
end
end
end
11 changes: 11 additions & 0 deletions spec/unit/relation_literal_spec.rb
Expand Up @@ -34,4 +34,15 @@
result.should == Veritas::TABLE_DEE
end
end

context "for a single-tuple single-attribute relation" do
let(:expr) { "RELATION { TUPLE { ID 20 } }" }

it "returns a relation with the same tuple" do
result.should == Veritas::Relation.new(
[ [:ID, Integer] ],
[ [20] ]
)
end
end
end
2 changes: 1 addition & 1 deletion veritas-tutorial-d.gemspec
Expand Up @@ -19,7 +19,7 @@ Gem::Specification.new do |s|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]

s.add_development_dependency "rspec", "~> 2.6"
s.add_development_dependency "rspec", "~> 2.6"

s.add_runtime_dependency "veritas"
s.add_runtime_dependency "parslet"
Expand Down

0 comments on commit a74c730

Please sign in to comment.