oneaccess.grammar
author Tomas Zeman <tzeman@volny.cz>
Thu, 23 Aug 2012 10:16:19 +0200
changeset 44 b9026c6ef3e9
parent 43 oneaccess.gramar@597596f302ef
child 45 9ba98ba61f6b
permissions -rw-r--r--
oneaccess.grammar fixed filename

# Grammar for OneAccess devices
# Configuration is expected to be preprocessed via following command:
# perl -ne '/(^\s*)(\S.*)$/; print length($1)." $2\n"'
#
# Copyright (c) 2012 Tomas Zeman <tzeman@volny.cz>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted providing that the following conditions 
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#

{ # perl code follows

$::res = {};

# Returns pointer to context hashref as specified by ctx stack.
# @param ctx_path array of ctx stack.
sub ctx {
	return ctx_rel($::res, @_);
}

# Returns pointer to context hashref as specified by ctx stack,
# relative to the supplied ctx pointer.
# @param ptr current ctx pointer from which ctx build starts.
# @param ctx_path array of ctx stack.
sub ctx_rel {
	my $ptr = shift;
	my @ctx_path = @_;
	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: 		s_controller
		| s_interface
		| s_voice_port
		| s_dial_peer
		| l_vrf
		| l_sntp
		| l_syslog
		| indent comment 
		| indent cmdline 
		| indent emptyline
		| <error>

emptyline:	eol

comment: 	/!.*/ eol

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

word:		/\S+/  # any non-space
		{ $item[1] }

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

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

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

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

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

# generic attribute/value pair, handles no ... and single-param switches
avpair: 	identifier word
		{
		if ($item[1] eq 'no') {
			$arg{ctx}->{$item[2]} = 0;
		} else {
			$arg{ctx}->{$item[1]} = $item[2];
		}
		}
		| identifier
		{ $arg{ctx}->{$item[1]} = 1 }

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

l_section:	/[1-9]/ word(s) eol
		| /[1-9]/ "exit" eol
		| /[1-9]/ eol
		| <error>

# Lines w/ hash (passwd, secret etc)
l_hash:		word(s) /\S+/ word(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+/
		{ $item[1] }

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

s_controller_l:	"1" s_controller_content[ctx => $arg{ctx}] eol
		| s_dsl_group[ctx => $arg{ctx}]
		| l_section

s_controller_content: l_description[ctx => $arg{ctx}]
		| "framing" keyword
		{ $arg{ctx}->{framing} = $item{keyword} }

s_dsl_group:	"2" "dsl-group" num eol s_dsl_group_l[ctx => ctx_rel($arg{ctx}, 'dsl-group', $item{num})](s) "2" "exit" eol

s_dsl_group_l:  indent s_dsl_group_content[ctx => $arg{ctx}] eol

s_dsl_group_content:	"autoconfig"
		{ $arg{ctx}->{autoconfig} = 1 }
		| "execute"
		{ $arg{ctx}->{execute} = 1 }
		| "caplist" identifier
		{ $arg{ctx}->{caplist} = $item{identifier} }
		| "vendorspecoctets" num
		{ $arg{ctx}->{vendorspecoctets} = $item{num} }

# interface section
iface_type:	/[0-9a-zA-Z:-]+/
		{ $item[1] }

iface_num:	/\d+([\.\/]\d+)*/
		{ $item[1] }

s_interface:	"0" "interface" iface_type iface_num /\S*/ eol s_interface_l[ctx => ctx("interface", $item{iface_type}.":".$item{iface_num}) ](s) "0" "exit" eol
		{
		my $id = $item{iface_type}.":".$item{iface_num};
		$::res->{interface}->{$id}->{type} = $item{iface_type};
		$::res->{interface}->{$id}->{num} = $item{iface_num}
		}

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

s_interface_l:  "1" s_interface_content[ctx => $arg{ctx}] eol
		| s_isdn[ctx => $arg{ctx}]
		| s_vrrp[ctx => $arg{ctx}]
		| l_section

s_interface_content: l_description[ctx => $arg{ctx}]
		| /(no)?/ "shutdown"
		{ $arg{ctx}->{shutdown} = ($item[1] eq 'no') ? 0 : 1 }
		| "ip" "address" ip ip
		{ $arg{ctx}->{ip} = $item[3]; $arg{ctx}->{mask} = $item[4] }
		| "encapsulation" keyword /\S*/
		{
		$arg{ctx}->{encap} = $item[2];
		$arg{ctx}->{encap_param} = $item[3] if length($item[3]) > 0
		}
		| "bandwidth" num
		{ $arg{ctx}->{bandwidth} = $item{num} }
		| "speed" num
		{ $arg{ctx}->{speed} = $item{num} }
		| "ip" "vrf" "forwarding" word
		{ $arg{ctx}->{"ip-vrf-fwd"} = $item{word} }
		| "bridge-group" num
		{ $arg{ctx}->{"bridge-group"} = $item{num} }
		| "framing" keyword
		{ $arg{ctx}->{framing} = $item{keyword} }
		| "ip" "mtu" num
		{ $arg{ctx}->{mtu} = $item{num} }
		| "service-policy" /input|output/ identifier
		{ $arg{ctx}->{'service-policy'}->{$item[2]} = $item{identifier} }
		| "ip" "access-group" identifier /in|out/
		{ $arg{ctx}->{'access-group'}->{$item[4]} = $item{identifier} }

s_isdn:		"1" "isdn" eol s_isdn_l[ctx => ctx_rel($arg{ctx}, 'isdn')](s) "1" "exit" eol

s_isdn_l:	"2" avpair[ctx => $arg{ctx}] eol

s_vrrp:		"1" "vrrp" num eol s_vrrp_l[ctx => ctx_rel($arg{ctx}, 'vrrp', $item{num})](s) "1" "exit" eol

s_vrrp_l:	"2" avpair[ctx => $arg{ctx}] eol
		| "2" "tracking-interface" iface_type iface_num eol
		{
		$arg{ctx}->{$item[2]} = {
			type => $item{iface_type},
			num => $item{iface_num}
		}
		}
		| "2" "address" ip ip eol
		{
		$arg{ctx}->{$item[2]} = {
			ip => $item[3],
			mask => $item[4]
		}
		}



# vrf
l_vrf:		"0" "ip" "vrf" keyword eol
		{ $::res->{"ip-vrf"} = $item{keyword} }

via_iface:	iface_type iface_num
		{
		$arg{ctx}->{interface} = {
			type => $item{iface_type},
			num => $item{iface_num}
		};
		}
		| ""

# sntp
l_sntp:		"0" "sntp" "server" ip via_iface[ctx => ctx('sntp-server', $item{ip})] eol

l_syslog:	"0" "syslog" "server" ip num via_iface[ctx => ctx('syslog', 'server', $item{ip})] eol
		| "0" "logging" "syslog" /\w+/ eol
		{ $::res->{syslog}->{level} = $item[4] }

# voice ports
s_voice_port:	"0" "voice-port" /\d+\/\d+/ eol s_voice_port_l[ctx => ctx('voice-port', $item[3])](s) "0" "exit" eol

s_voice_port_l:	"1" s_voice_port_content[ctx => $arg{ctx}] eol
		| l_section

s_voice_port_content: /clock-source|tone|caller-id/ identifier
		{ $arg{ctx}->{$item[1]} = $item{identifier} }
		| /(no)?/ /sntp-time|power-source-one/
		{ $arg{ctx}->{$item[2]} = ($item[1] eq 'no') ? 0 : 1 }
		| "modify-tone" identifier /\S+/
		{ $arg{ctx}->{$item[1]}->{$item{identifier}} = $item[3] }

# dial peers
s_dial_peer:	"0" "dial-peer" "voice" word num eol s_dial_peer_l[ctx => ctx('dial-peer', $item{word}.":".$item{num}), type => $item{word}, num => $item{num} ](s) "0" "exit" eol

s_dial_peer_l:	"1" avpair[ctx => $arg{ctx}] eol
		{
		$arg{ctx}->{type} = $arg{type};
		$arg{ctx}->{num} = $arg{num}
		}
		| l_section