summaryrefslogtreecommitdiff
path: root/chapter17/src
diff options
context:
space:
mode:
authorCliff Click <cliffc@acm.org>2024-12-30 14:16:36 -0800
committerCliff Click <cliffc@acm.org>2025-01-15 15:07:30 -0800
commitcaa2b978e5a0a83b2615e1e703ce1ec07bf86f77 (patch)
tree78300f36d033dd22b3bcf62ffec6bcc34da32be0 /chapter17/src
parent5fe11b24e49c0a7bb84eeb76f724133e3298a1a9 (diff)
downloadSimple-caa2b978e5a0a83b2615e1e703ce1ec07bf86f77.tar.gz
Simple-caa2b978e5a0a83b2615e1e703ce1ec07bf86f77.zip
Test for "arg--2"
Parse as "arg - -2" and not "arg-- 2" which is a syntax error
Diffstat (limited to 'chapter17/src')
-rw-r--r--chapter17/src/main/java/com/seaofnodes/simple/Parser.java2
-rw-r--r--chapter17/src/test/java/com/seaofnodes/simple/Chapter04Test.java6
2 files changed, 8 insertions, 0 deletions
diff --git a/chapter17/src/main/java/com/seaofnodes/simple/Parser.java b/chapter17/src/main/java/com/seaofnodes/simple/Parser.java
index 1cdf27fb..570ae67e 100644
--- a/chapter17/src/main/java/com/seaofnodes/simple/Parser.java
+++ b/chapter17/src/main/java/com/seaofnodes/simple/Parser.java
@@ -1412,6 +1412,7 @@ public class Parser {
// Next oper= character, or 0.
// As a convenience, mark "++" as a char 1 and "--" as char -1 (65535)
+ // Disallow e.g. "arg--1" which should parse as "arg - -1"
public char matchOperAssign() {
skipWhiteSpace();
if( _position+2 >= _input.length ) return 0;
@@ -1419,6 +1420,7 @@ public class Parser {
if( "+-/*&|^".indexOf(ch0) == -1 ) return 0;
char ch1 = (char)_input[_position+1];
if( ch1 == '=' ) { _position += 2; return ch0; }
+ if( Character.isDigit(_input[_position+2]) ) return 0;
if( ch0 == '+' && ch1 == '+' ) { _position += 2; return (char) 1; }
if( ch0 == '-' && ch1 == '-' ) { _position += 2; return (char)-1; }
return 0;
diff --git a/chapter17/src/test/java/com/seaofnodes/simple/Chapter04Test.java b/chapter17/src/test/java/com/seaofnodes/simple/Chapter04Test.java
index 24612fee..0db3afde 100644
--- a/chapter17/src/test/java/com/seaofnodes/simple/Chapter04Test.java
+++ b/chapter17/src/test/java/com/seaofnodes/simple/Chapter04Test.java
@@ -125,4 +125,10 @@ public class Chapter04Test {
assertEquals("return (-arg);", ret.print());
}
+ @Test
+ public void testBug5() {
+ Parser parser = new Parser("return arg--2;");
+ StopNode ret = parser.parse();
+ assertEquals("return (arg--2);", ret.print());
+ }
}