Permalink
Browse files

. Make examples runnable from outside examples folder

  • Loading branch information...
1 parent 725a584 commit 46d71288b3e84c78447377aa7e32b6f97d4d4c0a @txus txus committed Feb 28, 2011
View
@@ -1,7 +1,8 @@
# A small example on how to parse common types of comments. The example
# started out with parser code from Stephen Waits.
-$:.unshift '../lib'
+$: << 'lib'
+$: << '../lib'
require 'pp'
require 'parslet'
@@ -32,4 +33,4 @@ class ALanguage < Parslet::Parser
comment */
)
-pp ALanguage.new.parse_with_debug(code)
+pp ALanguage.new.parse_with_debug(code)
View
@@ -1,7 +1,8 @@
# A small example that shows a really small parser and what happens on parser
# errors.
-$:.unshift '../lib'
+$: << 'lib'
+$: << '../lib'
require 'pp'
require 'parslet'
View
@@ -2,7 +2,8 @@
# Example contributed by Hal Brodigan (postmodern). Thanks!
-$:.unshift '../lib'
+$: << 'lib'
+$: << '../lib'
require 'parslet'
class EmailParser < Parslet::Parser
@@ -45,11 +46,11 @@ class EmailSanitizer < Parslet::Transform
unless ARGV[0]
STDERR.puts "usage: #{$0} \"EMAIL_ADDR\""
- exit -1
+ STDOUT.puts "since you haven't specified any EMAIL_ADDR, for testing purposes we're using a.b.c.d@gmail.com"
end
begin
- p sanitizer.apply(parser.parse(ARGV[0]))
+ p sanitizer.apply(parser.parse(ARGV[0] || 'a.b.c.d@gmail.com'))
rescue Parslet::ParseFailed => error
puts error
puts parser.error_tree
View
@@ -1,7 +1,8 @@
# Basically just demonstrates that you can leave rules empty and get a nice
# NotImplementedError. A way to quickly spec out your parser rules?
-$:.unshift '../lib'
+$: << 'lib'
+$: << '../lib'
require 'parslet'
@@ -10,4 +11,4 @@ class Parser < Parslet::Parser
end
-Parser.new.empty.parslet
+Parser.new.empty.parslet
View
@@ -1,4 +1,6 @@
-$:.unshift File.dirname(__FILE__) + "/../lib"
+$: << 'lib'
+$: << '../lib'
+
require 'parslet'
class ErbParser < Parslet::Parser
@@ -41,4 +43,4 @@ class ErbParser < Parslet::Parser
The <%= 'nicely' %> printed result.
The <% b = 3 %>value of a is <%= a %>, and b is <%= b %>.
ERB
-))
+))
View
@@ -6,7 +6,8 @@
#
# See http://tools.ietf.org/html/rfc3986#appendix-A for more information.
-$:.unshift '../lib'
+$: << 'lib'
+$: << '../lib'
require 'pp'
require 'parslet'
@@ -122,4 +123,4 @@ def parse(str)
rescue Parslet::ParseFailed => m
puts "Failed: #{m}"
end
-end
+end
View
@@ -6,7 +6,9 @@
# b) Constructing non-greedy or non-blind parsers by transforming the
# grammar.
-$:.unshift File.dirname(__FILE__) + "/../lib"
+$: << 'lib'
+$: << '../lib'
+
require 'parslet'
include Parslet
View
@@ -1,7 +1,8 @@
# Reproduces [1] using parslet.
# [1] http://thingsaaronmade.com/blog/a-quick-intro-to-writing-a-parser-using-treetop.html
-$:.unshift '../lib'
+$: << 'lib'
+$: << '../lib'
require 'pp'
require 'parslet'
View
@@ -2,9 +2,8 @@
# uses '.as(:name)' to construct a tree that can reliably be matched
# afterwards.
-$:.unshift File.join(
- File.dirname(__FILE__),
- '/../lib')
+$: << 'lib'
+$: << '../lib'
require 'pp'
require 'parslet'
View
@@ -1,7 +1,8 @@
# The example from the readme. With this, I am making sure that the readme
# 'works'. Is this too messy?
-$:.unshift '../lib'
+$: << 'lib'
+$: << '../lib'
# cut here -------------------------------------------------------------------
require 'parslet'
View
@@ -1,4 +1,6 @@
-$:.unshift '../lib/'
+$: << 'lib'
+$: << '../lib'
+
require 'parslet'
require 'pp'
View
@@ -1,7 +1,8 @@
# 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.
-$:.unshift '../lib'
+$: << 'lib'
+$: << '../lib'
require 'pp'
require 'parslet'
@@ -51,4 +52,4 @@ def check(xml)
end
pp check("<a><b>some text in the tags</b></a>")
-pp check("<b><b>some text in the tags</b></a>")
+pp check("<b><b>some text in the tags</b></a>")
View
@@ -4,7 +4,10 @@
require 'pp'
-$:.unshift '../lib/'
+$: << 'lib'
+$: << '../lib'
+$: << 'example'
+
require 'parslet'
include Parslet
@@ -50,8 +53,10 @@ class LiteralsParser < Parslet::Parser
root :literals
end
-parsetree = LiteralsParser.new.parse(
- File.read('simple.lit'))
+file = File.exist?('simple.lit') ? File.read('simple.lit')\
+ : File.read('example/simple.lit')
+
+parsetree = LiteralsParser.new.parse(file)
class Lit < Struct.new(:text)
def to_s
@@ -72,4 +77,4 @@ def to_s
end
ast = transform.apply(parsetree)
-pp ast
+pp ast

0 comments on commit 46d7128

Please sign in to comment.