You are viewing a plain text version of this content. The canonical link for it is here.
Posted to pr@cassandra.apache.org by "jacek-lewandowski (via GitHub)" <gi...@apache.org> on 2023/03/10 08:18:11 UTC

[GitHub] [cassandra] jacek-lewandowski commented on a diff in pull request #2195: [CASSANDRA-18302] Statement source descriptions

jacek-lewandowski commented on code in PR #2195:
URL: https://github.com/apache/cassandra/pull/2195#discussion_r1132070531


##########
src/java/org/apache/cassandra/cql3/StatementSource.java:
##########
@@ -0,0 +1,162 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.cassandra.cql3;
+
+import java.util.Objects;
+import java.util.regex.Pattern;
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+
+import com.google.common.base.Preconditions;
+
+import org.antlr.runtime.Token;
+import org.apache.cassandra.cql3.statements.QualifiedStatement;
+
+public class StatementSource
+{
+    public static final StatementSource INTERNAL = Bounds.INTERNAL.describeSource(null);
+
+    public final String text;
+    public final Bounds bounds;
+
+    public StatementSource(@Nullable String text, @Nonnull Bounds bounds)
+    {
+        Objects.requireNonNull(bounds);
+        this.text = text;
+        this.bounds = bounds;
+    }
+
+    public String describe(boolean mayIncludeSourceText)
+    {
+        if (this == INTERNAL)
+        {
+            return "<<<internal statement>>>";
+        }
+        else
+        {
+            if (bounds != Bounds.INTERNAL && !bounds.isEmpty() && text != null && mayIncludeSourceText)
+                return String.format("at %d:%d - %s", bounds.startLine + 1, bounds.startChar + 1, text);
+            else if (bounds != Bounds.INTERNAL && !bounds.isEmpty())
+                return String.format("at %d:%d", bounds.startLine + 1, bounds.startChar + 1);
+            else if (text != null && !bounds.isEmpty() && mayIncludeSourceText)
+                return String.format("%s", text);
+            else
+                return "";
+        }
+    }
+
+    public static Bounds createBounds(Token startToken, Token endToken)
+    {
+        Objects.requireNonNull(startToken);
+        Objects.requireNonNull(endToken);
+
+        int startLine, startChar, endLine, endChar;
+
+        if (startToken.getType() == Token.EOF)
+        {
+            startLine = Character.MAX_VALUE;
+            startChar = Character.MAX_VALUE;
+        }
+        else
+        {
+            startLine = Math.min(Math.max(startToken.getLine(), 1) - 1, Character.MAX_VALUE);
+            startChar = Math.min(Math.max(startToken.getCharPositionInLine(), 0), Character.MAX_VALUE);
+        }
+
+        if (endToken.getType() == Token.EOF)
+        {
+            endLine = Character.MAX_VALUE;
+            endChar = Character.MAX_VALUE;
+        }
+        else
+        {
+            endLine = Math.min(Math.max(endToken.getLine(), 1) - 1, Character.MAX_VALUE);
+            endChar = Math.min(Math.max(endToken.getCharPositionInLine(), 0), Character.MAX_VALUE);
+        }
+
+        return new Bounds(startLine, startChar, endLine, endChar);
+    }
+
+    public static class Bounds
+    {
+        public static final Bounds INTERNAL = new Bounds(0, 0, Character.MAX_VALUE, Character.MAX_VALUE);
+
+        private static final Pattern LINE_BREAK = Pattern.compile("\\R");
+        private static final String SEPARATOR = " ";
+
+        private final int startLine;
+        private final int startChar;
+        private final int endLine;
+        private final int endChar;
+
+        private Bounds(int startLine, int startChar, int endLine, int endChar)
+        {
+            Preconditions.checkArgument(startLine >= 0 && startLine <= Character.MAX_VALUE);
+            Preconditions.checkArgument(startChar >= 0 && startChar <= Character.MAX_VALUE);
+            Preconditions.checkArgument(endLine >= 0 && endLine <= Character.MAX_VALUE);
+            Preconditions.checkArgument(endChar >= 0 && endChar <= Character.MAX_VALUE);
+
+            this.startLine = startLine;
+            this.startChar = startChar;
+            this.endLine = endLine;
+            this.endChar = endChar;
+        }
+
+        private boolean isEmpty()
+        {
+            return startLine == Character.MAX_VALUE ||
+                   startLine > endLine ||

Review Comment:
   In all those pointed out cases I tried to make this class super resistant to wrong input regardless of the circumstances. But it can fail as well.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org