parser.rs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. use crate::AmlError;
  2. use core::marker::PhantomData;
  3. use log::trace;
  4. pub type ParseResult<'a, R> = Result<(&'a [u8], R), (&'a [u8], AmlError)>;
  5. pub trait Parser<'a, R>: Sized {
  6. fn parse(&self, input: &'a [u8]) -> ParseResult<'a, R>;
  7. fn map<F, A>(self, map_fn: F) -> Map<'a, Self, F, R, A>
  8. where
  9. F: Fn(R) -> A,
  10. {
  11. Map { parser: self, map_fn, _phantom: PhantomData }
  12. }
  13. /// Try parsing with `self`. If it fails, try parsing with `other`, returning the result of the
  14. /// first of the two parsers to succeed. To `or` multiple parsers ergonomically, see the
  15. /// `choice!` macro.
  16. fn or<OtherParser>(self, other: OtherParser) -> Or<'a, Self, OtherParser, R>
  17. where
  18. OtherParser: Parser<'a, R>,
  19. {
  20. Or { p1: self, p2: other, _phantom: PhantomData }
  21. }
  22. }
  23. impl<'a, F, R> Parser<'a, R> for F
  24. where
  25. F: Fn(&'a [u8]) -> ParseResult<'a, R>,
  26. {
  27. fn parse(&self, input: &'a [u8]) -> ParseResult<'a, R> {
  28. self(input)
  29. }
  30. }
  31. pub fn take<'a>() -> impl Parser<'a, u8> {
  32. move |input: &'a [u8]| match input.first() {
  33. Some(&byte) => Ok((&input[1..], byte)),
  34. None => Err((input, AmlError::UnexpectedEndOfStream)),
  35. }
  36. }
  37. pub fn take_n<'a>(n: usize) -> impl Parser<'a, &'a [u8]> {
  38. move |input: &'a [u8]| {
  39. if input.len() < n {
  40. return Err((input, AmlError::UnexpectedEndOfStream));
  41. }
  42. let (result, new_input) = input.split_at(n);
  43. Ok((new_input, result))
  44. }
  45. }
  46. pub fn consume<'a, F>(condition: F) -> impl Parser<'a, u8>
  47. where
  48. F: Fn(u8) -> bool,
  49. {
  50. move |input: &'a [u8]| match input.first() {
  51. Some(&byte) if condition(byte) => Ok((&input[1..], byte)),
  52. Some(&byte) => Err((input, AmlError::UnexpectedByte(byte))),
  53. None => Err((input, AmlError::UnexpectedEndOfStream)),
  54. }
  55. }
  56. pub fn pair<'a, P1, P2, R1, R2>(a: P1, b: P2) -> impl Parser<'a, (R1, R2)>
  57. where
  58. P1: Parser<'a, R1>,
  59. P2: Parser<'a, R2>,
  60. {
  61. move |input| {
  62. a.parse(input).and_then(|(next_input, result_a)| {
  63. b.parse(next_input).map(|(final_input, result_b)| (final_input, (result_a, result_b)))
  64. })
  65. }
  66. }
  67. // TODO: can we make this formattable with stuff from the parse result?
  68. pub fn comment<'a, P, R>(parser: P, comment: &'static str) -> impl Parser<'a, R>
  69. where
  70. P: Parser<'a, R>,
  71. {
  72. move |input| {
  73. trace!("{}", comment);
  74. parser.parse(input)
  75. }
  76. }
  77. pub struct Or<'a, P1, P2, R>
  78. where
  79. P1: Parser<'a, R>,
  80. P2: Parser<'a, R>,
  81. {
  82. p1: P1,
  83. p2: P2,
  84. _phantom: PhantomData<&'a R>,
  85. }
  86. impl<'a, P1, P2, R> Parser<'a, R> for Or<'a, P1, P2, R>
  87. where
  88. P1: Parser<'a, R>,
  89. P2: Parser<'a, R>,
  90. {
  91. fn parse(&self, input: &'a [u8]) -> ParseResult<'a, R> {
  92. match self.p1.parse(input) {
  93. Ok(result) => return Ok(result),
  94. Err(_) => (),
  95. }
  96. self.p2.parse(input)
  97. }
  98. }
  99. pub struct Map<'a, P, F, R, A>
  100. where
  101. P: Parser<'a, R>,
  102. F: Fn(R) -> A,
  103. {
  104. parser: P,
  105. map_fn: F,
  106. _phantom: PhantomData<&'a (R, A)>,
  107. }
  108. impl<'a, P, F, R, A> Parser<'a, A> for Map<'a, P, F, R, A>
  109. where
  110. P: Parser<'a, R>,
  111. F: Fn(R) -> A,
  112. {
  113. fn parse(&self, input: &'a [u8]) -> ParseResult<'a, A> {
  114. self.parser.parse(input).map(|(new_input, result)| (new_input, (self.map_fn)(result)))
  115. }
  116. }
  117. /// Takes a number of parsers, and tries to apply each one to the input in order. Returns the
  118. /// result of the first one that succeeds, or fails if all of them fail.
  119. pub macro choice {
  120. ($first_parser: expr) => {
  121. $first_parser
  122. },
  123. ($first_parser: expr, $($other_parser: expr),*) => {
  124. $first_parser
  125. $(
  126. .or($other_parser)
  127. )*
  128. }
  129. }
  130. #[cfg(test)]
  131. mod tests {
  132. use super::*;
  133. use crate::test_utils::*;
  134. #[test]
  135. fn test_take_n() {
  136. check_err!(take_n(1).parse(&[]), AmlError::UnexpectedEndOfStream, &[]);
  137. check_err!(take_n(2).parse(&[0xf5]), AmlError::UnexpectedEndOfStream, &[0xf5]);
  138. check_ok!(take_n(1).parse(&[0xff]), &[0xff], &[]);
  139. check_ok!(take_n(1).parse(&[0xff, 0xf8]), &[0xff], &[0xf8]);
  140. check_ok!(take_n(2).parse(&[0xff, 0xf8]), &[0xff, 0xf8], &[]);
  141. }
  142. }