Class: CiscoAclIntp::Parser
- Inherits:
-
Racc::Parser
- Object
- Racc::Parser
- CiscoAclIntp::Parser
- Extended by:
- Forwardable
- Includes:
- ParserUtility
- Defined in:
- lib/cisco_acl_intp/parser_api.rb
Overview
ACL Parser
Instance Attribute Summary collapse
-
#acl_table ⇒ Hash
readonly
ACL Table by ACL name key.
Instance Method Summary collapse
-
#add_acl_table_with_acl(acl_name, acl_entry, acl_class) ⇒ Object
ACL table handling.
-
#dputs(str) ⇒ Object
private
debug print.
-
#err_pos_str ⇒ String
private
Generate error string.
-
#initialize(opts) ⇒ CiscoACLParser
constructor
Constructor.
-
#next_token ⇒ Array
private
Get next token.
-
#on_error(tok, val, _vstack) ⇒ Object
Syntax error handler.
-
#parse_file(filename) ⇒ IO
Scan/Parse ACL from file.
-
#parse_string(aclstr) ⇒ IO
Scan/Parse ACL from string.
-
#run_parser { ... } ⇒ Hash
private
Run parser and Handle its Error(exception).
-
#yyerror_with(str) ⇒ Object
private
print error message and enter error recovery mode.
Methods included from ParserUtility
#acl_arg_err_message, #acl_err_message, #err_message, #racc_parse_err_message
Constructor Details
#initialize(opts) ⇒ CiscoACLParser
Constructor
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_table ⇒ Hash (readonly)
Returns 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
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
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_str ⇒ String (private)
Generate error 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_token ⇒ Array (private)
Get next token
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.(errstr) end |
#parse_file(filename) ⇒ IO
Scan/Parse ACL from file
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
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)
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.((err)) rescue AclArgumentError => err @err_handler.((err, err_pos_str)) rescue AclError => err @err_handler.((err)) rescue => err @err_handler.((err)) end @acl_table end |
#yyerror_with(str) ⇒ Object (private)
print error message and enter error recovery mode
211 212 213 214 |
# File 'lib/cisco_acl_intp/parser_api.rb', line 211 def yyerror_with(str) @err_handler. "#{err_pos_str}, #{str}" yyerror end |