Module: Wildling

Defined in:
lib/wildling/cli.rb,
lib/wildling/token.rb,
lib/wildling/wildling.rb,
lib/wildling/generator.rb,
lib/wildling/parse_pattern.rb

Defined Under Namespace

Modules: Cli Classes: Client, Generator, Token

Constant Summary collapse

VERSION =
"2.0.7"
TOKEN_PARSING_REGEX =
/(\\[%@$*#&?!-]|[%@$*#&?!-]\{.*?\}|[%@$*#&?!-])/

Class Method Summary collapse

Class Method Details

.create(patterns, dictionaries = nil) ⇒ Object



54
55
56
# File 'lib/wildling/wildling.rb', line 54

def self.create(patterns, dictionaries = nil)
  Client.new(patterns, dictionaries)
end

.dictionary_tokenizer(part, dictionaries) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/wildling/parse_pattern.rb', line 72

def dictionary_tokenizer(part, dictionaries)
  options = parse_length_with_string(part)
  if options == false || (options["string"] && !options["string"].empty? && !dictionaries.key?(options["string"]))
    options = {
      "variants" => [part],
      "startLength" => 1,
      "endLength" => 1,
      "src" => part
    }
  else
    options["variants"] = dictionaries[options["string"] || ""] || []
  end
  Token.new(options)
end

.parse_length_with_string(part) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/wildling/parse_pattern.rb', line 34

def parse_length_with_string(part)
  match = part.match(/\{'(.*)'(?:,(\d+)-(\d+))?(?:,(\d+))?\}/)
  return false unless match

  if match[2] && match[3]
    return {
      "string" => match[1] || "",
      "startLength" => match[2].to_i,
      "endLength" => match[3].to_i,
      "src" => part
    }
  end

  if match[4]
    length = match[4].to_i
    return {
      "string" => match[1] || "",
      "startLength" => length,
      "endLength" => length,
      "src" => part
    }
  end

  {
    "string" => match[1] || "",
    "startLength" => 1,
    "endLength" => 1,
    "src" => part
  }
end

.parse_length_with_variants(part, variants) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/wildling/parse_pattern.rb', line 10

def parse_length_with_variants(part, variants)
  match = part.match(/\{((\d+)-(\d+)|(\d+))\}/)

  start_length = 1
  end_length = 1

  if match
    if match[2]
      start_length = match[2].to_i
      end_length = match[3].to_i
    elsif match[1]
      start_length = match[1].to_i
      end_length = start_length
    end
  end

  {
    "variants" => variants,
    "startLength" => start_length,
    "endLength" => end_length,
    "src" => part
  }
end

.parse_pattern(input_pattern, dictionaries) ⇒ Object



152
153
154
155
156
# File 'lib/wildling/parse_pattern.rb', line 152

def parse_pattern(input_pattern, dictionaries)
  dictionaries ||= {}
  parts = input_pattern.split(TOKEN_PARSING_REGEX).reject(&:empty?)
  parts.map { |part| part_to_token(part, dictionaries) }
end

.part_to_token(part, dictionaries) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/wildling/parse_pattern.rb', line 119

def part_to_token(part, dictionaries)
  tokenizers = {
    "#" => simple_tokenizer("0123456789"),
    "@" => simple_tokenizer("abcdefghijklmnopqrstuvwxyz"),
    "*" => simple_tokenizer("abcdefghijklmnopqrstuvwxyz0123456789"),
    "-" => simple_tokenizer(
      "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
    ),
    "!" => simple_tokenizer("ABCDEFGHIJKLMNOPQRSTUVWXYZ"),
    "?" => simple_tokenizer("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"),
    "&" => simple_tokenizer("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"),
    "%" => ->(p) { dictionary_tokenizer(p, dictionaries) },
    "$" => method(:words_tokenizer)
  }

  tokenizer = part.empty? ? nil : tokenizers[part[0]]
  is_escaped = part.length > 1 && part[0] == "\\" && tokenizers.key?(part[1])

  if tokenizer
    tokenizer.call(part)
  elsif is_escaped
    Token.new(
      "variants" => [part.sub(/^\\/, "")],
      "src" => part
    )
  else
    Token.new(
      "variants" => [part],
      "src" => part
    )
  end
end

.simple_tokenizer(variants_string) ⇒ Object



65
66
67
68
69
70
# File 'lib/wildling/parse_pattern.rb', line 65

def simple_tokenizer(variants_string)
  variants = variants_string.chars
  lambda do |part|
    Token.new(parse_length_with_variants(part, variants))
  end
end

.words_tokenizer(part) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/wildling/parse_pattern.rb', line 87

def words_tokenizer(part)
  options = parse_length_with_string(part)

  if options == false
    options = {
      "variants" => [part],
      "startLength" => 1,
      "endLength" => 1,
      "src" => part
    }
  else
    variants = []
    work_string = options["string"] || ""
    index = 0
    while index < work_string.length
      if work_string[index, 2] == "\\,"
        index += 2
      elsif work_string[index] == ","
        variants << work_string[0...index]
        work_string = work_string[(index + 1)..] || ""
        index = 0
      else
        index += 1
      end
    end
    variants << work_string
    options["variants"] = variants.map { |variant| variant.gsub("\\,", ",") }
  end

  Token.new(options)
end