nu_protocol/
syntax_shape.rs

1use crate::{DeclId, Type};
2use serde::{Deserialize, Serialize};
3use std::fmt::Display;
4
5/// The syntactic shapes that describe how a sequence should be parsed.
6///
7/// This extends beyond [`Type`] which describes how [`Value`](crate::Value)s are represented.
8/// `SyntaxShape`s can describe the parsing rules for arguments to a command.
9/// e.g. [`SyntaxShape::GlobPattern`]/[`SyntaxShape::Filepath`] serve the completer,
10/// but don't have an associated [`Value`](crate::Value)
11/// There are additional `SyntaxShape`s that only make sense in particular expressions or keywords
12#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
13pub enum SyntaxShape {
14    /// Any syntactic form is allowed
15    Any,
16
17    /// A binary literal
18    Binary,
19
20    /// A block is allowed, eg `{start this thing}`
21    Block,
22
23    /// A boolean value, eg `true` or `false`
24    Boolean,
25
26    /// A dotted path to navigate the table
27    CellPath,
28
29    /// A closure is allowed, eg `{|| start this thing}`
30    Closure(Option<Vec<SyntaxShape>>),
31
32    /// A [`SyntaxShape`] with custom completion logic
33    CompleterWrapper(Box<SyntaxShape>, DeclId),
34
35    /// A datetime value, eg `2022-02-02` or `2019-10-12T07:20:50.52+00:00`
36    DateTime,
37
38    /// A directory is allowed
39    Directory,
40
41    /// A duration value is allowed, eg `19day`
42    Duration,
43
44    /// An error value
45    Error,
46
47    /// A general expression, eg `1 + 2` or `foo --bar`
48    Expression,
49
50    /// A (typically) string argument that follows external command argument parsing rules.
51    ///
52    /// Filepaths are expanded if unquoted, globs are allowed, and quotes embedded within unknown
53    /// args are unquoted.
54    ExternalArgument,
55
56    /// A filepath is allowed
57    Filepath,
58
59    /// A filesize value is allowed, eg `10kb`
60    Filesize,
61
62    /// A floating point value, eg `1.0`
63    Float,
64
65    /// A dotted path including the variable to access items
66    ///
67    /// Fully qualified
68    FullCellPath,
69
70    /// A glob pattern is allowed, eg `foo*`
71    GlobPattern,
72
73    /// Only an integer value is allowed
74    Int,
75
76    /// A module path pattern used for imports
77    ImportPattern,
78
79    /// A specific match to a word or symbol
80    Keyword(Vec<u8>, Box<SyntaxShape>),
81
82    /// A list is allowed, eg `[first second]`
83    List(Box<SyntaxShape>),
84
85    /// A general math expression, eg `1 + 2`
86    MathExpression,
87
88    /// A block of matches, used by `match`
89    MatchBlock,
90
91    /// Nothing
92    Nothing,
93
94    /// Only a numeric (integer or float) value is allowed
95    Number,
96
97    /// One of a list of possible items, checked in order
98    OneOf(Vec<SyntaxShape>),
99
100    /// An operator, eg `+`
101    Operator,
102
103    /// A range is allowed (eg, `1..3`)
104    Range,
105
106    /// A record value, eg `{x: 1, y: 2}`
107    Record(Vec<(String, SyntaxShape)>),
108
109    /// A math expression which expands shorthand forms on the lefthand side, eg `foo > 1`
110    /// The shorthand allows us to more easily reach columns inside of the row being passed in
111    RowCondition,
112
113    /// A signature for a definition, `[x:int, --foo]`
114    Signature,
115
116    /// Strings and string-like bare words are allowed
117    String,
118
119    /// A table is allowed, eg `[[first, second]; [1, 2]]`
120    Table(Vec<(String, SyntaxShape)>),
121
122    /// A variable with optional type, `x` or `x: int`
123    VarWithOptType,
124}
125
126impl SyntaxShape {
127    /// If possible provide the associated concrete [`Type`]
128    ///
129    /// Note: Some [`SyntaxShape`]s don't have a corresponding [`Value`](crate::Value)
130    /// Here we currently return [`Type::Any`]
131    ///
132    /// ```rust
133    /// use nu_protocol::{SyntaxShape, Type};
134    /// let non_value = SyntaxShape::ImportPattern;
135    /// assert_eq!(non_value.to_type(), Type::Any);
136    /// ```
137    pub fn to_type(&self) -> Type {
138        let mk_ty = |tys: &[(String, SyntaxShape)]| {
139            tys.iter()
140                .map(|(key, val)| (key.clone(), val.to_type()))
141                .collect()
142        };
143
144        match self {
145            SyntaxShape::Any => Type::Any,
146            SyntaxShape::Block => Type::Any,
147            SyntaxShape::Closure(_) => Type::Closure,
148            SyntaxShape::Binary => Type::Binary,
149            SyntaxShape::CellPath => Type::Any,
150            SyntaxShape::CompleterWrapper(inner, _) => inner.to_type(),
151            SyntaxShape::DateTime => Type::Date,
152            SyntaxShape::Duration => Type::Duration,
153            SyntaxShape::Expression => Type::Any,
154            SyntaxShape::ExternalArgument => Type::Any,
155            SyntaxShape::Filepath => Type::String,
156            SyntaxShape::Directory => Type::String,
157            SyntaxShape::Float => Type::Float,
158            SyntaxShape::Filesize => Type::Filesize,
159            SyntaxShape::FullCellPath => Type::Any,
160            SyntaxShape::GlobPattern => Type::Glob,
161            SyntaxShape::Error => Type::Error,
162            SyntaxShape::ImportPattern => Type::Any,
163            SyntaxShape::Int => Type::Int,
164            SyntaxShape::List(x) => {
165                let contents = x.to_type();
166                Type::List(Box::new(contents))
167            }
168            SyntaxShape::Keyword(_, expr) => expr.to_type(),
169            SyntaxShape::MatchBlock => Type::Any,
170            SyntaxShape::MathExpression => Type::Any,
171            SyntaxShape::Nothing => Type::Nothing,
172            SyntaxShape::Number => Type::Number,
173            SyntaxShape::OneOf(_) => Type::Any,
174            SyntaxShape::Operator => Type::Any,
175            SyntaxShape::Range => Type::Range,
176            SyntaxShape::Record(entries) => Type::Record(mk_ty(entries)),
177            SyntaxShape::RowCondition => Type::Bool,
178            SyntaxShape::Boolean => Type::Bool,
179            SyntaxShape::Signature => Type::Any,
180            SyntaxShape::String => Type::String,
181            SyntaxShape::Table(columns) => Type::Table(mk_ty(columns)),
182            SyntaxShape::VarWithOptType => Type::Any,
183        }
184    }
185}
186
187impl Display for SyntaxShape {
188    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
189        let mk_fmt = |tys: &[(String, SyntaxShape)]| -> String {
190            tys.iter()
191                .map(|(x, y)| format!("{x}: {y}"))
192                .collect::<Vec<String>>()
193                .join(", ")
194        };
195
196        match self {
197            SyntaxShape::Keyword(kw, shape) => {
198                write!(f, "\"{}\" {}", String::from_utf8_lossy(kw), shape)
199            }
200            SyntaxShape::Any => write!(f, "any"),
201            SyntaxShape::String => write!(f, "string"),
202            SyntaxShape::CellPath => write!(f, "cell-path"),
203            SyntaxShape::FullCellPath => write!(f, "cell-path"),
204            SyntaxShape::Number => write!(f, "number"),
205            SyntaxShape::Range => write!(f, "range"),
206            SyntaxShape::Int => write!(f, "int"),
207            SyntaxShape::Float => write!(f, "float"),
208            SyntaxShape::Filepath => write!(f, "path"),
209            SyntaxShape::Directory => write!(f, "directory"),
210            SyntaxShape::GlobPattern => write!(f, "glob"),
211            SyntaxShape::ImportPattern => write!(f, "import"),
212            SyntaxShape::Block => write!(f, "block"),
213            SyntaxShape::Closure(args) => {
214                if let Some(args) = args {
215                    let arg_vec: Vec<_> = args.iter().map(|x| x.to_string()).collect();
216                    let arg_string = arg_vec.join(", ");
217                    write!(f, "closure({arg_string})")
218                } else {
219                    write!(f, "closure()")
220                }
221            }
222            SyntaxShape::Binary => write!(f, "binary"),
223            SyntaxShape::List(x) => write!(f, "list<{x}>"),
224            SyntaxShape::Table(columns) => {
225                if columns.is_empty() {
226                    write!(f, "table")
227                } else {
228                    write!(f, "table<{}>", mk_fmt(columns))
229                }
230            }
231            SyntaxShape::Record(entries) => {
232                if entries.is_empty() {
233                    write!(f, "record")
234                } else {
235                    write!(f, "record<{}>", mk_fmt(entries))
236                }
237            }
238            SyntaxShape::Filesize => write!(f, "filesize"),
239            SyntaxShape::Duration => write!(f, "duration"),
240            SyntaxShape::DateTime => write!(f, "datetime"),
241            SyntaxShape::Operator => write!(f, "operator"),
242            SyntaxShape::RowCondition => write!(f, "condition"),
243            SyntaxShape::MathExpression => write!(f, "variable"),
244            SyntaxShape::VarWithOptType => write!(f, "vardecl"),
245            SyntaxShape::Signature => write!(f, "signature"),
246            SyntaxShape::MatchBlock => write!(f, "match-block"),
247            SyntaxShape::Expression => write!(f, "expression"),
248            SyntaxShape::ExternalArgument => write!(f, "external-argument"),
249            SyntaxShape::Boolean => write!(f, "bool"),
250            SyntaxShape::Error => write!(f, "error"),
251            SyntaxShape::CompleterWrapper(x, _) => write!(f, "completable<{x}>"),
252            SyntaxShape::OneOf(list) => {
253                write!(f, "oneof<")?;
254                if let Some((last, rest)) = list.split_last() {
255                    for ty in rest {
256                        write!(f, "{ty}, ")?;
257                    }
258                    write!(f, "{last}")?;
259                }
260                write!(f, ">")
261            }
262            SyntaxShape::Nothing => write!(f, "nothing"),
263        }
264    }
265}