Try to parse a raw directive string. Returns None if it doesn't start with `.`.
(raw: &str)
| 18 | impl Directive { |
| 19 | /// Try to parse a raw directive string. Returns None if it doesn't start with `.`. |
| 20 | pub fn parse(raw: &str) -> Option<Self> { |
| 21 | Self::parse_with_constants(raw, &HashMap::new()) |
| 22 | } |
| 23 | |
| 24 | pub(crate) fn parse_with_constants( |
| 25 | raw: &str, |
| 26 | constants: &HashMap<String, i64>, |
| 27 | ) -> Option<Self> { |
| 28 | let s = raw.trim(); |
| 29 | if !s.starts_with('.') { |
| 30 | return None; |
| 31 | } |
| 32 | |
| 33 | let (mnemonic, rest) = s |
| 34 | .splitn(2, |c: char| c.is_whitespace()) |
| 35 | .collect::<Vec<_>>() |
| 36 | .split_first() |
| 37 | .map(|(&m, r)| (m, r.first().copied().unwrap_or("").trim())) |
| 38 | .unwrap_or((s, "")); |
| 39 | |
| 40 | match mnemonic { |
| 41 | ".section" if !rest.is_empty() => Some(Self::Section(rest.to_owned())), |
| 42 | ".text" if rest.is_empty() => Some(Self::Section(".text".to_owned())), |
| 43 | ".data" if rest.is_empty() => Some(Self::Section(".data".to_owned())), |
| 44 | ".rodata" if rest.is_empty() => Some(Self::Section(".rodata".to_owned())), |
| 45 | ".bss" if rest.is_empty() => Some(Self::Section(".bss".to_owned())), |
| 46 | ".globl" | ".global" if !rest.is_empty() => Some(Self::Globl(rest.to_owned())), |
| 47 | ".align" => parse_u64(rest, constants).map(Self::Align), |
| 48 | ".balign" => parse_u64(rest, constants).map(Self::Balign), |
| 49 | ".byte" => parse_u64(rest, constants) |
| 50 | .and_then(|value| value.try_into().ok()) |
| 51 | .map(Self::Byte), |
| 52 | ".half" | ".short" => parse_u64(rest, constants) |
| 53 | .and_then(|value| value.try_into().ok()) |
| 54 | .map(Self::Half), |
| 55 | ".word" | ".long" => parse_u64(rest, constants) |
| 56 | .and_then(|value| value.try_into().ok()) |
| 57 | .map(Self::Word), |
| 58 | ".dword" | ".quad" => parse_u64(rest, constants).map(Self::Dword), |
| 59 | ".asciz" | ".string" => parse_quoted_string(rest).map(Self::Asciz), |
nothing calls this directly
no test coverage detected