Skip to content

Commit

Permalink
Add mutable int tag
Browse files Browse the repository at this point in the history
  • Loading branch information
alancai98 committed Dec 10, 2024
1 parent c1db6ff commit 796f04e
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 14 deletions.
3 changes: 2 additions & 1 deletion partiql-ast/api/partiql-ast.api
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,11 @@ public abstract class org/partiql/ast/AstEnum : org/partiql/ast/AstNode {
}

public abstract class org/partiql/ast/AstNode {
public field tag Ljava/lang/String;
public fun <init> ()V
public abstract fun accept (Lorg/partiql/ast/AstVisitor;Ljava/lang/Object;)Ljava/lang/Object;
public abstract fun getChildren ()Ljava/util/List;
public fun getTag ()I
public fun setTag (I)V
}

public abstract class org/partiql/ast/AstRewriter : org/partiql/ast/AstVisitor {
Expand Down
8 changes: 5 additions & 3 deletions partiql-ast/src/main/java/org/partiql/ast/AstNode.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package org.partiql.ast;

import lombok.Getter;
import lombok.Setter;
import org.jetbrains.annotations.NotNull;

import java.util.List;
import java.util.Random;

/**
* TODO docs, equals, hashcode
* TODO support source location -- https://github.com/partiql/partiql-lang-kotlin/issues/1608
*/
@Setter
@Getter
public abstract class AstNode {
@NotNull
public String tag = "Ast-" + String.format("%06x", new Random().nextInt());
private int tag = 0;

@NotNull
public abstract List<AstNode> getChildren();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,9 +376,11 @@ internal class PartiQLParserDefault : PartiQLParser {
*/
private class Visitor(
private val tokens: CommonTokenStream,
private val locations: MutableMap<String, SourceLocation>,
private val locations: MutableMap<Int, SourceLocation>,
private val parameters: Map<Int, Int> = mapOf(),
) : PartiQLParserBaseVisitor<AstNode>() {
// Counter to store unique AstNode tags
private var counter = 0

companion object {

Expand All @@ -391,7 +393,7 @@ internal class PartiQLParserDefault : PartiQLParser {
tokens: CountingTokenStream,
tree: GeneratedParser.StatementsContext,
): PartiQLParser.Result {
val locations = mutableMapOf<String, SourceLocation>()
val locations = mutableMapOf<Int, SourceLocation>()
val visitor = Visitor(tokens, locations, tokens.parameterIndexes)
val statements = tree.statement().map { statementCtx ->
visitor.visit(statementCtx) as Statement
Expand Down Expand Up @@ -447,6 +449,7 @@ internal class PartiQLParserDefault : PartiQLParser {
*/
private inline fun <T : AstNode> translate(ctx: ParserRuleContext, block: () -> T): T {
val node = block()
node.tag = counter++
if (ctx.start != null) {
locations[node.tag] = SourceLocation(
ctx.start.line,
Expand Down
2 changes: 1 addition & 1 deletion partiql-spi/api/partiql-spi.api
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ public class org/partiql/spi/SourceLocations : java/util/Map {
public fun get (Ljava/lang/Object;)Lorg/partiql/spi/SourceLocation;
public fun isEmpty ()Z
public fun keySet ()Ljava/util/Set;
public fun put (Ljava/lang/Integer;Lorg/partiql/spi/SourceLocation;)Lorg/partiql/spi/SourceLocation;
public synthetic fun put (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
public fun put (Ljava/lang/String;Lorg/partiql/spi/SourceLocation;)Lorg/partiql/spi/SourceLocation;
public fun putAll (Ljava/util/Map;)V
public synthetic fun remove (Ljava/lang/Object;)Ljava/lang/Object;
public fun remove (Ljava/lang/Object;)Lorg/partiql/spi/SourceLocation;
Expand Down
14 changes: 7 additions & 7 deletions partiql-spi/src/main/java/org/partiql/spi/SourceLocations.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
* <b>Note!</b>: This class is immutable and does not support {@link Map#put(Object, Object)}, amongst others. Please
* handle the runtime exceptions indicated by {@link Map}'s Javadocs.
*/
public class SourceLocations implements Map<String, SourceLocation> {
public class SourceLocations implements Map<Integer, SourceLocation> {

private final Map<String, SourceLocation> delegate;
private final Map<Integer, SourceLocation> delegate;

/**
* Creates an empty instance.
Expand All @@ -29,20 +29,20 @@ public SourceLocations() {
* to an internal structure.
* @param delegate the delegate holding the locations.
*/
public SourceLocations(Map<String, SourceLocation> delegate) {
public SourceLocations(Map<Integer, SourceLocation> delegate) {
this.delegate = new java.util.HashMap<>();
this.delegate.putAll(delegate);
}

@NotNull
@Override
public Set<Map.Entry<String, SourceLocation>> entrySet() {
public Set<Map.Entry<Integer, SourceLocation>> entrySet() {
return delegate.entrySet();
}

@NotNull
@Override
public Set<String> keySet() {
public Set<Integer> keySet() {
return delegate.keySet();
}

Expand Down Expand Up @@ -74,7 +74,7 @@ public SourceLocation get(Object key) {

@Nullable
@Override
public SourceLocation put(String key, SourceLocation value) {
public SourceLocation put(Integer key, SourceLocation value) {
throw new UnsupportedOperationException();
}

Expand All @@ -84,7 +84,7 @@ public SourceLocation remove(Object key) {
}

@Override
public void putAll(@NotNull Map<? extends String, ? extends SourceLocation> m) {
public void putAll(@NotNull Map<? extends Integer, ? extends SourceLocation> m) {
throw new UnsupportedOperationException();
}

Expand Down

0 comments on commit 796f04e

Please sign in to comment.