mirror of
https://github.com/sagiegurari/duckscript
synced 2024-07-01 06:34:19 +00:00
lint rules
This commit is contained in:
parent
6169a5119b
commit
b8411d0964
|
@ -58,7 +58,7 @@ pub(crate) fn expand_by_wrapper(
|
||||||
} else if prefix_index == 0 && (next_char == '$' || next_char == '%') {
|
} else if prefix_index == 0 && (next_char == '$' || next_char == '%') {
|
||||||
prefix_index = 1;
|
prefix_index = 1;
|
||||||
|
|
||||||
single_type = if next_char == '$' { true } else { false };
|
single_type = next_char == '$';
|
||||||
} else if prefix_index == 1 && next_char == '{' {
|
} else if prefix_index == 1 && next_char == '{' {
|
||||||
found_prefix = true;
|
found_prefix = true;
|
||||||
prefix_index = 0;
|
prefix_index = 0;
|
||||||
|
@ -71,9 +71,8 @@ pub(crate) fn expand_by_wrapper(
|
||||||
value_string.push(next_char);
|
value_string.push(next_char);
|
||||||
}
|
}
|
||||||
} else if next_char == '}' {
|
} else if next_char == '}' {
|
||||||
match variables.get(&key) {
|
if let Some(variable_value) = variables.get(&key) {
|
||||||
Some(variable_value) => value_string.push_str(&variable_value),
|
value_string.push_str(variable_value)
|
||||||
_ => (),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
key.clear();
|
key.clear();
|
||||||
|
@ -95,7 +94,7 @@ pub(crate) fn expand_by_wrapper(
|
||||||
|
|
||||||
if force_push {
|
if force_push {
|
||||||
value_string.push('\\');
|
value_string.push('\\');
|
||||||
} else if key.len() > 0 {
|
} else if !key.is_empty() {
|
||||||
if prefix_index > 0 || found_prefix {
|
if prefix_index > 0 || found_prefix {
|
||||||
push_prefix(&mut value_string, single_type, found_prefix);
|
push_prefix(&mut value_string, single_type, found_prefix);
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,7 +38,7 @@ pub fn parse_text_with_source_file(
|
||||||
let mut meta_info = InstructionMetaInfo::new();
|
let mut meta_info = InstructionMetaInfo::new();
|
||||||
meta_info.source = Some(source_file.to_string());
|
meta_info.source = Some(source_file.to_string());
|
||||||
|
|
||||||
parse_lines(&text, meta_info)
|
parse_lines(text, meta_info)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_lines(
|
fn parse_lines(
|
||||||
|
@ -51,18 +51,17 @@ fn parse_lines(
|
||||||
for line in lines.lines() {
|
for line in lines.lines() {
|
||||||
let mut line_meta_info = meta_info.clone();
|
let mut line_meta_info = meta_info.clone();
|
||||||
line_meta_info.line = Some(line_number);
|
line_meta_info.line = Some(line_number);
|
||||||
line_number = line_number + 1;
|
line_number += 1;
|
||||||
|
|
||||||
match parse_line(&line, line_meta_info) {
|
match parse_line(line, line_meta_info) {
|
||||||
Ok(instruction) => {
|
Ok(instruction) => {
|
||||||
instructions.push(instruction.clone());
|
instructions.push(instruction.clone());
|
||||||
|
|
||||||
match instruction.instruction_type {
|
if let InstructionType::PreProcess(_) = instruction.instruction_type {
|
||||||
InstructionType::PreProcess(_) => match preprocessor::run(&instruction) {
|
match preprocessor::run(&instruction) {
|
||||||
Ok(mut added_instructions) => instructions.append(&mut added_instructions),
|
Ok(mut added_instructions) => instructions.append(&mut added_instructions),
|
||||||
Err(error) => return Err(error),
|
Err(error) => return Err(error),
|
||||||
},
|
}
|
||||||
_ => (),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(error) => return Err(error),
|
Err(error) => return Err(error),
|
||||||
|
@ -75,7 +74,7 @@ fn parse_lines(
|
||||||
fn parse_line(line_text: &str, meta_info: InstructionMetaInfo) -> Result<Instruction, ScriptError> {
|
fn parse_line(line_text: &str, meta_info: InstructionMetaInfo) -> Result<Instruction, ScriptError> {
|
||||||
let trimmed_text = line_text.trim();
|
let trimmed_text = line_text.trim();
|
||||||
|
|
||||||
if trimmed_text.is_empty() || trimmed_text.starts_with(&COMMENT_PREFIX_STR) {
|
if trimmed_text.is_empty() || trimmed_text.starts_with(COMMENT_PREFIX_STR) {
|
||||||
Ok(Instruction {
|
Ok(Instruction {
|
||||||
meta_info,
|
meta_info,
|
||||||
instruction_type: InstructionType::Empty,
|
instruction_type: InstructionType::Empty,
|
||||||
|
@ -104,7 +103,7 @@ fn parse_pre_process_line(
|
||||||
let end_index = line_text.len();
|
let end_index = line_text.len();
|
||||||
for _i in index..end_index {
|
for _i in index..end_index {
|
||||||
let character = line_text[index];
|
let character = line_text[index];
|
||||||
index = index + 1;
|
index += 1;
|
||||||
|
|
||||||
if character == ' ' {
|
if character == ' ' {
|
||||||
if !command.is_empty() {
|
if !command.is_empty() {
|
||||||
|
@ -118,7 +117,7 @@ fn parse_pre_process_line(
|
||||||
if command.is_empty() {
|
if command.is_empty() {
|
||||||
Err(ScriptError::PreProcessNoCommandFound(meta_info))
|
Err(ScriptError::PreProcessNoCommandFound(meta_info))
|
||||||
} else {
|
} else {
|
||||||
match parse_arguments(&meta_info, &line_text, index) {
|
match parse_arguments(&meta_info, line_text, index) {
|
||||||
Ok(arguments) => {
|
Ok(arguments) => {
|
||||||
let mut instruction = PreProcessInstruction::new();
|
let mut instruction = PreProcessInstruction::new();
|
||||||
instruction.command = Some(command);
|
instruction.command = Some(command);
|
||||||
|
@ -151,7 +150,7 @@ fn parse_command_line(
|
||||||
// search for label
|
// search for label
|
||||||
let mut index = start_index;
|
let mut index = start_index;
|
||||||
let mut instruction = ScriptInstruction::new();
|
let mut instruction = ScriptInstruction::new();
|
||||||
match find_label(&meta_info, &line_text, index) {
|
match find_label(&meta_info, line_text, index) {
|
||||||
Ok(output) => {
|
Ok(output) => {
|
||||||
let (next_index, value) = output;
|
let (next_index, value) = output;
|
||||||
index = next_index;
|
index = next_index;
|
||||||
|
@ -164,12 +163,12 @@ fn parse_command_line(
|
||||||
};
|
};
|
||||||
|
|
||||||
// find output variable and command
|
// find output variable and command
|
||||||
index = match find_output_and_command(&meta_info, &line_text, index, &mut instruction) {
|
index = match find_output_and_command(&meta_info, line_text, index, &mut instruction) {
|
||||||
Ok(next_index) => next_index,
|
Ok(next_index) => next_index,
|
||||||
Err(error) => return Err(error),
|
Err(error) => return Err(error),
|
||||||
};
|
};
|
||||||
|
|
||||||
match parse_arguments(&meta_info, &line_text, index) {
|
match parse_arguments(&meta_info, line_text, index) {
|
||||||
Ok(arguments) => {
|
Ok(arguments) => {
|
||||||
instruction.arguments = arguments;
|
instruction.arguments = arguments;
|
||||||
|
|
||||||
|
@ -218,7 +217,7 @@ fn parse_arguments_with_options(
|
||||||
|
|
||||||
let mut index = start_index;
|
let mut index = start_index;
|
||||||
loop {
|
loop {
|
||||||
match parse_next_argument(&meta_info, &line_text, index, control_as_char) {
|
match parse_next_argument(meta_info, line_text, index, control_as_char) {
|
||||||
Ok(output) => {
|
Ok(output) => {
|
||||||
let (next_index, argument) = output;
|
let (next_index, argument) = output;
|
||||||
|
|
||||||
|
@ -247,8 +246,8 @@ fn parse_next_argument(
|
||||||
control_as_char: bool,
|
control_as_char: bool,
|
||||||
) -> Result<(usize, Option<String>), ScriptError> {
|
) -> Result<(usize, Option<String>), ScriptError> {
|
||||||
parse_next_value(
|
parse_next_value(
|
||||||
&meta_info,
|
meta_info,
|
||||||
&line_text,
|
line_text,
|
||||||
start_index,
|
start_index,
|
||||||
true,
|
true,
|
||||||
!control_as_char,
|
!control_as_char,
|
||||||
|
@ -259,7 +258,7 @@ fn parse_next_argument(
|
||||||
|
|
||||||
fn parse_next_value(
|
fn parse_next_value(
|
||||||
meta_info: &InstructionMetaInfo,
|
meta_info: &InstructionMetaInfo,
|
||||||
line_text: &Vec<char>,
|
line_text: &[char],
|
||||||
start_index: usize,
|
start_index: usize,
|
||||||
allow_quotes: bool,
|
allow_quotes: bool,
|
||||||
allow_control: bool,
|
allow_control: bool,
|
||||||
|
@ -280,7 +279,7 @@ fn parse_next_value(
|
||||||
let mut found_variable_prefix = false;
|
let mut found_variable_prefix = false;
|
||||||
for _i in index..end_index {
|
for _i in index..end_index {
|
||||||
let character = line_text[index];
|
let character = line_text[index];
|
||||||
index = index + 1;
|
index += 1;
|
||||||
|
|
||||||
if in_argument {
|
if in_argument {
|
||||||
if in_control {
|
if in_control {
|
||||||
|
@ -327,7 +326,7 @@ fn parse_next_value(
|
||||||
|| (stop_on_equals && character == '='))
|
|| (stop_on_equals && character == '='))
|
||||||
{
|
{
|
||||||
if character == ' ' || character == '=' {
|
if character == ' ' || character == '=' {
|
||||||
index = index - 1;
|
index -= 1;
|
||||||
} else if character == '#' {
|
} else if character == '#' {
|
||||||
index = end_index;
|
index = end_index;
|
||||||
}
|
}
|
||||||
|
@ -394,27 +393,24 @@ fn find_label(
|
||||||
let mut index = start_index;
|
let mut index = start_index;
|
||||||
for _i in index..end_index {
|
for _i in index..end_index {
|
||||||
let character = line_text[index];
|
let character = line_text[index];
|
||||||
index = index + 1;
|
index += 1;
|
||||||
|
|
||||||
if character == LABEL_PREFIX {
|
if character == LABEL_PREFIX {
|
||||||
match parse_next_value(&meta_info, &line_text, index, false, false, false, false) {
|
match parse_next_value(meta_info, line_text, index, false, false, false, false) {
|
||||||
Ok(output) => {
|
Ok(output) => {
|
||||||
let (next_index, value) = output;
|
let (next_index, value) = output;
|
||||||
index = next_index;
|
index = next_index;
|
||||||
|
|
||||||
match value {
|
if let Some(label_value) = value {
|
||||||
Some(label_value) => {
|
if label_value.is_empty() {
|
||||||
if label_value.is_empty() {
|
return Err(ScriptError::EmptyLabel(meta_info.clone()));
|
||||||
return Err(ScriptError::EmptyLabel(meta_info.clone()));
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut text = String::new();
|
|
||||||
text.push(LABEL_PREFIX);
|
|
||||||
text.push_str(&label_value);
|
|
||||||
|
|
||||||
label = Some(text);
|
|
||||||
}
|
}
|
||||||
None => (),
|
|
||||||
|
let mut text = String::new();
|
||||||
|
text.push(LABEL_PREFIX);
|
||||||
|
text.push_str(&label_value);
|
||||||
|
|
||||||
|
label = Some(text);
|
||||||
};
|
};
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
@ -422,7 +418,7 @@ fn find_label(
|
||||||
Err(error) => return Err(error),
|
Err(error) => return Err(error),
|
||||||
};
|
};
|
||||||
} else if character != ' ' {
|
} else if character != ' ' {
|
||||||
index = index - 1;
|
index -= 1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -437,15 +433,7 @@ fn find_output_and_command(
|
||||||
start_index: usize,
|
start_index: usize,
|
||||||
instruction: &mut ScriptInstruction,
|
instruction: &mut ScriptInstruction,
|
||||||
) -> Result<usize, ScriptError> {
|
) -> Result<usize, ScriptError> {
|
||||||
match parse_next_value(
|
match parse_next_value(meta_info, line_text, start_index, false, false, true, false) {
|
||||||
&meta_info,
|
|
||||||
&line_text,
|
|
||||||
start_index,
|
|
||||||
false,
|
|
||||||
false,
|
|
||||||
true,
|
|
||||||
false,
|
|
||||||
) {
|
|
||||||
Ok(output) => {
|
Ok(output) => {
|
||||||
let (next_index, value) = output;
|
let (next_index, value) = output;
|
||||||
|
|
||||||
|
@ -456,7 +444,7 @@ fn find_output_and_command(
|
||||||
let end_index = line_text.len();
|
let end_index = line_text.len();
|
||||||
for _i in index..end_index {
|
for _i in index..end_index {
|
||||||
let character = line_text[index];
|
let character = line_text[index];
|
||||||
index = index + 1;
|
index += 1;
|
||||||
|
|
||||||
if character != ' ' {
|
if character != ' ' {
|
||||||
if character == '=' {
|
if character == '=' {
|
||||||
|
@ -468,9 +456,8 @@ fn find_output_and_command(
|
||||||
}
|
}
|
||||||
|
|
||||||
if instruction.output.is_some() {
|
if instruction.output.is_some() {
|
||||||
match parse_next_value(
|
match parse_next_value(meta_info, line_text, index, false, false, false, false)
|
||||||
&meta_info, &line_text, index, false, false, false, false,
|
{
|
||||||
) {
|
|
||||||
Ok(output) => {
|
Ok(output) => {
|
||||||
let (next_index, value) = output;
|
let (next_index, value) = output;
|
||||||
|
|
||||||
|
|
|
@ -26,7 +26,7 @@ pub(crate) fn run(
|
||||||
match path_buffer.parent() {
|
match path_buffer.parent() {
|
||||||
Some(path) => {
|
Some(path) => {
|
||||||
let mut parent_path_buffer = path.to_path_buf();
|
let mut parent_path_buffer = path.to_path_buf();
|
||||||
parent_path_buffer.push(&argument);
|
parent_path_buffer.push(argument);
|
||||||
|
|
||||||
let full_path_buffer = match parent_path_buffer.canonicalize() {
|
let full_path_buffer = match parent_path_buffer.canonicalize() {
|
||||||
Ok(new_buffer) => new_buffer,
|
Ok(new_buffer) => new_buffer,
|
||||||
|
|
|
@ -97,20 +97,13 @@ fn create_runtime(instructions: Vec<Instruction>, context: Context) -> Runtime {
|
||||||
|
|
||||||
let mut line = 0;
|
let mut line = 0;
|
||||||
for instruction in &instructions {
|
for instruction in &instructions {
|
||||||
match &instruction.instruction_type {
|
if let InstructionType::Script(ref value) = &instruction.instruction_type {
|
||||||
InstructionType::Script(ref value) => {
|
if let Some(ref label) = value.label {
|
||||||
match value.label {
|
runtime.label_to_line.insert(label.to_string(), line);
|
||||||
Some(ref label) => {
|
};
|
||||||
runtime.label_to_line.insert(label.to_string(), line);
|
|
||||||
()
|
|
||||||
}
|
|
||||||
None => (),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
_ => (),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
line = line + 1;
|
line += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
runtime.instructions = Some(instructions);
|
runtime.instructions = Some(instructions);
|
||||||
|
@ -145,7 +138,7 @@ fn run_instructions(
|
||||||
&mut runtime.context.commands,
|
&mut runtime.context.commands,
|
||||||
&mut runtime.context.variables,
|
&mut runtime.context.variables,
|
||||||
&mut state,
|
&mut state,
|
||||||
&instructions,
|
instructions,
|
||||||
instruction,
|
instruction,
|
||||||
line,
|
line,
|
||||||
);
|
);
|
||||||
|
@ -185,23 +178,18 @@ fn run_instructions(
|
||||||
|
|
||||||
let post_error_line = line + 1;
|
let post_error_line = line + 1;
|
||||||
|
|
||||||
match run_on_error_instruction(
|
if let Err(error) = run_on_error_instruction(
|
||||||
&mut runtime.context.commands,
|
&mut runtime.context.commands,
|
||||||
&mut runtime.context.variables,
|
&mut runtime.context.variables,
|
||||||
&mut state,
|
&mut state,
|
||||||
&instructions,
|
instructions,
|
||||||
error,
|
error,
|
||||||
meta_info.clone(),
|
meta_info.clone(),
|
||||||
) {
|
) {
|
||||||
Err(error) => {
|
return Err(ScriptError::Runtime(error, Some(meta_info.clone())));
|
||||||
return Err(ScriptError::Runtime(error, Some(meta_info.clone())));
|
|
||||||
}
|
|
||||||
_ => (),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
line = post_error_line;
|
line = post_error_line;
|
||||||
|
|
||||||
()
|
|
||||||
}
|
}
|
||||||
CommandResult::Crash(error) => {
|
CommandResult::Crash(error) => {
|
||||||
let script_error = ScriptError::Runtime(error, Some(meta_info));
|
let script_error = ScriptError::Runtime(error, Some(meta_info));
|
||||||
|
@ -215,9 +203,7 @@ fn run_instructions(
|
||||||
CommandResult::Continue(output) => {
|
CommandResult::Continue(output) => {
|
||||||
update_output(&mut runtime.context.variables, output_variable, output);
|
update_output(&mut runtime.context.variables, output_variable, output);
|
||||||
|
|
||||||
line = line + 1;
|
line += 1;
|
||||||
|
|
||||||
()
|
|
||||||
}
|
}
|
||||||
CommandResult::GoTo(output, goto_value) => {
|
CommandResult::GoTo(output, goto_value) => {
|
||||||
update_output(&mut runtime.context.variables, output_variable, output);
|
update_output(&mut runtime.context.variables, output_variable, output);
|
||||||
|
@ -314,26 +300,24 @@ pub fn run_instruction(
|
||||||
Some(ref command) => match commands.get_for_use(command) {
|
Some(ref command) => match commands.get_for_use(command) {
|
||||||
Some(command_instance) => {
|
Some(command_instance) => {
|
||||||
let command_arguments = bind_command_arguments(
|
let command_arguments = bind_command_arguments(
|
||||||
&variables,
|
variables,
|
||||||
&script_instruction,
|
script_instruction,
|
||||||
&instruction.meta_info,
|
&instruction.meta_info,
|
||||||
);
|
);
|
||||||
|
|
||||||
let command_result = if command_instance.requires_context() {
|
if command_instance.requires_context() {
|
||||||
command_instance.run_with_context(
|
command_instance.run_with_context(
|
||||||
command_arguments,
|
command_arguments,
|
||||||
state,
|
state,
|
||||||
variables,
|
variables,
|
||||||
output_variable.clone(),
|
output_variable.clone(),
|
||||||
&instructions,
|
instructions,
|
||||||
commands,
|
commands,
|
||||||
line,
|
line,
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
command_instance.run(command_arguments)
|
command_instance.run(command_arguments)
|
||||||
};
|
}
|
||||||
|
|
||||||
command_result
|
|
||||||
}
|
}
|
||||||
None => CommandResult::Crash(format!("Command: {} not found.", &command)),
|
None => CommandResult::Crash(format!("Command: {} not found.", &command)),
|
||||||
},
|
},
|
||||||
|
|
Loading…
Reference in New Issue
Block a user