Skip to content

Commit

Permalink
DefaultParser: parameter middle quoting
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Dec 1, 2018
1 parent f2e7070 commit d5e2dbc
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
20 changes: 16 additions & 4 deletions reader/src/main/java/org/jline/reader/impl/DefaultParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public ParsedLine parse(final String line, final int cursor, ParseContext contex
int rawWordCursor = -1;
int rawWordLength = -1;
int rawWordStart = 0;
boolean quotedWord = false;

for (int i = 0; (line != null) && (i < line.length()); i++) {
// once we reach the cursor, set the
Expand All @@ -110,12 +111,20 @@ public ParsedLine parse(final String line, final int cursor, ParseContext contex
if (quoteStart < 0 && isQuoteChar(line, i)) {
// Start a quote block
quoteStart = i;
if (current.length()==0) {
quotedWord = true;
} else {
current.append(line.charAt(i));
}
} else if (quoteStart >= 0 && line.charAt(quoteStart) == line.charAt(i) && !isEscaped(line, i)) {
// End quote block
quoteStart = -1;
if (rawWordCursor >= 0 && rawWordLength < 0) {
if (!quotedWord) {
current.append(line.charAt(i));
} else if (rawWordCursor >= 0 && rawWordLength < 0) {
rawWordLength = i - rawWordStart + 1;
}
quoteStart = -1;
quotedWord = false;
} else if (quoteStart < 0 && isDelimiter(line, i)) {
// Delimiter
if (current.length() > 0) {
Expand Down Expand Up @@ -155,7 +164,7 @@ public ParsedLine parse(final String line, final int cursor, ParseContext contex
? "quote" : "dquote");
}

String openingQuote = quoteStart >= 0 ? line.substring(quoteStart, quoteStart + 1) : null;
String openingQuote = quotedWord ? line.substring(quoteStart, quoteStart + 1) : null;
return new ArgumentList(line, words, wordIndex, wordCursor, cursor, openingQuote, rawWordCursor, rawWordLength);
}

Expand Down Expand Up @@ -381,9 +390,12 @@ public CharSequence escape(CharSequence candidate, boolean complete) {
}
} else if (openingQuote == null) {
for (int i = 0; i < sb.length(); i++) {
if (isQuoteChar(sb, i)) {
quote = null;
break;
}
if (isDelimiterChar(sb, i)) {
quote = "'";
break;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ public void testQuotedDelimit() {

delimited = parser.parse("1 '2 3'", 0);
assertEquals(Arrays.asList("1", "2 3"), delimited.words());

delimited = parser.parse("0'1 2' 3", 0);
assertEquals(Arrays.asList("0'1 2'", "3"), delimited.words());

delimited = parser.parse("'01 '2 3", 0);
assertEquals(Arrays.asList("01 2", "3"), delimited.words());
}

@Test
Expand All @@ -61,7 +67,7 @@ public void testMixedQuotes() {
assertEquals(Arrays.asList("1' '2", "3"), delimited.words());

delimited = parser.parse("'1\" 2' 3\"", 0);
assertEquals(Arrays.asList("1\" 2", "3"), delimited.words());
assertEquals(Arrays.asList("1\" 2", "3\""), delimited.words());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,27 @@ public void escapeChars() throws Exception {
assertBuffer("bar'f", new TestBuffer("bar'f").tab());
}

@Test
public void middleQuotesEscapeCharsNull() throws Exception {
DefaultParser dp = (DefaultParser) reader.getParser();
dp.setEscapeChars(null);
reader.setVariable(LineReader.ERRORS, 0);
reader.setParser(dp);
reader.setCompleter(new StringsCompleter("/foo?name='foo bar'","/foo?name='foo qux'"));

assertBuffer("/foo?name='foo ", new TestBuffer("/f").tab());
assertBuffer("/foo?name='foo bar' ", new TestBuffer("/foo?name='foo b").tab());
}

@Test
public void middleQuotesEscapeChars() throws Exception {
DefaultParser dp = (DefaultParser) reader.getParser();
dp.setEscapeChars(new char[] { '\\' });
reader.setVariable(LineReader.ERRORS, 0);
reader.setParser(dp);
reader.setCompleter(new StringsCompleter("/foo?name='foo bar'","/foo?name='foo qux'"));

assertBuffer("/foo?name=\\'foo\\ ", new TestBuffer("/f").tab());
assertBuffer("/foo?name=\\'foo\\ bar\\' ", new TestBuffer("/foo?name='foo b").tab());
}
}

0 comments on commit d5e2dbc

Please sign in to comment.