You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by gn...@apache.org on 2018/02/06 15:27:05 UTC

[karaf] branch master updated: [KARAF-5246] The command parser should be more tolerant when parsing the line during completion

This is an automated email from the ASF dual-hosted git repository.

gnodet pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/karaf.git


The following commit(s) were added to refs/heads/master by this push:
     new e637284  [KARAF-5246] The command parser should be more tolerant when parsing the line during completion
e637284 is described below

commit e637284031da0d8c8dc3e8ac2e8557928f0eaf00
Author: Guillaume Nodet <gn...@gmail.com>
AuthorDate: Tue Feb 6 16:26:33 2018 +0100

    [KARAF-5246] The command parser should be more tolerant when parsing the line during completion
---
 .../shell/impl/console/parsing/KarafParser.java    | 33 ++++++++++++++++++----
 1 file changed, 28 insertions(+), 5 deletions(-)

diff --git a/shell/core/src/main/java/org/apache/karaf/shell/impl/console/parsing/KarafParser.java b/shell/core/src/main/java/org/apache/karaf/shell/impl/console/parsing/KarafParser.java
index 0b87ccc..833745b 100644
--- a/shell/core/src/main/java/org/apache/karaf/shell/impl/console/parsing/KarafParser.java
+++ b/shell/core/src/main/java/org/apache/karaf/shell/impl/console/parsing/KarafParser.java
@@ -18,6 +18,7 @@
  */
 package org.apache.karaf.shell.impl.console.parsing;
 
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.List;
@@ -27,6 +28,7 @@ import org.apache.felix.gogo.runtime.EOFError;
 import org.apache.felix.gogo.runtime.Parser.Program;
 import org.apache.felix.gogo.runtime.Parser.Statement;
 import org.apache.felix.gogo.runtime.SyntaxError;
+import org.apache.felix.gogo.runtime.Token;
 import org.apache.karaf.shell.api.console.Command;
 import org.apache.karaf.shell.api.console.CommandLine;
 import org.apache.karaf.shell.api.console.Parser;
@@ -44,7 +46,7 @@ public class KarafParser implements org.jline.reader.Parser {
     @Override
     public ParsedLine parse(String line, int cursor, ParseContext parseContext) throws SyntaxError {
         try {
-            return doParse(line, cursor);
+            return doParse(line, cursor, parseContext);
         } catch (EOFError e) {
             throw new org.jline.reader.EOFError(e.line(), e.column(), e.getMessage(), e.missing());
         } catch (SyntaxError e) {
@@ -52,10 +54,24 @@ public class KarafParser implements org.jline.reader.Parser {
         }
     }
 
-    private ParsedLine doParse(CharSequence line, int cursor) throws SyntaxError {
-        org.apache.felix.gogo.runtime.Parser parser = new org.apache.felix.gogo.runtime.Parser(line);
-        Program program = parser.program();
-        List<Statement> statements = parser.statements();
+    private ParsedLine doParse(String line, int cursor, ParseContext parseContext) throws SyntaxError {
+        Program program = null;
+        List<Statement> statements = null;
+        String repaired = line;
+        while (program == null) {
+            try {
+                org.apache.felix.gogo.runtime.Parser parser = new org.apache.felix.gogo.runtime.Parser(repaired);
+                program = parser.program();
+                statements = parser.statements();
+            } catch (EOFError e) {
+                // Make sure we don't loop forever
+                if (parseContext == ParseContext.COMPLETE && repaired.length() < line.length() + 1024) {
+                    repaired = repaired + " " + e.repair();
+                } else {
+                    throw e;
+                }
+            }
+        }
         // Find corresponding statement
         Statement statement = null;
         for (int i = statements.size() - 1; i >= 0; i--) {
@@ -106,6 +122,13 @@ public class KarafParser implements org.jline.reader.Parser {
                     }
                 };
             }
+            if (repaired != line) {
+                Token stmt = statement.subSequence(0, line.length() - statement.start());
+                List<Token> tokens = new ArrayList<>(statement.tokens());
+                Token last = tokens.get(tokens.size() - 1);
+                tokens.set(tokens.size() - 1, last.subSequence(0, line.length() - last.start()));
+                return new ParsedLineImpl(program, stmt, cursor, tokens);
+            }
             return new ParsedLineImpl(program, statement, cursor, statement.tokens());
         } else {
             // TODO:

-- 
To stop receiving notification emails like this one, please contact
gnodet@apache.org.