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
|
# File 'lib/zerg_xcode/file_format/lexer.rb', line 2
def self.tokenize(string)
encoding_match = string.match(/^\/\/ \!\$\*(.*?)\*\$\!/)
raise "No encoding - #{string[0, 20]}" unless encoding_match
i = encoding_match[0].length
tokens = [[:encoding, encoding_match[1]]]
while i < string.length
if string[i, 2] == '/*'
i += 2
i += 1 while string[i, 2] != '*/'
i += 2
next
end
case string[i, 1]
when /\s/
i += 1
when '(', ')', '{', '}', '=', ';', ','
tokens << {'(' => :begin_array, ')' => :end_array,
'{' => :begin_hash, '}' => :end_hash,
'=' => :assign, ';' => :stop, ',' => :comma}[string[i, 1]]
i += 1
when '"'
i += 1
token = ''
while string[i, 1] != '"'
if string[i, 1] == '\\'
i += 1
case string[i, 1]
when 'n', 'r', 't'
token << { 'n' => "\n", 't' => "\t", 'r' => "\r" }[string[i, 1]]
i += 1
when '"', "'", '\\'
token << string[i]
i += 1
else
raise "Uknown escape sequence \\#{string[i, 20]}"
end
else
token << string[i]
i += 1
end
end
tokens << [:string, token]
i += 1
else
len = 0
len += 1 while /[^\s\t\r\n\f(){}=;,]/ =~ string[i + len, 1]
tokens << [:symbol, string[i, len]]
i += len
end
end
return tokens
end
|