simple-parser 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.html ADDED
@@ -0,0 +1,33 @@
1
+ <h2>simple-parser</h2>
2
+
3
+ <p>A library to parse simple strings into named tokens</p>
4
+
5
+ <h3>Installation</h3>
6
+
7
+ <pre><code>$ sudo gem install ddollar-simple-parser --source http://gems.github.com
8
+ </code></pre>
9
+
10
+ <h3>Usage</h3>
11
+
12
+ <h4>As a library</h4>
13
+
14
+ <pre><code>&gt;&gt; require 'simple-parser'
15
+ &gt;&gt; SimpleParser::String.parse(
16
+ 'this is "a test" string',
17
+ ':word1 :: ":phrase1" :word2'
18
+ )
19
+
20
+ =&gt; { :word1 =&gt; 'this',
21
+ :phrase1 =&gt; 'a test',
22
+ :word2 =&gt; 'string' }
23
+ </code></pre>
24
+
25
+ <h4>As a mixin</h4>
26
+
27
+ <pre><code>&gt;&gt; require 'simple-parser'
28
+ &gt;&gt; class String; include SimpleParser::String; end
29
+
30
+ &gt;&gt; 'test now'.parse(':word1 :word2')
31
+
32
+ =&gt; { :word1 =&gt; 'test', :word2 =&gt; 'now' }
33
+ </code></pre>
@@ -0,0 +1,43 @@
1
+ module SimpleParser
2
+
3
+ # :stopdoc:
4
+ VERSION = '0.1.1'
5
+ LIBPATH = File.expand_path(File.dirname(__FILE__)) + File::SEPARATOR
6
+ PATH = File.dirname(LIBPATH) + File::SEPARATOR
7
+ # :startdoc:
8
+
9
+ # Returns the version string for the library.
10
+ #
11
+ def self.version
12
+ VERSION
13
+ end
14
+
15
+ # Returns the library path for the module. If any arguments are given,
16
+ # they will be joined to the end of the libray path using
17
+ # <tt>File.join</tt>.
18
+ #
19
+ def self.libpath( *args )
20
+ args.empty? ? LIBPATH : ::File.join(LIBPATH, *args)
21
+ end
22
+
23
+ # Returns the lpath for the module. If any arguments are given,
24
+ # they will be joined to the end of the path using
25
+ # <tt>File.join</tt>.
26
+ #
27
+ def self.path( *args )
28
+ args.empty? ? PATH : ::File.join(PATH, *args)
29
+ end
30
+
31
+ # Utility method used to rquire all files ending in .rb that lie in the
32
+ # directory below this file that has the same name as the filename passed
33
+ # in. Optionally, a specific _directory_ name can be passed in such that
34
+ # the _filename_ does not have to be equivalent to the directory.
35
+ #
36
+ def self.require_all_libs_relative_to(fname)
37
+ search_me = File.expand_path(File.join(File.dirname(fname), '**', '*.rb'))
38
+ Dir.glob(search_me).sort.each { |rb| require rb }
39
+ end
40
+
41
+ SimpleParser.require_all_libs_relative_to __FILE__
42
+
43
+ end unless defined?(SimpleParser)
@@ -0,0 +1,41 @@
1
+ module SimpleParser::String
2
+
3
+ def self.included(base)
4
+ base.send(:define_method, :parse) do |format|
5
+ SimpleParser::String::parse(self, format)
6
+ end
7
+ end
8
+
9
+ @parser_cache = {}
10
+
11
+ def self.parse(string, format)
12
+ parser = @parser_cache[format] || SimpleParser::String::build_parser(format)
13
+
14
+ matches = string.match(parser[:regexp]).to_a
15
+ matches.shift
16
+
17
+ parser[:tokens].inject({}) do |memo, token|
18
+ memo[token.intern] = matches.shift
19
+ memo
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ def self.build_parser(format)
26
+ tokens = format.scan(/\:\w+/).map { |token| token[1..-1] }
27
+
28
+ regexp_builder = "^#{Regexp.escape(format)}$"
29
+
30
+ tokens.each do |token|
31
+ regexp_builder.gsub!(":#{token}", '(.+?)')
32
+ end
33
+
34
+ regexp_builder.gsub!('::', '.+?')
35
+
36
+ regexp = Regexp.new(regexp_builder)
37
+
38
+ @parser_cache[format] = { :tokens => tokens, :regexp => regexp }
39
+ end
40
+
41
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple-parser
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - David Dollar
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-08-25 00:00:00 -04:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: ddollar@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.html
24
+ files:
25
+ - lib/simple-parser/string.rb
26
+ - lib/simple-parser.rb
27
+ - README.html
28
+ has_rdoc: true
29
+ homepage: http://peervoice.com/software/simple-parser
30
+ licenses: []
31
+
32
+ post_install_message:
33
+ rdoc_options: []
34
+
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: "0"
42
+ version:
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ version:
49
+ requirements: []
50
+
51
+ rubyforge_project: simple-parser
52
+ rubygems_version: 1.3.5
53
+ signing_key:
54
+ specification_version: 3
55
+ summary: Simple parser
56
+ test_files: []
57
+