Skip to content

Commit 946924c

Browse files
authored
Merge pull request #57 from arnaud-m/Allow_&&
Allow && and double ticks close #49 close #50
2 parents 9e4b410 + 657e7d9 commit 946924c

File tree

4 files changed

+369
-25
lines changed

4 files changed

+369
-25
lines changed

src/main/antlr4/Cryptator.g4

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ program : equations EOF{}; //additional token to simplify the passage in paramet
2626

2727
equations returns [ICryptaNode node] //create a node of conjuncts
2828
: equation (AND*) {$node=$equation.node;}
29-
| e1=equation (AND+) e2=equations (AND*) {$node=new CryptaNode($AND.getText(), $e1.node, $e2.node);};
29+
| e1=equation (AND+) e2=equations (AND*) {$node=new CryptaNode("&&", $e1.node, $e2.node);};
3030

3131
equation returns [ICryptaNode node] //create a node of the tree corresponding to an equation and return this node
3232
: '(' equation ')' {$node=$equation.node;}
@@ -36,6 +36,7 @@ equation returns [ICryptaNode node] //create a node of the tree corresponding t
3636
expression returns [ICryptaNode node] //create recursively the tree of expressions with priority and return the root of the tree
3737
: word {$node=new CryptaLeaf($word.text);} //create a node of the tree corresponding to a leaf and return this node
3838
| '\'' number '\'' {$node=new CryptaConstant($number.text);}
39+
| '"' number '"' {$node=new CryptaConstant($number.text);}
3940
| '(' expression ')' {$node=$expression.node;}
4041
| e1=expression modORpow e2=expression {$node=new CryptaNode($modORpow.text, $e1.node, $e2.node);} //create a node of the tree corresponding to an operation and return this node
4142
| sub expression {$node=new CryptaNode($sub.text, new CryptaConstant("0"), $expression.node);}
@@ -65,4 +66,4 @@ DIGIT : [0-9] {};
6566

6667
WHITESPACE : ( '\t' | ' ' | '\r' | '\n'| '\u000C' )+ -> skip ;
6768

68-
AND : ';';
69+
AND : ';' | '&&';

src/main/java/cryptator/CryptaOperator.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ public enum CryptaOperator {
3535
LE("<=", (a, b) -> toBigInt(a.compareTo(b) <= 0), (a, b) -> a.le(b)),
3636
GE(">=", (a, b) -> toBigInt(a.compareTo(b) >= 0), (a, b) -> a.ge(b)),
3737

38-
AND(";", (a, b) -> toBigInt(!a.equals(BigInteger.ZERO) && !b.equals(BigInteger.ZERO)), (a, b) -> ((ReExpression) a).and((ReExpression) b));
39-
38+
AND("&&", (a, b) -> toBigInt(!a.equals(BigInteger.ZERO) && !b.equals(BigInteger.ZERO)), (a, b) -> ((ReExpression) a).and((ReExpression) b));
4039
public final String token;
4140

4241
public final BinaryOperator<BigInteger> function;

src/test/java/cryptator/ParserTest.java

Lines changed: 201 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -126,18 +126,18 @@ public void testParserError7() throws CryptaParserException {
126126
public void testParserAND() throws CryptaParserException {
127127
final ICryptaNode node = parser.parse("send+more=money; d+e>=y");
128128

129-
testPreorder("; = + send more money >= + d e y ", node);
130-
testPostorder("send more + money = d e + y >= ; ", node);
131-
testInorder("send + more = money ; d + e >= y ", node);
129+
testPreorder("&& = + send more money >= + d e y ", node);
130+
testPostorder("send more + money = d e + y >= && ", node);
131+
testInorder("send + more = money && d + e >= y ", node);
132132
}
133133

134134
@Test
135135
public void testParserAND2() throws CryptaParserException {
136136
final ICryptaNode node = parser.parse("send+more= money; -send -more= \n -money");
137137

138-
testPreorder("; = + send more money = - - " + ParserTest.ZERO + " send more - " + ParserTest.ZERO + " money ", node);
139-
testPostorder("send more + money = " + ParserTest.ZERO + " send - more - " + ParserTest.ZERO + " money - = ; ", node);
140-
testInorder("send + more = money ; " + ParserTest.ZERO + " - send - more = " + ParserTest.ZERO + " - money ", node);
138+
testPreorder("&& = + send more money = - - " + ParserTest.ZERO + " send more - " + ParserTest.ZERO + " money ", node);
139+
testPostorder("send more + money = " + ParserTest.ZERO + " send - more - " + ParserTest.ZERO + " money - = && ", node);
140+
testInorder("send + more = money && " + ParserTest.ZERO + " - send - more = " + ParserTest.ZERO + " - money ", node);
141141

142142
}
143143

@@ -146,35 +146,35 @@ public void testParserAND3() throws CryptaParserException {
146146
var str = "send + more ^ more = money ";
147147
final ICryptaNode node = parser.parse(str + ";" + str);
148148
var preord = "= + send ^ more more money ";
149-
testPreorder("; " + preord + preord, node);
149+
testPreorder("&& " + preord + preord, node);
150150
var postord = "send more more ^ + money =";
151-
testPostorder(postord + " " + postord + " ; ", node);
152-
testInorder(str + "; " + str, node);
151+
testPostorder(postord + " " + postord + " && ", node);
152+
testInorder(str + "&& " + str, node);
153153
}
154154

155155
@Test
156156
public void testParserAND4() throws CryptaParserException {
157157
final ICryptaNode node = parser.parse("send+more=money;; d+e>=y");
158158

159-
testPreorder("; = + send more money >= + d e y ", node);
160-
testPostorder("send more + money = d e + y >= ; ", node);
161-
testInorder("send + more = money ; d + e >= y ", node);
159+
testPreorder("&& = + send more money >= + d e y ", node);
160+
testPostorder("send more + money = d e + y >= && ", node);
161+
testInorder("send + more = money && d + e >= y ", node);
162162
}
163163

164164
@Test
165165
public void testParserAND5() throws CryptaParserException {
166166
final ICryptaNode node = parser.parse("A = B;; A = B");
167-
testPreorder("; = A B = A B ", node);
168-
testPostorder("A B = A B = ; ", node);
169-
testInorder("A = B ; A = B ", node);
167+
testPreorder("&& = A B = A B ", node);
168+
testPostorder("A B = A B = && ", node);
169+
testInorder("A = B && A = B ", node);
170170
}
171171

172172
@Test
173173
public void testParserAND6() throws CryptaParserException {
174174
final ICryptaNode node = parser.parse("A = B;; A = B;;;;;");
175-
testPreorder("; = A B = A B ", node);
176-
testPostorder("A B = A B = ; ", node);
177-
testInorder("A = B ; A = B ", node);
175+
testPreorder("&& = A B = A B ", node);
176+
testPostorder("A B = A B = && ", node);
177+
testInorder("A = B && A = B ", node);
178178
}
179179

180180
@Test
@@ -225,9 +225,9 @@ public void testParserErrorAND7() throws CryptaParserException {
225225
public void testParserInteger1() throws CryptaParserException {
226226
final ICryptaNode node = parser.parse("send+more='1234';; d+e>=y");
227227

228-
testPreorder("; = + send more '1234' >= + d e y ", node);
229-
testPostorder("send more + '1234' = d e + y >= ; ", node);
230-
testInorder("send + more = '1234' ; d + e >= y ", node);
228+
testPreorder("&& = + send more '1234' >= + d e y ", node);
229+
testPostorder("send more + '1234' = d e + y >= && ", node);
230+
testInorder("send + more = '1234' && d + e >= y ", node);
231231
}
232232

233233
@Test(expected = CryptaParserException.class)
@@ -240,4 +240,184 @@ public void testParserIntegerError2() throws CryptaParserException {
240240
parser.parse("send + more >= money; 1 + '12a45' = 3");
241241
}
242242

243+
@Test
244+
public void testParserANDsymbol() throws CryptaParserException {
245+
final ICryptaNode node = parser.parse("send+more=money && d+e>=y");
246+
247+
testPreorder("&& = + send more money >= + d e y ", node);
248+
testPostorder("send more + money = d e + y >= && ", node);
249+
testInorder("send + more = money && d + e >= y ", node);
250+
}
251+
252+
@Test
253+
public void testParserAND2symbol() throws CryptaParserException {
254+
final ICryptaNode node = parser.parse("send+more= money&& -send -more= \n -money");
255+
256+
testPreorder("&& = + send more money = - - " + ParserTest.ZERO + " send more - " + ParserTest.ZERO + " money ", node);
257+
testPostorder("send more + money = " + ParserTest.ZERO + " send - more - " + ParserTest.ZERO + " money - = && ", node);
258+
testInorder("send + more = money && " + ParserTest.ZERO + " - send - more = " + ParserTest.ZERO + " - money ", node);
259+
260+
}
261+
262+
@Test
263+
public void testParserAND3symbol() throws CryptaParserException {
264+
var str = "send + more ^ more = money ";
265+
final ICryptaNode node = parser.parse(str + "&&" + str);
266+
var preord = "= + send ^ more more money ";
267+
testPreorder("&& " + preord + preord, node);
268+
var postord = "send more more ^ + money =";
269+
testPostorder(postord + " " + postord + " && ", node);
270+
testInorder(str + "&& " + str, node);
271+
}
272+
273+
@Test
274+
public void testParserAND4symbol() throws CryptaParserException {
275+
final ICryptaNode node = parser.parse("send+more=money&&&& d+e>=y");
276+
277+
testPreorder("&& = + send more money >= + d e y ", node);
278+
testPostorder("send more + money = d e + y >= && ", node);
279+
testInorder("send + more = money && d + e >= y ", node);
280+
}
281+
282+
@Test
283+
public void testParserAND5symbol() throws CryptaParserException {
284+
final ICryptaNode node = parser.parse("A = B&&&& A = B");
285+
testPreorder("&& = A B = A B ", node);
286+
testPostorder("A B = A B = && ", node);
287+
testInorder("A = B && A = B ", node);
288+
}
289+
290+
@Test
291+
public void testParserAND6symbol() throws CryptaParserException {
292+
final ICryptaNode node = parser.parse("A = B&&&& A = B&&&&&&&&&&");
293+
testPreorder("&& = A B = A B ", node);
294+
testPostorder("A B = A B = && ", node);
295+
testInorder("A = B && A = B ", node);
296+
}
297+
298+
@Test
299+
public void testParserAND7symbol() throws CryptaParserException {
300+
final ICryptaNode node = parser.parse("a=b&&");
301+
testPreorder("= a b ", node);
302+
testPostorder("a b = ", node);
303+
testInorder("a = b ", node);
304+
}
305+
306+
@Test
307+
public void testParserAND8symbol() throws CryptaParserException {
308+
final ICryptaNode node = parser.parse("a=b&& b=c ; c=d");
309+
testPreorder("&& = a b && = b c = c d ", node);
310+
testPostorder("a b = b c = c d = && && ", node);
311+
testInorder("a = b && b = c && c = d ", node);
312+
}
313+
314+
@Test(expected = CryptaParserException.class)
315+
public void testParserErrorAND2symbol() throws CryptaParserException {
316+
parser.parse("&& a=b");
317+
}
318+
319+
@Test(expected = CryptaParserException.class)
320+
public void testParserErrorAND3symbol() throws CryptaParserException {
321+
parser.parse("(A = B&& A = B)");
322+
}
323+
324+
@Test(expected = CryptaParserException.class)
325+
public void testParserErrorAND4symbol() throws CryptaParserException {
326+
parser.parse("(A !=&& A = B)");
327+
}
328+
329+
@Test(expected = CryptaParserException.class)
330+
public void testParserErrorAND5symbol() throws CryptaParserException {
331+
parser.parse("A !=&& (A = B)");
332+
}
333+
334+
@Test(expected = CryptaParserException.class)
335+
public void testParserErrorAND6symbol() throws CryptaParserException {
336+
parser.parse("&&");
337+
}
338+
339+
@Test(expected = CryptaParserException.class)
340+
public void testParserErrorAND7symbol() throws CryptaParserException {
341+
parser.parse("&&a=b&& b=c&&");
342+
}
343+
344+
@Test(expected = CryptaParserException.class)
345+
public void testParserErrorAND8symbol() throws CryptaParserException {
346+
parser.parse("a=b& b=c");
347+
}
348+
349+
@Test(expected = CryptaParserException.class)
350+
public void testParserErrorAND9symbol() throws CryptaParserException {
351+
parser.parse("a=b&&& b=c");
352+
}
353+
354+
// Test on parse integers
355+
@Test
356+
public void testParserInteger1symbol() throws CryptaParserException {
357+
final ICryptaNode node = parser.parse("send+more='1234' && && d+e>=y");
358+
359+
testPreorder("&& = + send more '1234' >= + d e y ", node);
360+
testPostorder("send more + '1234' = d e + y >= && ", node);
361+
testInorder("send + more = '1234' && d + e >= y ", node);
362+
}
363+
364+
@Test(expected = CryptaParserException.class)
365+
public void testParserIntegerError1symbol() throws CryptaParserException {
366+
parser.parse("send + more >= money&&1 + 'aabc' = 3");
367+
}
368+
369+
@Test(expected = CryptaParserException.class)
370+
public void testParserIntegerError2symbol() throws CryptaParserException {
371+
parser.parse("send + more >= money&& 1 + '12a45' = 3");
372+
}
373+
374+
@Test(expected = CryptaParserException.class)
375+
public void testParserIntegerError3symbol() throws CryptaParserException {
376+
parser.parse("send + more >= money&&1 + '' = 3");
377+
}
378+
379+
@Test(expected = CryptaParserException.class)
380+
public void testParserIntegerError4symbol() throws CryptaParserException {
381+
parser.parse("send + more >= money&&1 + \"\" = 3");
382+
}
383+
384+
@Test(expected = CryptaParserException.class)
385+
public void testParserIntegerError5symbol() throws CryptaParserException {
386+
parser.parse("send + more >= money&&1 + '\"\"' = 3");
387+
}
388+
389+
@Test(expected = CryptaParserException.class)
390+
public void testParserIntegerError6symbol() throws CryptaParserException {
391+
parser.parse("send + more >= money&&1 + \"''\" = 3");
392+
}
393+
394+
@Test
395+
public void testParserInteger1symboldoubleticks() throws CryptaParserException {
396+
final ICryptaNode node = parser.parse("send+more=\"1234\" && && d+e>=y");
397+
398+
testPreorder("&& = + send more '1234' >= + d e y ", node);
399+
testPostorder("send more + '1234' = d e + y >= && ", node);
400+
testInorder("send + more = '1234' && d + e >= y ", node);
401+
}
402+
403+
@Test(expected = CryptaParserException.class)
404+
public void testParserIntegerError1symboldoubleticks() throws CryptaParserException {
405+
parser.parse("send + more >= money&&1 + \"aabc\" = 3");
406+
}
407+
408+
@Test(expected = CryptaParserException.class)
409+
public void testParserIntegerError2symboldoubleticks() throws CryptaParserException {
410+
parser.parse("send + more >= money&& 1 + \"12a45\" = 3");
411+
}
412+
413+
@Test(expected = CryptaParserException.class)
414+
public void testParserIntegerError3symboldoubleticks() throws CryptaParserException {
415+
final ICryptaNode node = parser.parse("send+more=\"1234' && && d+e>=y");
416+
}
417+
418+
@Test(expected = CryptaParserException.class)
419+
public void testParserIntegerError4symboldoubleticks() throws CryptaParserException {
420+
final ICryptaNode node = parser.parse("send+more='1234\" && && d+e>=y");
421+
}
422+
243423
}

0 commit comments

Comments
 (0)