| 1 | use proc_macro2::{Ident, Span}; |
| 2 | use syn::{spanned::Spanned, LitByteStr, LitStr}; |
| 3 | |
| 4 | use crate::error::{Errors, Result}; |
| 5 | use crate::leaf::Callback; |
| 6 | use crate::mir::Mir; |
| 7 | use crate::parser::nested::NestedValue; |
| 8 | use crate::parser::{IgnoreFlags, Parser, Subpatterns}; |
| 9 | |
| 10 | use super::ignore_flags::ascii_case::MakeAsciiCaseInsensitive; |
| 11 | |
| 12 | pub struct Definition { |
| 13 | pub literal: Literal, |
| 14 | pub priority: Option<usize>, |
| 15 | pub callback: Option<Callback>, |
| 16 | pub ignore_flags: IgnoreFlags, |
| 17 | } |
| 18 | |
| 19 | pub enum Literal { |
| 20 | Utf8(LitStr), |
| 21 | Bytes(LitByteStr), |
| 22 | } |
| 23 | |
| 24 | impl Definition { |
| 25 | pub fn new(literal: Literal) -> Self { |
| 26 | Definition { |
| 27 | literal, |
| 28 | priority: None, |
| 29 | callback: None, |
| 30 | ignore_flags: IgnoreFlags::Empty, |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | pub fn named_attr(&mut self, name: Ident, value: NestedValue, parser: &mut Parser) { |
| 35 | match (name.to_string().as_str(), value) { |
| 36 | ("priority", NestedValue::Assign(tokens)) => { |
| 37 | let prio = match tokens.to_string().parse() { |
| 38 | Ok(prio) => prio, |
| 39 | Err(_) => { |
| 40 | parser.err("Expected an unsigned integer", tokens.span()); |
| 41 | return; |
| 42 | } |
| 43 | }; |
| 44 | |
| 45 | if self.priority.replace(prio).is_some() { |
| 46 | parser.err("Resetting previously set priority", tokens.span()); |
| 47 | } |
| 48 | } |
| 49 | ("priority", _) => { |
| 50 | parser.err("Expected: priority = <integer>", name.span()); |
| 51 | } |
| 52 | ("callback", NestedValue::Assign(tokens)) => { |
| 53 | let span = tokens.span(); |
| 54 | let callback = match parser.parse_callback(tokens) { |
| 55 | Some(callback) => callback, |
| 56 | None => { |
| 57 | parser.err("Not a valid callback", span); |
| 58 | return; |
| 59 | } |
| 60 | }; |
| 61 | |
| 62 | if let Some(previous) = self.callback.replace(callback) { |
| 63 | parser |
| 64 | .err( |
| 65 | "Callback has been already set", |
| 66 | span.join(name.span()).unwrap(), |
| 67 | ) |
| 68 | .err("Previous callback set here", previous.span()); |
| 69 | } |
| 70 | } |
| 71 | ("callback", _) => { |
| 72 | parser.err("Expected: callback = ...", name.span()); |
| 73 | } |
| 74 | ("ignore", NestedValue::Group(tokens)) => { |
| 75 | self.ignore_flags.parse_group(name, tokens, parser); |
| 76 | } |
| 77 | ("ignore", _) => { |
| 78 | parser.err("Expected: ignore(<flag>, ...)", name.span()); |
| 79 | } |
| 80 | (unknown, _) => { |
| 81 | parser.err( |
| 82 | format!( |
| 83 | "\ |
| 84 | Unknown nested attribute: {}\n\ |
| 85 | \n\ |
| 86 | Expected one of: priority, callback\ |
| 87 | ", |
| 88 | unknown |
| 89 | ), |
| 90 | name.span(), |
| 91 | ); |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | impl Literal { |
| 98 | pub fn to_bytes(&self) -> Vec<u8> { |
| 99 | match self { |
| 100 | Literal::Utf8(string) => string.value().into_bytes(), |
| 101 | Literal::Bytes(bytes) => bytes.value(), |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | pub fn escape_regex(&self) -> Literal { |
| 106 | match self { |
| 107 | Literal::Utf8(string) => Literal::Utf8(LitStr::new( |
| 108 | regex_syntax::escape(&string.value()).as_str(), |
| 109 | self.span(), |
| 110 | )), |
| 111 | Literal::Bytes(bytes) => Literal::Bytes(LitByteStr::new( |
| 112 | regex_syntax::escape(&bytes_to_regex_string(bytes.value())).as_bytes(), |
| 113 | self.span(), |
| 114 | )), |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | pub fn to_mir( |
| 119 | &self, |
| 120 | subpatterns: &Subpatterns, |
| 121 | ignore_flags: IgnoreFlags, |
| 122 | errors: &mut Errors, |
| 123 | ) -> Result<Mir> { |
| 124 | let value = subpatterns.fix(self, errors); |
| 125 | |
| 126 | if ignore_flags.contains(IgnoreFlags::IgnoreAsciiCase) { |
| 127 | match self { |
| 128 | Literal::Utf8(_) => { |
| 129 | Mir::utf8(&value).map(MakeAsciiCaseInsensitive::make_ascii_case_insensitive) |
| 130 | } |
| 131 | Literal::Bytes(_) => Mir::binary_ignore_case(&value), |
| 132 | } |
| 133 | } else if ignore_flags.contains(IgnoreFlags::IgnoreCase) { |
| 134 | match self { |
| 135 | Literal::Utf8(_) => Mir::utf8_ignore_case(&value), |
| 136 | Literal::Bytes(_) => Mir::binary_ignore_case(&value), |
| 137 | } |
| 138 | } else { |
| 139 | match self { |
| 140 | Literal::Utf8(_) => Mir::utf8(&value), |
| 141 | Literal::Bytes(_) => Mir::binary(&value), |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | pub fn span(&self) -> Span { |
| 147 | match self { |
| 148 | Literal::Utf8(string) => string.span(), |
| 149 | Literal::Bytes(bytes) => bytes.span(), |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | impl syn::parse::Parse for Literal { |
| 155 | fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> { |
| 156 | let la = input.lookahead1(); |
| 157 | if la.peek(LitStr) { |
| 158 | Ok(Literal::Utf8(input.parse()?)) |
| 159 | } else if la.peek(LitByteStr) { |
| 160 | Ok(Literal::Bytes(input.parse()?)) |
| 161 | } else { |
| 162 | Err(la.error()) |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | pub fn bytes_to_regex_string(bytes: Vec<u8>) -> String { |
| 168 | if bytes.is_ascii() { |
| 169 | unsafe { |
| 170 | // Unicode values are prohibited, so we can't use |
| 171 | // safe version of String::from_utf8 |
| 172 | // |
| 173 | // We can, however, construct a safe ASCII string |
| 174 | return String::from_utf8_unchecked(bytes); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | let mut string = String::with_capacity(bytes.len() * 2); |
| 179 | |
| 180 | for byte in bytes { |
| 181 | if byte < 0x80 { |
| 182 | string.push(byte as char); |
| 183 | } else { |
| 184 | static DIGITS: [u8; 16] = *b"0123456789abcdef"; |
| 185 | |
| 186 | string.push_str(r"\x"); |
| 187 | string.push(DIGITS[(byte / 16) as usize] as char); |
| 188 | string.push(DIGITS[(byte % 16) as usize] as char); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | string |
| 193 | } |