-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathreader.rb
287 lines (238 loc) · 7.53 KB
/
reader.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# frozen-string-literal: true
module Nanaimo
# Transforms plist strings into Plist objects.
#
class Reader
autoload :StringScanner, 'strscan'
# Raised when attempting to read a plist with an unsupported file format.
#
class UnsupportedPlistFormatError < Error
# @return [Symbol] The unsupported format.
#
attr_reader :format
def initialize(format)
@format = format
end
def to_s
"#{format} plists are currently unsupported"
end
end
# Raised when parsing fails.
#
class ParseError < Error
# @return [[Integer, Integer]] The (line, column) offset into the plist
# where the error occurred
#
attr_accessor :location
# @return [String] The contents of the plist.
#
attr_accessor :plist_string
def to_s
"[!] #{super}#{context}"
end
def context(n = 2)
line_number, column = location
line_number -= 1
lines = plist_string.split(NEWLINE)
s = line_number.succ.to_s.size
indent = "#{' ' * s}# "
indicator = "#{line_number.succ}> "
m = ::String.new("\n")
m << "#{indent}-------------------------------------------\n"
m << lines[[line_number - n, 0].max...line_number].map do |l|
"#{indent}#{l}\n"
end.join
line = lines[line_number].to_s
m << "#{indicator}#{line}\n"
m << ' ' * indent.size
m << line[0, column.pred].gsub(/[^\t]/, ' ')
m << "^\n"
m << Array(lines[line_number.succ..[lines.count.pred, line_number + n].min]).map do |l|
l.strip.empty? ? '' : "#{indent}#{l}\n"
end.join
m << "#{indent}-------------------------------------------\n"
end
end
# @param plist_contents [String]
#
# @return [Symbol] The file format of the plist in the given string.
#
def self.plist_type(plist_contents)
case plist_contents
when /\Abplist/
:binary
when /\A<\?xml/
:xml
else
:ascii
end
end
# @param file_path [String]
#
# @return [Plist] A parsed plist from the given file
#
def self.from_file(file_path)
new(File.read(file_path))
end
# @param contents [String] The plist to be parsed
#
def initialize(contents)
@scanner = StringScanner.new(contents)
end
# Parses the contents of the plist
#
# @return [Plist] The parsed Plist object.
#
def parse!
plist_format = ensure_ascii_plist!
read_string_encoding
root_object = parse_object
eat_whitespace!
raise_parser_error ParseError, 'Found additional characters after parsing the root plist object' unless @scanner.eos?
Nanaimo::Plist.new(root_object, plist_format)
end
private
def ensure_ascii_plist!
self.class.plist_type(@scanner.string).tap do |plist_format|
raise UnsupportedPlistFormatError, plist_format unless plist_format == :ascii
end
end
def read_string_encoding
# TODO
end
def parse_object
_comment = skip_to_non_space_matching_annotations
start_pos = @scanner.pos
raise_parser_error ParseError, 'Unexpected end of string while parsing' if @scanner.eos?
if @scanner.skip(/\{/)
parse_dictionary
elsif @scanner.skip(/\(/)
parse_array
elsif @scanner.skip(/</)
parse_data
elsif quote = @scanner.scan(/['"]/)
parse_quotedstring(quote)
else
parse_string
end.tap do |o|
o.annotation = skip_to_non_space_matching_annotations
Nanaimo.debug { "parsed #{o.inspect} from #{start_pos}..#{@scanner.pos}" }
end
end
def parse_string
eat_whitespace!
unless match = @scanner.scan(%r{[\w_$/:.-]+}o)
raise_parser_error ParseError, "Invalid character #{current_character.inspect} in unquoted string"
end
Nanaimo::String.new(match, nil)
end
def parse_quotedstring(quote)
unless string = @scanner.scan(/(?:([^#{quote}\\]|\\.)*)#{quote}/)
raise_parser_error ParseError, "Unterminated quoted string, expected #{quote} but never found it"
end
string = Unicode.unquotify_string(string.chomp!(quote))
Nanaimo::QuotedString.new(string, nil)
end
def parse_array
objects = []
until @scanner.eos?
eat_whitespace!
break if @scanner.skip(/\)/)
objects << parse_object
eat_whitespace!
break if @scanner.skip(/\)/)
unless @scanner.skip(/,/)
raise_parser_error ParseError, "Array missing ',' in between objects"
end
end
Nanaimo::Array.new(objects, nil)
end
def parse_dictionary
objects = {}
until @scanner.eos?
skip_to_non_space_matching_annotations
break if @scanner.skip(/}/)
key = parse_object
eat_whitespace!
unless @scanner.skip(/=/)
raise_parser_error ParseError, "Dictionary missing value for key #{key.as_ruby.inspect}, expected '=' and found #{current_character.inspect}"
end
value = parse_object
objects[key] = value
eat_whitespace!
break if @scanner.skip(/}/)
unless @scanner.skip(/;/)
raise_parser_error ParseError, "Dictionary missing ';' after key-value pair for #{key.as_ruby.inspect}, found #{current_character.inspect}"
end
end
Nanaimo::Dictionary.new(objects, nil)
end
def parse_data
unless data = @scanner.scan(/[\h ]*>/)
raise_parser_error ParseError, "Data missing closing '>'"
end
data.chomp!('>')
data.delete!(' ')
unless data.size.even?
@scanner.unscan
raise_parser_error ParseError, 'Data has an uneven number of hex digits'
end
data = [data].pack('H*')
Nanaimo::Data.new(data, nil)
end
def current_character
@scanner.peek(1)
end
def read_singleline_comment
unless comment = @scanner.scan_until(NEWLINE)
raise_parser_error ParseError, 'Failed to terminate single line comment'
end
comment
end
def eat_whitespace!
@scanner.skip(MANY_WHITESPACES)
end
NEWLINE_CHARACTERS = %W(\x0A \x0D \u2028 \u2029).freeze
NEWLINE = Regexp.union(*NEWLINE_CHARACTERS)
WHITESPACE_CHARACTERS = NEWLINE_CHARACTERS + %W(\x09 \x0B \x0C \x20)
WHITESPACE = Regexp.union(*WHITESPACE_CHARACTERS)
MANY_WHITESPACES = /#{WHITESPACE}+/
def read_multiline_comment
unless annotation = @scanner.scan(%r{(?:.+?)(?=\*/)}m)
raise_parser_error ParseError, 'Failed to terminate multiline comment'
end
@scanner.skip(%r{\*/})
annotation
end
def skip_to_non_space_matching_annotations
annotation = ''.freeze
until @scanner.eos?
eat_whitespace!
# Comment Detection
if @scanner.skip(%r{//})
annotation = read_singleline_comment
next
elsif @scanner.skip(%r{/\*})
annotation = read_multiline_comment
next
end
eat_whitespace!
break
end
annotation
end
def location_in(scanner)
pos = scanner.charpos
line = scanner.string[0..scanner.charpos].scan(NEWLINE).size + 1
column = pos - (scanner.string.rindex(NEWLINE, pos - 1) || -1)
[line, column]
end
def raise_parser_error(klass, message)
exception = klass.new(message).tap do |error|
error.location = location_in(@scanner)
error.plist_string = @scanner.string
end
raise(exception)
end
end
end