@Override protected void parse(final ArrayList<Command> cmds) throws QueryException { final Scanner sc = new Scanner(string).useDelimiter(single ? "\0" : "\r\n?|\n"); while (sc.hasNext()) { final String line = sc.next().trim(); if (line.isEmpty() || line.startsWith("#")) continue; parser = new InputParser(line); parser.file = ctx.options.get(MainOptions.QUERYPATH); while (parser.more()) { final Cmd cmd = consume(Cmd.class, null); if (cmd != null) cmds.add(parse(cmd)); if (parser.more() && !parser.consume(';')) throw help(null, cmd); } } }
/** * Parses and returns a string, delimited by a semicolon or, optionally, a space. Quotes can be * used to include spaces. * * @param cmd referring command; if specified, the result must not be empty * @param space stop when encountering space * @return string * @throws QueryException query exception */ private String string(final Cmd cmd, final boolean space) throws QueryException { final StringBuilder sb = new StringBuilder(); consumeWS(); boolean q = false; while (parser.more()) { final char c = parser.curr(); if (!q && ((space ? c <= ' ' : c < ' ') || eoc())) break; if (c == '"') q ^= true; else sb.append(c); parser.consume(); } return finish(sb, cmd); }
/** * Parses and returns the remaining string. Quotes at the beginning and end of the argument will * be stripped. * * @param cmd referring command; if specified, the result must not be empty * @return remaining string * @throws QueryException query exception */ private String remaining(final Cmd cmd) throws QueryException { if (single) { final StringBuilder sb = new StringBuilder(); consumeWS(); while (parser.more()) sb.append(parser.consume()); String arg = finish(sb, cmd); if (arg != null) { // chop quotes; substrings are faster than replaces... if (arg.startsWith("\"")) arg = arg.substring(1); if (arg.endsWith("\"")) arg = arg.substring(0, arg.length() - 1); } return arg; } return string(cmd, false); }
/** * Checks if the end of a command has been reached. * * @return true if command has ended */ private boolean eoc() { return !parser.more() || parser.curr() == ';'; }