Commit 1d798bb7 authored by Jonathan Shahen's avatar Jonathan Shahen

Moving parsers to Global repo and created a ParserHelper instead of using the...

Moving parsers to Global repo and created a ParserHelper instead of using the ConverterRegressionTests static function runParser
parent 967ef569
<!--
@author Jonathan Shahen
-->
<project name="mohawk_converter" default="dist" basedir=".">
<project name="mohawkT_converter" default="dist" basedir=".">
<description>Mohawk-T Converter Tool.</description>
<!-- set global properties for this build -->
......@@ -11,8 +11,6 @@
<property name="data" location="data" />
<property name="build" location="${bin}/build" />
<property name="dist" location="${bin}/dist" />
<property name="mohawksrc" location="${src}/mohawk" />
<property name="antlr-script" location="${mohawksrc}/converter/generated" />
<property name="antlr" location="${lib}/antlr-4.4-complete.jar" />
<property name="junit" location="${lib}/junit-4.12.jar" />
<property name="qtest" location="${data}/" />
......@@ -37,23 +35,7 @@
<mkdir dir="${dist}" />
</target>
<target name="parser" depends="init" description="generate parser files">
<!-- Create the parser files -->
<!-- This uses an old version of ANTLR (something below 4)
<antlr target="${antlr-script}/mohawktarbac.g4" outputdirectory="${antlr-script}">
<classpath>
<pathelement location="${antlr}" />
</classpath>
</antlr>
-->
<java jar="${antlr}" fork="true">
<arg value="-o" />
<arg value="${antlr-script}" />
<arg value="${antlr-script}/MohawkTARBAC.g4" />
</java>
</target>
<target name="compile" depends="parser" description="compile the source ">
<target name="compile" depends="init" description="compile the source ">
<!-- compile the java code from ${src} into ${build} -->
<javac srcdir="${src}" destdir="${build}" debug="on" deprecation="true">
<src path="${src}" />
......
/**
*
*/
grammar MohawkTARBAC;
@header {
package mohawk.converter.generated;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.logging.*;
import org.apache.commons.lang3.StringUtils;
import mohawk.converter.helper.*;
import mohawk.global.pieces.*;
import mohawk.global.helper.*;
}
@members {
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;
public boolean expected_found = false;
int tabsize = 1;
/* Global States */
public MohawkT mohawkT = new MohawkT("Mohawk Converter");
private void logmsg(String msg) {
if(logger.getLevel() == Level.FINE) {
System.out.println(StringUtils.repeat(" ", tabsize) + msg);
}
}
}
init
:
stat stat stat stat stat stat
;
stat
:
{canassign_found==false}?
canassign
{canassign_found=true;}
|
{canrevoke_found==false}?
canrevoke
{canrevoke_found=true;}
|
{canenable_found==false}?
canenable
{canenable_found=true;}
|
{candisable_found==false}?
candisable
{candisable_found=true;}
|
{query_found==false}?
query
{query_found=true;}
|
{expected_found==false}?
expected
{expected_found=true;}
;
canassign
:
{logmsg("Entering Can Assign");tabsize++;}
CanAssign LeftBrace
(
myrule [RuleType.ASSIGN]
{
mohawkT.canAssign.addRule($myrule.r);
}
)* RightBrace
{tabsize--; logmsg("Leaving Can Assign");}
;
canrevoke
:
{logmsg("Entering Can Revoke");tabsize++;}
CanRevoke LeftBrace
(
myrule [RuleType.REVOKE]
{
mohawkT.canRevoke.addRule($myrule.r);
}
)* RightBrace
{tabsize--; logmsg("Leaving Can Revoke");}
;
canenable
:
{logmsg("Entering Can Enable"); tabsize++;}
CanEnable LeftBrace
(
myrule [RuleType.ENABLE]
{
mohawkT.canEnable.addRule($myrule.r);
}
)* RightBrace
{tabsize--; logmsg("Leaving Can Enable");}
;
candisable
:
{logmsg("Entering Can Disable");tabsize++;}
CanDisable LeftBrace
(
myrule [RuleType.DISABLE]
{
mohawkT.canDisable.addRule($myrule.r);
}
)* RightBrace
{tabsize--; logmsg("Leaving Can Disable");}
;
query
:
Query Colon t = timeslot Comma ra = roleArray
{
mohawkT.query._timeslot = new TimeSlot($t.val);
mohawkT.query._roles = $ra.r;
mohawkT.timeIntervalHelper.add(mohawkT.query._timeslot);
}
;
expected
:
Expected Colon
(
Reachable
{mohawkT.expectedResult = ExpectedResult.REACHABLE;}
| Unreachable
{mohawkT.expectedResult = ExpectedResult.UNREACHABLE;}
| Unknown
{mohawkT.expectedResult = ExpectedResult.UNKNOWN;}
)
;
myrule [RuleType rt] returns [Rule r]
locals [Boolean adminAllRoles]
:
{logmsg("Adding Rule: "); tabsize++;}
LeftAngle
(
adminRole = myrole
{$adminAllRoles=false;}
| True
{$adminAllRoles=true;}
) Comma ti = timeInterval Comma pc = precondition Comma tsa = timeslotArray
Comma role = myrole RightAngle
{
$r = new Rule();
$r._type = rt;
if($adminAllRoles == true) {
$r._adminRole = Role.allRoles();
} else {
$r._adminRole = $adminRole.role;
}
$r._adminTimeInterval = new TimeInterval($ti.t0, $ti.t1);
$r._preconditions = $pc.p;
$r._roleSchedule = $tsa.t;
$r._role = $role.role;
tabsize--;
logmsg($r.toString());
}
;
precondition returns [ArrayList<Role> p] @init {
$p = new ArrayList<Role>();
}
:
a = rolecondition
{$p.add($a.r);}
(
AND b = rolecondition
{$p.add($b.r);}
)*
| True
;
rolecondition returns [Role r] @init {
boolean not = false;
}
:
(
not
{not = true;}
)? myrole
{
$r = new Role($myrole.rolet, "", not);
/* logmsg("Role: "+ $r); */
}
;
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
| a = myrole
{$r.add($a.role);}
;
timeslotArray returns [ArrayList<TimeSlot> t] @init {
$t = new ArrayList<TimeSlot>();
}
:
LeftBracket a = timeslot
{
$t.add(new TimeSlot($a.val));
mohawkT.timeIntervalHelper.add(new TimeSlot($a.val));
}
(
',' b = timeslot
{
$t.add(new TimeSlot($b.val));
mohawkT.timeIntervalHelper.add(new TimeSlot($b.val));
}
)* RightBracket
| c = timeslot
{
$t.add(new TimeSlot($c.val));
mohawkT.timeIntervalHelper.add(new TimeSlot($c.val));
}
| True
;
timeInterval returns [Integer t0, Integer t1]
:
a = timeslot
{$t0 = $a.val; $t1=$t0;}
(
'-' b = timeslot
{$t1=$b.val;}
)?
{
mohawkT.timeIntervalHelper.add(new TimeInterval($t0, $t1));
}
;
timeslot returns [Integer val]
:
Timeslot
{
$val = new Integer($Timeslot.text.substring(1));
}
;
myrole returns [String rolet, Role role]
:
MyRole
{
$rolet = $MyRole.getText();
$role = new Role($rolet);
mohawkT.roleHelper.add($role);
}
;
not
:
Not '~'
;
/* Whitespace and Comments */
Whitespace
:
[ \t]+ -> skip
;
Newline
:
(
'\r' '\n'?
| '\n'
) -> skip
;
BlockComment
:
'/*' .*? '*/' -> skip
;
LineComment
:
'//' ~[\r\n]* -> skip
;
/* Key Words */
Timeslot
:
't' INT
;
CanAssign
:
'CanAssign'
;
CanRevoke
:
'CanRevoke'
;
CanEnable
:
'CanEnable'
;
CanDisable
:
'CanDisable'
;
/* NOT USED ANYMORE
MaxRoles
:
'MaxRoles'
;
MaxTimeSlots
:
'MaxTimeSlots'
;
*/
Query
:
'Query'
;
Expected
:
'Expected'
;
Not
:
'NOT'
;
True
:
'TRUE'
;
False
:
'FALSE'
;
Unknown
:
'UNKNOWN'
;
Reachable
:
'REACHABLE'
;
Unreachable
:
'UNREACHABLE'
;
// Special Notation
/*
RoleStrict
:
'role' INT
;
*/
/* Called MyRole because there exists a class named Role */
MyRole
:
[A-Za-z0-9]+
;
// Literals
LeftAngle
:
'<'
;
RightAngle
:
'>'
;
LeftBracket
:
'['
;
RightBracket
:
']'
;
LeftBrace
:
'{'
;
RightBrace
:
'}'
;
AND
:
'&'
;
Colon
:
':'
;
Comma
:
','
;
fragment
DIGIT
:
[0-9]
;
INT
:
DIGIT+
;
\ No newline at end of file
......@@ -7,10 +7,10 @@ import java.util.logging.*;
import org.junit.Before;
import org.junit.Test;
import mohawk.converter.generated.MohawkTARBACParser;
import mohawk.converter.logging.MohawkConsoleFormatter;
import mohawk.converter.to.asaptime.ConvertToASAPTimeNSA;
import mohawk.converter.to.asaptime.ConvertToASAPTimeSA;
import mohawk.global.parser.mohawkT.MohawkTARBACParser;
import mohawk.global.pieces.MohawkT;
import mohawk.global.timing.MohawkTiming;
......
......@@ -7,9 +7,9 @@ import java.util.logging.*;
import org.junit.Before;
import org.junit.Test;
import mohawk.converter.generated.MohawkTARBACParser;
import mohawk.converter.logging.MohawkConsoleFormatter;
import mohawk.converter.to.tred.ConvertToTRole;
import mohawk.global.parser.mohawkT.MohawkTARBACParser;
import mohawk.global.pieces.MohawkT;
import mohawk.global.timing.MohawkTiming;
......
......@@ -6,14 +6,14 @@ import java.io.*;
import java.util.ArrayList;
import java.util.logging.Logger;
import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.BaseErrorListener;
import org.apache.commons.lang3.StringUtils;
import org.junit.Before;
import org.junit.Test;
import mohawk.converter.generated.MohawkTARBACLexer;
import mohawk.converter.generated.MohawkTARBACParser;
import mohawk.global.FileExtensions;
import mohawk.global.helper.ParserHelper;
import mohawk.global.parser.mohawkT.MohawkTARBACParser;
import mohawk.global.pieces.*;
/**
......@@ -188,35 +188,6 @@ public class ConverterRegressionTests {
}
public static MohawkTARBACParser runParser(InputStream is, BaseErrorListener errorListener) throws IOException {
return runParser(is, errorListener, true);
}
public static MohawkTARBACParser runParser(InputStream is, BaseErrorListener errorListener, Boolean displayStats)
throws IOException {
ANTLRInputStream input = new ANTLRInputStream(is);
MohawkTARBACLexer lexer = new MohawkTARBACLexer(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
MohawkTARBACParser parser = new MohawkTARBACParser(tokens);
parser.removeErrorListeners();
parser.addErrorListener(errorListener);
parser.init();
if (displayStats) {
MohawkT m = parser.mohawkT;
System.out.println("Tokes: " + tokens.getText());
System.out.println("Roles: " + m.roleHelper);
System.out.println("Timeslots: " + m.timeIntervalHelper);
System.out.println("Max Timeslot: " + m.timeIntervalHelper.maxTimeSlot);
System.out.println(m.query.toString());
System.out.println(m.expectedResult.toString());
System.out.println(m.canAssign.toString());
System.out.println(m.canDisable.toString());
System.out.println(m.canEnable.toString());
System.out.println(m.canRevoke.toString());
}
return parser;
return ParserHelper.runParser(is, errorListener, true);
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment