Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: provide span for more nodes #17

Merged
merged 2 commits into from
Nov 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ version: 2.1
jobs:
test:
docker:
- image: rust:1.42-slim-buster
- image: rust:1.55-slim-buster
steps:
- checkout
- run:
Expand All @@ -19,7 +19,7 @@ jobs:

publish:
docker:
- image: rust:1.42-slim-buster
- image: rust:1.55-slim-buster
steps:
- checkout
- run:
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ lazy_static = "1.4"

[dev-dependencies]
indoc = "0.3"
pretty_assertions = "0.7.2"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will admit that it's a little bit overboard how many spans get tested with this PR, but this makes it a lot more manageable. Perhaps in the future we could make it so that spans can be skipped in some of the assertions.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine by me, I'd also like to reduce the test code verbosity eventually but I don't think we need to deal with that now.


[lib]
name = "dockerfile_parser"
Expand Down
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ errors in addition to a full syntax tree.
## Limitations

* Buildkit parser directives are not handled at all.
* Not all instructions record spans; feel free to file an issue if some use
case is missing a span
* Unknown instructions are parsed as `MiscInstruction` rather than producing
an explicit error. A number of valid but less interesting Docker instructions
are handled this way, e.g. `ONBUILD`, `MAINTAINER`, etc. See notes in
Expand Down
2 changes: 1 addition & 1 deletion examples/splice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn wrap() -> Result<()> {

for ins in dockerfile.instructions {
if let Instruction::From(f) = ins {
splicer.splice(&f.image_span, "splice:test");
splicer.splice(&f.image.span, "splice:test");
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/dockerfile_parser.pest
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,15 @@ label = { ^"label" ~ (label_single | (arg_ws ~ label_pair?)+) }

run_shell = @{ any_breakable }
run_exec = { string_array }
run = _{ ^"run" ~ arg_ws ~ (run_exec | run_shell) }
run = { ^"run" ~ arg_ws ~ (run_exec | run_shell) }

entrypoint_shell = @{ any_breakable }
entrypoint_exec = { string_array }
entrypoint = _{ ^"entrypoint" ~ arg_ws ~ (entrypoint_exec | entrypoint_shell) }
entrypoint = { ^"entrypoint" ~ arg_ws ~ (entrypoint_exec | entrypoint_shell) }

cmd_shell = @{ any_breakable }
cmd_exec = { string_array }
cmd = _{ ^"cmd" ~ arg_ws ~ (cmd_exec | cmd_shell) }
cmd = { ^"cmd" ~ arg_ws ~ (cmd_exec | cmd_shell) }

copy_flag_name = @{ ASCII_ALPHA+ }
copy_flag_value = @{ any_whitespace }
Expand All @@ -149,7 +149,7 @@ env_pairs = { (arg_ws ~ env_pair?)+ }
env_single_quoted_value = ${ string }
env_single_value = @{ any_breakable }
env_single = { arg_ws ~ env_name ~ arg_ws ~ (env_single_quoted_value | env_single_value) }
env = _{ ^"env" ~ (env_single | env_pairs) }
env = { ^"env" ~ (env_single | env_pairs) }

misc_instruction = @{ ASCII_ALPHA+ }
misc_arguments = @{ any_breakable }
Expand Down
31 changes: 20 additions & 11 deletions src/dockerfile_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,21 @@ impl Instruction {
_ => None,
}
}

/// Gets the span of the instruction.
pub fn span(&self) -> Span {
match self {
Instruction::From(instruction) => instruction.span,
Instruction::Arg(instruction) => instruction.span,
Instruction::Label(instruction) => instruction.span,
Instruction::Run(instruction) => instruction.span,
Instruction::Entrypoint(instruction) => instruction.span,
Instruction::Cmd(instruction) => instruction.span,
Instruction::Copy(instruction) => instruction.span,
Instruction::Env(instruction) => instruction.span,
Instruction::Misc(instruction) => instruction.span,
}
}
}

/// Maps an instruction struct to its enum variant, implementing From<T> on
Expand Down Expand Up @@ -240,21 +255,15 @@ impl TryFrom<Pair<'_>> for Instruction {
Rule::arg => ArgInstruction::from_record(record)?.into(),
Rule::label => LabelInstruction::from_record(record)?.into(),

Rule::run_exec => RunInstruction::from_exec_record(record)?.into(),
Rule::run_shell => RunInstruction::from_shell_record(record)?.into(),
Rule::run => RunInstruction::from_record(record)?.into(),

Rule::entrypoint_exec =>
EntrypointInstruction::from_exec_record(record)?.into(),
Rule::entrypoint_shell =>
EntrypointInstruction::from_shell_record(record)?.into(),
Rule::entrypoint => EntrypointInstruction::from_record(record)?.into(),

Rule::cmd_exec => CmdInstruction::from_exec_record(record)?.into(),
Rule::cmd_shell => CmdInstruction::from_shell_record(record)?.into(),
Rule::cmd => CmdInstruction::from_record(record)?.into(),

Rule::copy => Instruction::Copy(CopyInstruction::from_record(record)?),

Rule::env_single => EnvInstruction::from_single_record(record)?.into(),
Rule::env_pairs => EnvInstruction::from_pairs_record(record)?.into(),
Rule::env => EnvInstruction::from_record(record)?.into(),

Rule::misc => MiscInstruction::from_record(record)?.into(),

Expand Down Expand Up @@ -393,7 +402,7 @@ impl Dockerfile {
for ins in &self.instructions {
match ins {
Instruction::Arg(a) => {
if a.name == name {
if a.name.content == name {
return Some(a);
} else {
continue
Expand Down
4 changes: 2 additions & 2 deletions src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ impl ImageRef {
let vars: HashMap<&'a str, &'a str> = HashMap::from_iter(
dockerfile.global_args
.iter()
.filter_map(|a| match a.value.as_deref() {
Some(v) => Some((a.name.as_str(), v)),
.filter_map(|a| match a.value.as_ref() {
Some(v) => Some((a.name.as_ref(), v.as_ref())),
None => None
})
);
Expand Down
75 changes: 35 additions & 40 deletions src/instructions/arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
use std::convert::TryFrom;

use crate::dockerfile_parser::Instruction;
use crate::SpannedString;
use crate::error::*;
use crate::parse_string;
use crate::parser::{Pair, Rule};
use crate::splicer::Span;

use enquote::unquote;
use snafu::ResultExt;

/// A Dockerfile [`ARG` instruction][arg].
///
/// [arg]: https://docs.docker.com/engine/reference/builder/#arg
Expand All @@ -18,19 +17,15 @@ pub struct ArgInstruction {
pub span: Span,

/// The argument key
pub name: String,

pub name_span: Span,
pub name: SpannedString,

/// An optional argument value.
///
/// This may be unset when passing arguments through to later stages in a
/// [multi-stage build][build].
///
/// [build]: https://docs.docker.com/develop/develop-images/multistage-build/
pub value: Option<String>,

pub value_span: Option<Span>,
pub value: Option<SpannedString>,
}

impl ArgInstruction {
Expand All @@ -41,39 +36,25 @@ impl ArgInstruction {

for field in record.into_inner() {
match field.as_rule() {
Rule::arg_name => name = Some((field.as_str(), Span::from_pair(&field))),
Rule::arg_quoted_value => {
let v = unquote(field.as_str()).context(UnescapeError)?;

value = Some((v, Span::from_pair(&field)));
},
Rule::arg_value => value = Some((
field.as_str().to_string(),
Span::from_pair(&field)
)),
Rule::arg_name => name = Some(parse_string(&field)?),
Rule::arg_quoted_value => value = Some(parse_string(&field)?),
Rule::arg_value => value = Some(parse_string(&field)?),
Rule::comment => continue,
_ => return Err(unexpected_token(field))
}
}

let (name, name_span) = match name {
Some((name, name_span)) => (name.to_string(), name_span),
let name = match name {
Some(name) => name,
_ => return Err(Error::GenericParseError {
message: "arg name is required".into()
})
};

let (value, value_span) = match value {
Some((value, value_span)) => (Some(value), Some(value_span)),
None => (None, None)
};

Ok(ArgInstruction {
span,
name,
name_span,
value,
value_span,
})
}
}
Expand All @@ -95,6 +76,8 @@ impl<'a> TryFrom<&'a Instruction> for &'a ArgInstruction {

#[cfg(test)]
mod tests {
use pretty_assertions::assert_eq;

use super::*;
use crate::Dockerfile;
use crate::test_util::*;
Expand All @@ -105,32 +88,44 @@ mod tests {
parse_single(r#"arg foo=bar"#, Rule::arg)?,
ArgInstruction {
span: Span::new(0, 11),
name: "foo".into(),
name_span: Span::new(4, 7),
value: Some("bar".into()),
value_span: Some(Span::new(8, 11)),
name: SpannedString {
span: Span::new(4, 7),
content: "foo".into(),
},
value: Some(SpannedString {
span: Span::new(8, 11),
content: "bar".into(),
}),
}.into()
);

assert_eq!(
parse_single(r#"arg foo="bar""#, Rule::arg)?,
ArgInstruction {
span: Span::new(0, 13),
name: "foo".into(),
name_span: Span::new(4, 7),
value: Some("bar".into()),
value_span: Some(Span::new(8, 13)),
name: SpannedString {
span: Span::new(4, 7),
content: "foo".into(),
},
value: Some(SpannedString {
span: Span::new(8, 13),
content: "bar".into(),
}),
}.into()
);

assert_eq!(
parse_single(r#"arg foo='bar'"#, Rule::arg)?,
ArgInstruction {
span: Span::new(0, 13),
name: "foo".into(),
name_span: Span::new(4, 7),
value: Some("bar".into()),
value_span: Some(Span::new(8, 13)),
name: SpannedString {
span: Span::new(4, 7),
content: "foo".into(),
},
value: Some(SpannedString {
span: Span::new(8, 13),
content: "bar".into(),
}),
}.into()
);

Expand Down
Loading