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
|
# File 'lib/rubyang/cli/parser.rb', line 18
def parse( str )
@tokens = Array.new
s = StringScanner.new( str )
keywords = {
}
scanre_list = [
["dqstr", /^"(?:[^"]|\\.)*"/],
["sqstr", /^'(?:[^']|\\.)*'/],
["nqstr", /^[a-zA-Z0-9:\/\|\.=_-]+/],
["wsp", /^[ \t]+/],
]
scanres = scanre_list.to_h
scanre = Regexp.union( scanre_list.map{ |scanre| scanre[1] } )
last = ''
until s.eos?
token = s.scan( scanre )
case token
when scanres["dqstr"] then
token_ = token.gsub(/^"/,'').gsub(/"$/,'').gsub(/\\n/,"\n").gsub(/\\t/,"\t").gsub(/\\"/,"\"").gsub(/\\\\/,"\\")
@tokens.push token_
last = "dqstr"
when scanres["sqstr"] then
token_ = token.gsub(/^'/,'').gsub(/'$/,'')
@tokens.push token_
last = "sqstr"
when scanres["nqstr"] then
@tokens.push token
last = "nqstr"
when scanres["wsp"] then
last = "wsp"
else raise "token not match to any scanres: #{token.inspect}"
end
end
if last == "wsp"
@tokens.push ''
end
return @tokens
end
|