Java Code Examples for org.jline.reader.impl.DefaultParser#parse()

The following examples show how to use org.jline.reader.impl.DefaultParser#parse() . 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: 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 2
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 3
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 4
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 5
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;
    }
  }
}