Skip to content

Commit 5512610

Browse files
committed
refactor: lite: bool -> arg_parsing_level: ArgumentParsingLevel
1 parent 083d5b5 commit 5512610

File tree

3 files changed

+156
-102
lines changed

3 files changed

+156
-102
lines changed

crates/nu-parser/src/parse_keywords.rs

Lines changed: 98 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
use crate::{
22
exportable::Exportable,
33
parse_block,
4-
parser::{compile_block, parse_attribute, parse_redirection, redirecting_builtin_error},
4+
parser::{
5+
ArgumentParsingLevel, compile_block, parse_attribute, parse_redirection,
6+
redirecting_builtin_error,
7+
},
58
type_check::{check_block_input_output, type_compatible},
69
};
710
use itertools::Itertools;
@@ -280,8 +283,13 @@ pub fn parse_for(working_set: &mut StateWorkingSet, lite_command: &LiteCommand)
280283
}
281284
Some(decl_id) => {
282285
working_set.enter_scope();
283-
let ParsedInternalCall { call, output } =
284-
parse_internal_call(working_set, spans[0], &spans[1..], decl_id, false);
286+
let ParsedInternalCall { call, output } = parse_internal_call(
287+
working_set,
288+
spans[0],
289+
&spans[1..],
290+
decl_id,
291+
ArgumentParsingLevel::Full,
292+
);
285293

286294
working_set.exit_scope();
287295

@@ -589,7 +597,7 @@ fn parse_def_inner(
589597
Span::concat(command_spans),
590598
rest_spans,
591599
decl_id,
592-
false,
600+
ArgumentParsingLevel::Full,
593601
);
594602
// This is to preserve the order of the errors so that
595603
// the check errors below come first
@@ -843,7 +851,7 @@ fn parse_extern_inner(
843851
Span::concat(command_spans),
844852
rest_spans,
845853
decl_id,
846-
false,
854+
ArgumentParsingLevel::Full,
847855
);
848856
working_set.exit_scope();
849857

@@ -1105,7 +1113,7 @@ pub fn parse_alias(
11051113
Span::concat(command_spans),
11061114
rest_spans,
11071115
decl_id,
1108-
false,
1116+
ArgumentParsingLevel::Full,
11091117
);
11101118

11111119
working_set
@@ -1368,7 +1376,7 @@ pub fn parse_export_in_block(
13681376
&parts[2..]
13691377
},
13701378
decl_id,
1371-
false,
1379+
ArgumentParsingLevel::FirstK { k: 0 },
13721380
);
13731381
// don't need errors generated by parse_internal_call
13741382
// further error will be generated by detail `parse_xxx` function.
@@ -1839,8 +1847,13 @@ pub fn parse_export_env(
18391847

18401848
let call = match working_set.find_decl(b"export-env") {
18411849
Some(decl_id) => {
1842-
let ParsedInternalCall { call, output } =
1843-
parse_internal_call(working_set, spans[0], &[spans[1]], decl_id, false);
1850+
let ParsedInternalCall { call, output } = parse_internal_call(
1851+
working_set,
1852+
spans[0],
1853+
&[spans[1]],
1854+
decl_id,
1855+
ArgumentParsingLevel::Full,
1856+
);
18441857
let decl = working_set.get_decl(decl_id);
18451858

18461859
let call_span = Span::concat(spans);
@@ -2343,7 +2356,7 @@ pub fn parse_module(
23432356
1
23442357
};
23452358

2346-
let (call, call_span) = match working_set.find_decl(b"module") {
2359+
let (mut call, call_span) = match working_set.find_decl(b"module") {
23472360
Some(decl_id) => {
23482361
let (command_spans, rest_spans) = spans.split_at(split_id);
23492362

@@ -2352,7 +2365,7 @@ pub fn parse_module(
23522365
Span::concat(command_spans),
23532366
rest_spans,
23542367
decl_id,
2355-
true,
2368+
ArgumentParsingLevel::FirstK { k: 1 },
23562369
);
23572370
let decl = working_set.get_decl(decl_id);
23582371

@@ -2388,56 +2401,45 @@ pub fn parse_module(
23882401
}
23892402
};
23902403

2391-
let (module_name_or_path, module_name_or_path_span, module_name_or_path_expr) =
2392-
if let Some(name) = call.positional_nth(0) {
2393-
// Here name is supposed to be a garbage expression
2394-
// since argument `lite` is set to true in last `parse_internal_call` call.
2395-
// Need to parse the name expression here for later override.
2396-
let name_expr = parse_string(working_set, name.span);
2397-
if let Some(s) = name_expr.as_string() {
2398-
if let Some(mod_name) = module_name {
2399-
if s.as_bytes() == mod_name {
2400-
working_set.error(ParseError::NamedAsModule(
2401-
"module".to_string(),
2402-
s,
2403-
"mod".to_string(),
2404-
name.span,
2405-
));
2406-
return (
2407-
Pipeline::from_vec(vec![Expression::new(
2408-
working_set,
2409-
Expr::Call(call),
2410-
call_span,
2411-
Type::Any,
2412-
)]),
2413-
None,
2414-
);
2415-
}
2404+
let (module_name_or_path, module_name_or_path_span) = if let Some(name) = call.positional_nth(0)
2405+
{
2406+
if let Some(s) = name.as_string() {
2407+
if let Some(mod_name) = module_name {
2408+
if s.as_bytes() == mod_name {
2409+
working_set.error(ParseError::NamedAsModule(
2410+
"module".to_string(),
2411+
s,
2412+
"mod".to_string(),
2413+
name.span,
2414+
));
2415+
return (
2416+
Pipeline::from_vec(vec![Expression::new(
2417+
working_set,
2418+
Expr::Call(call),
2419+
call_span,
2420+
Type::Any,
2421+
)]),
2422+
None,
2423+
);
24162424
}
2417-
(s, name.span, name_expr)
2418-
} else {
2419-
working_set.error(ParseError::UnknownState(
2420-
"internal error: name not a string".into(),
2421-
Span::concat(spans),
2422-
));
2423-
return (garbage_pipeline(working_set, spans), None);
24242425
}
2426+
(s, name.span)
24252427
} else {
24262428
working_set.error(ParseError::UnknownState(
2427-
"internal error: missing positional".into(),
2429+
"internal error: name not a string".into(),
24282430
Span::concat(spans),
24292431
));
24302432
return (garbage_pipeline(working_set, spans), None);
2431-
};
2433+
}
2434+
} else {
2435+
working_set.error(ParseError::UnknownState(
2436+
"internal error: missing positional".into(),
2437+
Span::concat(spans),
2438+
));
2439+
return (garbage_pipeline(working_set, spans), None);
2440+
};
24322441

24332442
if spans.len() == split_id + 1 {
2434-
let call = Box::new(Call {
2435-
head: Span::concat(&spans[..split_id]),
2436-
decl_id: call.decl_id,
2437-
arguments: vec![Argument::Positional(module_name_or_path_expr)],
2438-
parser_info: HashMap::new(),
2439-
});
2440-
24412443
let pipeline = Pipeline::from_vec(vec![Expression::new(
24422444
working_set,
24432445
Expr::Call(call),
@@ -2507,19 +2509,7 @@ pub fn parse_module(
25072509
Type::Block,
25082510
);
25092511

2510-
let module_decl_id = working_set
2511-
.find_decl(b"module")
2512-
.expect("internal error: missing module command");
2513-
2514-
let call = Box::new(Call {
2515-
head: Span::concat(&spans[..split_id]),
2516-
decl_id: module_decl_id,
2517-
arguments: vec![
2518-
Argument::Positional(module_name_or_path_expr),
2519-
Argument::Positional(block_expr),
2520-
],
2521-
parser_info: HashMap::new(),
2522-
});
2512+
call.set_kth_argument(1, Argument::Positional(block_expr));
25232513

25242514
(
25252515
Pipeline::from_vec(vec![Expression::new(
@@ -2577,7 +2567,7 @@ pub fn parse_use(
25772567
Span::concat(command_spans),
25782568
rest_spans,
25792569
decl_id,
2580-
false,
2570+
ArgumentParsingLevel::Full,
25812571
);
25822572
let decl = working_set.get_decl(decl_id);
25832573

@@ -2801,8 +2791,13 @@ pub fn parse_hide(working_set: &mut StateWorkingSet, lite_command: &LiteCommand)
28012791

28022792
let (call, args_spans) = match working_set.find_decl(b"hide") {
28032793
Some(decl_id) => {
2804-
let ParsedInternalCall { call, output } =
2805-
parse_internal_call(working_set, spans[0], &spans[1..], decl_id, false);
2794+
let ParsedInternalCall { call, output } = parse_internal_call(
2795+
working_set,
2796+
spans[0],
2797+
&spans[1..],
2798+
decl_id,
2799+
ArgumentParsingLevel::Full,
2800+
);
28062801
let decl = working_set.get_decl(decl_id);
28072802

28082803
let call_span = Span::concat(spans);
@@ -3415,8 +3410,13 @@ pub fn parse_let(working_set: &mut StateWorkingSet, spans: &[Span]) -> Pipeline
34153410
}
34163411
}
34173412
}
3418-
let ParsedInternalCall { call, output } =
3419-
parse_internal_call(working_set, spans[0], &spans[1..], decl_id, false);
3413+
let ParsedInternalCall { call, output } = parse_internal_call(
3414+
working_set,
3415+
spans[0],
3416+
&spans[1..],
3417+
decl_id,
3418+
ArgumentParsingLevel::Full,
3419+
);
34203420

34213421
return Pipeline::from_vec(vec![Expression::new(
34223422
working_set,
@@ -3576,8 +3576,13 @@ pub fn parse_const(working_set: &mut StateWorkingSet, spans: &[Span]) -> (Pipeli
35763576
}
35773577
}
35783578
}
3579-
let ParsedInternalCall { call, output } =
3580-
parse_internal_call(working_set, spans[0], &spans[1..], decl_id, false);
3579+
let ParsedInternalCall { call, output } = parse_internal_call(
3580+
working_set,
3581+
spans[0],
3582+
&spans[1..],
3583+
decl_id,
3584+
ArgumentParsingLevel::Full,
3585+
);
35813586

35823587
return (
35833588
Pipeline::from_vec(vec![Expression::new(
@@ -3698,8 +3703,13 @@ pub fn parse_mut(working_set: &mut StateWorkingSet, spans: &[Span]) -> Pipeline
36983703
}
36993704
}
37003705
}
3701-
let ParsedInternalCall { call, output } =
3702-
parse_internal_call(working_set, spans[0], &spans[1..], decl_id, false);
3706+
let ParsedInternalCall { call, output } = parse_internal_call(
3707+
working_set,
3708+
spans[0],
3709+
&spans[1..],
3710+
decl_id,
3711+
ArgumentParsingLevel::Full,
3712+
);
37033713

37043714
return Pipeline::from_vec(vec![Expression::new(
37053715
working_set,
@@ -3746,8 +3756,13 @@ pub fn parse_source(working_set: &mut StateWorkingSet, lite_command: &LiteComman
37463756

37473757
// Is this the right call to be using here?
37483758
// Some of the others (`parse_let`) use it, some of them (`parse_hide`) don't.
3749-
let ParsedInternalCall { call, output } =
3750-
parse_internal_call(working_set, spans[0], &spans[1..], decl_id, false);
3759+
let ParsedInternalCall { call, output } = parse_internal_call(
3760+
working_set,
3761+
spans[0],
3762+
&spans[1..],
3763+
decl_id,
3764+
ArgumentParsingLevel::Full,
3765+
);
37513766

37523767
let Ok(is_help) = has_flag_const(working_set, &call, "help") else {
37533768
return garbage_pipeline(working_set, spans);
@@ -3906,8 +3921,13 @@ pub fn parse_where_expr(working_set: &mut StateWorkingSet, spans: &[Span]) -> Ex
39063921

39073922
let call = match working_set.find_decl(b"where") {
39083923
Some(decl_id) => {
3909-
let ParsedInternalCall { call, output } =
3910-
parse_internal_call(working_set, spans[0], &spans[1..], decl_id, false);
3924+
let ParsedInternalCall { call, output } = parse_internal_call(
3925+
working_set,
3926+
spans[0],
3927+
&spans[1..],
3928+
decl_id,
3929+
ArgumentParsingLevel::Full,
3930+
);
39113931
let decl = working_set.get_decl(decl_id);
39123932

39133933
let call_span = Span::concat(spans);

0 commit comments

Comments
 (0)