Class MCollective::Matcher::Parser
In: lib/mcollective/matcher/parser.rb
Parent: Object

Methods

new   parse  

Attributes

execution_stack  [R] 
scanner  [R] 

Public Class methods

[Source]

    # File lib/mcollective/matcher/parser.rb, line 6
 6:       def initialize(args)
 7:         @scanner = Scanner.new(args)
 8:         @execution_stack = []
 9:         parse
10:       end

Public Instance methods

Parse the input string, one token at a time a contruct the call stack

[Source]

    # File lib/mcollective/matcher/parser.rb, line 13
13:       def parse
14:         p_token,p_token_value = nil
15:         c_token,c_token_value = @scanner.get_token
16:         parenth = 0
17: 
18:         while (c_token != nil)
19:           @scanner.token_index += 1
20:           n_token, n_token_value = @scanner.get_token
21: 
22:           unless n_token == " "
23:             case c_token
24:             when "and"
25:               unless (n_token =~ /not|statement|\(/) || (scanner.token_index == scanner.arguments.size)
26:                 raise "Error at column #{scanner.token_index}. \nExpected 'not', 'statement' or '('. Found '#{n_token_value}'"
27:               end
28: 
29:               if p_token == nil
30:                 raise "Error at column #{scanner.token_index}. \n Expression cannot start with 'and'"
31:               elsif (p_token == "and" || p_token == "or")
32:                 raise "Error at column #{scanner.token_index}. \n #{p_token} cannot be followed by 'and'"
33:               end
34: 
35:             when "or"
36:               unless (n_token =~ /not|statement|\(/) || (scanner.token_index == scanner.arguments.size)
37:                 raise "Error at column #{scanner.token_index}. \nExpected 'not', 'statement', '('. Found '#{n_token_value}'"
38:               end
39: 
40:               if p_token == nil
41:                 raise "Error at column #{scanner.token_index}. \n Expression cannot start with 'or'"
42:               elsif (p_token == "and" || p_token == "or")
43:                 raise "Error at column #{scanner.token_index}. \n #{p_token} cannot be followed by 'or'"
44:               end
45: 
46:             when "not"
47:               unless n_token =~ /statement|\(|not/
48:                 raise "Error at column #{scanner.token_index}. \nExpected 'statement' or '('. Found '#{n_token_value}'"
49:               end
50: 
51:             when "statement"
52:               unless n_token =~ /and|or|\)/
53:                 unless scanner.token_index == scanner.arguments.size
54:                   raise "Error at column #{scanner.token_index}. \nExpected 'and', 'or', ')'. Found '#{n_token_value}'"
55:                 end
56:               end
57: 
58:             when ")"
59:               unless (n_token =~ /|and|or|not|\(/)
60:                 unless(scanner.token_index == scanner.arguments.size)
61:                   raise "Error at column #{scanner.token_index}. \nExpected 'and', 'or', 'not' or '('. Found '#{n_token_value}'"
62:                 end
63:               end
64:               parenth += 1
65: 
66:             when "("
67:               unless n_token =~ /statement|not|\(/
68:                 raise "Error at column #{scanner.token_index}. \nExpected 'statement', '(',  not. Found '#{n_token_value}'"
69:               end
70:               parenth -= 1
71: 
72:             else
73:               raise "Unexpected token found at column #{scanner.token_index}. '#{c_token_value}'"
74:             end
75: 
76:             unless n_token == " "
77:               @execution_stack << {c_token => c_token_value}
78:             end
79: 
80:             p_token, p_token_value = c_token, c_token_value
81:             c_token, c_token_value = n_token, n_token_value
82:           end
83:         end
84: 
85:         if parenth < 0
86:           raise "Error. Missing parentheses ')'."
87:         elsif parenth > 0
88:           raise "Error. Missing parentheses '('."
89:         end
90:       end

[Validate]