mohawktarbac.g4 5.5 KB
Newer Older
1
2
3
4
5
6
7
/**
 * 
 */
grammar MohawkTARBAC;

@header {
package mohawk.converter.generated;
8
9
10
11
12
13

import java.util.SortedSet;
import java.util.TreeSet;
import java.util.logging.Logger;

import org.apache.commons.lang3.StringUtils;
14
import mohawk.converter.helper.*;
15
import mohawk.global.pieces.*;
16
17
18
}

@members {
19
20
21
22
23
24
25
	public final Logger logger = Logger.getLogger("mohawk-converter");

	public boolean canassign_found = false;
	public boolean canrevoke_found = false;
	public boolean canenable_found = false;
	public boolean candisable_found = false;
	public boolean query_found = false;
26
	public boolean expected_found = false;
27
28
29
	int tabsize = 1;
	
	/* Global States */
30
31
	public RoleHelper roleHelper = new RoleHelper();
	public TimeIntervalHelper timeIntervalHelper = new TimeIntervalHelper();
32
33
34
35
36
37
38
39
	
	/* Blocks */
	public CanAssign canAssign = new CanAssign();
	public CanRevoke canRevoke = new CanRevoke();
	public CanEnable canEnable = new CanEnable();
	public CanDisable canDisable = new CanDisable();
	
	public Query query = new Query();
40
	public ExpectedResult expectedResult = ExpectedResult.UNKNOWN;
41
42
43
44
45
	
	
	private void logmsg(String msg) {
		System.out.println(StringUtils.repeat("  ", tabsize) + msg);
	}
46
47
48
49
}

init
:
50
	stat stat stat stat stat stat
51
52
53
54
55
56
57
58
59
;

stat
:
	{canassign_found==false}?

	canassign
	{canassign_found=true;}

60
	|
61
62
63
64
65
	{canrevoke_found==false}?

	canrevoke
	{canrevoke_found=true;}

66
	|
67
68
69
70
71
	{canenable_found==false}?

	canenable
	{canenable_found=true;}

72
	|
73
74
75
76
77
	{candisable_found==false}?

	candisable
	{candisable_found=true;}

78
	|
79
80
81
82
83
	{query_found==false}?

	query
	{query_found=true;}

84
85
86
87
88
89
	|
	{expected_found==false}?

	expected
	{expected_found=true;}

90
91
92
93
;

canassign
:
94
95
96
97
98
99
100
101
102
103
104
105
	{logmsg("Entering Can Assign");tabsize++;}

	CanAssign LeftBrace
	(
		myrule [RuleType.ASSIGN]
		{
			canAssign.addRule($myrule.r);
		}

	)* RightBrace
	{tabsize--; logmsg("Leaving Can Assign");}

106
107
108
109
;

canrevoke
:
110
111
112
113
114
115
116
117
118
119
120
121
	{logmsg("Entering Can Revoke");tabsize++;}

	CanRevoke LeftBrace
	(
		myrule [RuleType.REVOKE]
		{
			canRevoke.addRule($myrule.r);
		}

	)* RightBrace
	{tabsize--; logmsg("Leaving Can Revoke");}

122
123
124
125
;

canenable
:
126
127
128
129
130
131
132
133
134
135
136
137
	{logmsg("Entering Can Enable"); tabsize++;}

	CanEnable LeftBrace
	(
		myrule [RuleType.ENABLE]
		{
			canEnable.addRule($myrule.r);
		}

	)* RightBrace
	{tabsize--; logmsg("Leaving Can Enable");}

138
139
140
141
;

candisable
:
142
143
144
145
146
147
148
149
150
151
152
153
	{logmsg("Entering Can Disable");tabsize++;}

	CanDisable LeftBrace
	(
		myrule [RuleType.DISABLE]
		{
			canDisable.addRule($myrule.r);
		}

	)* RightBrace
	{tabsize--; logmsg("Leaving Can Disable");}

154
155
;

156
query
157
:
158
	Query Colon t = timeslot Comma ra = roleArray
159
	{
160
		query._timeslot = new TimeSlot($t.val);
161
162
		query._roles = $ra.r; 
	}
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179

;

expected
:
	Expected Colon
	(
		True
		{expectedResult = ExpectedResult.TRUE;}

		| False
		{expectedResult = ExpectedResult.FALSE;}

		| Unknown
		{expectedResult = ExpectedResult.UNKNOWN;}

	)
180
181
;

182
myrule [RuleType rt] returns [Rule r]
183
locals [Boolean adminAllRoles]
184
:
185
186
	{logmsg("Adding Rule: "); tabsize++;}

187
188
189
190
191
192
193
194
	LeftAngle
	(
		adminRole = myrole
		{$adminAllRoles=false;}

		| True
		{$adminAllRoles=true;}

Jonathan Shahen's avatar
Jonathan Shahen committed
195
	) Comma ti = timeInterval Comma pc = precondition Comma tsa = timeslotArray
196
	Comma role = myrole RightAngle
197
198
199
	{
		$r = new Rule();
		$r._type = rt;
200
		if($adminAllRoles == true) {
201
			$r._adminRole = Role.allRoles();
202
203
204
		} else {
			$r._adminRole = $adminRole.role;
		}
205
		$r._adminTimeInterval = new TimeInterval($ti.t0, $ti.t1);
206
207
208
209
210
211
212
		$r._preconditions = $pc.p;
		$r._roleSchedule = $tsa.t;
		$r._role = $role.role;
		tabsize--; 
		logmsg($r.toString());
	}

213
214
;

215
216
217
precondition returns [ArrayList<Role> p] @init {
	$p = new ArrayList<Role>();
}
218
:
219
220
221
222
223
224
225
226
	a = rolecondition
	{$p.add($a.r);}

	(
		AND b = rolecondition
		{$p.add($b.r);}

	)*
227
	| True
228
229
;

230
231
232
rolecondition returns [Role r] @init {
	boolean not = false;
}
233
234
:
	(
235
		not
236
237
238
239
240
		{not = true;}

	)? myrole
	{
		$r = new Role($myrole.rolet, not);
241
		/* logmsg("Role: "+ $r); */
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
	}

;

roleArray returns [ArrayList<Role> r] @init {
	$r = new ArrayList<Role>();
}
:
	LeftBracket a = myrole
	{$r.add($a.role);}

	(
		Comma b = myrole
		{$r.add($b.role);}

	)* RightBracket
;

timeslotArray returns [ArrayList<Integer> t] @init {
	$t = new ArrayList<Integer>();
}
:
	LeftBracket a = timeslot
	{$t.add($a.val);}

	(
		',' b = timeslot
		{$t.add($b.val);}

	)? RightBracket
272
	| c = timeslot {$t.add($c.val);}
273
274
;

Jonathan Shahen's avatar
Jonathan Shahen committed
275
timeInterval returns [Integer t0, Integer t1]
276
:
277
278
279
280
281
282
	a = timeslot
	{$t0 = $a.val; $t1=$t0;}

	(
		'-' b = timeslot
		{$t1=$b.val;}
283

284
	)?
285
286
287
288
289
290
291
;

timeslot returns [Integer val]
:
	Timeslot
	{
		$val = new Integer($Timeslot.text.substring(1));
292
		timeIntervalHelper.add(new TimeInterval($val));
293
294
295
296
297
298
	}

;

myrole returns [String rolet, Role role]
:
299
	MyRole
300
	{
301
		$rolet = $MyRole.getText();
302
		$role = new Role($rolet);
303
		roleHelper.add($role);
304
305
	}

306
307
;

308
309
310
311
312
313
314
315
316
317
not
:
	Not '~'
;

/* Whitespace and Comments */
Whitespace
:
	[ \t]+ -> skip
;
318

319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
Newline
:
	(
		'\r' '\n'?
		| '\n'
	) -> skip
;

BlockComment
:
	'/*' .*? '*/' -> skip
;

LineComment
:
	'//' ~[\r\n]* -> skip
;

/* Key Words */
338
339
340
341
342
Timeslot
:
	't' INT
;

343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
CanAssign
:
	'CanAssign'
;

CanRevoke
:
	'CanRevoke'
;

CanEnable
:
	'CanEnable'
;

CanDisable
:
	'CanDisable'
;

363
/*  NOT USED ANYMORE
364
365
366
367
368
369
370
371
372
MaxRoles
:
	'MaxRoles'
;

MaxTimeSlots
:
	'MaxTimeSlots'
;
373
*/
374
375
376
377
378
Query
:
	'Query'
;

379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
Expected
:
	'Expected'
;

Not
:
	'NOT'
;

True
:
	'TRUE'
;

False
:
	'FALSE'
;

Unknown
:
	'UNKNOWN'
;

404
// Special Notation
405
406
/*
RoleStrict
407
408
:
	'role' INT
409
410
;
*/
411
412
/* Called MyRole because there exists a class named Role */
MyRole
413
:
414
	[A-Za-z0-9]+
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
;

// Literals

LeftAngle
:
	'<'
;

RightAngle
:
	'>'
;

LeftBracket
:
	'['
;

RightBracket
:
	']'
;

LeftBrace
:
	'{'
;

RightBrace
:
	'}'
;

AND
:
	'&'
;

Colon
:
	':'
;

Comma
:
	','
;

fragment
DIGIT
:
	[0-9]
;

INT
:
	DIGIT+
;