Module: Expgen

Defined in:
lib/expgen.rb,
lib/expgen/nodes.rb,
lib/expgen/parser.rb,
lib/expgen/version.rb,
lib/expgen/transform.rb,
lib/expgen/randomizer.rb

Defined Under Namespace

Modules: Nodes, Parser, Randomizer Classes: ParseError, Transform

Constant Summary collapse

ASCII =
(32..126).map(&:chr)
LOWER =
("a".."z").to_a
UPPER =
("A".."Z").to_a
DIGIT =
(0..9).map(&:to_s)
ALPHA =
LOWER + UPPER
WORD =
ALPHA + DIGIT + ["_"]
NEGATIVE_WORD =
ASCII - WORD
NON_DIGIT =
ASCII - DIGIT
HEX_DIGIT =
("a".."f").to_a + ("A".."F").to_a + DIGIT
NON_HEX_DIGIT =
ASCII - HEX_DIGIT
SPACE =
[" "]
NON_SPACE =
ASCII.drop(1)
CONTROL_CHARS =
(0.chr..31.chr).to_a
PUNCT =
(33..47).map(&:chr) + (58..64).map(&:chr) + (91..96).map(&:chr) + (123..126).map(&:chr)
ESCAPE_CHARS =
{ "n" => "\n", "s" => "\s", "r" => "\r", "t" => "\t", "v" => "\v", "f" => "\f", "a" => "\a", "e" => "\e" }
NON_LITERALS =
"[]\^$.|?*+()".split("").map { |l| "\\" + l }.join
VERSION =
"0.1.1"

Class Method Summary collapse

Class Method Details

.cacheObject



28
29
30
# File 'lib/expgen.rb', line 28

def self.cache
  @cache ||= {}
end

.clear_cacheObject



32
33
34
# File 'lib/expgen.rb', line 32

def self.clear_cache
  @cache = nil
end

.gen(exp) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/expgen.rb', line 36

def self.gen(exp)
  source = if exp.respond_to?(:source) then exp.source else exp.to_s end
  cache[source] ||= Transform.new.apply((Parser::Expression.new.parse(source)))
  Randomizer.randomize(cache[source])
rescue Parslet::ParseFailed => e
  raise Expgen::ParseError, e.message
end