Class: UdonParser::Parser

Inherits:
StringScanner
  • Object
show all
Defined in:
lib/udon/udon_parser.rb

Instance Method Summary collapse

Instance Method Details

#ensure_encoding(source) ⇒ Object



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
97
# File 'lib/udon/udon_parser.rb', line 69

def ensure_encoding(source)
  if defined?(::Encoding)
    if source.encoding == ::Encoding::ASCII_8BIT
      b = source[0, 4].bytes.to_a
      source =
        case
        when b.size>=4 && b[0]==0 && b[1]==0 && b[2]==0
          source.dup.force_encoding(::Encoding::UTF_32BE).encode!(::Encoding::UTF_8)
        when b.size>=4 && b[0]==0 && b[2]==0
          source.dup.force_encoding(::Encoding::UTF_16BE).encode!(::Encoding::UTF_8)
        when b.size>=4 && b[1]==0 && b[2]==0 && b[3]==0
          source.dup.force_encoding(::Encoding::UTF_32LE).encode!(::Encoding::UTF_8)
        when b.size>=4 && b[1]==0 && b[3]==0
          source.dup.force_encoding(::Encoding::UTF_16LE).encode!(::Encoding::UTF_8)
        else source.dup end
    else source = source.encode(::Encoding::UTF_8) end
    source.force_encoding(::Encoding::ASCII_8BIT)
  else
    b = source
    source =
      case
      when b.size >= 4 && b[0] == 0 && b[1] == 0 && b[2] == 0; Iconv.iconv('utf-8', 'utf-32be', b)
      when b.size >= 4 && b[0] == 0 && b[2] == 0; Iconv.iconv('utf-8', 'utf-16be', b)
      when b.size >= 4 && b[1] == 0 && b[2] == 0 && b[3] == 0; Iconv.iconv('utf-8', 'utf-32le', b)
      when b.size >= 4 && b[1] == 0 && b[3] == 0; Iconv.iconv('utf-8', 'utf-16le', b)
      else b end
  end
  return source
end

#init(source, opts = {}) ⇒ Object



63
64
65
66
67
# File 'lib/udon/udon_parser.rb', line 63

def init(source, opts={})
  opts ||= {}
  super ensure_encoding(source)
  @global = {}
end

#parseObject



99
100
101
102
103
104
105
106
107
# File 'lib/udon/udon_parser.rb', line 99

def parse
  reset
  @line = 1
  @pos = 1
  @leading = true
  @indent = 0
  @ast = document
  return @ast
end