cisco.grammar
author "Tomas Zeman <tzeman@volny.cz>"
Mon, 09 Nov 2009 17:48:33 +0100
changeset 10 91148603fd70
child 11 c170b1da9b6e
permissions -rw-r--r--
added cisco.grammar: parse hostname, controllers, interfaces

# Grammar for Cisco devices

<autotree>
{ # perl code follows

$::res = {};

# Returns pointer to context hasref as specified by ctx stack.
# @param ctx_path array of ctx stack.
sub ctx {
	my @ctx_path = @_;
	my $ptr = $::res;
	foreach my $part (@ctx_path) {
		$ptr->{$part} = {} unless exists ($ptr->{$part});
		$ptr = $ptr->{$part};
	}
	return $ptr;
}

} # end of perl code

file:		<skip: qr/[^\S\n]*/>	# Ignore non-newline whitespace
		line(s) eofile

line: 		emptyline
		| s_controller
		| s_interface
		| comment
		| cmdline 
		| <error>

emptyline:	eol

comment: 	/^\!.*/ eol

cmdline:	l_hostname eol
		| l_hash eol
		| word(s) eol

word:		/[0-9a-zA-Z:\/_\#\"\.,+<>-]+/
		{ $item[1] }

type:		/\w+/
		{ $item[1] }

num:		/\d+/
		{ $item[1] }

range:		/\d+/"-"/\d+/
		{ { from => $item[1], to => $item[3] } } 

keyword:	/[\w-]+/
		{ $item[1] }

identifier:	/[0-9a-zA-Z:_-]+/
		{ $item[1] }

quoted_text:	<perl_quotelike>
		{ $item[1][2] }
		| /[0-9a-zA-Z:\/_\#\"\.,-]+/
		{ $item[1] }

value:		/[0-9a-zA-Z:\/_\#\"\.,-]+/
		{ $item[1] }

eofile:		/^\Z/

eol:		/\n/

rest_of_line:	word(s)
		| eol

# section
section:	cmdline(s) "!"
		{ print "section\n"; }

# Lines w/ hash (passwd, secret etc)
l_hash:		word(s) /\S+/ words(s)
		| word(s) /\S+/

# Hostname
l_hostname:	"hostname" identifier
		{ $::res->{hostname} = $item{identifier} }

# Description
l_description:	"description" /[^\n]+/
		{ $arg{ctx}->{description} = $item[2] }

# controller section
controller_num:	/\d+\/\d+/
		{ $item[1] }

s_controller:	"controller" type controller_num s_controller_l[ctx => ctx('controller', $item{controller_num}) ](s) "!" eol
		{ $::res->{controller}->{$item{controller_num}}->{type} = $item{type} }

s_controller_l:	l_description[ctx => $arg{ctx}]
		| "channel-group" num "timeslots" range
		{ $arg{ctx}->{"channel-group"}->{$item{num}}->{ts} = $item{range} }
		| "channel-group" num "unframed"
		{ $arg{ctx}->{"channel-group"}->{$item{num}}->{unframed} = 1 }
		| cmdline
		| emptyline
		| <error>

# interface section
iface_name:	/\w+\d+(\/\d+)?(:\d+(\.\d+)?)?/
		{ $item[1] }

s_interface:	"interface" iface_name /\S*/ eol s_interface_l[ctx => ctx("interface", $item{iface_name}) ](s) "!" eol
		{
		print "interface $item{iface_name}\n";
		$::res->{interface}->{$item{iface_name}}->{type} = $item[3]
			if length($item[3]) > 0
		}

ip:		/\d+\.\d+\.\d+\.\d+/
		{ $item[1] }

s_interface_l:	l_description
		| /(no)?/ "shutdown" eol
		{ $arg{ctx}->{shutdown} = ($item[1] eq 'no') ? 0 : 1 }
		| "ip" "address" ip ip eol
		{ $arg{ctx}->{ip} = $item[3]; $arg{ctx}->{mask} = $item[4] }
		| "encapsulation" keyword /\S*/ eol
		{
		$arg{ctx}->{encap} = $item[2];
		$arg{ctx}->{encap_param} = $item[3] if length($item[3]) > 0
		}
		| "frame-relay" keyword keyword
		{ $arg{ctx}->{"frame-relay"} = {type => $item[2], value => $item[3]} }
		| "bandwidth" num
		{ $arg{ctx}->{bandwidth} = $item{num}; }
		|cmdline
		| emptyline
		| <error>