@@ -5,37 +5,18 @@ use nu_protocol::{
55 DeclId , IntoSpanned , ParseError , Span , Spanned , SyntaxShape , Type , engine:: StateWorkingSet ,
66} ;
77
8- #[ derive( Debug , Clone , Copy , PartialEq ) ]
9- pub enum ShapeDescriptorUse {
10- /// Used in an argument position allowing the addition of custom completion
11- Argument ,
12- /// Used to define the type of a variable or input/output types
13- Type ,
14- }
15-
16- /// equivalent to [`parse_shape_name`] with [`ShapeDescriptorUse::Type`] converting the
17- /// [`SyntaxShape`] to its [`Type`]
8+ /// [`parse_shape_name`] then convert to Type
189pub fn parse_type ( working_set : & mut StateWorkingSet , bytes : & [ u8 ] , span : Span ) -> Type {
19- parse_shape_name ( working_set, bytes, span, ShapeDescriptorUse :: Type ) . to_type ( )
10+ parse_shape_name ( working_set, bytes, span) . to_type ( )
2011}
2112
2213/// Parse the literals of [`Type`]-like [`SyntaxShape`]s including inner types.
23- /// Also handles the specification of custom completions with `type@completer`.
24- ///
25- /// Restrict the parsing with `use_loc`
26- /// Used in:
27- /// - [`ShapeDescriptorUse::Argument`]
28- /// - `: ` argument type (+completer) positions in signatures
29- /// - [`ShapeDescriptorUse::Type`]
30- /// - `type->type` input/output type pairs
31- /// - `let name: type` variable type infos
3214///
3315/// NOTE: Does not provide a mapping to every [`SyntaxShape`]
3416pub fn parse_shape_name (
3517 working_set : & mut StateWorkingSet ,
3618 bytes : & [ u8 ] ,
3719 span : Span ,
38- use_loc : ShapeDescriptorUse ,
3920) -> SyntaxShape {
4021 match bytes {
4122 b"any" => SyntaxShape :: Any ,
@@ -70,16 +51,24 @@ pub fn parse_shape_name(
7051 || bytes. starts_with ( b"record" )
7152 || bytes. starts_with ( b"table" ) =>
7253 {
73- parse_generic_shape ( working_set, bytes, span, use_loc )
54+ parse_generic_shape ( working_set, bytes, span)
7455 }
7556 _ => {
57+ if bytes. contains ( & b'@' ) {
58+ working_set. error ( ParseError :: LabeledError (
59+ "Unexpected custom completer in type spec" . into ( ) ,
60+ "Type specifications do not support custom completers" . into ( ) ,
61+ span,
62+ ) ) ;
63+ }
7664 //TODO: Handle error case for unknown shapes
7765 working_set. error ( ParseError :: UnknownType ( span) ) ;
7866 SyntaxShape :: Any
7967 }
8068 }
8169}
8270
71+ /// Handles the specification of custom completions with `type@completer`.
8372pub fn parse_completer (
8473 working_set : & mut StateWorkingSet ,
8574 bytes : & [ u8 ] ,
@@ -100,17 +89,16 @@ fn parse_generic_shape(
10089 working_set : & mut StateWorkingSet < ' _ > ,
10190 bytes : & [ u8 ] ,
10291 span : Span ,
103- use_loc : ShapeDescriptorUse ,
10492) -> SyntaxShape {
10593 let ( type_name, type_params) = split_generic_params ( working_set, bytes, span) ;
10694 match type_name {
10795 b"oneof" => SyntaxShape :: OneOf ( match type_params {
108- Some ( params) => parse_type_params ( working_set, params, use_loc ) ,
96+ Some ( params) => parse_type_params ( working_set, params) ,
10997 None => vec ! [ ] ,
11098 } ) ,
11199 b"list" => SyntaxShape :: List ( Box :: new ( match type_params {
112100 Some ( params) => {
113- let mut parsed_params = parse_type_params ( working_set, params, use_loc ) ;
101+ let mut parsed_params = parse_type_params ( working_set, params) ;
114102 if parsed_params. len ( ) > 1 {
115103 working_set. error ( ParseError :: LabeledError (
116104 "expected a single type parameter" . into ( ) ,
@@ -125,11 +113,11 @@ fn parse_generic_shape(
125113 None => SyntaxShape :: Any ,
126114 } ) ) ,
127115 b"record" => SyntaxShape :: Record ( match type_params {
128- Some ( params) => parse_named_type_params ( working_set, params, use_loc ) ,
116+ Some ( params) => parse_named_type_params ( working_set, params) ,
129117 None => vec ! [ ] ,
130118 } ) ,
131119 b"table" => SyntaxShape :: Table ( match type_params {
132- Some ( params) => parse_named_type_params ( working_set, params, use_loc ) ,
120+ Some ( params) => parse_named_type_params ( working_set, params) ,
133121 None => vec ! [ ] ,
134122 } ) ,
135123 _ => {
@@ -180,7 +168,6 @@ fn split_generic_params<'a>(
180168fn parse_named_type_params (
181169 working_set : & mut StateWorkingSet ,
182170 Spanned { item : source, span } : Spanned < & [ u8 ] > ,
183- use_loc : ShapeDescriptorUse ,
184171) -> Vec < ( String , SyntaxShape ) > {
185172 let ( tokens, err) = lex_signature ( source, span. start , & [ b'\n' , b'\r' ] , & [ b':' , b',' ] , true ) ;
186173
@@ -255,7 +242,7 @@ fn parse_named_type_params(
255242 }
256243
257244 let shape_bytes = working_set. get_span_contents ( tokens[ idx] . span ) . to_vec ( ) ;
258- let shape = parse_shape_name ( working_set, & shape_bytes, tokens[ idx] . span , use_loc ) ;
245+ let shape = parse_shape_name ( working_set, & shape_bytes, tokens[ idx] . span ) ;
259246 sig. push ( ( key, shape) ) ;
260247 idx += 1 ;
261248 }
@@ -266,7 +253,6 @@ fn parse_named_type_params(
266253fn parse_type_params (
267254 working_set : & mut StateWorkingSet ,
268255 Spanned { item : source, span } : Spanned < & [ u8 ] > ,
269- use_loc : ShapeDescriptorUse ,
270256) -> Vec < SyntaxShape > {
271257 let ( tokens, err) = lex_signature ( source, span. start , & [ b'\n' , b'\r' ] , & [ b':' , b',' ] , true ) ;
272258
@@ -288,7 +274,7 @@ fn parse_type_params(
288274 }
289275
290276 let shape_bytes = working_set. get_span_contents ( tokens[ idx] . span ) . to_vec ( ) ;
291- let shape = parse_shape_name ( working_set, & shape_bytes, tokens[ idx] . span , use_loc ) ;
277+ let shape = parse_shape_name ( working_set, & shape_bytes, tokens[ idx] . span ) ;
292278 sig. push ( shape) ;
293279 idx += 1 ;
294280 }
0 commit comments