aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-10-22 22:36:25 +0100
committerMichael Williamson <mike@zwobble.org>2023-10-22 22:36:33 +0100
commit7a91dc973a23e34cee3843ca3793d5e0879d1c74 (patch)
tree93a9ef7794cf370b0e16625b6aacf6e7713f2c80 /src/main/java
parent6f4afccfe077fa305d5cabe24c1a133bc8c20fcc (diff)
Forbid leading zeroes in int literal
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/org/shedlang/compiler/parser/IntLiteralCannotHaveLeadingZeroesError.java10
-rw-r--r--src/main/java/org/shedlang/compiler/parser/Parser.java6
2 files changed, 16 insertions, 0 deletions
diff --git a/src/main/java/org/shedlang/compiler/parser/IntLiteralCannotHaveLeadingZeroesError.java b/src/main/java/org/shedlang/compiler/parser/IntLiteralCannotHaveLeadingZeroesError.java
new file mode 100644
index 00000000..28421cad
--- /dev/null
+++ b/src/main/java/org/shedlang/compiler/parser/IntLiteralCannotHaveLeadingZeroesError.java
@@ -0,0 +1,10 @@
+package org.shedlang.compiler.parser;
+
+import org.shedlang.compiler.errors.SourceError;
+import org.shedlang.compiler.sources.Source;
+
+public class IntLiteralCannotHaveLeadingZeroesError extends SourceError {
+ public IntLiteralCannotHaveLeadingZeroesError(Source source) {
+ super("int literal cannot have leading zeroes", source);
+ }
+}
diff --git a/src/main/java/org/shedlang/compiler/parser/Parser.java b/src/main/java/org/shedlang/compiler/parser/Parser.java
index 763d0a01..fcc92d99 100644
--- a/src/main/java/org/shedlang/compiler/parser/Parser.java
+++ b/src/main/java/org/shedlang/compiler/parser/Parser.java
@@ -481,6 +481,12 @@ public class Parser {
var end = tokens.position();
var source = start.to(end);
+ if (string.length() > 1 && string.startsWith("0")) {
+ throw new IntLiteralCannotHaveLeadingZeroesError(source);
+ } else if (string.length() > 2 && string.startsWith("-0")) {
+ throw new IntLiteralCannotHaveLeadingZeroesError(source);
+ }
+
return new UntypedIntLiteralNode(value, source);
}