Skip to content

Commit

Permalink
Add string AST node
Browse files Browse the repository at this point in the history
  • Loading branch information
Evan Phoenix committed Feb 5, 2011
1 parent 86c7679 commit 0ec2f01
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
34 changes: 34 additions & 0 deletions lib/prattle/ast/string.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module Prattle
module AST
class String < AST::Node
Prattle::Parser.register self

def self.rule_name
"string"
end

def initialize(value)
@value = value
end

attr_reader :value

def self.grammar(g)
escapes = g.str("\\'") { "'" } \
| g.str('\n') { "\n" } \
| g.str('\r') { "\r" } \
| g.str('\t') { "\t" } \
| g.str('\\\\') { "\\" }
not_quote = g.many(g.any(escapes, /[^']/)) { |*a| a.join }
g.string = g.seq("'", g.t(not_quote), "'") do |str|
String.new(str)
end
end

def bytecode(g)
g.push_literal @value
g.string_dup
end
end
end
end
17 changes: 17 additions & 0 deletions test/ast/test_string.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require 'test/unit'
require 'prattle/parser'

class TestString < Test::Unit::TestCase
def test_parse
str = "'hello'"
parser = Prattle::Parser.new(str)
node = parser.parse :string

assert_kind_of Prattle::AST::String, node
assert_equal "hello", node.value
end

def test_run
assert_equal "evan", Prattle::AST::String.new("evan").run
end
end

0 comments on commit 0ec2f01

Please sign in to comment.