Module: Wukong::FlatPack

Defined in:
lib/wukong/model/flatpack_parser/flat.rb,
lib/wukong/model/flatpack_parser/lang.rb,
lib/wukong/model/flatpack_parser/parser.rb,
lib/wukong/model/flatpack_parser/tokens.rb

Defined Under Namespace

Modules: Language, Tokens Classes: Parser

Class Method Summary collapse

Class Method Details

.create_parser(str, delimiter_width = 0, strict_fixed_point = true) ⇒ Object

Validates the supplied format string and creates a parser from it.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/wukong/model/flatpack_parser/flat.rb', line 35

def self.create_parser(str, delimiter_width=0, strict_fixed_point=true)
  return nil unless Language.string_in_lang str
  lang = []
  str.match_all(Language::CAPTURE_TOKEN_RE).each do |match|
    token_str = match[0]
    case token_str
    when Language::TOTAL_SIMPLE_TYPE_RE
      lang << simple_token_from_string(token_str, match.begin(0))
    when Language::TOTAL_FIXED_POINT_RE
      lang << fixed_point_token_from_string(token_str, match.begin(0), strict_fixed_point)
    when Language::TOTAL_DATE_RE
      date_match = token_str.match(Language::NAMED_DATE_RE)
      #TODO: Implement
    end
    if delimiter_width != 0
      t = Flat::Tokens::IgnoreToken.new
      t.position = -1
      t.length = delimiter_width
      lang << t
    end
  end
  lang = lang[0..-2] if delimiter_width != 0 #pop off the delimiter on the end
  return Flat::Parser.new(lang)
end

.fixed_point_token_from_string(str, position, strict = true) ⇒ Object

Creates a fixed point token. Strict input formatting is enforced if the strict param is true.



23
24
25
26
27
28
29
30
31
# File 'lib/wukong/model/flatpack_parser/flat.rb', line 23

def self.fixed_point_token_from_string(str, position, strict=true)
  float_pieces = str.match(Language::NAMED_FIXED_POINT_RE)
  t = Flat::Tokens::FixedPointToken.new
  t.position = position
  t.strict = strict
  t.power = float_pieces[:power].nil? ? nil : float_pieces[:power].to_i
  t.length = float_pieces[:length].to_i
  return t
end

.simple_token_from_string(str, position) ⇒ Object

Creates a 'simple' token from the supplied string and position.



12
13
14
15
16
17
18
19
# File 'lib/wukong/model/flatpack_parser/flat.rb', line 12

def self.simple_token_from_string(str, position)
  token_pieces = str.match(Language::NAMED_SIMPLE_TYPE_RE)
  t = Flat::Tokens.token_for_indicator(token_pieces[:type])
  t.position = position
  t.length = token_pieces[:length].nil? ? nil : token_pieces[:length].to_i
  t.modifier = token_pieces[:modifier]
  return t
end