|
| 1 | +use dockerfile_parser::*; |
| 2 | + |
| 3 | +macro_rules! create_node_ref { |
| 4 | + ($($variant_name:ident($node_name:ident),)*) => { |
| 5 | + #[derive(Clone, Copy)] |
| 6 | + pub enum Node<'a> { |
| 7 | + $( |
| 8 | + $variant_name(&'a $node_name), |
| 9 | + )* |
| 10 | + } |
| 11 | + |
| 12 | + $( |
| 13 | + impl<'a> From<&'a $node_name> for Node<'a> { |
| 14 | + fn from(instruction: &'a $node_name) -> Node<'a> { |
| 15 | + Node::$variant_name(instruction) |
| 16 | + } |
| 17 | + } |
| 18 | + )* |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +create_node_ref!( |
| 23 | + Arg(ArgInstruction), |
| 24 | + Cmd(CmdInstruction), |
| 25 | + Copy(CopyInstruction), |
| 26 | + CopyFlag(CopyFlag), |
| 27 | + From(FromInstruction), |
| 28 | + Label(LabelInstruction), |
| 29 | + LabelLabel(Label), |
| 30 | + Run(RunInstruction), |
| 31 | + Entrypoint(EntrypointInstruction), |
| 32 | + Env(EnvInstruction), |
| 33 | + EnvVar(EnvVar), |
| 34 | + Misc(MiscInstruction), |
| 35 | + String(SpannedString), |
| 36 | + BreakableString(BreakableString), |
| 37 | + StringArray(StringArray), |
| 38 | + Comment(Comment), |
| 39 | +); |
| 40 | + |
| 41 | +impl<'a> Node<'a> { |
| 42 | + #[allow(dead_code)] |
| 43 | + pub fn span(&self) -> Span { |
| 44 | + use Node::*; |
| 45 | + match self { |
| 46 | + From(node) => node.span, |
| 47 | + Arg(node) => node.span, |
| 48 | + Label(node) => node.span, |
| 49 | + LabelLabel(node) => node.span, |
| 50 | + Run(node) => node.span, |
| 51 | + Entrypoint(node) => node.span, |
| 52 | + Cmd(node) => node.span, |
| 53 | + Copy(node) => node.span, |
| 54 | + CopyFlag(node) => node.span, |
| 55 | + Env(node) => node.span, |
| 56 | + EnvVar(node) => node.span, |
| 57 | + Misc(node) => node.span, |
| 58 | + String(node) => node.span, |
| 59 | + BreakableString(node) => node.span, |
| 60 | + StringArray(node) => node.span, |
| 61 | + Comment(node) => node.span, |
| 62 | + } |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +pub struct Comment { |
| 67 | + pub span: Span, |
| 68 | + pub text: String, |
| 69 | +} |
| 70 | + |
| 71 | +pub fn parse_comments(text: &str, offset: usize) -> Vec<Comment> { |
| 72 | + let mut comments = Vec::new(); |
| 73 | + let mut char_iterator = text.char_indices(); |
| 74 | + let mut in_start_comment_context = true; |
| 75 | + |
| 76 | + while let Some((i, c)) = char_iterator.next() { |
| 77 | + // leading whitespace is supported but discouraged |
| 78 | + if in_start_comment_context && c.is_whitespace() { |
| 79 | + continue; |
| 80 | + } |
| 81 | + |
| 82 | + if in_start_comment_context && matches!(c, '#') { |
| 83 | + let start_index = i; |
| 84 | + let mut end_index = i; |
| 85 | + while let Some((i, c)) = char_iterator.next() { |
| 86 | + if c == '\n' { |
| 87 | + break; |
| 88 | + } |
| 89 | + end_index = i + c.len_utf8(); |
| 90 | + } |
| 91 | + comments.push(Comment { |
| 92 | + span: Span::new(offset + start_index, offset + end_index), |
| 93 | + text: text[start_index + 1..end_index].to_string(), |
| 94 | + }); |
| 95 | + in_start_comment_context = true; |
| 96 | + } else { |
| 97 | + in_start_comment_context = false; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + comments |
| 102 | +} |
| 103 | + |
| 104 | +impl<'a> From<&'a Instruction> for Node<'a> { |
| 105 | + fn from(instruction: &'a Instruction) -> Node<'a> { |
| 106 | + use Instruction::*; |
| 107 | + match instruction { |
| 108 | + From(node) => node.into(), |
| 109 | + Arg(node) => node.into(), |
| 110 | + Label(node) => node.into(), |
| 111 | + Run(node) => node.into(), |
| 112 | + Entrypoint(node) => node.into(), |
| 113 | + Cmd(node) => node.into(), |
| 114 | + Copy(node) => node.into(), |
| 115 | + Env(node) => node.into(), |
| 116 | + Misc(node) => node.into(), |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments