summaryrefslogtreecommitdiff
path: root/src/main/java/org/zwobble
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2026-06-14 11:12:46 +0100
committerMichael Williamson <mike@zwobble.org>2026-06-14 11:12:46 +0100
commit6465a06977c79ccb3c328d3c97d6873066e46b11 (patch)
treee50fbbee8b097051a3a1b9c96943a42895390c4c /src/main/java/org/zwobble
parent8f4049e29116e1f80a2ab8d740ca1f4519a64aa2 (diff)
Create ancestor depth field
Diffstat (limited to 'src/main/java/org/zwobble')
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedImportNode.java16
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/parser/Parser.java55
2 files changed, 30 insertions, 41 deletions
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedImportNode.java b/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedImportNode.java
index d457b60..2355e41 100644
--- a/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedImportNode.java
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedImportNode.java
@@ -2,22 +2,26 @@
package org.zwobble.hobgoblin.compiler.ast.untyped;
-public record UntypedImportNode(java.util.List<String> importedNames, org.zwobble.hobgoblin.compiler.types.NamespaceName namespaceName) {
+public record UntypedImportNode(java.util.List<String> importedNames, java.util.Optional<java.lang.Integer> ancestorDepth, org.zwobble.hobgoblin.compiler.types.NamespaceName namespaceName) {
public static org.zwobble.hobgoblin.compiler.ast.untyped.UntypedImportNode.Builder arbitrary() {
- return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedImportNode.Builder(java.util.List.of(), org.zwobble.hobgoblin.compiler.ast.untyped.Native.arbitraryNamespaceName());
+ return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedImportNode.Builder(java.util.List.of(), java.util.Optional.empty(), org.zwobble.hobgoblin.compiler.ast.untyped.Native.arbitraryNamespaceName());
}
- public record Builder(java.util.List<String> importedNames, org.zwobble.hobgoblin.compiler.types.NamespaceName namespaceName) {
+ public record Builder(java.util.List<String> importedNames, java.util.Optional<java.lang.Integer> ancestorDepth, org.zwobble.hobgoblin.compiler.types.NamespaceName namespaceName) {
public org.zwobble.hobgoblin.compiler.ast.untyped.UntypedImportNode build() {
- return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedImportNode(importedNames, namespaceName);
+ return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedImportNode(importedNames, ancestorDepth, namespaceName);
}
public org.zwobble.hobgoblin.compiler.ast.untyped.UntypedImportNode.Builder withImportedNames(java.util.List<String> importedNames) {
- return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedImportNode.Builder(importedNames, namespaceName);
+ return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedImportNode.Builder(importedNames, ancestorDepth, namespaceName);
+ }
+
+ public org.zwobble.hobgoblin.compiler.ast.untyped.UntypedImportNode.Builder withAncestorDepth(java.util.Optional<java.lang.Integer> ancestorDepth) {
+ return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedImportNode.Builder(importedNames, ancestorDepth, namespaceName);
}
public org.zwobble.hobgoblin.compiler.ast.untyped.UntypedImportNode.Builder withNamespaceName(org.zwobble.hobgoblin.compiler.types.NamespaceName namespaceName) {
- return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedImportNode.Builder(importedNames, namespaceName);
+ return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedImportNode.Builder(importedNames, ancestorDepth, namespaceName);
}
// Custom area start: org.zwobble.hobgoblin.compiler.ast.untyped.UntypedImportNode.Builder body
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/parser/Parser.java b/src/main/java/org/zwobble/hobgoblin/compiler/parser/Parser.java
index 2a51257..72bd60e 100644
--- a/src/main/java/org/zwobble/hobgoblin/compiler/parser/Parser.java
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/parser/Parser.java
@@ -11,6 +11,7 @@ import org.zwobble.sourcetext.SourceText;
import java.util.ArrayList;
import java.util.List;
+import java.util.Optional;
import java.util.function.BooleanSupplier;
import java.util.function.Supplier;
@@ -68,11 +69,28 @@ public class Parser {
tokens.skip(TokenType.SYMBOL_BRACE_CLOSE);
tokens.skip(TokenType.KEYWORD_FROM);
- var namespaceName = parseRelativeNamespaceName(tokens);
+ var isSibling = tokens.trySkip(TokenType.SYMBOL_DOT);
+
+ Optional<Integer> ancestorDepth;
+ if (isSibling) {
+ tokens.skip(TokenType.SYMBOL_FORWARD_SLASH);
+ ancestorDepth = Optional.of(0);
+ } else if (tokens.isNext(TokenType.SYMBOL_DOT_DOT)) {
+ int depth = 0;
+ while (tokens.trySkip(TokenType.SYMBOL_DOT_DOT)) {
+ depth += 1;
+ tokens.skip(TokenType.SYMBOL_FORWARD_SLASH);
+ }
+ ancestorDepth = Optional.of(depth);
+ } else {
+ ancestorDepth = Optional.empty();
+ }
+
+ var namespaceName = parseNamespaceName(tokens);
tokens.skip(TokenType.SYMBOL_SEMICOLON);
- return new UntypedImportNode(importedNames, namespaceName);
+ return new UntypedImportNode(importedNames, ancestorDepth, namespaceName);
}
static UntypedNamespaceStatementNode parseNamespaceStatement(
@@ -307,39 +325,6 @@ public class Parser {
return new NamespaceName(namespaceParts);
}
- private static NamespaceName parseRelativeNamespaceName(TokenIterator tokens) {
- var isSibling = tokens.trySkip(TokenType.SYMBOL_DOT);
-
- List<String> parentParts;
- if (isSibling) {
- tokens.skip(TokenType.SYMBOL_FORWARD_SLASH);
- parentParts = List.of(".");
- } else if (tokens.isNext(TokenType.SYMBOL_DOT_DOT)) {
- parentParts = new ArrayList<>();
- while (tokens.trySkip(TokenType.SYMBOL_DOT_DOT)) {
- parentParts.add("..");
- tokens.skip(TokenType.SYMBOL_FORWARD_SLASH);
- }
- } else {
- parentParts = List.of(parseIdentifier(tokens));
- }
-
- var childParts = parseMany(
- () -> !tokens.isNext(TokenType.IDENTIFIER),
- () -> {
- tokens.trySkip(TokenType.SYMBOL_FORWARD_SLASH);
- return parseIdentifier(tokens);
- },
- () -> true
- );
-
- var namespaceParts = new ArrayList<String>();
- namespaceParts.addAll(parentParts);
- namespaceParts.addAll(childParts);
-
- return new NamespaceName(namespaceParts);
- }
-
private static String parseIdentifier(TokenIterator tokens) {
return tokens.next(TokenType.IDENTIFIER).charSequence().toString();
}