-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Evan Phoenix
committed
Feb 5, 2011
1 parent
86c7679
commit 0ec2f01
Showing
2 changed files
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |