diff --git a/crates/nu-parser/src/parser.rs b/crates/nu-parser/src/parser.rs index c355a5df01..beb6777a6d 100644 --- a/crates/nu-parser/src/parser.rs +++ b/crates/nu-parser/src/parser.rs @@ -1688,6 +1688,12 @@ pub(crate) fn parse_dollar_expr( if contents.starts_with(b"$\"") || contents.starts_with(b"$'") { parse_string_interpolation(working_set, span, expand_aliases_denylist) + } else if contents.starts_with(b"$.") { + parse_simple_cell_path( + working_set, + Span::new(span.start + 2, span.end), + expand_aliases_denylist, + ) } else if let (expr, None) = parse_range(working_set, span, expand_aliases_denylist) { (expr, None) } else { @@ -2129,6 +2135,33 @@ pub fn parse_cell_path( (tail, error) } +pub fn parse_simple_cell_path( + working_set: &mut StateWorkingSet, + span: Span, + expand_aliases_denylist: &[usize], +) -> (Expression, Option) { + let source = working_set.get_span_contents(span); + let mut error = None; + + let (tokens, err) = lex(source, span.start, &[b'\n', b'\r'], &[b'.', b'?'], true); + error = error.or(err); + + let tokens = tokens.into_iter().peekable(); + + let (cell_path, err) = parse_cell_path(working_set, tokens, false, expand_aliases_denylist); + error = error.or(err); + + ( + Expression { + expr: Expr::CellPath(CellPath { members: cell_path }), + span, + ty: Type::CellPath, + custom_completion: None, + }, + error, + ) +} + pub fn parse_full_cell_path( working_set: &mut StateWorkingSet, implicit_head: Option, @@ -4646,29 +4679,7 @@ pub fn parse_value( ) } } - SyntaxShape::CellPath => { - let source = working_set.get_span_contents(span); - let mut error = None; - - let (tokens, err) = lex(source, span.start, &[b'\n', b'\r'], &[b'.', b'?'], true); - error = error.or(err); - - let tokens = tokens.into_iter().peekable(); - - let (cell_path, err) = - parse_cell_path(working_set, tokens, false, expand_aliases_denylist); - error = error.or(err); - - ( - Expression { - expr: Expr::CellPath(CellPath { members: cell_path }), - span, - ty: Type::CellPath, - custom_completion: None, - }, - error, - ) - } + SyntaxShape::CellPath => parse_simple_cell_path(working_set, span, expand_aliases_denylist), SyntaxShape::Boolean => { // Redundant, though we catch bad boolean parses here if bytes == b"true" || bytes == b"false" { diff --git a/src/tests/test_cell_path.rs b/src/tests/test_cell_path.rs index 04a08a37a7..021246b6a3 100644 --- a/src/tests/test_cell_path.rs +++ b/src/tests/test_cell_path.rs @@ -133,3 +133,8 @@ fn list_row_optional_access_succeeds() -> TestResult { fn do_not_delve_too_deep_in_nested_lists() -> TestResult { fail_test("[[{foo: bar}]].foo", "cannot find column") } + +#[test] +fn cell_path_literals() -> TestResult { + run_test("let cell_path = $.a.b; {a: {b: 3}} | get $cell_path", "3") +}