dcapwell commented on code in PR #2195:
URL: https://github.com/apache/cassandra/pull/2195#discussion_r1137382160


##########
src/java/org/apache/cassandra/cql3/StatementSource.java:
##########
@@ -0,0 +1,87 @@
+/*
+ * 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.Nullable;
+
+import org.antlr.runtime.Token;
+import org.antlr.runtime.TokenStream;
+
+import static java.lang.Math.max;
+import static java.lang.Math.min;
+
+public class StatementSource
+{
+    public static final StatementSource INTERNAL = new StatementSource(0, 0, 
null);
+    private static final Pattern TWO_OR_MORE_WHITESPACES = 
Pattern.compile("\\s{2,}");
+
+    public final int line;
+    public final int charPositionInLine;
+    public final String text;
+
+    public StatementSource(int line, int charPositionInLine, @Nullable String 
text)
+    {
+        this.line = line;
+        this.charPositionInLine = charPositionInLine;
+        this.text = text;
+    }
+
+    public String describe(boolean mayIncludeSourceText)
+    {
+        if (this == INTERNAL)
+        {
+            return "<<<internal statement>>>";
+        }
+        else
+        {
+            if (!isEmpty() && text != null && mayIncludeSourceText)
+                return String.format("at [%d:%d] - %s", line + 1, 
charPositionInLine + 1, text);
+            else if (!isEmpty())
+                return String.format("at [%d:%d]", line + 1, 
charPositionInLine + 1);
+            else
+                return "";
+        }
+    }
+
+    public boolean isEmpty()
+    {
+        return line > Character.MAX_VALUE || line == Character.MAX_VALUE && 
charPositionInLine > Character.MAX_VALUE;
+    }
+
+    public static StatementSource create(Token startToken, Token endToken, 
TokenStream input)
+    {
+        Objects.requireNonNull(startToken);
+        Objects.requireNonNull(endToken);
+        Objects.requireNonNull(input);
+
+        if (startToken.getType() == Token.EOF)
+            return new StatementSource(Character.MAX_VALUE + 1, 0, null);
+
+        int startLine = min(max(startToken.getLine(), 1) - 1, 
Character.MAX_VALUE);
+        int startChar = min(max(startToken.getCharPositionInLine(), 0), 
Character.MAX_VALUE);
+        String txt = input.toString(startToken, endToken);
+        txt = txt.replace('\n', ' ');
+        txt = TWO_OR_MORE_WHITESPACES.matcher(txt).replaceAll(" ").trim();

Review Comment:
   we should leave the input alone, if we want to show the string the users 
provided, we should show as-is



##########
src/java/org/apache/cassandra/cql3/StatementSource.java:
##########
@@ -0,0 +1,87 @@
+/*
+ * 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.Nullable;
+
+import org.antlr.runtime.Token;
+import org.antlr.runtime.TokenStream;
+
+import static java.lang.Math.max;
+import static java.lang.Math.min;
+
+public class StatementSource
+{
+    public static final StatementSource INTERNAL = new StatementSource(0, 0, 
null);
+    private static final Pattern TWO_OR_MORE_WHITESPACES = 
Pattern.compile("\\s{2,}");
+
+    public final int line;
+    public final int charPositionInLine;
+    public final String text;
+
+    public StatementSource(int line, int charPositionInLine, @Nullable String 
text)
+    {
+        this.line = line;
+        this.charPositionInLine = charPositionInLine;
+        this.text = text;
+    }
+
+    public String describe(boolean mayIncludeSourceText)
+    {
+        if (this == INTERNAL)
+        {
+            return "<<<internal statement>>>";
+        }
+        else
+        {
+            if (!isEmpty() && text != null && mayIncludeSourceText)
+                return String.format("at [%d:%d] - %s", line + 1, 
charPositionInLine + 1, text);
+            else if (!isEmpty())
+                return String.format("at [%d:%d]", line + 1, 
charPositionInLine + 1);
+            else
+                return "";
+        }
+    }
+
+    public boolean isEmpty()
+    {
+        return line > Character.MAX_VALUE || line == Character.MAX_VALUE && 
charPositionInLine > Character.MAX_VALUE;
+    }
+
+    public static StatementSource create(Token startToken, Token endToken, 
TokenStream input)
+    {
+        Objects.requireNonNull(startToken);
+        Objects.requireNonNull(endToken);
+        Objects.requireNonNull(input);
+
+        if (startToken.getType() == Token.EOF)
+            return new StatementSource(Character.MAX_VALUE + 1, 0, null);
+
+        int startLine = min(max(startToken.getLine(), 1) - 1, 
Character.MAX_VALUE);
+        int startChar = min(max(startToken.getCharPositionInLine(), 0), 
Character.MAX_VALUE);
+        String txt = input.toString(startToken, endToken);
+        txt = txt.replace('\n', ' ');

Review Comment:
   we should keep the newline,
   
   ```
   SELECT a,
                  b,
                  c -- should fail at line 3 due to 3 not being in the schema
   ```



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to