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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
use derive_new::new;
use itertools::Itertools;
use std::fmt;

use nu_source::{HasSpan, Span, Spanned, SpannedItem};

use super::token_group::TokenBuilder;

#[derive(Debug, Clone, PartialEq)]
pub enum TokenContents {
    /// A baseline token is an atomic chunk of source code. This means that the
    /// token contains the entirety of string literals, as well as the entirety
    /// of sections delimited by paired delimiters.
    ///
    /// For example, if the token begins with `{`, the baseline token continues
    /// until the closing `}` (after taking comments and string literals into
    /// consideration).
    Baseline(String),
    Comment(LiteComment),
    Pipe,
    Semicolon,
    Eol,
}

impl fmt::Display for TokenContents {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            TokenContents::Baseline(base) => write!(f, "{}", base),
            TokenContents::Comment(comm) => write!(f, "{}", comm),
            TokenContents::Pipe => write!(f, "|"),
            TokenContents::Semicolon => write!(f, ";"),
            TokenContents::Eol => write!(f, "\\n"),
        }
    }
}

impl TokenContents {
    pub fn is_eol(&self) -> bool {
        matches!(self, Self::Eol)
    }
}

pub type CommandBuilder = TokenBuilder<Spanned<String>>;
pub type CommentsBuilder = TokenBuilder<LiteComment>;
pub type PipelineBuilder = TokenBuilder<LiteCommand>;
pub type GroupBuilder = TokenBuilder<PipelineBuilder>;

/// A LiteComment is a line comment. It begins with `#` and continues until (but not including) the
/// next newline.
///
/// It remembers any leading whitespace, which is used in later processing steps to strip off
/// leading whitespace for an entire comment block when it is associated with a definition.
#[derive(Debug, PartialEq, Clone)]
pub struct LiteComment {
    leading_ws: Option<Spanned<String>>,
    rest: Spanned<String>,
}

impl LiteComment {
    pub fn new(string: impl Into<Spanned<String>>) -> LiteComment {
        LiteComment {
            leading_ws: None,
            rest: string.into(),
        }
    }

    pub fn new_with_ws(
        ws: impl Into<Spanned<String>>,
        comment: impl Into<Spanned<String>>,
    ) -> LiteComment {
        LiteComment {
            leading_ws: Some(ws.into()),
            rest: comment.into(),
        }
    }

    pub fn unindent(&self, excluded_spaces: usize) -> LiteComment {
        match &self.leading_ws {
            // If there's no leading whitespace, there's no whitespace to exclude
            None => self.clone(),
            Some(Spanned { item, span }) => {
                // If the number of spaces to exclude is larger than the amount of whitespace we
                // have, there's no whitespace to move into the comment body.
                if excluded_spaces > item.len() {
                    self.clone()
                } else {
                    // If there are no spaces to exclude, prepend all of the leading_whitespace to
                    // the comment body.
                    if excluded_spaces == 0 {
                        let rest_span = self.span();
                        let rest = format!("{}{}", item, self.rest.item).spanned(rest_span);
                        return LiteComment {
                            leading_ws: None,
                            rest,
                        };
                    }

                    // Pull off excluded_spaces number of spaces, and create a new Spanned<String>
                    // for that whitespace. Any remaining spaces will be added to the comment.
                    let excluded_ws = item[..excluded_spaces]
                        .to_string()
                        .spanned(Span::new(span.start(), span.start() + excluded_spaces));

                    let included_ws = &item[excluded_spaces..];
                    let rest_start = span.start() + excluded_spaces;
                    let rest_span = Span::new(rest_start, rest_start + self.rest.len());

                    let rest = format!("{}{}", included_ws, self.rest.item).spanned(rest_span);

                    LiteComment {
                        leading_ws: Some(excluded_ws),
                        rest,
                    }
                }
            }
        }
    }

    pub fn ws_len(&self) -> usize {
        match &self.leading_ws {
            None => 0,
            Some(ws) => ws.item.len(),
        }
    }

    pub(crate) fn trim(&self) -> Spanned<String> {
        let trimmed = self.rest.trim();

        trimmed.to_string().spanned(Span::new(
            self.rest.span().start(),
            self.rest.span().start() + trimmed.len(),
        ))
    }
}

impl fmt::Display for LiteComment {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match &self.leading_ws {
            None => write!(f, "#{}", self.rest.item),
            Some(leading) => write!(f, "#{}{}", leading.item, self.rest.item),
        }
    }
}

impl HasSpan for LiteComment {
    fn span(&self) -> Span {
        match &self.leading_ws {
            None => self.rest.span(),
            Some(leading) => leading.span().until(self.rest.span()),
        }
    }
}

/// A `LiteCommand` is a list of words that will get meaning when processed by
/// the parser.
#[derive(Debug, Default, Clone)]
pub struct LiteCommand {
    pub parts: Vec<Spanned<String>>,
    /// Preceding comments.
    pub comments: Option<Vec<LiteComment>>,
}

impl HasSpan for LiteCommand {
    fn span(&self) -> Span {
        Span::from_list(&self.parts)
    }
}

impl LiteCommand {
    pub fn comments_joined(&self) -> String {
        match &self.comments {
            None => "".to_string(),
            Some(text) => text.iter().map(|s| s.trim().item).join("\n"),
        }
    }
}

/// A `LitePipeline` is a series of `LiteCommand`s, separated by `|`.
#[derive(Debug, Clone, new)]
pub struct LitePipeline {
    pub commands: Vec<LiteCommand>,
}

impl HasSpan for LitePipeline {
    fn span(&self) -> Span {
        Span::from_list(&self.commands)
    }
}

/// A `LiteGroup` is a series of `LitePipeline`s, separated by `;`.
#[derive(Debug, Clone, new)]
pub struct LiteGroup {
    pub pipelines: Vec<LitePipeline>,
}

impl From<GroupBuilder> for LiteGroup {
    fn from(group: GroupBuilder) -> Self {
        LiteGroup::new(group.map(|p| LitePipeline::new(p.into())))
    }
}

impl HasSpan for LiteGroup {
    fn span(&self) -> Span {
        Span::from_list(&self.pipelines)
    }
}

/// A `LiteBlock` is a series of `LiteGroup`s, separated by newlines.
#[derive(Debug, Clone, new)]
pub struct LiteBlock {
    pub block: Vec<LiteGroup>,
}

impl HasSpan for LiteBlock {
    fn span(&self) -> Span {
        Span::from_list(&self.block)
    }
}