org.jline.reader.Parser Java Examples

The following examples show how to use org.jline.reader.Parser. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SshShellCommandFactory.java    From ssh-shell-spring-boot with Apache License 2.0 6 votes vote down vote up
/**
 * Constructor
 *
 * @param shellListenerService shell listener service
 * @param banner               shell banner
 * @param promptProvider       prompt provider
 * @param shell                spring shell
 * @param completerAdapter     completer adapter
 * @param parser               jline parser
 * @param environment          spring environment
 * @param properties           ssh shell properties
 */
public SshShellCommandFactory(SshShellListenerService shellListenerService,
                              @Autowired(required = false) Banner banner,
                              @Lazy PromptProvider promptProvider,
                              Shell shell,
                              JLineShellAutoConfiguration.CompleterAdapter completerAdapter, Parser parser,
                              Environment environment,
                              SshShellProperties properties) {
    this.shellListenerService = shellListenerService;
    this.shellBanner = banner;
    this.promptProvider = promptProvider;
    this.shell = shell;
    this.completerAdapter = completerAdapter;
    this.parser = parser;
    this.environment = environment;
    this.properties = properties;
}
 
Example #2
Source File: SshShellRunnable.java    From ssh-shell-spring-boot with Apache License 2.0 6 votes vote down vote up
public SshShellRunnable(SshShellProperties properties, ChannelSession session,
                        SshShellListenerService shellListenerService, Banner shellBanner,
                        PromptProvider promptProvider, Shell shell,
                        JLineShellAutoConfiguration.CompleterAdapter completerAdapter, Parser parser,
                        Environment environment, org.apache.sshd.server.Environment sshEnv,
                        SshShellCommandFactory sshShellCommandFactory, InputStream is,
                        OutputStream os, ExitCallback ec) {
    this.properties = properties;
    this.session = session;
    this.shellListenerService = shellListenerService;
    this.shellBanner = shellBanner;
    this.promptProvider = promptProvider;
    this.shell = shell;
    this.completerAdapter = completerAdapter;
    this.parser = parser;
    this.environment = environment;
    this.sshEnv = sshEnv;
    this.sshShellCommandFactory = sshShellCommandFactory;
    this.is = is;
    this.os = os;
    this.ec = ec;
}
 
Example #3
Source File: SshShellAutoConfiguration.java    From ssh-shell-spring-boot with Apache License 2.0 6 votes vote down vote up
/**
 * Primary shell application runner which answers true to {@link InteractiveShellApplicationRunner#isEnabled()}
 *
 * @param lineReader     line reader
 * @param promptProvider prompt provider
 * @param parser         parser
 * @param shell          spring shell
 * @param environment    spring environment
 * @return shell application runner
 */
@Bean
@Primary
public InteractiveShellApplicationRunner sshInteractiveShellApplicationRunner(LineReader lineReader,
                                                                              PromptProvider promptProvider,
                                                                              Parser parser, Shell shell,
                                                                              Environment environment) {
    return new InteractiveShellApplicationRunner(lineReader, promptProvider, parser, shell, environment) {

        @Override
        public boolean isEnabled() {
            return true;
        }

        @Override
        public void run(ApplicationArguments args) {
            // do nothing
        }
    };
}
 
Example #4
Source File: SqlLineParserTest.java    From sqlline with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * In case of exception while {@link sqlline.SqlLineParser#parse}
 * line continuation will be switched off for particular line.
 */
@Test
public void testSqlLineParserWithException() {
  new MockUp<SqlLineHighlighter>() {
    @Mock
    private boolean isLineFinishedWithSemicolon(
        final int lastNonQuoteCommentIndex, final CharSequence buffer) {
      throw new RuntimeException("Line continuation exception");
    }
  };

  final SqlLine sqlLine = new SqlLine();
  sqlLine.getOpts().set(BuiltInProperty.USE_LINE_CONTINUATION, false);
  final DefaultParser parser = new SqlLineParser(sqlLine);
  final Parser.ParseContext acceptLine = Parser.ParseContext.ACCEPT_LINE;
  for (String line : WRONG_LINES) {
    try {
      parser.parse(line, line.length(), acceptLine);
    } catch (Throwable t) {
      System.err.println("Problem line: [" + line + "]");
      throw t;
    }
  }
}
 
Example #5
Source File: CliClientTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private void verifySqlCompletion(String statement, int position, List<String> expectedHints, List<String> notExpectedHints) throws IOException {
	final SessionContext context = new SessionContext("test-session", new Environment());
	final MockExecutor mockExecutor = new MockExecutor();

	final SqlCompleter completer = new SqlCompleter(context, mockExecutor);
	final SqlMultiLineParser parser = new SqlMultiLineParser();

	try (Terminal terminal = TerminalUtils.createDummyTerminal()) {
		final LineReader reader = LineReaderBuilder.builder().terminal(terminal).build();

		final ParsedLine parsedLine = parser.parse(statement, position, Parser.ParseContext.COMPLETE);
		final List<Candidate> candidates = new ArrayList<>();
		final List<String> results = new ArrayList<>();
		completer.complete(reader, parsedLine, candidates);
		candidates.forEach(item -> results.add(item.value()));

		assertTrue(results.containsAll(expectedHints));

		assertEquals(statement, mockExecutor.receivedStatement);
		assertEquals(context, mockExecutor.receivedContext);
		assertEquals(position, mockExecutor.receivedPosition);
		assertTrue(results.contains("HintA"));
		assertTrue(results.contains("Hint B"));

		results.retainAll(notExpectedHints);
		assertEquals(0, results.size());
	}
}
 
Example #6
Source File: CliClientTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private void verifySqlCompletion(String statement, int position, List<String> expectedHints, List<String> notExpectedHints) throws IOException {
	final SessionContext context = new SessionContext("test-session", new Environment());
	final MockExecutor mockExecutor = new MockExecutor();

	final SqlCompleter completer = new SqlCompleter(context, mockExecutor);
	final SqlMultiLineParser parser = new SqlMultiLineParser();

	try (Terminal terminal = TerminalUtils.createDummyTerminal()) {
		final LineReader reader = LineReaderBuilder.builder().terminal(terminal).build();

		final ParsedLine parsedLine = parser.parse(statement, position, Parser.ParseContext.COMPLETE);
		final List<Candidate> candidates = new ArrayList<>();
		final List<String> results = new ArrayList<>();
		completer.complete(reader, parsedLine, candidates);
		candidates.forEach(item -> results.add(item.value()));

		assertTrue(results.containsAll(expectedHints));

		assertEquals(statement, mockExecutor.receivedStatement);
		assertEquals(context, mockExecutor.receivedContext);
		assertEquals(position, mockExecutor.receivedPosition);
		assertTrue(results.contains("HintA"));
		assertTrue(results.contains("Hint B"));

		results.retainAll(notExpectedHints);
		assertEquals(0, results.size());
	}
}
 
Example #7
Source File: CliClientTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private void verifySqlCompletion(String statement, int position, List<String> expectedHints, List<String> notExpectedHints) throws IOException {
	final SessionContext context = new SessionContext("test-session", new Environment());
	final MockExecutor mockExecutor = new MockExecutor();
	String sessionId = mockExecutor.openSession(context);

	final SqlCompleter completer = new SqlCompleter(sessionId, mockExecutor);
	final SqlMultiLineParser parser = new SqlMultiLineParser();

	try (Terminal terminal = TerminalUtils.createDummyTerminal()) {
		final LineReader reader = LineReaderBuilder.builder().terminal(terminal).build();

		final ParsedLine parsedLine = parser.parse(statement, position, Parser.ParseContext.COMPLETE);
		final List<Candidate> candidates = new ArrayList<>();
		final List<String> results = new ArrayList<>();
		completer.complete(reader, parsedLine, candidates);
		candidates.forEach(item -> results.add(item.value()));

		assertTrue(results.containsAll(expectedHints));

		assertEquals(statement, mockExecutor.receivedStatement);
		assertEquals(context, mockExecutor.receivedContext);
		assertEquals(position, mockExecutor.receivedPosition);
		assertTrue(results.contains("HintA"));
		assertTrue(results.contains("Hint B"));

		results.retainAll(notExpectedHints);
		assertEquals(0, results.size());
	}
}
 
Example #8
Source File: Commands.java    From sqlline with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private boolean isSqlContinuationRequired(String sql) {
  if (sqlLine.getLineReader() == null) {
    return false;
  }
  return SqlLineParser.SqlParserState.OK
      != ((SqlLineParser) sqlLine.getLineReader().getParser())
          .parseState(sql, sql.length(), Parser.ParseContext.ACCEPT_LINE)
          .getState();
}
 
Example #9
Source File: SqlCompleter.java    From sqlline with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override public void complete(
    LineReader reader, ParsedLine commandLine, List<Candidate> candidates) {
  String sql = commandLine.line().substring(0, commandLine.cursor());
  SqlLineParser.SqlLineArgumentList argumentList =
      ((SqlLineParser) sqlLine.getLineReader().getParser())
          .parseState(sql, sql.length(), Parser.ParseContext.UNSPECIFIED);
  final String supplierMsg = argumentList.getSupplier().get();
  final char openQuote = sqlLine.getDialect().getOpenQuote();
  if (argumentList.getState()
      == SqlLineParser.SqlParserState.MULTILINE_COMMENT
      || (argumentList.getState() == SqlLineParser.SqlParserState.QUOTED
      && ((openQuote == '"' && !supplierMsg.endsWith("dquote"))
      || (openQuote == '`' && !supplierMsg.endsWith("`"))))) {
    return;
  }

  if (!skipMeta) {
    Deque<String> lastWords = getSchemaTableColumn(argumentList.word());
    candidates.addAll(getSchemaBasedCandidates(new ArrayDeque<>(lastWords)));
    candidates.addAll(getTableBasedCandidates(new ArrayDeque<>(lastWords)));
  }
  // suggest other candidates if not quoted
  // and previous word not finished with '.'
  if (argumentList.getState() != SqlLineParser.SqlParserState.QUOTED
      && ((argumentList.getState()
          != SqlLineParser.SqlParserState.SEMICOLON_REQUIRED
              && argumentList.getState()
                 != SqlLineParser.SqlParserState.ROUND_BRACKET_BALANCE_FAILED)
          || sql.isEmpty()
          || sql.charAt(sql.length() - 1) != '.')) {
    candidates.addAll(this.candidates);
  }
}
 
Example #10
Source File: SqlLineParserTest.java    From sqlline with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testSqlLineParserForWrongLines() {
  final DefaultParser parser = new SqlLineParser(new SqlLine());
  final Parser.ParseContext acceptLine = Parser.ParseContext.ACCEPT_LINE;
  for (String line : WRONG_LINES) {
    try {
      parser.parse(line, line.length(), acceptLine);
      fail("Missing closing quote or semicolon for line " + line);
    } catch (EOFError eofError) {
      //ok
    }
  }
}
 
Example #11
Source File: SqlLineParserTest.java    From sqlline with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testSqlLineParserForWrongLinesWithEmptyPrompt() {
  SqlLine sqlLine = new SqlLine();
  sqlLine.getOpts().set(BuiltInProperty.PROMPT, "");
  final DefaultParser parser = new SqlLineParser(sqlLine);
  final Parser.ParseContext acceptLine = Parser.ParseContext.ACCEPT_LINE;
  for (String line : WRONG_LINES) {
    try {
      parser.parse(line, line.length(), acceptLine);
      fail("Missing closing comment, quote or semicolon for line " + line);
    } catch (EOFError eofError) {
      //ok
    }
  }
}
 
Example #12
Source File: SqlLineParserTest.java    From sqlline with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testSqlLineParserOfWrongLinesForSwitchedOfflineContinuation() {
  final SqlLine sqlLine = new SqlLine();
  sqlLine.getOpts().set(BuiltInProperty.USE_LINE_CONTINUATION, false);
  final DefaultParser parser = new SqlLineParser(sqlLine);
  final Parser.ParseContext acceptLine = Parser.ParseContext.ACCEPT_LINE;
  for (String line : WRONG_LINES) {
    try {
      parser.parse(line, line.length(), acceptLine);
    } catch (Throwable t) {
      System.err.println("Problem line: [" + line + "]");
      throw t;
    }
  }
}
 
Example #13
Source File: CliClientTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public org.apache.flink.table.delegation.Parser getSqlParser(String sessionId) {
	return helper.getSqlParser();
}
 
Example #14
Source File: Commands.java    From sqlline with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Runs a script from the specified file.
 *
 * @param line Command line
 * @param callback Callback for command status
 */
public void run(String line, DispatchCallback callback) {
  String filename;
  if (line.length() == "run".length()
      || (filename =
          sqlLine.dequote(line.substring("run".length() + 1))) == null) {
    sqlLine.error("Usage: run <file name>");
    callback.setToFailure();
    return;
  }
  List<String> cmds = new LinkedList<>();

  try {
    try (BufferedReader reader = new BufferedReader(
        new InputStreamReader(
            new FileInputStream(expand(filename)), StandardCharsets.UTF_8))) {
      // ### NOTE: fix for sf.net bug 879427
      final StringBuilder cmd = new StringBuilder();
      boolean needsContinuation;
      for (;;) {
        final String scriptLine = reader.readLine();
        if (scriptLine == null) {
          break;
        }
        // we're continuing an existing command
        cmd.append(" \n");
        cmd.append(scriptLine);

        needsContinuation = isSqlContinuationRequired(cmd.toString());
        if (!needsContinuation && !cmd.toString().trim().isEmpty()) {
          cmds.add(maybeTrim(flush(cmd)));
        }
      }

      if (SqlLineParser.isSql(sqlLine, cmd.toString(),
          Parser.ParseContext.ACCEPT_LINE)) {
        // ### REVIEW: oops, somebody left the last command
        // unterminated; should we fix it for them or complain?
        // For now be nice and fix it.
        cmd.append(";");
        cmds.add(cmd.toString());
      }
    }

    // success only if all the commands were successful
    if (sqlLine.runCommands(cmds, callback) == cmds.size()) {
      callback.setToSuccess();
    } else {
      callback.setToFailure();
    }
  } catch (Exception e) {
    callback.setToFailure();
    sqlLine.error(e);
  }
}
 
Example #15
Source File: SqlLineParserTest.java    From sqlline with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Test
public void testSqlLineParserForOkLines() {
  final DefaultParser parser = new SqlLineParser(new SqlLine());
  final Parser.ParseContext acceptLine = Parser.ParseContext.ACCEPT_LINE;
  final String[] lines = {
      // commands
      "!set",
      " !history",
      "   !scan",
      " \n !set",
      " \n test;",
      " \n test';\n;\n';",
      "select \n 1\n, '\na\n ';",
      // sql
      "select 1;",
      "select '1';",
      // sqlline command comment with odd number of quotes
      "  #select '1",
      "--select '1`",
      " -- select '\"",
      // one line comment right after semicolon
      "select '1';--comment",
      "select '1';-----comment",
      "select '1';--comment\n",
      "select '1';--comment\n\n",
      "select '1'; --comment",
      "select '1';\n--comment",
      "select '1';\n\n--comment",
      "select '1';\n \n--comment",
      "select '1'\n;\n--comment",
      "select '1'\n\n;--comment",
      "select '1'\n\n;---comment",
      "select '1'\n\n;-- --comment",
      "select '1'\n\n;\n--comment",
      "select '1';/*comment*/",
      "select '1';/*---comment */",
      "select '1';/*comment\n*/\n",
      "select '1';/*comment*/\n\n",
      "select '1'; /*--comment*/",
      // /* inside a quoted line
      "select '1/*' as \"asd\";",
      "select '/*' as \"asd*/\";",
      // quoted line
      "select '1' as `asd`;",
      "select '1' as `\\`asd\\``;",
      "select '1' as \"asd\";",
      "select '1' as \"a's'd\";",
      "select '1' as \"'a's'd\n\" from t;",
      "select '1' as \"'a'\\\ns'd\\\n\n\" from t;",
      "select ' ''1'', ''2''' as \"'a'\\\ns'd\\\n\n\" from t;",
      "select ' ''1'', ''2''' as \"'a'\\\"\n s'd \\\" \n \\\"\n\" from t;",
      // not a valid sql, but from sqlline parser's point of view it is ok
      // as there are no non-closed brackets, quotes, comments
      // and it ends with a semicolon
      " \n test;",
      " \n test';\n;\n';",

      "select sum(my_function(x.[qwe], x.qwe)) as \"asd\" from t;",
      "select \n 1\n, '\na\n ';",
      "select /*\njust a comment\n*/\n'1';",
      "--comment \n values (';\n' /* comment */, '\"'"
          + "/*multiline;\n ;\n comment*/)\n -- ; \n;",

      // non-closed or extra brackets but commented or quoted
      "select '1(' from dual;",
      "select ')1' from dual;",
      "select 1/*count(123 */ from dual;",
      "select 2/* [qwe */ from dual;",
      "select 2 \" [qwe \" from dual;",
      "select 2 \" ]]][[[ \" from dual;",
      "select 2 \" ]]]\n[[[ \" from dual;",
      "select 2 \" \n]]]\n[[[ \n\" from dual;",
      "select 2 \n --]]]\n --[[[ \n from dual;",
  };
  for (String line : lines) {
    try {
      parser.parse(line, line.length(), acceptLine);
    } catch (Throwable t) {
      System.err.println("Problem line: [" + line + "]");
      throw t;
    }
  }
}