1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
// This file is part of yash, an extended POSIX shell.
// Copyright (C) 2020 WATANABE Yuki
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//! Extension of the core for implementing the rest of the lexer.
use super::core::is_blank;
use super::core::Lexer;
use crate::parser::core::Result;
impl Lexer<'_> {
/// Skips a character if the given function returns true for it.
///
/// Returns `Ok(true)` if the character was skipped, `Ok(false)` if the function returned
/// false, and `Err(_)` if an error occurred, respectively.
///
/// `skip_if` is a simpler version of [`consume_char_if`](Lexer::consume_char_if).
pub async fn skip_if<F>(&mut self, f: F) -> Result<bool>
where
F: FnMut(char) -> bool,
{
Ok(self.consume_char_if(f).await?.is_some())
}
/// Skips blank characters until reaching a non-blank.
pub async fn skip_blanks(&mut self) -> Result<()> {
while self.skip_if(is_blank).await? {}
Ok(())
}
/// Skips a comment, if any.
///
/// A comment ends just before a newline. The newline is *not* part of the comment.
///
/// This function does not recognize line continuation inside the comment.
pub async fn skip_comment(&mut self) -> Result<()> {
if self.skip_if(|c| c == '#').await? {
let mut lexer = self.disable_line_continuation();
while lexer.skip_if(|c| c != '\n').await? {}
Lexer::enable_line_continuation(lexer);
}
Ok(())
}
/// Skips blank characters and a comment, if any.
///
/// This function is the same as [`skip_blanks`](Lexer::skip_blanks)
/// followed by [`skip_comment`](Lexer::skip_comment).
pub async fn skip_blanks_and_comment(&mut self) -> Result<()> {
self.skip_blanks().await?;
self.skip_comment().await
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::source::Source;
use futures_util::FutureExt;
#[test]
fn lexer_skip_blanks() {
let mut lexer = Lexer::from_memory(" \t w", Source::Unknown);
let c = async {
lexer.skip_blanks().await?;
lexer.peek_char().await
}
.now_or_never()
.unwrap();
assert_eq!(c, Ok(Some('w')));
// Test idempotence
let c = async {
lexer.skip_blanks().await?;
lexer.peek_char().await
}
.now_or_never()
.unwrap();
assert_eq!(c, Ok(Some('w')));
}
#[test]
fn lexer_skip_blanks_does_not_skip_newline() {
let mut lexer = Lexer::from_memory("\n", Source::Unknown);
lexer.skip_blanks().now_or_never().unwrap().unwrap();
assert_eq!(lexer.peek_char().now_or_never().unwrap(), Ok(Some('\n')));
}
#[test]
fn lexer_skip_blanks_skips_line_continuations() {
let mut lexer = Lexer::from_memory("\\\n \\\n\\\n\\\n \\\nX", Source::Unknown);
let c = async {
lexer.skip_blanks().await?;
lexer.peek_char().await
}
.now_or_never()
.unwrap();
assert_eq!(c, Ok(Some('X')));
let mut lexer = Lexer::from_memory(" \\\n\\\n \\\n Y", Source::Unknown);
let c = async {
lexer.skip_blanks().await?;
lexer.peek_char().await
}
.now_or_never()
.unwrap();
assert_eq!(c, Ok(Some('Y')));
}
#[test]
fn lexer_skip_comment_no_comment() {
let mut lexer = Lexer::from_memory("\n", Source::Unknown);
lexer.skip_comment().now_or_never().unwrap().unwrap();
assert_eq!(lexer.peek_char().now_or_never().unwrap(), Ok(Some('\n')));
}
#[test]
fn lexer_skip_comment_empty_comment() {
let mut lexer = Lexer::from_memory("#\n", Source::Unknown);
let c = async {
lexer.skip_comment().await?;
lexer.peek_char().await
}
.now_or_never()
.unwrap();
assert_eq!(c, Ok(Some('\n')));
// Test idempotence
let c = async {
lexer.skip_comment().await?;
lexer.peek_char().await
}
.now_or_never()
.unwrap();
assert_eq!(c, Ok(Some('\n')));
}
#[test]
fn lexer_skip_comment_non_empty_comment() {
let mut lexer = Lexer::from_memory("\\\n### foo bar\\\n", Source::Unknown);
let c = async {
lexer.skip_comment().await?;
lexer.peek_char().await
}
.now_or_never()
.unwrap();
assert_eq!(c, Ok(Some('\n')));
assert_eq!(lexer.index(), 14);
// Test idempotence
let c = async {
lexer.skip_comment().await?;
lexer.peek_char().await
}
.now_or_never()
.unwrap();
assert_eq!(c, Ok(Some('\n')));
assert_eq!(lexer.index(), 14);
}
#[test]
fn lexer_skip_comment_not_ending_with_newline() {
let mut lexer = Lexer::from_memory("#comment", Source::Unknown);
let c = async {
lexer.skip_comment().await?;
lexer.peek_char().await
}
.now_or_never()
.unwrap();
assert_eq!(c, Ok(None));
// Test idempotence
let c = async {
lexer.skip_comment().await?;
lexer.peek_char().await
}
.now_or_never()
.unwrap();
assert_eq!(c, Ok(None));
}
}