summaryrefslogtreecommitdiffstats
path: root/mmss/config.y
blob: 63bfcb80289b4757f806c36e7fabcfefd77d7320 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
  Copyright (c) 2013-2014, Matthias Schiffer <mschiffer@universe-factory.net>
  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided 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 COPYRIGHT HOLDERS AND CONTRIBUTORS "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 COPYRIGHT HOLDER OR CONTRIBUTORS 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.
*/


%define api.pure
%define api.push-pull push
%name-prefix "mmss_config_"
%locations
%parse-param {const std::shared_ptr<config_t> &conf}
%parse-param {const char *filename}


%code requires {
	#include <config.hpp>
	#include <context.hpp>

	using namespace MMSS;
}

%union {
	uint64_t uint64;
	float fnum;
	bool boolean;
	char *str;
	gmrf_addr_t addr;

	const char *error;
}

%token <uint64> TOK_UINT
%token <fnum> TOK_FLOAT
%token <str> TOK_STRING
%token <addr> TOK_GMRF_ADDRESS

%token TOK_YES
%token TOK_NO
%token TOK_NETWORK
%token TOK_PROTOCOL
%token TOK_NODE
%token TOK_LOAD
%token TOK_DEFAULT
%token TOK_INTERFACE
%token TOK_ADDRESS
%token TOK_ETX
%token TOK_CONST
%token TOK_MIN
%token TOK_MAX
%token TOK_SINE
%token TOK_PERIOD
%token TOK_PHASE

%token TOK_S
%token TOK_M
%token TOK_H
%token TOK_DAYS


%code {
	void mmss_config_error(YYLTYPE *loc, const std::shared_ptr<config_t> &conf, const char *filename, const char *s);
}


%type <boolean> boolean
%type <boolean> maybe_default
%type <str> maybe_protocol
%type <fnum> time
%type <fnum> float

%%

start:		config
	;

config:		config statement
	|
	;

statement:	TOK_NETWORK network '{' network_config '}'
	|	TOK_PROTOCOL protocol ';'
	|	TOK_NODE node '{' node_config '}'
	;

network:	TOK_STRING {
			if (!conf->add_network($1))
				YYERROR;
		}
	;

network_config: network_config network_statement
	|
	;

network_statement:
		TOK_ETX network_etx ';'
	;

network_etx:
		TOK_CONST float {
			if (!conf->set_packet_loss($2))
				YYERROR;
		}
	|	TOK_MIN float TOK_MAX float TOK_SINE TOK_PERIOD time {
			if (!conf->set_packet_loss($2, $4, $7, 0))
				YYERROR;
		}
	|	TOK_MIN float TOK_MAX float TOK_SINE TOK_PERIOD time TOK_PHASE time {
			if (!conf->set_packet_loss($2, $4, $7, $9))
				YYERROR;
		}
	;

protocol:	TOK_STRING TOK_LOAD TOK_STRING maybe_default {
			if (!conf->load_proto($1, $3, $4))
				YYERROR;
		}
	;

node:		TOK_STRING maybe_protocol {
			if (!conf->add_node($1, $2))
				YYERROR;
		}
	;

node_config:	node_config node_statement
	|
	;

node_statement:	TOK_INTERFACE node_interface ';'
	;

node_interface:	TOK_STRING TOK_NETWORK TOK_STRING TOK_ADDRESS TOK_GMRF_ADDRESS {
			if (!conf->add_iface($1, $3, &$5))
				YYERROR;
		}
	;

float:		TOK_FLOAT	{ $$ = $1; }
	|	TOK_UINT	{ $$ = $1; }
	;

boolean:	TOK_YES		{ $$ = true; }
	|	TOK_NO		{ $$ = false; }
	;

maybe_default:	TOK_DEFAULT	{ $$ = true; }
	|			{ $$ = false; }
	;

maybe_protocol:	TOK_PROTOCOL TOK_STRING { $$ = $2; }
	|			{ $$ = nullptr; }
	;

time:		float TOK_DAYS	{ $$ = $1*86400; }
	|	float TOK_H	{ $$ = $1*3600; }
	|	float TOK_M	{ $$ = $1*60; }
	|	float TOK_S	{ $$ = $1; }
	|	float		{ $$ = $1; }
	;

%%

void mmss_config_error(YYLTYPE *loc, const std::shared_ptr<config_t> &conf, const char *filename, const char *s) {
	conf->get_context()->logf(LOG_ERR, "config error: %s at %s:%i:%i", s, filename, loc->first_line, loc->first_column);
}