Class: Glaemscribe::API::CharsetParser

Inherits:
Object
  • Object
show all
Defined in:
lib/api/charset_parser.rb

Instance Method Summary collapse

Constructor Details

#initializeCharsetParser

Returns a new instance of CharsetParser.



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

def initialize()
  @charset = nil
end

Instance Method Details

#parse(file_path) ⇒ Object



32
33
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/api/charset_parser.rb', line 32

def parse(file_path)
  @charset = Charset.new(ResourceManager::charset_name_from_file_path(file_path))

  raw = File.open(file_path,"rb:utf-8").read
  doc = Glaeml::Parser.new.parse(raw)

  if(doc.errors.any?)
    @charset.errors = doc.errors
    return @charset
  end

  # TODO : verify charset glaeml like we do with modes

  doc.root_node.gpath("char").each { |char_element|
    code   = char_element.args[0].hex
    names  = char_element.args[1..-1].map{|cname| cname.strip }.reject{ |cname| cname.empty? }
    @charset.add_char(char_element.line,code,names)
  }

  doc.root_node.gpath("seq").each{ |seq_elemnt|
    names       = seq_elemnt.args
    child_node  = seq_elemnt.children.first
    seq         = (child_node && child_node.text?)?(child_node.args.first):("")
    @charset.add_sequence_char(seq_elemnt.line,names,seq)
  }

  doc.root_node.gpath("swap").each{ |element|
    trigger_one      = element.args.first
    text_lines       = element.children.select{ |c| c.text? }.map{ |c| c.args.first }
    second_triggers  = text_lines.join(" ").split(/\s/).select{ |e| e != '' }
    @charset.add_swap(element.line, trigger_one, second_triggers)
  }

  doc.root_node.gpath("virtual").each { |virtual_element|
    names     = virtual_element.args
    reversed  = false
    default   = nil
    classes   = []

    virtual_element.gpath("class").each { |class_element|
      vc =  Charset::VirtualChar::VirtualClass.new
      vc.target    = class_element.args[0]
      vc.triggers  = class_element.args[1..-1].map{|cname| cname.strip }.reject{ |cname| cname.empty? }

      # Allow triggers to be defined inside the body of the class element
      text_lines      = class_element.children.select { |c| c.text? }.map{ |c| c.args.first}
      inner_triggers  = text_lines.join(" ").split(/\s/).select{ |e| e != '' }
      vc.triggers    += inner_triggers

      classes << vc
    }
    virtual_element.gpath("reversed").each { |reversed_element|
      reversed = true
    }
    virtual_element.gpath("default").each { |default_element|
      default  = default_element.args[0]
    }

    @charset.add_virtual_char(virtual_element.line,classes,names,reversed,default)
  }

  @charset.finalize

  @charset
end