Class: CiscoAclIntp::Parser

Inherits:
Racc::Parser
  • Object
show all
Extended by:
Forwardable
Includes:
ParserUtility
Defined in:
lib/cisco_acl_intp/parser_api.rb

Overview

ACL Parser

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ParserUtility

#acl_arg_err_message, #acl_err_message, #err_message, #racc_parse_err_message

Constructor Details

#initialize(opts) ⇒ CiscoACLParser

Constructor

Parameters:

  • opts (Hash)

    Options

  • [Boolean] (Hash)

    a customizable set of options

  • [Symbol] (Hash)

    a customizable set of options


108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/cisco_acl_intp/parser_api.rb', line 108

def initialize(opts)
  @yydebug = opts[:yydebug] || false
  @debug_print = opts[:debug] || false
  @silent_mode = @debug_print || opts[:silent] || false

  @color_mode = opts[:color] || :none
  AccessControlContainer.color_mode = @color_mode

  @err_handler = ParserErrorHandler.new
  @err_handler.reset_count

  @acl_table = {}
  @curr_acl_name = ''
  @line_number = 0
end

Instance Attribute Details

#acl_tableHash (readonly)

Returns ACL Table by ACL name key.

Returns:

  • (Hash)

    ACL Table by ACL name key


93
94
95
# File 'lib/cisco_acl_intp/parser_api.rb', line 93

def acl_table
  @acl_table
end

Instance Method Details

#add_acl_table_with_acl(acl_name, acl_entry, acl_class) ⇒ Object

ACL table handling

Parameters:

  • acl_name (String)

    ACL Name

  • acl_entry (AceBase)

    ACL Object

  • acl_class (Class)

    ACL Class


173
174
175
176
177
178
179
180
181
# File 'lib/cisco_acl_intp/parser_api.rb', line 173

def add_acl_table_with_acl(acl_name, acl_entry, acl_class)
  @curr_acl_name = acl_name
  unless @acl_table.key?(acl_name)
    @acl_table[acl_name] = acl_class.new(acl_name)
    @line_number = 0
  end
  @acl_table[acl_name].add_entry(acl_entry)
  @line_number += 1
end

#dputs(str) ⇒ Object (private)

debug print

Parameters:

  • str (String)

    String to print


218
219
220
# File 'lib/cisco_acl_intp/parser_api.rb', line 218

def dputs(str)
  puts "[debug] #{str}" if @debug_print
end

#err_pos_strString (private)

Generate error string

Returns:

  • (String)

    error position string


230
231
232
233
234
235
236
237
238
239
240
# File 'lib/cisco_acl_intp/parser_api.rb', line 230

def err_pos_str
  format(
    'in acl: %s, line: %s',
    @curr_acl_name,
    if @acl_table.key?(@curr_acl_name)
      @acl_table[@curr_acl_name].length + 1
    else
      ''
    end
  )
end

#next_tokenArray (private)

Get next token

Returns:

  • (Array)

    Next token array


224
225
226
# File 'lib/cisco_acl_intp/parser_api.rb', line 224

def next_token
  @queue.shift
end

#on_error(tok, val, _vstack) ⇒ Object

Syntax error handler


161
162
163
164
165
166
167
# File 'lib/cisco_acl_intp/parser_api.rb', line 161

def on_error(tok, val, _vstack)
  errstr = format(
    '%s, near value: %s, (token: %s)',
    err_pos_str, val, token_to_str(tok)
  )
  @err_handler.regist_message(errstr)
end

#parse_file(filename) ⇒ IO

Scan/Parse ACL from file

Parameters:

  • filename (String)

    File name

Returns:

  • (IO)

    IO object (Raw AcL)

Raises:


128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/cisco_acl_intp/parser_api.rb', line 128

def parse_file(filename)
  run_parser do
    case filename
    when String
      File.new(filename)
    when IO, StringIO
      filename
    else
      @err_handler.count
      raise AclError, "File: #{filename} not found."
    end
  end
end

#parse_string(aclstr) ⇒ IO

Scan/Parse ACL from string

Parameters:

  • aclstr (String)

    ACL string

Returns:

  • (IO)

    IO object (Raw AcL)

Raises:


146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/cisco_acl_intp/parser_api.rb', line 146

def parse_string(aclstr)
  run_parser do
    case aclstr
    when String
      StringIO.new(aclstr)
    when IO, StringIO
      aclstr
    else
      @err_handler.count
      raise AclError, "Argment: #{aclstr} not found."
    end
  end
end

#run_parser { ... } ⇒ Hash (private)

Run parser and Handle its Error(exception)

Yields:

  • Pre-process of running parser

Yield Returns:

  • (IO)

    IO object of file/string

Returns:

  • (Hash)

    ACL Table


189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/cisco_acl_intp/parser_api.rb', line 189

def run_parser
  begin
    # reset error count
    @err_handler.reset_count
    # do scan/parse
    scanner = Scanner.new
    @queue = scanner.scan_file(yield)
    do_parse
  rescue Racc::ParseError => err
    @err_handler.regist_message(racc_parse_err_message(err))
  rescue AclArgumentError => err
    @err_handler.regist_message(acl_arg_err_message(err, err_pos_str))
  rescue AclError => err
    @err_handler.regist_message(acl_err_message(err))
  rescue => err
    @err_handler.regist_message(err_message(err))
  end
  @acl_table
end

#yyerror_with(str) ⇒ Object (private)

print error message and enter error recovery mode

Parameters:

  • str (String)

    Error message


211
212
213
214
# File 'lib/cisco_acl_intp/parser_api.rb', line 211

def yyerror_with(str)
  @err_handler.regist_message "#{err_pos_str}, #{str}"
  yyerror
end