-
-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Line breaks are supported in similar places to sh(1). Line breaks can be escaped anywhere by ending a line with a `\` character. Unescaped line breaks are also supported: - after logical operators (`&&` and `||`), - within strings, - between commands wrapped in parentheses, and - between commands in subshells. The Lexer has been expanded to insert a `MISSING` token in all situations where the input is known to be incomplete (i.e. when the input ends with an escape character, or the input ends in any of the places where an unescaped line break can be used). After invoking the `Lexer`, the `Interpreter` checks the token stream for `MISSING` tokens. If it finds any it requests another line of input from the current input strategy, appends it to the current input, and tries again. A new `EOL` token has been introduced to represent line breaks between commands. In the `Parser` it's treated exactly like the `SEMICOLON` token. Lexical analysis of comments needed to be improved to allow for comments at the end of lines in a multi-line command. For example, the following input is valid: (:echo 1 # comment :echo 2) It is semantically equivalent to: (:echo 1; :echo 2) To support this, the Lexer will now: - ignore whitespace before a comment's initial `#` character. This prevents extraneous `SPACE` tokens from being produced. A trailing `SPACE` token in a single line command is fine, but it can cause problems in a multi-line command. - pop the `:comment` state without consuming the newline character at the end of a comment, allowing the default parsing rules to handle the newline, and produce an `EOL` token.
- Loading branch information
1 parent
d73f120
commit a5d9652
Showing
14 changed files
with
350 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
require 'spec_helper' | ||
|
||
describe 'Multi-line input' do | ||
it 'supports escaped line breaks within commands' do | ||
GitshRunner.interactive do |gitsh| | ||
gitsh.type(':echo Hello \\') | ||
|
||
expect(gitsh).to output_no_errors | ||
expect(gitsh).to prompt_with('> ') | ||
|
||
gitsh.type('world') | ||
|
||
expect(gitsh).to output_no_errors | ||
expect(gitsh).to output(/Hello world/) | ||
end | ||
end | ||
|
||
it 'supports line breaks after logical operators' do | ||
GitshRunner.interactive do |gitsh| | ||
gitsh.type(':echo Hello &&') | ||
|
||
expect(gitsh).to output_no_errors | ||
expect(gitsh).to prompt_with('> ') | ||
|
||
gitsh.type(':echo World') | ||
|
||
expect(gitsh).to output_no_errors | ||
expect(gitsh).to output(/Hello\nWorld/) | ||
end | ||
end | ||
|
||
it 'supports line breaks within strings' do | ||
GitshRunner.interactive do |gitsh| | ||
gitsh.type(':echo "Hello, world') | ||
|
||
expect(gitsh).to output_no_errors | ||
expect(gitsh).to prompt_with('> ') | ||
|
||
gitsh.type('') | ||
gitsh.type('Goodbye, world"') | ||
|
||
expect(gitsh).to output(/\AHello, world\n\nGoodbye, world\Z/) | ||
end | ||
end | ||
|
||
it 'supports line breaks within parentheses' do | ||
GitshRunner.interactive do |gitsh| | ||
gitsh.type('(:echo 1') | ||
|
||
expect(gitsh).to output_no_errors | ||
expect(gitsh).to prompt_with('> ') | ||
|
||
gitsh.type(':echo 2') | ||
gitsh.type(':echo 3)') | ||
|
||
expect(gitsh).to output_no_errors | ||
expect(gitsh).to output(/1\n2\n3/) | ||
end | ||
end | ||
|
||
it 'supports line breaks within subshells' do | ||
GitshRunner.interactive do |gitsh| | ||
gitsh.type(':echo $(') | ||
gitsh.type(' :set greeting Hello') | ||
gitsh.type(' :echo $greeting') | ||
gitsh.type(')') | ||
|
||
expect(gitsh).to output_no_errors | ||
expect(gitsh).to output(/Hello/) | ||
end | ||
end | ||
|
||
it 'supports comments in the middle of multi-line commands' do | ||
GitshRunner.interactive do |gitsh| | ||
gitsh.type('(:echo 1 # comment') | ||
|
||
expect(gitsh).to output_no_errors | ||
expect(gitsh).to prompt_with('> ') | ||
|
||
gitsh.type(':echo 2') | ||
gitsh.type('# another comment') | ||
gitsh.type(')') | ||
|
||
expect(gitsh).to output_no_errors | ||
expect(gitsh).to output(/1\n2/) | ||
end | ||
end | ||
|
||
it 'supports line breaks within strings in scripts' do | ||
in_a_temporary_directory do | ||
write_file('multiline.gitsh', ":echo 'foo\nbar'") | ||
|
||
expect("#{gitsh_path} multiline.gitsh"). | ||
to execute.successfully. | ||
with_output_matching(/foo\nbar/) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
require 'rltk' | ||
|
||
module Tokens | ||
def tokens(*tokens) | ||
tokens.map.with_index do |token, i| | ||
type, value = token | ||
pos = RLTK::StreamPosition.new(i, 1, i, 10, nil) | ||
RLTK::Token.new(type, value, pos) | ||
end | ||
end | ||
end | ||
|
||
RSpec.configure do |config| | ||
config.include Tokens | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.