2018-05-21 15:19:38 +02:00
|
|
|
= Command Parsing
|
|
|
|
|
2022-04-11 23:00:43 +02:00
|
|
|
Kakoune commands, either loaded from a script or written in the command prompt, are parsed according to the following rules:
|
2018-05-21 15:19:38 +02:00
|
|
|
|
|
|
|
== Basic parsing
|
|
|
|
|
2022-04-11 23:00:43 +02:00
|
|
|
- Commands are delimited by a `;` or an end of line
|
2018-05-21 15:19:38 +02:00
|
|
|
|
2022-04-11 23:00:43 +02:00
|
|
|
- Words (command names and parameters) are delimited by whitespaces
|
2018-05-21 15:19:38 +02:00
|
|
|
|
|
|
|
== Quoted Strings
|
|
|
|
|
2022-04-11 23:00:43 +02:00
|
|
|
If a word starts with `'`, `"`, or `%X` with `X` a non-nestable punctuation character (see below for nestable characters),
|
|
|
|
it is parsed as a quoted string whose delimiter is, respectively, `'`, `"`, or `X`.
|
2018-05-21 15:19:38 +02:00
|
|
|
|
2022-04-11 23:00:43 +02:00
|
|
|
A quoted string contains every character (including whitespaces). Doubling a closing delimiter escapes it.
|
|
|
|
Thus, for example, entering two closing delimiting characters at the end of a quoted string will render one of the characters literally;
|
|
|
|
that is, it will be considered as part of the quoted string's content.
|
2018-05-21 15:19:38 +02:00
|
|
|
|
2022-04-11 23:00:43 +02:00
|
|
|
Inside double quotes, `%`-strings are processed unless the `%` is escaped by doubling it.
|
|
|
|
Double quotes inside these nested strings must still be escaped.
|
2019-01-31 14:41:58 +01:00
|
|
|
|
2018-05-21 15:19:38 +02:00
|
|
|
No other escaping takes place in quoted strings.
|
|
|
|
|
|
|
|
=== Quoted Strings Examples
|
|
|
|
|
2019-01-31 14:41:58 +01:00
|
|
|
- `'foo'` contains *foo*
|
|
|
|
|
|
|
|
- `foo'bar'` is read verbatim, so it contains *foo'bar'*
|
|
|
|
|
|
|
|
- `foo%|bar|` is read verbatim, so it contains *foo%|bar|*.
|
2018-05-21 15:19:38 +02:00
|
|
|
|
|
|
|
- `'foo''bar'` is a single word whose content is *foo'bar*
|
|
|
|
|
|
|
|
- `"baz"""` is a single word whose content is *baz"*.
|
|
|
|
|
2019-01-31 14:41:58 +01:00
|
|
|
- `%|foo||bar|` is a single word whose content is *foo|bar*.
|
|
|
|
|
|
|
|
- `"foo %|""bar| %%,baz,"` is a single word whose content is *foo "bar %,baz,*.
|
|
|
|
|
2018-05-21 15:19:38 +02:00
|
|
|
== Balanced Strings
|
|
|
|
|
2022-04-11 23:00:43 +02:00
|
|
|
If a word starts with `%X` with `X` a nestable punctuation character (one of `(`, `[`, `{` and `<`),
|
|
|
|
it is parsed as a balanced string
|
|
|
|
whose closing delimiter matches that of its opening delimiter (respectively `)`, `]`, `}` and `>`).
|
2018-05-21 15:19:38 +02:00
|
|
|
|
2022-04-11 23:00:43 +02:00
|
|
|
Characters may be escaped in the same manner as those for quoted strings.
|
2018-05-21 15:19:38 +02:00
|
|
|
|
|
|
|
=== Balanced Strings Examples
|
|
|
|
|
2019-01-31 14:41:58 +01:00
|
|
|
- `%{foo}` contains *foo*
|
|
|
|
|
|
|
|
- `%{foo\{bar}}` contains *foo\{bar}*
|
|
|
|
|
|
|
|
- `foo%{bar}` contains *foo%{bar}*
|
2018-05-21 15:19:38 +02:00
|
|
|
|
2019-01-31 14:41:58 +01:00
|
|
|
- `"foo %{bar}"` is a single word whose content is *foo bar*
|
2018-05-21 15:19:38 +02:00
|
|
|
|
|
|
|
== Non Quoted words
|
|
|
|
|
2022-04-11 23:00:43 +02:00
|
|
|
Other words are non-quoted.
|
|
|
|
Non-quoted words end either at a whitespace or a `;`.
|
2018-05-21 15:19:38 +02:00
|
|
|
|
2022-04-11 23:00:43 +02:00
|
|
|
If they start with a `\` followed by a `%`, `'`, or `"`,
|
|
|
|
then that leading `\` escapes those characters and is discarded.
|
2018-05-21 15:19:38 +02:00
|
|
|
|
2022-04-11 23:00:43 +02:00
|
|
|
If a whitespace or `;` is preceded by a `\`,
|
|
|
|
then the `\` is discarded, and the whitespace or `;` becomes part of the word.
|
|
|
|
Any other `\` is treated as a literal `\`.
|
2018-05-21 15:19:38 +02:00
|
|
|
|
|
|
|
== Typed Expansions
|
|
|
|
|
2022-04-11 23:00:43 +02:00
|
|
|
Quoted and Balanced strings starting with `%` might have an optional alphabetic *expansion type* between the `%` and their delimiter
|
|
|
|
(which is always a punctuation character).
|
|
|
|
This *expansion type* defines how the string's content is going to be expanded.
|
|
|
|
Rules for expanding and escaping expansion types are the same as for `%`-strings.
|
2018-05-21 15:19:38 +02:00
|
|
|
|
|
|
|
- If the *expansion type* is empty, the string content is used verbatim.
|
|
|
|
|
|
|
|
- If the *expansion type* is one of `sh`, `reg`, `opt`, `val` or `arg`,
|
2019-01-31 14:31:48 +01:00
|
|
|
The string is expanded as described in <<expansions#,`:doc expansions`>>
|
2018-05-21 15:19:38 +02:00
|
|
|
|
|
|
|
- For any other *expansion type* a parsing error is raised.
|