Class: Rubyang::Cli::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyang/cli/parser.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeParser

Returns a new instance of Parser.



15
16
# File 'lib/rubyang/cli/parser.rb', line 15

def initialize
end

Class Method Details

.parse(str) ⇒ Object



10
11
12
13
# File 'lib/rubyang/cli/parser.rb', line 10

def self.parse( str )
  parser = self.new
  parser.parse( str )
end

Instance Method Details

#next_tokenObject



64
65
66
67
# File 'lib/rubyang/cli/parser.rb', line 64

def next_token
  #p @tokens.first
  @tokens.shift
end

#parse(str) ⇒ Object



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

  #result = self.do_parse
  return @tokens
end