Skip to content

Commit

Permalink
allow comments
Browse files Browse the repository at this point in the history
  • Loading branch information
alandipert committed May 4, 2012
1 parent 3ed791c commit 50a4a4f
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
lector (0.0.2)
lector (0.0.3)
citrus

GEM
Expand Down
5 changes: 3 additions & 2 deletions lib/lector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ module Lector
module Lector
module RubyParse; end
def self.read_s(string, opts = {})
$_LECTOR_READ_EVAL = opts[:read_eval]
Lector::RubyParse::parse(string).val
$_LECTOR_READ_EVAL = opts[:read_eval] || false
string_without_comments = $_LECTOR_READ_EVAL ? string : string.gsub(/#[^=].*$/, '')
Lector::RubyParse::parse(string_without_comments).val
end
def self.read_file(file, opts = {})
read_s(File.read(file), opts)
Expand Down
2 changes: 1 addition & 1 deletion lib/lector/reader.citrus
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ grammar Lector::RubyParse
end

rule code
("#=" code:string) <Lector::Types::Code>
("#=" string) <Lector::Types::Code>
end

rule symbol
Expand Down
4 changes: 2 additions & 2 deletions lib/lector/types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ module String; def val; captures[:content].first.gsub(/\\\"/,"\""); end; end
module Code
def val
if $_LECTOR_READ_EVAL
eval(code.to_s.slice(3..-2))
eval(to_s.slice(3..-2))
else
code.to_s.slice(3..-2)
to_s.slice(3..-2)
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions spec/lector/reader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,14 @@
it "returns the code when read-eval is off" do
Lector::read_s("#='1+2'").should == '1+2'
end

it "evals the code when read-eval is on" do
Lector::read_s("#='1+2'", :read_eval => true).should == 3
end

it "copes when strings have comments" do
Lector::read_s("[:a#some comment\n,:b,:c]").should == [:a, :b, :c]
end
end

context 'reading files' do
Expand Down

0 comments on commit 50a4a4f

Please sign in to comment.