From 7a91dc973a23e34cee3843ca3793d5e0879d1c74 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 22 Oct 2023 22:36:25 +0100 Subject: Forbid leading zeroes in int literal --- .../parser/IntLiteralCannotHaveLeadingZeroesError.java | 10 ++++++++++ src/main/java/org/shedlang/compiler/parser/Parser.java | 6 ++++++ 2 files changed, 16 insertions(+) create mode 100644 src/main/java/org/shedlang/compiler/parser/IntLiteralCannotHaveLeadingZeroesError.java (limited to 'src/main/java') 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); } -- cgit v1.2.3