Skip to content

Commit 4d8cd43

Browse files
authored
Merge pull request #106 from arnaud-m/raise_error_for_word_WS_word
Raise error when detecting words or numbers separated by white-spaces - close #81
2 parents 20b7ac8 + fc92e15 commit 4d8cd43

File tree

2 files changed

+45
-8
lines changed

2 files changed

+45
-8
lines changed

src/main/antlr4/Cryptator.g4

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ expression returns [ICryptaNode node] //create recursively the tree of expressio
4343
| e1=expression divORmul e2=expression {$node=new CryptaNode($divORmul.text, $e1.node, $e2.node);}
4444
| e1=expression addORsub e2=expression {$node=new CryptaNode($addORsub.text, $e1.node, $e2.node);};
4545

46+
47+
4648
word : //additional token to simplify the passage in parameter
4749
(SYMBOL|DIGIT)+;
4850

@@ -58,12 +60,15 @@ sub : '-';
5860

5961
// Lexer Rules
6062

63+
ERROR : (SYMBOL|DIGIT)+ WHITESPACE (SYMBOL|DIGIT)+;
64+
6165
COMPARATOR : '=' | '!=' | '<' | '>' | '<=' | '>=';
6266

6367
SYMBOL : [a-zA-Z\u0080-\uFFFF] {};
6468

6569
DIGIT : [0-9] {};
6670

67-
WHITESPACE : ( '\t' | ' ' | '\r' | '\n'| '\u000C' )+ -> skip ;
71+
AND : ';' | '&&';
72+
6873

69-
AND : ';' | '&&';
74+
WHITESPACE : ( '\t' | ' ' | '\r' | '\n'| '\u000C' )+ -> skip ;

src/test/java/cryptator/ParserTest.java

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,12 @@
88
*/
99
package cryptator;
1010

11-
import static cryptator.TreeTest.testInorder;
12-
import static cryptator.TreeTest.testPostorder;
13-
import static cryptator.TreeTest.testPreorder;
14-
15-
import org.junit.Test;
16-
1711
import cryptator.parser.CryptaParserException;
1812
import cryptator.parser.CryptaParserWrapper;
1913
import cryptator.specs.ICryptaNode;
14+
import org.junit.Test;
15+
16+
import static cryptator.TreeTest.*;
2017

2118
public class ParserTest {
2219

@@ -125,6 +122,31 @@ public void testParserError7() throws CryptaParserException {
125122
parser.parse("[send + more] >= money");
126123
}
127124

125+
@Test(expected = CryptaParserException.class)
126+
public void testParserError8() throws CryptaParserException {
127+
parser.parse(" send more = money");
128+
}
129+
130+
@Test(expected = CryptaParserException.class)
131+
public void testParserError9() throws CryptaParserException {
132+
parser.parse(" send more = money");
133+
}
134+
135+
@Test(expected = CryptaParserException.class)
136+
public void testParserError10() {
137+
parser.parse("send+more='1000 1'");
138+
}
139+
140+
@Test(expected = CryptaParserException.class)
141+
public void testParserError11() {
142+
parser.parse("send+more=\"1000 1\"");
143+
}
144+
145+
@Test(expected = CryptaParserException.class)
146+
public void testParserError12() {
147+
parser.parse("send + more = '1000' money");
148+
}
149+
128150
@Test
129151
public void testParserAND() throws CryptaParserException {
130152
final ICryptaNode node = parser.parse("send+more=money; d+e>=y");
@@ -246,6 +268,16 @@ public void testParserIntegerError2() throws CryptaParserException {
246268
parser.parse("send + more >= money; 1 + '12a45' = 3");
247269
}
248270

271+
@Test(expected = CryptaParserException.class)
272+
public void testParserIntegerError3() throws CryptaParserException {
273+
parser.parse("1000 1= 2");
274+
}
275+
276+
@Test(expected = CryptaParserException.class)
277+
public void testParserIntegerError4() throws CryptaParserException {
278+
parser.parse("10aa00 1zx= 2");
279+
}
280+
249281
@Test
250282
public void testParserANDsymbol() throws CryptaParserException {
251283
final ICryptaNode node = parser.parse("send+more=money && d+e>=y");

0 commit comments

Comments
 (0)