Skip to content

Commit

Permalink
tree is working
Browse files Browse the repository at this point in the history
  • Loading branch information
tenderlove committed Nov 22, 2010
1 parent 987d074 commit 16e89e0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
6 changes: 6 additions & 0 deletions lib/mrc.rb
Expand Up @@ -3,4 +3,10 @@

module MRC
VERSION = '1.0.0'

class Node < Struct.new(:children); end
class Mustache < Node; end
class Id < Node; alias :name :children; end
class String < Node; end
class Params < Node; end
end
7 changes: 3 additions & 4 deletions lib/mrc/parser.y
Expand Up @@ -5,7 +5,7 @@ token EOF OPEN CLOSE IDENT WS STRING
rule

expressions
: mustache EOF { result = Mustache.new(*val) }
: mustache { result = Mustache.new(*val) }
;

mustache
Expand All @@ -21,13 +21,12 @@ inMustache

params
: params WS param
{ result = val.first.push val.last }
{ result = val.first.children.push val.last }
| param
{ result = Param.new(val.first) }
{ result = Params.new(val) }
;

param
: IDENT { result = Id.new(val.first) }
| STRING { result = String.new(val.first) }
;

10 changes: 10 additions & 0 deletions test/test_mrc.rb
Expand Up @@ -12,4 +12,14 @@ def test_tokenizer
expected = [[:OPEN, "{{ "], [:IDENT, "foo"], [:WS, " "], [:IDENT, "bar"], [:WS, " "], [:STRING, "\"baz\""], [:CLOSE, " }}"]]
assert_equal expected, tokens
end

def test_parser
tokenizer = MRC::Tokenizer.new
tree = tokenizer.scan_str('{{ foo bar "baz" }}')
assert_equal 2, tree.children.length
assert_equal 'foo', tree.children.first.name

params = tree.children[1]
assert_equal 2, params.length
end
end

0 comments on commit 16e89e0

Please sign in to comment.