forked from adammck/fuzz
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
hacked on everything, made it better
- Loading branch information
Showing
11 changed files
with
484 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#!/usr/bin/env ruby | ||
# vim: noet | ||
|
||
module Fuzz | ||
module Error | ||
|
||
class FuzzError < StandardError | ||
end | ||
|
||
# Raised by Fuzz::Parser when results | ||
# are accessed before they have been | ||
# built (by calling Fuzz::Parser#parse) | ||
class NotParsedYet < FuzzError | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
#!/usr/bin/env ruby | ||
# vim: noet | ||
|
||
$spec = "../../spec/parser.rb" | ||
|
||
|
||
module Fuzz | ||
class Parser | ||
attr_reader :tokens | ||
|
||
def initialize | ||
@tokens = [] | ||
@matches = nil | ||
end | ||
|
||
def add_token(title, token, *init_args) | ||
|
||
# resolve symbolic types into | ||
# a predefined token class | ||
token = Fuzz::Token.const_get(camelize(token))\ | ||
if token.is_a?(Symbol) | ||
|
||
# resolve classes into | ||
# instances if necessary | ||
token = token.new(title, *init_args)\ | ||
if token.is_a?(Class) | ||
|
||
@tokens.push(token) | ||
end | ||
|
||
def parse(str) | ||
@matches = [] | ||
summary = {} | ||
|
||
@tokens.each do |token| | ||
unless(extracted = token.extract!(str)).nil? | ||
summary[token.name] = extracted.value | ||
@matches.push(extracted) | ||
end | ||
end | ||
|
||
# store the remains of the input, in | ||
# case we want to do something useful | ||
# with it (like refer it to a human) | ||
@unparsed_str = str | ||
|
||
# return nil for no matches, or hash | ||
# containing a summary of the matches | ||
(summary.length == 0) ? nil : summary | ||
end | ||
|
||
# Returns an array of the tokens matched | ||
# by the parser, or raises NotParsedYet | ||
# if _parse_ has not been called yet. | ||
def matches | ||
raise_unless_parsed | ||
@matches | ||
end | ||
|
||
# Returns an Array containing the parts | ||
# of the parsed string that were not captured | ||
# and normalized into useful data by _parse_. | ||
# | ||
# p = Fuzz::Parser.new | ||
# p.add_token "age", :age | ||
# | ||
# p.parse("13 year old") | ||
# p.remainder => [] | ||
# | ||
# p.parse("13 blah blah") | ||
# p.remainder => ["blah blah"] | ||
# | ||
def unparsed | ||
raise_unless_parsed | ||
@unparsed_str.split(Fuzz::Replacement) | ||
end | ||
|
||
private | ||
|
||
def camelize(sym) | ||
sym.to_s.gsub(/(?:\A|_)(.)/) { $1.upcase } | ||
end | ||
|
||
# Raised NotParsedYet unless @matches | ||
# has been populated by _parse_, to | ||
# be called by methods that don't make | ||
# sense until something has been parsed. | ||
def raise_unless_parsed | ||
raise Fuzz::Error::NotParsedYet\ | ||
if @matches.nil? | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#!/usr/bin/env ruby | ||
# vim: noet | ||
|
||
module Fuzz::Token | ||
class Age < Base | ||
|
||
Prefix = '(?:aged?\s*)?' | ||
Meat = '(\d+)' | ||
Suffix = '(?:\s*(?:years? old|years?|yrs?|y/?o))?' | ||
|
||
# create one big ugly regex | ||
Pattern = Prefix + Meat + Suffix | ||
|
||
# set reasonable boundaries as | ||
# default, which can be overridden | ||
# at initialization | ||
Options = { | ||
:min => 1, | ||
:max => 99 | ||
} | ||
|
||
# convert the long numeric | ||
# capture into an integer | ||
def normalize(age_str) | ||
age_str.to_i | ||
end | ||
|
||
# various ways of specifying someones age | ||
Examples = { | ||
"1" => 1, | ||
"2 year" => 2, | ||
"3 years" => 3, | ||
"4 years old" => 4, | ||
"age 5" => 5, | ||
"aged 6 years" => 6, | ||
"999 years old" => 999 } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#!/usr/bin/env ruby | ||
# vim: noet | ||
|
||
module Fuzz::Token | ||
class Number < Base | ||
Pattern = '\d+' | ||
|
||
# convert captured digits | ||
# into a fixnum object | ||
def normalize(digits_str) | ||
digits_str.to_i | ||
end | ||
|
||
Examples = { | ||
"123" => 123 } | ||
end | ||
end |
Oops, something went wrong.