|
| 1 | +// Copyright (c) 2021 Brendan Molloy <brendan@bbqsrc.net>, |
| 2 | +// Ilya Solovyiov <ilya.solovyiov@gmail.com>, |
| 3 | +// Kai Ren <tyranron@gmail.com> |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +//! [Cucumber Expressions][1] [AST]. |
| 12 | +//! |
| 13 | +//! See details in the [grammar spec][0]. |
| 14 | +//! |
| 15 | +//! [0]: crate#grammar |
| 16 | +//! [1]: https://github.com/cucumber/cucumber-expressions#readme |
| 17 | +//! [AST]: https://en.wikipedia.org/wiki/Abstract_syntax_tree |
| 18 | +
|
| 19 | +use derive_more::{AsRef, Deref, DerefMut}; |
| 20 | +use nom::{error::ErrorKind, Err, InputLength}; |
| 21 | +use nom_locate::LocatedSpan; |
| 22 | + |
| 23 | +use crate::parse; |
| 24 | + |
| 25 | +/// [`str`] along with its location information in the original input. |
| 26 | +pub type Spanned<'s> = LocatedSpan<&'s str>; |
| 27 | + |
| 28 | +/// Top-level `expression` defined in the [grammar spec][0]. |
| 29 | +/// |
| 30 | +/// See [`parse::expression()`] for the detailed grammar and examples. |
| 31 | +/// |
| 32 | +/// [0]: crate#grammar |
| 33 | +#[derive(AsRef, Clone, Debug, Deref, DerefMut, Eq, PartialEq)] |
| 34 | +pub struct Expression<Input>(pub Vec<SingleExpression<Input>>); |
| 35 | + |
| 36 | +impl<'s> TryFrom<&'s str> for Expression<Spanned<'s>> { |
| 37 | + type Error = parse::Error<Spanned<'s>>; |
| 38 | + |
| 39 | + fn try_from(value: &'s str) -> Result<Self, Self::Error> { |
| 40 | + parse::expression(Spanned::new(value)) |
| 41 | + .map_err(|e| match e { |
| 42 | + Err::Error(e) | Err::Failure(e) => e, |
| 43 | + Err::Incomplete(n) => parse::Error::Needed(n), |
| 44 | + }) |
| 45 | + .and_then(|(rest, parsed)| { |
| 46 | + rest.is_empty() |
| 47 | + .then(|| parsed) |
| 48 | + .ok_or(parse::Error::Other(rest, ErrorKind::Verify)) |
| 49 | + }) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +impl<'s> Expression<Spanned<'s>> { |
| 54 | + /// Parses the given `input` as an [`Expression`]. |
| 55 | + /// |
| 56 | + /// # Errors |
| 57 | + /// |
| 58 | + /// See [`parse::Error`] for details. |
| 59 | + pub fn parse<I: AsRef<str> + ?Sized>( |
| 60 | + input: &'s I, |
| 61 | + ) -> Result<Self, parse::Error<Spanned<'s>>> { |
| 62 | + Self::try_from(input.as_ref()) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +/// `single-expression` defined in the [grammar spec][0], representing a single |
| 67 | +/// entry of an [`Expression`]. |
| 68 | +/// |
| 69 | +/// See [`parse::single_expression()`] for the detailed grammar and examples. |
| 70 | +/// |
| 71 | +/// [0]: crate#grammar |
| 72 | +#[derive(Clone, Debug, Eq, PartialEq)] |
| 73 | +pub enum SingleExpression<Input> { |
| 74 | + /// [`alternation`][0] expression. |
| 75 | + /// |
| 76 | + /// [0]: crate#grammar |
| 77 | + Alternation(Alternation<Input>), |
| 78 | + |
| 79 | + /// [`optional`][0] expression. |
| 80 | + /// |
| 81 | + /// [0]: crate#grammar |
| 82 | + Optional(Optional<Input>), |
| 83 | + |
| 84 | + /// [`parameter`][0] expression. |
| 85 | + /// |
| 86 | + /// [0]: crate#grammar |
| 87 | + Parameter(Parameter<Input>), |
| 88 | + |
| 89 | + /// Text without whitespaces. |
| 90 | + Text(Input), |
| 91 | + |
| 92 | + /// Whitespaces are treated as a special case to avoid placing every `text` |
| 93 | + /// character in a separate [AST] node, as described in the |
| 94 | + /// [grammar spec][0]. |
| 95 | + /// |
| 96 | + /// [0]: crate#grammar |
| 97 | + /// [AST]: https://en.wikipedia.org/wiki/Abstract_syntax_tree |
| 98 | + Whitespaces(Input), |
| 99 | +} |
| 100 | + |
| 101 | +/// `single-alternation` defined in the [grammar spec][0], representing a |
| 102 | +/// building block of an [`Alternation`]. |
| 103 | +/// |
| 104 | +/// [0]: crate#grammar |
| 105 | +pub type SingleAlternation<Input> = Vec<Alternative<Input>>; |
| 106 | + |
| 107 | +/// `alternation` defined in the [grammar spec][0], allowing to match one of |
| 108 | +/// [`SingleAlternation`]s. |
| 109 | +/// |
| 110 | +/// See [`parse::alternation()`] for the detailed grammar and examples. |
| 111 | +/// |
| 112 | +/// [0]: crate#grammar |
| 113 | +#[derive(AsRef, Clone, Debug, Deref, DerefMut, Eq, PartialEq)] |
| 114 | +pub struct Alternation<Input>(pub Vec<SingleAlternation<Input>>); |
| 115 | + |
| 116 | +impl<Input: InputLength> Alternation<Input> { |
| 117 | + /// Returns length of this [`Alternation`]'s span in the `Input`. |
| 118 | + pub(crate) fn span_len(&self) -> usize { |
| 119 | + self.0 |
| 120 | + .iter() |
| 121 | + .flatten() |
| 122 | + .map(|alt| match alt { |
| 123 | + Alternative::Text(t) => t.input_len(), |
| 124 | + Alternative::Optional(opt) => opt.input_len() + 2, |
| 125 | + }) |
| 126 | + .sum::<usize>() |
| 127 | + + self.len() |
| 128 | + - 1 |
| 129 | + } |
| 130 | + |
| 131 | + /// Indicates whether any of [`SingleAlternation`]s consists only from |
| 132 | + /// [`Optional`]s. |
| 133 | + pub(crate) fn contains_only_optional(&self) -> bool { |
| 134 | + (**self).iter().any(|single_alt| { |
| 135 | + single_alt |
| 136 | + .iter() |
| 137 | + .all(|alt| matches!(alt, Alternative::Optional(_))) |
| 138 | + }) |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +/// `alternative` defined in the [grammar spec][0]. |
| 143 | +/// |
| 144 | +/// See [`parse::alternative()`] for the detailed grammar and examples. |
| 145 | +/// |
| 146 | +/// [0]: crate#grammar |
| 147 | +#[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 148 | +pub enum Alternative<Input> { |
| 149 | + /// [`optional`][1] expression. |
| 150 | + /// |
| 151 | + /// [1]: crate#grammar |
| 152 | + Optional(Optional<Input>), |
| 153 | + |
| 154 | + /// Text. |
| 155 | + Text(Input), |
| 156 | +} |
| 157 | + |
| 158 | +/// `optional` defined in the [grammar spec][0], allowing to match an optional |
| 159 | +/// `Input`. |
| 160 | +/// |
| 161 | +/// See [`parse::optional()`] for the detailed grammar and examples. |
| 162 | +/// |
| 163 | +/// [0]: crate#grammar |
| 164 | +#[derive(AsRef, Clone, Copy, Debug, Deref, DerefMut, Eq, PartialEq)] |
| 165 | +pub struct Optional<Input>(pub Input); |
| 166 | + |
| 167 | +/// `parameter` defined in the [grammar spec][0], allowing to match some special |
| 168 | +/// `Input` described by a [`Parameter`] name. |
| 169 | +/// |
| 170 | +/// See [`parse::parameter()`] for the detailed grammar and examples. |
| 171 | +/// |
| 172 | +/// [0]: crate#grammar |
| 173 | +#[derive(AsRef, Clone, Copy, Debug, Deref, DerefMut, Eq, PartialEq)] |
| 174 | +pub struct Parameter<Input>(pub Input); |
0 commit comments