77
88import biscuit .format .schema .Schema ;
99import io .vavr .Tuple2 ;
10- import io .vavr .collection .Stream ;
1110import java .time .OffsetDateTime ;
1211import java .time .format .DateTimeParseException ;
1312import java .util .ArrayList ;
@@ -46,12 +45,13 @@ public static final class RuleBody {
4645 public final List <Expression > expressions ;
4746 public final List <Scope > scopes ;
4847
49- public RuleBody (String head , List <Predicate > predicates , List <Expression > expressions , List <Scope > scopes ) {
50- this .head = head ;
51- this .predicates = predicates ;
52- this .expressions = expressions ;
53- this .scopes = scopes ;
54- }
48+ public RuleBody (
49+ String head , List <Predicate > predicates , List <Expression > expressions , List <Scope > scopes ) {
50+ this .head = head ;
51+ this .predicates = predicates ;
52+ this .expressions = expressions ;
53+ this .scopes = scopes ;
54+ }
5555 }
5656
5757 /**
@@ -75,76 +75,72 @@ public static Result<DatalogComponents, Map<Integer, List<Error>>> datalogCompon
7575 s = removeCommentsAndWhitespaces (s );
7676 String [] codeLines = s .split (";" );
7777
78- Stream .of (codeLines )
79- .zipWithIndex ()
80- .forEach (
81- indexedLine -> {
82- String code = indexedLine ._1 .strip ();
83-
84- if (!code .isEmpty ()) {
85- List <Error > lineErrors = new ArrayList <>();
86-
87- boolean parsed ;
88- var ruleResult = rule (code );
89- if (ruleResult .isOk ()) {
90- components .rules .add (ruleResult .getOk ()._2 );
91- parsed = true ;
92- } else {
93- lineErrors .add (ruleResult .getErr ());
94- parsed = false ;
95- }
96-
97- if (!parsed ) {
98- var factResult = fact (code );
99- if (factResult .isOk ()) {
100- components .facts .add (factResult .getOk ()._2 );
101- parsed = true ;
102- } else {
103- lineErrors .add (factResult .getErr ());
104- parsed = false ;
105- }
106- }
107-
108- if (!parsed ) {
109- var checkResult = check (code );
110- if (checkResult .isOk ()) {
111- components .checks .add (checkResult .getOk ()._2 );
112- parsed = true ;
113- } else {
114- lineErrors .add (checkResult .getErr ());
115- parsed = false ;
116- }
117- }
118-
119- if (!parsed ) {
120- var scopeResult = scope (code );
121- if (scopeResult .isOk ()) {
122- components .scopes .add (scopeResult .getOk ()._2 );
123- parsed = true ;
124- } else {
125- lineErrors .add (scopeResult .getErr ());
126- parsed = false ;
127- }
128- }
129-
130- if (!parsed ) {
131- var policyResult = policy (code );
132- if (policyResult .isOk ()) {
133- components .policies .add (policyResult .getOk ()._2 );
134- parsed = true ;
135- } else {
136- lineErrors .add (policyResult .getErr ());
137- parsed = false ;
138- }
139- }
140-
141- if (!parsed ) {
142- lineErrors .forEach (System .out ::println );
143- int lineNumber = indexedLine ._2 ;
144- errors .put (lineNumber , lineErrors );
145- }
146- }
147- });
78+ for (int i = 0 ; i < codeLines .length ; ++i ) {
79+ String code = codeLines [i ];
80+
81+ if (!code .isEmpty ()) {
82+ List <Error > lineErrors = new ArrayList <>();
83+
84+ boolean parsed ;
85+ var ruleResult = rule (code );
86+ if (ruleResult .isOk ()) {
87+ components .rules .add (ruleResult .getOk ()._2 );
88+ parsed = true ;
89+ } else {
90+ lineErrors .add (ruleResult .getErr ());
91+ parsed = false ;
92+ }
93+
94+ if (!parsed ) {
95+ var factResult = fact (code );
96+ if (factResult .isOk ()) {
97+ components .facts .add (factResult .getOk ()._2 );
98+ parsed = true ;
99+ } else {
100+ lineErrors .add (factResult .getErr ());
101+ parsed = false ;
102+ }
103+ }
104+
105+ if (!parsed ) {
106+ var checkResult = check (code );
107+ if (checkResult .isOk ()) {
108+ components .checks .add (checkResult .getOk ()._2 );
109+ parsed = true ;
110+ } else {
111+ lineErrors .add (checkResult .getErr ());
112+ parsed = false ;
113+ }
114+ }
115+
116+ if (!parsed ) {
117+ var scopeResult = scope (code );
118+ if (scopeResult .isOk ()) {
119+ components .scopes .add (scopeResult .getOk ()._2 );
120+ parsed = true ;
121+ } else {
122+ lineErrors .add (scopeResult .getErr ());
123+ parsed = false ;
124+ }
125+ }
126+
127+ if (!parsed ) {
128+ var policyResult = policy (code );
129+ if (policyResult .isOk ()) {
130+ components .policies .add (policyResult .getOk ()._2 );
131+ parsed = true ;
132+ } else {
133+ lineErrors .add (policyResult .getErr ());
134+ parsed = false ;
135+ }
136+ }
137+
138+ if (!parsed ) {
139+ lineErrors .forEach (System .out ::println );
140+ errors .put (i , lineErrors );
141+ }
142+ }
143+ }
148144
149145 if (!errors .isEmpty ()) {
150146 return Result .err (errors );
@@ -233,7 +229,8 @@ public static Result<Tuple2<String, Rule>, Error> rule(String s) {
233229 RuleBody body = bodyRes .getOk ();
234230
235231 if (!body .head .isEmpty ()) {
236- return Result .err (new Error (s , "the string was not entirely parsed, remaining: " + body .head ));
232+ return Result .err (
233+ new Error (s , "the string was not entirely parsed, remaining: " + body .head ));
237234 }
238235
239236 Predicate head = t0 ._2 ;
@@ -313,7 +310,12 @@ public static Result<Tuple2<String, List<Rule>>, Error> checkBody(String s) {
313310
314311 s = body .head ;
315312 // FIXME: parse scopes
316- queries .add (new Rule (new Predicate ("query" , new ArrayList <>()), body .predicates , body .expressions , body .scopes ));
313+ queries .add (
314+ new Rule (
315+ new Predicate ("query" , new ArrayList <>()),
316+ body .predicates ,
317+ body .expressions ,
318+ body .scopes ));
317319
318320 int i = 0 ;
319321 while (true ) {
@@ -337,7 +339,11 @@ public static Result<Tuple2<String, List<Rule>>, Error> checkBody(String s) {
337339
338340 s = body2 .head ;
339341 queries .add (
340- new Rule (new Predicate ("query" , new ArrayList <>()), body2 .predicates , body2 .expressions , body2 .scopes ));
342+ new Rule (
343+ new Predicate ("query" , new ArrayList <>()),
344+ body2 .predicates ,
345+ body2 .expressions ,
346+ body2 .scopes ));
341347 }
342348
343349 return Result .ok (new Tuple2 <>(s , queries ));
@@ -377,7 +383,7 @@ public static Result<RuleBody, Error> ruleBody(String s) {
377383
378384 var res = scopes (s );
379385 if (res .isErr ()) {
380- return Result .ok (new RuleBody (s , predicates , expressions , new ArrayList <>()));
386+ return Result .ok (new RuleBody (s , predicates , expressions , new ArrayList <>()));
381387 } else {
382388 Tuple2 <String , List <Scope >> t = res .getOk ();
383389 return Result .ok (new RuleBody (t ._1 , predicates , expressions , t ._2 ));
0 commit comments