Skip to content

Commit c0655d3

Browse files
committed
:ec[ho] throws E15 if an expression doesn't parse
1 parent 4d6bc89 commit c0655d3

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

src/cmd_line/commands/echo.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
1-
import { optWhitespace, Parser, whitespace } from 'parsimmon';
1+
import { all, optWhitespace, Parser, seq, whitespace } from 'parsimmon';
22
import { VimState } from '../../state/vimState';
33
import { StatusBar } from '../../statusBar';
44
import { ExCommand } from '../../vimscript/exCommand';
55
import { EvaluationContext } from '../../vimscript/expression/evaluate';
66
import { expressionParser } from '../../vimscript/expression/parser';
77
import { Expression } from '../../vimscript/expression/types';
88
import { displayValue } from '../../vimscript/expression/displayValue';
9+
import { VimError } from '../../error';
910

1011
export class EchoCommand extends ExCommand {
1112
public static argParser(echoArgs: { sep: string; error: boolean }): Parser<EchoCommand> {
1213
return optWhitespace
13-
.then(expressionParser.sepBy(whitespace))
14-
.map((expressions) => new EchoCommand(echoArgs, expressions));
14+
.then(seq(expressionParser.sepBy(whitespace), all))
15+
.map(([expressions, trailing]) => {
16+
trailing = trailing.trimStart();
17+
if (trailing) {
18+
throw VimError.InvalidExpression(trailing);
19+
}
20+
return new EchoCommand(echoArgs, expressions);
21+
});
1522
}
1623

1724
private sep: string;

src/error.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ export class VimError extends Error {
110110
static InvalidAddress(): VimError {
111111
return new VimError(ErrorCode.InvalidAddress, 'Invalid address');
112112
}
113-
static InvalidExpression(): VimError {
114-
return new VimError(ErrorCode.InvalidExpression, 'Invalid expression');
113+
static InvalidExpression(expr: string): VimError {
114+
return new VimError(ErrorCode.InvalidExpression, `Invalid expression: "${expr}"`);
115115
}
116116
static InvalidRange(): VimError {
117117
return new VimError(ErrorCode.InvalidRange, 'Invalid range');

src/vimscript/exCommandParser.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -691,10 +691,11 @@ export const exCommandParser: Parser<{ lineRange: LineRange | undefined; command
691691
}
692692
throw VimError.InvalidArgument474();
693693
}
694-
if (result.value[1]) {
694+
const [command, remaining] = result.value;
695+
if (remaining) {
695696
// TODO: Implement `:help :bar`
696697
// TODO: Implement `:help :comment`
697-
throw VimError.TrailingCharacters(result.value[1]);
698+
throw VimError.TrailingCharacters(remaining);
698699
}
699-
return { lineRange, command: result.value[0] };
700+
return { lineRange, command };
700701
});

0 commit comments

Comments
 (0)