yash_syntax/parser/
error.rs

1// This file is part of yash, an extended POSIX shell.
2// Copyright (C) 2020 WATANABE Yuki
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program.  If not, see <https://www.gnu.org/licenses/>.
16
17//! Definition of errors that happen in the parser
18
19use crate::source::Location;
20use crate::source::pretty::Annotation;
21use crate::source::pretty::AnnotationType;
22use crate::source::pretty::MessageBase;
23use crate::syntax::AndOr;
24use std::borrow::Cow;
25use std::rc::Rc;
26use thiserror::Error;
27
28/// Types of syntax errors
29#[derive(Clone, Debug, Eq, Error, PartialEq)]
30#[error("{}", self.message())]
31#[non_exhaustive]
32pub enum SyntaxError {
33    /// A backslash is at the end of the input.
34    IncompleteEscape,
35    /// A backslash is not followed by a character that makes a valid escape.
36    InvalidEscape,
37    /// A `(` lacks a closing `)`.
38    UnclosedParen { opening_location: Location },
39    /// A single quotation lacks a closing `'`.
40    UnclosedSingleQuote { opening_location: Location },
41    /// A double quotation lacks a closing `"`.
42    UnclosedDoubleQuote { opening_location: Location },
43    /// A `$'` lacks a closing `'`.
44    UnclosedDollarSingleQuote { opening_location: Location },
45    /// A parameter expansion lacks a closing `}`.
46    UnclosedParam { opening_location: Location },
47    /// A parameter expansion lacks a name.
48    EmptyParam,
49    /// A parameter expansion has an invalid name.
50    InvalidParam,
51    /// A modifier does not have a valid form in a parameter expansion.
52    InvalidModifier,
53    /// A braced parameter expansion has both a prefix and suffix modifier.
54    MultipleModifier,
55    /// A command substitution started with `$(` but lacks a closing `)`.
56    UnclosedCommandSubstitution { opening_location: Location },
57    /// A command substitution started with `` ` `` but lacks a closing `` ` ``.
58    UnclosedBackquote { opening_location: Location },
59    /// An arithmetic expansion lacks a closing `))`.
60    UnclosedArith { opening_location: Location },
61    /// A command begins with an inappropriate keyword or operator token.
62    InvalidCommandToken,
63    /// A separator is missing between commands.
64    MissingSeparator,
65    /// The file descriptor specified for a redirection cannot be used.
66    FdOutOfRange,
67    /// An I/O location prefix attached to a redirection has an unsupported format.
68    InvalidIoLocation,
69    /// A redirection operator is missing its operand.
70    MissingRedirOperand,
71    /// A here-document operator is missing its delimiter token.
72    MissingHereDocDelimiter,
73    /// A here-document operator is missing its corresponding content.
74    MissingHereDocContent,
75    /// A here-document content is missing its delimiter.
76    UnclosedHereDocContent { redir_op_location: Location },
77    /// An array assignment started with `=(` but lacks a closing `)`.
78    UnclosedArrayValue { opening_location: Location },
79    /// A `}` appears without a matching `{`.
80    UnopenedGrouping,
81    /// A grouping is not closed.
82    UnclosedGrouping { opening_location: Location },
83    /// A grouping contains no commands.
84    EmptyGrouping,
85    /// A `)` appears without a matching `(`.
86    UnopenedSubshell,
87    /// A subshell is not closed.
88    UnclosedSubshell { opening_location: Location },
89    /// A subshell contains no commands.
90    EmptySubshell,
91    /// A `do` appears outside a loop.
92    UnopenedLoop,
93    /// A `done` appears outside a loop.
94    UnopenedDoClause,
95    /// A do clause is not closed.
96    UnclosedDoClause { opening_location: Location },
97    /// A do clause contains no commands.
98    EmptyDoClause,
99    /// The variable name is missing in a for loop.
100    MissingForName,
101    /// The variable name is not a valid word in a for loop.
102    InvalidForName,
103    /// A value is not a valid word in a for loop.
104    InvalidForValue,
105    /// A for loop is missing a do clause.
106    MissingForBody { opening_location: Location },
107    /// A while loop is missing a do clause.
108    UnclosedWhileClause { opening_location: Location },
109    /// A while loop's condition is empty.
110    EmptyWhileCondition,
111    /// An until loop is missing a do clause.
112    UnclosedUntilClause { opening_location: Location },
113    /// An until loop's condition is empty.
114    EmptyUntilCondition,
115    /// An if command is missing the then clause.
116    IfMissingThen { if_location: Location },
117    /// An if command's condition is empty.
118    EmptyIfCondition,
119    /// An if command's body is empty.
120    EmptyIfBody,
121    /// An elif clause is missing the then clause.
122    ElifMissingThen { elif_location: Location },
123    /// An elif clause's condition is empty.
124    EmptyElifCondition,
125    /// An elif clause's body is empty.
126    EmptyElifBody,
127    /// An else clause is empty.
128    EmptyElse,
129    /// An `elif`, `else`, `then`, or `fi` appears outside an if command.
130    UnopenedIf,
131    /// An if command is not closed.
132    UnclosedIf { opening_location: Location },
133    /// The case command is missing its subject.
134    MissingCaseSubject,
135    /// The subject of the case command is not a valid word.
136    InvalidCaseSubject,
137    /// The case command is missing `in` after the subject.
138    MissingIn { opening_location: Location },
139    /// The `)` is missing in a case item.
140    UnclosedPatternList,
141    /// The pattern is missing in a case item.
142    MissingPattern,
143    /// The pattern is not a valid word token.
144    InvalidPattern,
145    /// The first pattern of a case item is `esac`.
146    #[deprecated = "this error no longer occurs"]
147    EsacAsPattern,
148    /// An `esac` or `;;` appears outside a case command.
149    UnopenedCase,
150    /// A case command is not closed.
151    UnclosedCase { opening_location: Location },
152    /// The `(` is not followed by `)` in a function definition.
153    UnmatchedParenthesis,
154    /// The function body is missing in a function definition command.
155    MissingFunctionBody,
156    /// A function body is not a compound command.
157    InvalidFunctionBody,
158    /// The keyword `in` is used as a command name.
159    InAsCommandName,
160    /// A pipeline is missing after a `&&` or `||` token.
161    MissingPipeline(AndOr),
162    /// Two successive `!` tokens.
163    DoubleNegation,
164    /// A `|` token is followed by a `!`.
165    BangAfterBar,
166    /// A command is missing after a `!` token.
167    MissingCommandAfterBang,
168    /// A command is missing after a `|` token.
169    MissingCommandAfterBar,
170    /// There is a redundant token.
171    RedundantToken,
172    /// A control escape (`\c...`) is incomplete in a dollar-single-quoted string.
173    IncompleteControlEscape,
174    /// A control-backslash escape (`\c\\`) is incomplete in a dollar-single-quoted string.
175    IncompleteControlBackslashEscape,
176    /// A control escape (`\c...`) does not have a valid control character.
177    InvalidControlEscape,
178    /// An octal escape is out of range (greater than `\377`) in a dollar-single-quoted string.
179    OctalEscapeOutOfRange,
180    /// An hexadecimal escape (`\x...`) is incomplete in a dollar-single-quoted string.
181    IncompleteHexEscape,
182    /// A Unicode escape (`\u...`) is incomplete in a dollar-single-quoted string.
183    IncompleteShortUnicodeEscape,
184    /// A Unicode escape (`\U...`) is incomplete in a dollar-single-quoted string.
185    IncompleteLongUnicodeEscape,
186    /// A Unicode escape (`\u...` or `\U...`) is out of range in a dollar-single-quoted string.
187    UnicodeEscapeOutOfRange,
188}
189
190impl SyntaxError {
191    /// Returns an error message describing the error.
192    #[must_use]
193    pub fn message(&self) -> &'static str {
194        use SyntaxError::*;
195        match self {
196            IncompleteEscape => "the backslash is escaping nothing",
197            InvalidEscape => "the backslash escape is invalid",
198            UnclosedParen { .. } => "the parenthesis is not closed",
199            UnclosedSingleQuote { .. } => "the single quote is not closed",
200            UnclosedDoubleQuote { .. } => "the double quote is not closed",
201            UnclosedDollarSingleQuote { .. } => "the dollar single quote is not closed",
202            UnclosedParam { .. } => "the parameter expansion is not closed",
203            EmptyParam => "the parameter name is missing",
204            InvalidParam => "the parameter name is invalid",
205            InvalidModifier => "the parameter expansion contains a malformed modifier",
206            MultipleModifier => "a suffix modifier cannot be used together with a prefix modifier",
207            UnclosedCommandSubstitution { .. } => "the command substitution is not closed",
208            UnclosedBackquote { .. } => "the backquote is not closed",
209            UnclosedArith { .. } => "the arithmetic expansion is not closed",
210            InvalidCommandToken => "the command starts with an inappropriate token",
211            MissingSeparator => "a separator is missing between the commands",
212            FdOutOfRange => "the file descriptor is too large",
213            InvalidIoLocation => "the I/O location prefix is not valid",
214            MissingRedirOperand => "the redirection operator is missing its operand",
215            MissingHereDocDelimiter => "the here-document operator is missing its delimiter",
216            MissingHereDocContent => "content of the here-document is missing",
217            UnclosedHereDocContent { .. } => {
218                "the delimiter to close the here-document content is missing"
219            }
220            UnclosedArrayValue { .. } => "the array assignment value is not closed",
221            UnopenedGrouping | UnopenedSubshell | UnopenedLoop | UnopenedDoClause | UnopenedIf
222            | UnopenedCase | InAsCommandName => "the compound command delimiter is unmatched",
223            UnclosedGrouping { .. } => "the grouping is not closed",
224            EmptyGrouping => "the grouping is missing its content",
225            UnclosedSubshell { .. } => "the subshell is not closed",
226            EmptySubshell => "the subshell is missing its content",
227            UnclosedDoClause { .. } => "the `do` clause is missing its closing `done`",
228            EmptyDoClause => "the `do` clause is missing its content",
229            MissingForName => "the variable name is missing in the `for` loop",
230            InvalidForName => "the variable name is invalid",
231            InvalidForValue => "the operator token is invalid in the word list of the `for` loop",
232            MissingForBody { .. } => "the `for` loop is missing its `do` clause",
233            UnclosedWhileClause { .. } => "the `while` loop is missing its `do` clause",
234            EmptyWhileCondition => "the `while` loop is missing its condition",
235            UnclosedUntilClause { .. } => "the `until` loop is missing its `do` clause",
236            EmptyUntilCondition => "the `until` loop is missing its condition",
237            IfMissingThen { .. } => "the `if` command is missing the `then` clause",
238            EmptyIfCondition => "the `if` command is missing its condition",
239            EmptyIfBody => "the `if` command is missing its body",
240            ElifMissingThen { .. } => "the `elif` clause is missing the `then` clause",
241            EmptyElifCondition => "the `elif` clause is missing its condition",
242            EmptyElifBody => "the `elif` clause is missing its body",
243            EmptyElse => "the `else` clause is missing its content",
244            UnclosedIf { .. } => "the `if` command is missing its closing `fi`",
245            MissingCaseSubject => "the subject is missing after `case`",
246            InvalidCaseSubject => "the `case` command subject is not a valid word",
247            MissingIn { .. } => "`in` is missing in the `case` command",
248            UnclosedPatternList => "the pattern list is not properly closed by a `)`",
249            MissingPattern => "a pattern is missing in the `case` command",
250            InvalidPattern => "the pattern is not a valid word token",
251            #[allow(deprecated)]
252            EsacAsPattern => "`esac` cannot be the first of a pattern list",
253            UnclosedCase { .. } => "the `case` command is missing its closing `esac`",
254            UnmatchedParenthesis => "`)` is missing after `(`",
255            MissingFunctionBody => "the function body is missing",
256            InvalidFunctionBody => "the function body must be a compound command",
257            MissingPipeline(AndOr::AndThen) => "a command is missing after `&&`",
258            MissingPipeline(AndOr::OrElse) => "a command is missing after `||`",
259            DoubleNegation => "`!` cannot be used twice in a row",
260            BangAfterBar => "`!` cannot be used in the middle of a pipeline",
261            MissingCommandAfterBang => "a command is missing after `!`",
262            MissingCommandAfterBar => "a command is missing after `|`",
263            RedundantToken => "there is a redundant token",
264            IncompleteControlEscape => "the control escape is incomplete",
265            IncompleteControlBackslashEscape => "the control-backslash escape is incomplete",
266            InvalidControlEscape => "the control escape is invalid",
267            OctalEscapeOutOfRange => "the octal escape is out of range",
268            IncompleteHexEscape => "the hexadecimal escape is incomplete",
269            IncompleteShortUnicodeEscape | IncompleteLongUnicodeEscape => {
270                "the Unicode escape is incomplete"
271            }
272            UnicodeEscapeOutOfRange => "the Unicode escape is out of range",
273        }
274    }
275
276    /// Returns a label for annotating the error location.
277    #[must_use]
278    pub fn label(&self) -> &'static str {
279        use SyntaxError::*;
280        match self {
281            IncompleteEscape => "expected an escaped character after the backslash",
282            InvalidEscape => "invalid escape sequence",
283            UnclosedParen { .. }
284            | UnclosedCommandSubstitution { .. }
285            | UnclosedArrayValue { .. }
286            | UnclosedSubshell { .. }
287            | UnclosedPatternList
288            | UnmatchedParenthesis => "expected `)`",
289            EmptyGrouping
290            | EmptySubshell
291            | EmptyDoClause
292            | EmptyWhileCondition
293            | EmptyUntilCondition
294            | EmptyIfCondition
295            | EmptyIfBody
296            | EmptyElifCondition
297            | EmptyElifBody
298            | EmptyElse
299            | MissingPipeline(_)
300            | MissingCommandAfterBang
301            | MissingCommandAfterBar => "expected a command",
302            InvalidForValue | MissingCaseSubject | InvalidCaseSubject | MissingPattern
303            | InvalidPattern => "expected a word",
304            UnclosedSingleQuote { .. } | UnclosedDollarSingleQuote { .. } => "expected `'`",
305            UnclosedDoubleQuote { .. } => "expected `\"`",
306            UnclosedParam { .. } | UnclosedGrouping { .. } => "expected `}`",
307            EmptyParam => "expected a parameter name",
308            InvalidParam => "not a valid named or positional parameter",
309            InvalidModifier => "broken modifier",
310            MultipleModifier => "conflicting modifier",
311            UnclosedBackquote { .. } => "expected '`'",
312            UnclosedArith { .. } => "expected `))`",
313            InvalidCommandToken => "does not begin a valid command",
314            MissingSeparator => "expected `;` or `&` before this token",
315            FdOutOfRange => "unsupported file descriptor",
316            InvalidIoLocation => "unsupported I/O location prefix",
317            MissingRedirOperand => "expected a redirection operand",
318            MissingHereDocDelimiter => "expected a delimiter word",
319            MissingHereDocContent => "content not found",
320            UnclosedHereDocContent { .. } => "missing delimiter",
321            UnopenedGrouping => "no grouping command to close",
322            UnopenedSubshell => "no subshell to close",
323            UnopenedLoop => "not in a loop",
324            UnopenedDoClause => "no `do` clause to close",
325            UnclosedDoClause { .. } => "expected `done`",
326            MissingForName => "expected a variable name",
327            InvalidForName => "not a valid variable name",
328            MissingForBody { .. } | UnclosedWhileClause { .. } | UnclosedUntilClause { .. } => {
329                "expected `do ... done`"
330            }
331            IfMissingThen { .. } | ElifMissingThen { .. } => "expected `then ... fi`",
332            UnopenedIf => "not in an `if` command",
333            UnclosedIf { .. } => "expected `fi`",
334            MissingIn { .. } => "expected `in`",
335            #[allow(deprecated)]
336            EsacAsPattern => "needs quoting",
337            UnopenedCase => "not in a `case` command",
338            UnclosedCase { .. } => "expected `esac`",
339            MissingFunctionBody | InvalidFunctionBody => "expected a compound command",
340            InAsCommandName => "cannot be used as a command name",
341            DoubleNegation => "only one `!` allowed",
342            BangAfterBar => "`!` not allowed here",
343            RedundantToken => "unexpected token",
344            IncompleteControlEscape => r"expected a control character after `\c`",
345            IncompleteControlBackslashEscape => r"expected another backslash after `\c\`",
346            InvalidControlEscape => "not a valid control character",
347            OctalEscapeOutOfRange => r"expected a value between \0 and \377",
348            IncompleteHexEscape => r"expected a hexadecimal digit after `\x`",
349            IncompleteShortUnicodeEscape => r"expected a hexadecimal digit after `\u`",
350            IncompleteLongUnicodeEscape => r"expected a hexadecimal digit after `\U`",
351            UnicodeEscapeOutOfRange => "not a valid Unicode scalar value",
352        }
353    }
354
355    /// Returns a location related with the error cause and a message describing
356    /// the location.
357    #[must_use]
358    pub fn related_location(&self) -> Option<(&Location, &'static str)> {
359        use SyntaxError::*;
360        match self {
361            UnclosedParen { opening_location }
362            | UnclosedSubshell { opening_location }
363            | UnclosedArrayValue { opening_location } => {
364                Some((opening_location, "the opening parenthesis was here"))
365            }
366            UnclosedSingleQuote { opening_location }
367            | UnclosedDoubleQuote { opening_location }
368            | UnclosedDollarSingleQuote { opening_location } => {
369                Some((opening_location, "the opening quote was here"))
370            }
371            UnclosedParam { opening_location } => {
372                Some((opening_location, "the parameter started here"))
373            }
374            UnclosedCommandSubstitution { opening_location } => {
375                Some((opening_location, "the command substitution started here"))
376            }
377            UnclosedBackquote { opening_location } => {
378                Some((opening_location, "the opening backquote was here"))
379            }
380            UnclosedArith { opening_location } => {
381                Some((opening_location, "the arithmetic expansion started here"))
382            }
383            UnclosedHereDocContent { redir_op_location } => {
384                Some((redir_op_location, "the redirection operator was here"))
385            }
386            UnclosedGrouping { opening_location } => {
387                Some((opening_location, "the opening brace was here"))
388            }
389            UnclosedDoClause { opening_location } => {
390                Some((opening_location, "the `do` clause started here"))
391            }
392            MissingForBody { opening_location } => {
393                Some((opening_location, "the `for` loop started here"))
394            }
395            UnclosedWhileClause { opening_location } => {
396                Some((opening_location, "the `while` loop started here"))
397            }
398            UnclosedUntilClause { opening_location } => {
399                Some((opening_location, "the `until` loop started here"))
400            }
401            IfMissingThen { if_location }
402            | UnclosedIf {
403                opening_location: if_location,
404            } => Some((if_location, "the `if` command started here")),
405            ElifMissingThen { elif_location } => {
406                Some((elif_location, "the `elif` clause started here"))
407            }
408            MissingIn { opening_location } | UnclosedCase { opening_location } => {
409                Some((opening_location, "the `case` command started here"))
410            }
411            _ => None,
412        }
413    }
414}
415
416/// Types of errors that may happen in parsing
417#[derive(Clone, Debug, Error)]
418#[error("{}", self.message())]
419pub enum ErrorCause {
420    /// Error in an underlying input function
421    Io(#[from] Rc<std::io::Error>),
422    /// Syntax error
423    Syntax(#[from] SyntaxError),
424}
425
426impl PartialEq for ErrorCause {
427    fn eq(&self, other: &Self) -> bool {
428        match (self, other) {
429            (ErrorCause::Syntax(e1), ErrorCause::Syntax(e2)) => e1 == e2,
430            _ => false,
431        }
432    }
433}
434
435impl ErrorCause {
436    /// Returns an error message describing the error cause.
437    #[must_use]
438    pub fn message(&self) -> Cow<'static, str> {
439        use ErrorCause::*;
440        match self {
441            Io(e) => format!("cannot read commands: {e}").into(),
442            Syntax(e) => e.message().into(),
443        }
444    }
445
446    /// Returns a label for annotating the error location.
447    #[must_use]
448    pub fn label(&self) -> &'static str {
449        use ErrorCause::*;
450        match self {
451            Io(_) => "the command could be read up to here",
452            Syntax(e) => e.label(),
453        }
454    }
455
456    /// Returns a location related with the error cause and a message describing
457    /// the location.
458    #[must_use]
459    pub fn related_location(&self) -> Option<(&Location, &'static str)> {
460        use ErrorCause::*;
461        match self {
462            Io(_) => None,
463            Syntax(e) => e.related_location(),
464        }
465    }
466}
467
468impl From<std::io::Error> for ErrorCause {
469    fn from(e: std::io::Error) -> ErrorCause {
470        ErrorCause::from(Rc::new(e))
471    }
472}
473
474/// Explanation of a failure in parsing
475#[derive(Clone, Debug, Error, PartialEq)]
476#[error("{cause}")]
477pub struct Error {
478    pub cause: ErrorCause,
479    pub location: Location,
480}
481
482impl MessageBase for Error {
483    fn message_title(&self) -> Cow<str> {
484        self.cause.message()
485    }
486
487    fn main_annotation(&self) -> Annotation {
488        Annotation::new(
489            AnnotationType::Error,
490            self.cause.label().into(),
491            &self.location,
492        )
493    }
494
495    fn additional_annotations<'a, T: Extend<Annotation<'a>>>(&'a self, results: &mut T) {
496        // TODO Use Extend::extend_one
497        if let Some((location, label)) = self.cause.related_location() {
498            results.extend(std::iter::once(Annotation::new(
499                AnnotationType::Info,
500                label.into(),
501                location,
502            )));
503        }
504        if let ErrorCause::Syntax(SyntaxError::BangAfterBar) = &self.cause {
505            results.extend(std::iter::once(Annotation::new(
506                AnnotationType::Help,
507                "surround this in a grouping: `{ ! ...; }`".into(),
508                &self.location,
509            )));
510        }
511    }
512}
513
514#[cfg(test)]
515mod tests {
516    use super::*;
517    use crate::source::Code;
518    use crate::source::Source;
519    use crate::source::pretty::Message;
520    use std::num::NonZeroU64;
521    use std::rc::Rc;
522
523    #[test]
524    fn display_for_error() {
525        let code = Rc::new(Code {
526            value: "".to_string().into(),
527            start_line_number: NonZeroU64::new(1).unwrap(),
528            source: Source::Unknown.into(),
529        });
530        let location = Location { code, range: 0..42 };
531        let error = Error {
532            cause: SyntaxError::MissingHereDocDelimiter.into(),
533            location,
534        };
535        assert_eq!(
536            error.to_string(),
537            "the here-document operator is missing its delimiter"
538        );
539    }
540
541    #[test]
542    fn from_error_for_message() {
543        let code = Rc::new(Code {
544            value: "".to_string().into(),
545            start_line_number: NonZeroU64::new(1).unwrap(),
546            source: Source::Unknown.into(),
547        });
548        let location = Location { code, range: 0..42 };
549        let error = Error {
550            cause: SyntaxError::MissingHereDocDelimiter.into(),
551            location,
552        };
553        let message = Message::from(&error);
554        assert_eq!(message.r#type, AnnotationType::Error);
555        assert_eq!(
556            message.title,
557            "the here-document operator is missing its delimiter"
558        );
559        assert_eq!(message.annotations.len(), 1);
560        assert_eq!(message.annotations[0].r#type, AnnotationType::Error);
561        assert_eq!(message.annotations[0].label, "expected a delimiter word");
562        assert_eq!(message.annotations[0].location, &error.location);
563    }
564}