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

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

import java.util.SortedSet;
import java.util.TreeSet;
11
import java.util.logging.*;
12
13

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

@members {
20
21
22
23
24
25
26
	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;
27
	public boolean expected_found = false;
28
29
30
	int tabsize = 1;
	
	/* Global States */
31
	public MohawkT mohawkT = new MohawkT("Mohawk Converter");
32
33
34
	
	
	private void logmsg(String msg) {
35
36
37
		if(logger.getLevel() == Level.FINE) {
			System.out.println(StringUtils.repeat("  ", tabsize) + msg);
		}
38
	}
39
40
41
42
}

init
:
43
	stat stat stat stat stat stat
44
45
46
47
48
49
50
51
52
;

stat
:
	{canassign_found==false}?

	canassign
	{canassign_found=true;}

53
	|
54
55
56
57
58
	{canrevoke_found==false}?

	canrevoke
	{canrevoke_found=true;}

59
	|
60
61
62
63
64
	{canenable_found==false}?

	canenable
	{canenable_found=true;}

65
	|
66
67
68
69
70
	{candisable_found==false}?

	candisable
	{candisable_found=true;}

71
	|
72
73
74
75
76
	{query_found==false}?

	query
	{query_found=true;}

77
78
79
80
81
82
	|
	{expected_found==false}?

	expected
	{expected_found=true;}

83
84
85
86
;

canassign
:
87
88
89
90
91
92
	{logmsg("Entering Can Assign");tabsize++;}

	CanAssign LeftBrace
	(
		myrule [RuleType.ASSIGN]
		{
93
			mohawkT.canAssign.addRule($myrule.r);
94
95
96
97
98
		}

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

99
100
101
102
;

canrevoke
:
103
104
105
106
107
108
	{logmsg("Entering Can Revoke");tabsize++;}

	CanRevoke LeftBrace
	(
		myrule [RuleType.REVOKE]
		{
109
			mohawkT.canRevoke.addRule($myrule.r);
110
111
112
113
114
		}

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

115
116
117
118
;

canenable
:
119
120
121
122
123
124
	{logmsg("Entering Can Enable"); tabsize++;}

	CanEnable LeftBrace
	(
		myrule [RuleType.ENABLE]
		{
125
			mohawkT.canEnable.addRule($myrule.r);
126
127
128
129
130
		}

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

131
132
133
134
;

candisable
:
135
136
137
138
139
140
	{logmsg("Entering Can Disable");tabsize++;}

	CanDisable LeftBrace
	(
		myrule [RuleType.DISABLE]
		{
141
			mohawkT.canDisable.addRule($myrule.r);
142
143
144
145
146
		}

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

147
148
;

149
query
150
:
151
	Query Colon t = timeslot Comma ra = roleArray
152
	{
153
		mohawkT.query._timeslot = new TimeSlot($t.val);
154
155
		mohawkT.query._roles = $ra.r;
		mohawkT.timeIntervalHelper.add(mohawkT.query._timeslot);
156
	}
157
158
159
160
161
162
163

;

expected
:
	Expected Colon
	(
164
		Reachable
165
		{mohawkT.expectedResult = ExpectedResult.REACHABLE;}
166

167
		| Unreachable
168
		{mohawkT.expectedResult = ExpectedResult.UNREACHABLE;}
169
170

		| Unknown
171
		{mohawkT.expectedResult = ExpectedResult.UNKNOWN;}
172
173

	)
174
175
;

176
myrule [RuleType rt] returns [Rule r]
177
locals [Boolean adminAllRoles]
178
:
179
180
	{logmsg("Adding Rule: "); tabsize++;}

181
182
183
184
185
186
187
188
	LeftAngle
	(
		adminRole = myrole
		{$adminAllRoles=false;}

		| True
		{$adminAllRoles=true;}

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

207
208
;

209
210
211
precondition returns [ArrayList<Role> p] @init {
	$p = new ArrayList<Role>();
}
212
:
213
214
215
216
217
218
219
220
	a = rolecondition
	{$p.add($a.r);}

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

	)*
221
	| True
222
223
;

224
225
226
rolecondition returns [Role r] @init {
	boolean not = false;
}
227
228
:
	(
229
		not
230
231
232
233
234
		{not = true;}

	)? myrole
	{
		$r = new Role($myrole.rolet, not);
235
		/* logmsg("Role: "+ $r); */
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
	}

;

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
;

254
255
timeslotArray returns [ArrayList<TimeSlot> t] @init {
	$t = new ArrayList<TimeSlot>();
256
257
258
}
:
	LeftBracket a = timeslot
259
260
261
262
	{
		$t.add(new TimeSlot($a.val));
		mohawkT.timeIntervalHelper.add(new TimeSlot($a.val));
	}
263
264
265

	(
		',' b = timeslot
266
267
268
269
		{
			$t.add(new TimeSlot($b.val));
			mohawkT.timeIntervalHelper.add(new TimeSlot($b.val));
		}
270
271

	)? RightBracket
272
273
274
275
276
277
	| c = timeslot
	{
		$t.add(new TimeSlot($c.val));
		mohawkT.timeIntervalHelper.add(new TimeSlot($c.val));
	}

278
279
;

Jonathan Shahen's avatar
Jonathan Shahen committed
280
timeInterval returns [Integer t0, Integer t1]
281
:
282
283
284
285
286
287
	a = timeslot
	{$t0 = $a.val; $t1=$t0;}

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

289
	)?
290
291
292
293
	{
		mohawkT.timeIntervalHelper.add(new TimeInterval($t0, $t1));
	}

294
295
296
297
298
299
300
;

timeslot returns [Integer val]
:
	Timeslot
	{
		$val = new Integer($Timeslot.text.substring(1));
301
		
302
303
304
305
306
307
	}

;

myrole returns [String rolet, Role role]
:
308
	MyRole
309
	{
310
		$rolet = $MyRole.getText();
311
		$role = new Role($rolet);
312
		mohawkT.roleHelper.add($role);
313
314
	}

315
316
;

317
318
319
320
321
322
323
324
325
326
not
:
	Not '~'
;

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

328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
Newline
:
	(
		'\r' '\n'?
		| '\n'
	) -> skip
;

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

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

/* Key Words */
347
348
349
350
351
Timeslot
:
	't' INT
;

352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
CanAssign
:
	'CanAssign'
;

CanRevoke
:
	'CanRevoke'
;

CanEnable
:
	'CanEnable'
;

CanDisable
:
	'CanDisable'
;

372
/*  NOT USED ANYMORE
373
374
375
376
377
378
379
380
381
MaxRoles
:
	'MaxRoles'
;

MaxTimeSlots
:
	'MaxTimeSlots'
;
382
*/
383
384
385
386
387
Query
:
	'Query'
;

388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
Expected
:
	'Expected'
;

Not
:
	'NOT'
;

True
:
	'TRUE'
;

False
:
	'FALSE'
;

Unknown
:
	'UNKNOWN'
;

413
414
415
416
417
418
419
420
421
422
Reachable
:
	'REACHABLE'
;

Unreachable
:
	'UNREACHABLE'
;

423
// Special Notation
424
425
/*
RoleStrict
426
427
:
	'role' INT
428
429
;
*/
430
431
/* Called MyRole because there exists a class named Role */
MyRole
432
:
433
	[A-Za-z0-9]+
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
;

// Literals

LeftAngle
:
	'<'
;

RightAngle
:
	'>'
;

LeftBracket
:
	'['
;

RightBracket
:
	']'
;

LeftBrace
:
	'{'
;

RightBrace
:
	'}'
;

AND
:
	'&'
;

Colon
:
	':'
;

Comma
:
	','
;

fragment
DIGIT
:
	[0-9]
;

INT
:
	DIGIT+
;