Docstring some intricacies around SyntaxShape (#10544)

Inspired by @fdncred and @amtoine's questions
https://github.com/nushell/nushell/pull/10512#issuecomment-1739996967
This commit is contained in:
Stefan Holderbach 2023-09-29 16:35:22 +02:00 committed by GitHub
parent 7ad4c679b3
commit f2af12af2c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 7 deletions

View File

@ -2696,7 +2696,15 @@ pub fn parse_string_strict(working_set: &mut StateWorkingSet, span: Span) -> Exp
}
}
//TODO: Handle error case for unknown shapes
/// Parse the literals of [`Type`]-like [`SyntaxShape`]s including inner types.
/// Also handles the specification of custom completions with `type@completer`.
///
/// Used in:
/// - `: ` argument type (+completer) positions in signatures
/// - `type->type` input/output type pairs
/// - `let name: type` variable type infos
///
/// NOTE: Does not provide a mapping to every [`SyntaxShape`]
pub fn parse_shape_name(
working_set: &mut StateWorkingSet,
bytes: &[u8],
@ -2757,6 +2765,7 @@ pub fn parse_shape_name(
return shape;
}
} else {
//TODO: Handle error case for unknown shapes
working_set.error(ParseError::UnknownType(span));
return SyntaxShape::Any;
}

View File

@ -1,10 +1,14 @@
use crate::{DeclId, Type};
use serde::{Deserialize, Serialize};
use std::fmt::Display;
use serde::{Deserialize, Serialize};
use crate::{DeclId, Type};
/// The syntactic shapes that values must match to be passed into a command. You can think of this as the type-checking that occurs when you call a function.
/// The syntactic shapes that describe how a sequence should be parsed.
///
/// This extends beyond [`Type`] which describes how [`Value`](crate::Value)s are represented.
/// `SyntaxShape`s can describe the parsing rules for arguments to a command.
/// e.g. [`SyntaxShape::GlobPattern`]/[`SyntaxShape::Filepath`] serve the completer,
/// but don't have an associated [`Value`](crate::Value)
/// There are additional `SyntaxShape`s that only make sense in particular expressions or keywords
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum SyntaxShape {
/// Any syntactic form is allowed
@ -52,7 +56,9 @@ pub enum SyntaxShape {
/// A floating point value, eg `1.0`
Float,
/// A dotted path to navigate the table (including variable)
/// A dotted path including the variable to access items
///
/// Fully qualified
FullCellPath,
/// A glob pattern is allowed, eg `foo*`
@ -115,6 +121,16 @@ pub enum SyntaxShape {
}
impl SyntaxShape {
/// If possible provide the associated concrete [`Type`]
///
/// Note: Some [`SyntaxShape`]s don't have a corresponding [`Value`](crate::Value)
/// Here we currently return [`Type::Any`]
///
/// ```rust
/// use nu_protocol::{SyntaxShape, Type};
/// let non_value = SyntaxShape::ImportPattern;
/// assert_eq!(non_value.to_type(), Type::Any);
/// ```
pub fn to_type(&self) -> Type {
let mk_ty = |tys: &[(String, SyntaxShape)]| {
tys.iter()