Skip to content

Commit

Permalink
+ Cleaning up txus patch
Browse files Browse the repository at this point in the history
Mostly code formatting and style transmogrification. Thanks for the
patch! We should have had examples as spec much earlier.
  • Loading branch information
kschiess committed Feb 28, 2011
1 parent 01f1232 commit 5a9a753
Show file tree
Hide file tree
Showing 17 changed files with 50 additions and 51 deletions.
3 changes: 1 addition & 2 deletions example/comments.rb
@@ -1,8 +1,7 @@
# A small example on how to parse common types of comments. The example # A small example on how to parse common types of comments. The example
# started out with parser code from Stephen Waits. # started out with parser code from Stephen Waits.


$: << 'lib' $:.unshift File.dirname(__FILE__) + "/../lib"
$: << '../lib'


require 'pp' require 'pp'
require 'parslet' require 'parslet'
Expand Down
3 changes: 1 addition & 2 deletions example/documentation.rb
@@ -1,8 +1,7 @@
# A small example that shows a really small parser and what happens on parser # A small example that shows a really small parser and what happens on parser
# errors. # errors.


$: << 'lib' $:.unshift File.dirname(__FILE__) + "/../lib"
$: << '../lib'


require 'pp' require 'pp'
require 'parslet' require 'parslet'
Expand Down
3 changes: 1 addition & 2 deletions example/email_parser.rb
Expand Up @@ -2,8 +2,7 @@


# Example contributed by Hal Brodigan (postmodern). Thanks! # Example contributed by Hal Brodigan (postmodern). Thanks!


$: << 'lib' $:.unshift File.dirname(__FILE__) + "/../lib"
$: << '../lib'
require 'parslet' require 'parslet'


class EmailParser < Parslet::Parser class EmailParser < Parslet::Parser
Expand Down
3 changes: 1 addition & 2 deletions example/empty.rb
@@ -1,8 +1,7 @@
# Basically just demonstrates that you can leave rules empty and get a nice # Basically just demonstrates that you can leave rules empty and get a nice
# NotImplementedError. A way to quickly spec out your parser rules? # NotImplementedError. A way to quickly spec out your parser rules?


$: << 'lib' $:.unshift File.dirname(__FILE__) + "/../lib"
$: << '../lib'


require 'parslet' require 'parslet'


Expand Down
5 changes: 3 additions & 2 deletions example/erb.rb
@@ -1,5 +1,6 @@
$: << 'lib' # Example that demonstrates how a simple erb-like parser could be constructed.
$: << '../lib'
$:.unshift File.dirname(__FILE__) + "/../lib"


require 'parslet' require 'parslet'


Expand Down
3 changes: 1 addition & 2 deletions example/ip_address.rb
Expand Up @@ -6,8 +6,7 @@
# #
# See http://tools.ietf.org/html/rfc3986#appendix-A for more information. # See http://tools.ietf.org/html/rfc3986#appendix-A for more information.


$: << 'lib' $:.unshift File.dirname(__FILE__) + "/../lib"
$: << '../lib'


require 'pp' require 'pp'
require 'parslet' require 'parslet'
Expand Down
3 changes: 1 addition & 2 deletions example/local.rb
Expand Up @@ -6,8 +6,7 @@
# b) Constructing non-greedy or non-blind parsers by transforming the # b) Constructing non-greedy or non-blind parsers by transforming the
# grammar. # grammar.


$: << 'lib' $:.unshift File.dirname(__FILE__) + "/../lib"
$: << '../lib'


require 'parslet' require 'parslet'
include Parslet include Parslet
Expand Down
3 changes: 1 addition & 2 deletions example/minilisp.rb
@@ -1,8 +1,7 @@
# Reproduces [1] using parslet. # Reproduces [1] using parslet.
# [1] http://thingsaaronmade.com/blog/a-quick-intro-to-writing-a-parser-using-treetop.html # [1] http://thingsaaronmade.com/blog/a-quick-intro-to-writing-a-parser-using-treetop.html


$: << 'lib' $:.unshift File.dirname(__FILE__) + "/../lib"
$: << '../lib'


require 'pp' require 'pp'
require 'parslet' require 'parslet'
Expand Down
5 changes: 4 additions & 1 deletion example/output/documentation.err
@@ -1 +1,4 @@
Don't know what to do with bbbb at line 1 char 1. (Parslet::ParseFailed) /Users/kaspar/git_work/own/parslet/lib/parslet/atoms/base.rb:326:in `parse_failed': Don't know what to do with bbbb at line 1 char 1. (Parslet::ParseFailed)
from /Users/kaspar/git_work/own/parslet/lib/parslet/atoms/base.rb:55:in `parse'
from example/documentation.rb:13:in `parse'
from example/documentation.rb:18:in `<main>'
1 change: 1 addition & 0 deletions example/output/documentation.out
@@ -0,0 +1 @@
slice("aaaa", 0)
2 changes: 1 addition & 1 deletion example/output/empty.err
@@ -1 +1 @@
empty.rb:14:in `<main>': rule(:empty) { ... } returns nil. Still not implemented, but already used? (NotImplementedError) example/empty.rb:13:in `<main>': rule(:empty) { ... } returns nil. Still not implemented, but already used? (NotImplementedError)
3 changes: 1 addition & 2 deletions example/parens.rb
Expand Up @@ -2,8 +2,7 @@
# uses '.as(:name)' to construct a tree that can reliably be matched # uses '.as(:name)' to construct a tree that can reliably be matched
# afterwards. # afterwards.


$: << 'lib' $:.unshift File.dirname(__FILE__) + "/../lib"
$: << '../lib'


require 'pp' require 'pp'
require 'parslet' require 'parslet'
Expand Down
3 changes: 1 addition & 2 deletions example/readme.rb
@@ -1,8 +1,7 @@
# The example from the readme. With this, I am making sure that the readme # The example from the readme. With this, I am making sure that the readme
# 'works'. Is this too messy? # 'works'. Is this too messy?


$: << 'lib' $:.unshift File.dirname(__FILE__) + "/../lib"
$: << '../lib'


# cut here ------------------------------------------------------------------- # cut here -------------------------------------------------------------------
require 'parslet' require 'parslet'
Expand Down
3 changes: 1 addition & 2 deletions example/seasons.rb
@@ -1,5 +1,4 @@
$: << 'lib' $:.unshift File.dirname(__FILE__) + "/../lib"
$: << '../lib'


require 'parslet' require 'parslet'
require 'pp' require 'pp'
Expand Down
3 changes: 1 addition & 2 deletions example/simple_xml.rb
@@ -1,8 +1,7 @@
# A simple xml parser. It is simple in the respect as that it doesn't address # A simple xml parser. It is simple in the respect as that it doesn't address
# any of the complexities of XML. This is ruby 1.9. # any of the complexities of XML. This is ruby 1.9.


$: << 'lib' $:.unshift File.dirname(__FILE__) + "/../lib"
$: << '../lib'


require 'pp' require 'pp'
require 'parslet' require 'parslet'
Expand Down
9 changes: 3 additions & 6 deletions example/string_parser.rb
Expand Up @@ -4,10 +4,7 @@


require 'pp' require 'pp'


$: << 'lib' $:.unshift File.dirname(__FILE__) + "/../lib"
$: << '../lib'
$: << 'example'

require 'parslet' require 'parslet'


include Parslet include Parslet
Expand Down Expand Up @@ -53,8 +50,8 @@ class LiteralsParser < Parslet::Parser
root :literals root :literals
end end


file = File.exist?('simple.lit') ? File.read('simple.lit')\ input_name = File.join(File.dirname(__FILE__), 'simple.lit')
: File.read('example/simple.lit') file = File.read(input_name)


parsetree = LiteralsParser.new.parse(file) parsetree = LiteralsParser.new.parse(file)


Expand Down
46 changes: 27 additions & 19 deletions spec/acceptance/examples_spec.rb
Expand Up @@ -2,28 +2,36 @@
require 'open3' require 'open3'


describe "Regression examples from example/ directory" do describe "Regression examples from example/ directory" do

Dir["example/*.rb"].each do |example| Dir["example/*.rb"].each do |example|

context example do
it "parses #{example} successfully" do # Generates a product path for a given example file.
stdin, stdout, stderr = Open3.popen3("ruby #{example}") def product_path(str, ext)

str.
expected_output_file = example.gsub('.rb', '.out').gsub('example/','example/output/') gsub('.rb', ".#{ext}").
expected_error_file = example.gsub('.rb', '.err').gsub('example/','example/output/') gsub('example/','example/output/')

if File.exists?(expected_output_file)

stdout.readlines.join.strip.should include(File.read(expected_output_file).strip)

elsif File.exists?(expected_error_file)
stderr.readlines.join.strip.should include(File.read(expected_error_file).strip)
else
error = "Neither #{expected_output_file} nor #{expected_error_file} exists. Cannot compare results with any output or error."
fail error
end end


it "runs successfully" do
stdin, stdout, stderr = Open3.popen3("ruby #{example}")

handle_map = {
stdout => :out,
stderr => :err
}
expectation_found = handle_map.any? do |io, ext|
name = product_path(example, ext)

if File.exists?(name)
io.read.strip.should == File.read(name).strip
true
end
end

unless expectation_found
fail "Example doesn't have either an .err or an .out file. "+
"Please create in examples/output!"
end
end
end end

end end

end end

0 comments on commit 5a9a753

Please sign in to comment.