Permalink
Browse files

! regression: error massage on unconsumed input

  • Loading branch information...
1 parent 72c1a95 commit 12c79fe339d8cff2b588176fe3d4e73a1fae9fa5 @kschiess committed Feb 2, 2011
Showing with 22 additions and 18 deletions.
  1. +11 −17 example/minilisp.rb
  2. +1 −1 lib/parslet/atoms/base.rb
  3. +10 −0 spec/parslet/atoms/base_spec.rb
View
28 example/minilisp.rb
@@ -5,12 +5,13 @@
require 'pp'
require 'parslet'
+require 'parslet/convenience'
module MiniLisp
class Parser < Parslet::Parser
root :expression
rule(:expression) {
- space? >> str('(') >> space? >> body >> str(')')
+ space? >> str('(') >> space? >> body >> str(')') >> space?
}
rule(:body) {
@@ -77,24 +78,17 @@ def do(tree)
parser = MiniLisp::Parser.new
transform = MiniLisp::Transform.new
-# Parse stage
-begin
- result = parser.parse %Q{
- (define test (lambda ()
- (begin
- (display "something")
- (display 1)
- (display 3.08))))
- (test)
- }
-rescue Parslet::ParseFailed => failure
- puts failure
- puts parser.root.error_tree if parser.root.cause
- exit
-end
+result = parser.parse_with_debug %Q{
+ (define test (lambda ()
+ (begin
+ (display "something")
+ (display 1)
+ (display 3.08))))
+ (test)
+}
# Transform the result
-pp transform.do(result)
+pp transform.do(result) if result
# Thereby reducing it to the earlier problem:
# http://github.com/kschiess/toylisp
View
2 lib/parslet/atoms/base.rb
@@ -297,7 +297,7 @@ def error(source, str, pos=nil)
def parse_failed(cause)
@last_cause = cause
raise Parslet::ParseFailed,
- @last_cause
+ @last_cause.to_s
end
class Cause < Struct.new(:message, :source, :pos)
View
10 spec/parslet/atoms/base_spec.rb
@@ -47,6 +47,16 @@
end
end
end
+ context "when not all input is consumed" do
+ let(:parslet) { Parslet.str('foo') }
+ it "should raise with a proper error message" do
+ begin
+ parslet.parse('foobar')
+ rescue Parslet::ParseFailed => ex
+ ex.message.should == "Don't know what to do with bar at line 1 char 4."
+ end
+ end
+ end
context "when a match succeeds" do
context "when there is an error from a previous run" do
before(:each) do

0 comments on commit 12c79fe

Please sign in to comment.