0001-Port-src-parsers.rs-to-nom-7.patch 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. From bcf3d534e38d6afaca9e898cef1af7fa3e0ecdb3 Mon Sep 17 00:00:00 2001
  2. From: Maxime Devos <maximedevos@telenet.be>
  3. Date: Sat, 21 May 2022 11:31:16 +0000
  4. Subject: [PATCH] Port src/parsers.rs to nom 7
  5. TODO:
  6. * [ ] run tests (the build system I use (not Cargo) hasn't
  7. implemented running tests yet)
  8. * [ ] probably not done idiomatically
  9. * [ ] how to resolve expr_res
  10. ---
  11. Cargo.toml | 2 +-
  12. src/parsers.rs | 479 ++++++++++++++++++++++---------------------------
  13. 2 files changed, 213 insertions(+), 268 deletions(-)
  14. diff --git a/Cargo.toml b/Cargo.toml
  15. index 3afe4c2..f02795e 100644
  16. --- a/Cargo.toml
  17. +++ b/Cargo.toml
  18. @@ -13,7 +13,7 @@ edition = "2018"
  19. heapless = "0.5.6"
  20. [dependencies.nom]
  21. -version = "4.2.3"
  22. +version = "^7."
  23. default-features = false
  24. [features]
  25. diff --git a/src/parsers.rs b/src/parsers.rs
  26. index b0bf613..66651db 100644
  27. --- a/src/parsers.rs
  28. +++ b/src/parsers.rs
  29. @@ -5,235 +5,177 @@ use crate::AnsiSequence;
  30. use core::convert::TryInto;
  31. use heapless::Vec;
  32. +use nom::branch::alt;
  33. +use nom::bytes::streaming::tag;
  34. +use nom::character::streaming::{digit0, digit1};
  35. +use nom::combinator::{map_res, opt};
  36. +use nom::error::ErrorKind;
  37. use nom::*;
  38. +// TODO: copied from rust-nom@4.2.3 -- what should it be changed to
  39. +// when using combinators instead of macros?
  40. +macro_rules! expr_res (
  41. + ($i:expr, $e:expr) => (
  42. + {
  43. + match $e {
  44. + Ok(output) => Ok(($i, output)),
  45. + // TODO: what should be the error for a failed Vec::from_slice?
  46. + // ErrorKind::ExprRes has been removed in rust-nom@7.0.0
  47. + Err(_) => Err(Err::Error(error_position!($i, ErrorKind::TooLarge)))
  48. + }
  49. + }
  50. + )
  51. +);
  52. +
  53. macro_rules! tag_parser {
  54. ($sig:ident, $tag:expr, $ret:expr) => {
  55. - named!(
  56. - $sig<&str, AnsiSequence>,
  57. - do_parse!(
  58. - tag!($tag) >>
  59. - ($ret)
  60. - )
  61. - );
  62. - }
  63. + fn $sig(input: &str) -> IResult<&str, AnsiSequence> {
  64. + let (input, _) = tag($tag)(input)?;
  65. + Ok((input, $ret))
  66. + }
  67. + };
  68. }
  69. -named!(
  70. - parse_int<&str, u32>,
  71. - map_res!(
  72. - nom::digit,
  73. - |s: &str| s.parse::<u32>()
  74. - )
  75. -);
  76. +fn parse_int(input: &str) -> IResult<&str, u32> {
  77. + combinator::map_res(digit1, |s: &str| s.parse::<u32>())(input)
  78. +}
  79. // TODO kind of ugly, would prefer to pass in the default so we could use it for
  80. // all escapes with defaults (not just those that default to 1).
  81. -named!(
  82. - parse_def_cursor_int<&str, u32>,
  83. - map!(
  84. - nom::digit0,
  85. - |s: &str| s.parse::<u32>().unwrap_or(1)
  86. - )
  87. -);
  88. +fn parse_def_cursor_int(input: &str) -> IResult<&str, u32> {
  89. + combinator::map(digit0, |s: &str| s.parse::<u32>().unwrap_or(1))(input)
  90. +}
  91. -named!(
  92. - cursor_pos<&str, AnsiSequence>,
  93. - do_parse!(
  94. - tag!("[") >>
  95. - x: parse_def_cursor_int >>
  96. - opt!(tag!(";")) >>
  97. - y: parse_def_cursor_int >>
  98. - alt!(
  99. - tag!("H") |
  100. - tag!("f")
  101. - ) >>
  102. - (AnsiSequence::CursorPos(x, y))
  103. - )
  104. -);
  105. +fn cursor_pos(input: &str) -> IResult<&str, AnsiSequence> {
  106. + let (input, _) = tag("[")(input)?;
  107. + let (input, x) = parse_def_cursor_int(input)?;
  108. + let (input, _) = opt(tag(";"))(input)?;
  109. + let (input, y) = parse_def_cursor_int(input)?;
  110. + let (input, _) = alt((tag("H"), tag("f")))(input)?;
  111. + Ok((input, AnsiSequence::CursorPos(x, y)))
  112. +}
  113. -named!(
  114. - escape<&str, AnsiSequence>,
  115. - do_parse!(
  116. - tag!("\u{1b}") >>
  117. - (AnsiSequence::Escape)
  118. - )
  119. -);
  120. +fn escape(input: &str) -> IResult<&str, AnsiSequence> {
  121. + let (input, _) = tag("\u{1b}")(input)?;
  122. + Ok((input, AnsiSequence::Escape))
  123. +}
  124. -named!(
  125. - cursor_up<&str, AnsiSequence>,
  126. - do_parse!(
  127. - tag!("[") >>
  128. - am: parse_def_cursor_int >>
  129. - tag!("A") >>
  130. - (AnsiSequence::CursorUp(am))
  131. - )
  132. -);
  133. +fn cursor_up(input: &str) -> IResult<&str, AnsiSequence> {
  134. + let (input, _) = tag("[")(input)?;
  135. + let (input, am) = parse_def_cursor_int(input)?;
  136. + Ok((input, AnsiSequence::CursorUp(am)))
  137. +}
  138. -named!(
  139. - cursor_down<&str, AnsiSequence>,
  140. - do_parse!(
  141. - tag!("[") >>
  142. - am: parse_def_cursor_int >>
  143. - tag!("B") >>
  144. - (AnsiSequence::CursorDown(am))
  145. - )
  146. -);
  147. +fn cursor_down(input: &str) -> IResult<&str, AnsiSequence> {
  148. + let (input, _) = tag("[")(input)?;
  149. + let (input, am) = parse_def_cursor_int(input)?;
  150. + let (input, _) = tag("B")(input)?;
  151. + Ok((input, AnsiSequence::CursorDown(am)))
  152. +}
  153. -named!(
  154. - cursor_forward<&str, AnsiSequence>,
  155. - do_parse!(
  156. - tag!("[") >>
  157. - am: parse_def_cursor_int >>
  158. - tag!("C") >>
  159. - (AnsiSequence::CursorForward(am))
  160. - )
  161. -);
  162. +fn cursor_forward(input: &str) -> IResult<&str, AnsiSequence> {
  163. + let (input, _) = tag("[")(input)?;
  164. + let (input, am) = parse_def_cursor_int(input)?;
  165. + let (input, _) = tag("C")(input)?;
  166. + Ok((input, AnsiSequence::CursorForward(am)))
  167. +}
  168. -named!(
  169. - cursor_backward<&str, AnsiSequence>,
  170. - do_parse!(
  171. - tag!("[") >>
  172. - am: parse_def_cursor_int >>
  173. - tag!("D") >>
  174. - (AnsiSequence::CursorBackward(am))
  175. - )
  176. -);
  177. +fn cursor_backward(input: &str) -> IResult<&str, AnsiSequence> {
  178. + let (input, _) = tag("[")(input)?;
  179. + let (input, am) = parse_def_cursor_int(input)?;
  180. + let (input, _) = tag("D")(input)?;
  181. + Ok((input, AnsiSequence::CursorBackward(am)))
  182. +}
  183. -named!(
  184. - graphics_mode1<&str, AnsiSequence>,
  185. - do_parse!(
  186. - tag!("[") >>
  187. - val: parse_int >>
  188. - tag!("m") >>
  189. - val: expr_res!(val.try_into()) >>
  190. - conv: expr_res!(Vec::from_slice(&[val])) >>
  191. - (AnsiSequence::SetGraphicsMode(conv))
  192. - )
  193. -);
  194. +fn graphics_mode1(input: &str) -> IResult<&str, AnsiSequence> {
  195. + let (input, _) = tag("[")(input)?;
  196. + let (input, val) = parse_int(input)?;
  197. + let (input, _) = tag("m")(input)?;
  198. + let (input, val) = expr_res!(input, val.try_into())?;
  199. + let (input, conv) = expr_res!(input, Vec::from_slice(&[val]))?;
  200. + Ok((input, AnsiSequence::SetGraphicsMode(conv)))
  201. +}
  202. -named!(
  203. - graphics_mode2<&str, AnsiSequence>,
  204. - do_parse!(
  205. - tag!("[") >>
  206. - val1: parse_int >>
  207. - tag!(";") >>
  208. - val2: parse_int >>
  209. - tag!("m") >>
  210. - val1: expr_res!(val1.try_into()) >>
  211. - val2: expr_res!(val2.try_into()) >>
  212. - conv: expr_res!(Vec::from_slice(&[
  213. - val1,
  214. - val2,
  215. - ])) >>
  216. - (AnsiSequence::SetGraphicsMode(conv))
  217. - )
  218. -);
  219. +fn graphics_mode2(input: &str) -> IResult<&str, AnsiSequence> {
  220. + let (input, _) = tag("[")(input)?;
  221. + let (input, val1) = parse_int(input)?;
  222. + let (input, _) = tag(";")(input)?;
  223. + let (input, val2) = parse_int(input)?;
  224. + let (input, _) = tag("m")(input)?;
  225. + let (input, val1) = expr_res!(input, val1.try_into())?;
  226. + let (input, val2) = expr_res!(input, val2.try_into())?;
  227. + let (input, conv) = expr_res!(input, Vec::from_slice(&[val1, val2,]))?;
  228. + Ok((input, AnsiSequence::SetGraphicsMode(conv)))
  229. +}
  230. -named!(
  231. - graphics_mode3<&str, AnsiSequence>,
  232. - do_parse!(
  233. - tag!("[") >>
  234. - val1: parse_int >>
  235. - tag!(";") >>
  236. - val2: parse_int >>
  237. - tag!(";") >>
  238. - val3: parse_int >>
  239. - tag!("m") >>
  240. - val1: expr_res!(val1.try_into()) >>
  241. - val2: expr_res!(val2.try_into()) >>
  242. - val3: expr_res!(val3.try_into()) >>
  243. - conv: expr_res!(Vec::from_slice(&[
  244. - val1,
  245. - val2,
  246. - val3,
  247. - ])) >>
  248. - (AnsiSequence::SetGraphicsMode(conv))
  249. - )
  250. -);
  251. +fn graphics_mode3(input: &str) -> IResult<&str, AnsiSequence> {
  252. + let (input, _) = tag("[")(input)?;
  253. + let (input, val1) = parse_int(input)?;
  254. + let (input, _) = tag(";")(input)?;
  255. + let (input, val2) = parse_int(input)?;
  256. + let (input, _) = tag(";")(input)?;
  257. + let (input, val3) = parse_int(input)?;
  258. + let (input, _) = tag("m")(input)?;
  259. + let (input, val1) = expr_res!(input, val1.try_into())?;
  260. + let (input, val2) = expr_res!(input, val2.try_into())?;
  261. + let (input, val3) = expr_res!(input, val3.try_into())?;
  262. + let (input, conv) = expr_res!(input, Vec::from_slice(&[val1, val2, val3,]))?;
  263. + Ok((input, AnsiSequence::SetGraphicsMode(conv)))
  264. +}
  265. -named!(
  266. - graphics_mode4<&str, AnsiSequence>,
  267. - do_parse!(
  268. - tag!("[m") >>
  269. - (AnsiSequence::SetGraphicsMode(Vec::new()))
  270. - )
  271. -);
  272. +fn graphics_mode4(input: &str) -> IResult<&str, AnsiSequence> {
  273. + let (input, _) = tag("[m")(input)?;
  274. + Ok((input, AnsiSequence::SetGraphicsMode(Vec::new())))
  275. +}
  276. -named!(
  277. - graphics_mode5<&str, AnsiSequence>,
  278. - do_parse!(
  279. - tag!("[") >>
  280. - val1: parse_int >>
  281. - tag!(";") >>
  282. - val2: parse_int >>
  283. - tag!(";") >>
  284. - val3: parse_int >>
  285. - tag!(";") >>
  286. - val4: parse_int >>
  287. - tag!(";") >>
  288. - val5: parse_int >>
  289. - tag!("m") >>
  290. - val1: expr_res!(val1.try_into()) >>
  291. - val2: expr_res!(val2.try_into()) >>
  292. - val3: expr_res!(val3.try_into()) >>
  293. - val4: expr_res!(val4.try_into()) >>
  294. - val5: expr_res!(val5.try_into()) >>
  295. - conv: expr_res!(Vec::from_slice(&[
  296. - val1,
  297. - val2,
  298. - val3,
  299. - val4,
  300. - val5,
  301. - ])) >>
  302. - (AnsiSequence::SetGraphicsMode(conv))
  303. - )
  304. -);
  305. +fn graphics_mode5(input: &str) -> IResult<&str, AnsiSequence> {
  306. + let (input, _) = tag("[")(input)?;
  307. + let (input, val1) = map_res(parse_int, |s| s.try_into())(input)?;
  308. + let (input, _) = tag(";")(input)?;
  309. + let (input, val2) = map_res(parse_int, |s| s.try_into())(input)?;
  310. + let (input, _) = tag(";")(input)?;
  311. + let (input, val3) = map_res(parse_int, |s| s.try_into())(input)?;
  312. + let (input, _) = tag(";")(input)?;
  313. + let (input, val4) = map_res(parse_int, |s| s.try_into())(input)?;
  314. + let (input, _) = tag(";")(input)?;
  315. + let (input, val5) = map_res(parse_int, |s| s.try_into())(input)?;
  316. + let (input, _) = tag("m")(input)?;
  317. + let (input, conv) = expr_res!(input, Vec::from_slice(&[val1, val2, val3, val4, val5,]))?;
  318. + Ok((input, AnsiSequence::SetGraphicsMode(conv)))
  319. +}
  320. -named!(
  321. - graphics_mode<&str, AnsiSequence>,
  322. - alt!(
  323. - graphics_mode1
  324. - | graphics_mode2
  325. - | graphics_mode3
  326. - | graphics_mode4
  327. - | graphics_mode5
  328. - )
  329. -);
  330. +fn graphics_mode(input: &str) -> IResult<&str, AnsiSequence> {
  331. + alt((
  332. + graphics_mode1,
  333. + graphics_mode2,
  334. + graphics_mode3,
  335. + graphics_mode4,
  336. + graphics_mode5,
  337. + ))(input)
  338. +}
  339. -named!(
  340. - set_mode<&str, AnsiSequence>,
  341. - do_parse!(
  342. - tag!("[=") >>
  343. - mode: parse_int >>
  344. - conv: expr_res!(mode.try_into()) >>
  345. - tag!("h") >>
  346. - (AnsiSequence::SetMode(conv))
  347. - )
  348. -);
  349. +fn set_mode(input: &str) -> IResult<&str, AnsiSequence> {
  350. + let (input, _) = tag("[=")(input)?;
  351. + let (input, conv) = map_res(parse_int, |s| s.try_into())(input)?;
  352. + let (input, _) = tag("h")(input)?;
  353. + Ok((input, AnsiSequence::SetMode(conv)))
  354. +}
  355. -named!(
  356. - reset_mode<&str, AnsiSequence>,
  357. - do_parse!(
  358. - tag!("[=") >>
  359. - mode: parse_int >>
  360. - conv: expr_res!(mode.try_into()) >>
  361. - tag!("l") >>
  362. - (AnsiSequence::ResetMode(conv))
  363. - )
  364. -);
  365. +fn reset_mode(input: &str) -> IResult<&str, AnsiSequence> {
  366. + let (input, _) = tag("[=")(input)?;
  367. + let (input, conv) = map_res(parse_int, |s| s.try_into())(input)?;
  368. + let (input, _) = tag("l")(input)?;
  369. + Ok((input, AnsiSequence::ResetMode(conv)))
  370. +}
  371. -named!(
  372. - set_top_and_bottom<&str, AnsiSequence>,
  373. - do_parse!(
  374. - tag!("[") >>
  375. - x: parse_int >>
  376. - tag!(";") >>
  377. - y: parse_int >>
  378. - tag!("r") >>
  379. - (AnsiSequence::SetTopAndBottom(x, y))
  380. - )
  381. -);
  382. +fn set_top_and_bottom(input: &str) -> IResult<&str, AnsiSequence> {
  383. + let (input, _) = tag("[")(input)?;
  384. + let (input, x) = parse_int(input)?;
  385. + let (input, _) = tag(";")(input)?;
  386. + let (input, y) = parse_int(input)?;
  387. + let (input, _) = tag("r")(input)?;
  388. + Ok((input, AnsiSequence::SetTopAndBottom(x, y)))
  389. +}
  390. tag_parser!(cursor_save, "[s", AnsiSequence::CursorSave);
  391. tag_parser!(cursor_restore, "[u", AnsiSequence::CursorRestore);
  392. @@ -276,66 +218,69 @@ tag_parser!(set_g1_graph, ")2", AnsiSequence::SetG1AltAndSpecialGraph);
  393. tag_parser!(set_single_shift2, "N", AnsiSequence::SetSingleShift2);
  394. tag_parser!(set_single_shift3, "O", AnsiSequence::SetSingleShift3);
  395. -named!(
  396. - combined<&str, AnsiSequence>,
  397. - alt!(
  398. - escape
  399. - | cursor_pos
  400. - | cursor_up
  401. - | cursor_down
  402. - | cursor_forward
  403. - | cursor_backward
  404. - | cursor_save
  405. - | cursor_restore
  406. - | erase_display
  407. - | erase_line
  408. - | graphics_mode
  409. - | set_mode
  410. - | reset_mode
  411. - | hide_cursor
  412. - | show_cursor
  413. - | cursor_to_app
  414. - | set_new_line_mode
  415. - | set_col_132
  416. - | set_smooth_scroll
  417. - | set_reverse_video
  418. - | set_origin_rel
  419. - | set_auto_wrap
  420. - | set_auto_repeat
  421. - | set_interlacing
  422. - | set_linefeed
  423. - | set_cursorkey
  424. - | set_vt52
  425. - | set_col80
  426. - | set_jump_scroll
  427. - | set_normal_video
  428. - | set_origin_abs
  429. - | reset_auto_wrap
  430. - | reset_auto_repeat
  431. - | reset_interlacing
  432. - | set_top_and_bottom
  433. - | set_alternate_keypad
  434. - | set_numeric_keypad
  435. - | set_uk_g0
  436. - | set_uk_g1
  437. - | set_us_g0
  438. - | set_us_g1
  439. - | set_g0_special
  440. - | set_g1_special
  441. - | set_g0_alternate
  442. - | set_g1_alternate
  443. - | set_g0_graph
  444. - | set_g1_graph
  445. - | set_single_shift2
  446. - | set_single_shift3
  447. - )
  448. -);
  449. +fn combined(input: &str) -> IResult<&str, AnsiSequence> {
  450. + // alt supports only 21 cases, so avoid the limit by nesting them
  451. + alt((
  452. + alt((
  453. + escape,
  454. + cursor_pos,
  455. + cursor_up,
  456. + cursor_down,
  457. + cursor_forward,
  458. + cursor_backward,
  459. + cursor_save,
  460. + cursor_restore,
  461. + erase_display,
  462. + erase_line,
  463. + graphics_mode,
  464. + set_mode,
  465. + reset_mode,
  466. + hide_cursor,
  467. + show_cursor,
  468. + cursor_to_app,
  469. + )),
  470. + alt((
  471. + set_new_line_mode,
  472. + set_col_132,
  473. + set_smooth_scroll,
  474. + set_reverse_video,
  475. + set_origin_rel,
  476. + set_auto_wrap,
  477. + set_auto_repeat,
  478. + set_interlacing,
  479. + set_linefeed,
  480. + set_cursorkey,
  481. + set_vt52,
  482. + set_col80,
  483. + set_jump_scroll,
  484. + set_normal_video,
  485. + set_origin_abs,
  486. + )),
  487. + alt((
  488. + reset_auto_wrap,
  489. + reset_auto_repeat,
  490. + reset_interlacing,
  491. + set_top_and_bottom,
  492. + set_alternate_keypad,
  493. + set_numeric_keypad,
  494. + set_uk_g0,
  495. + set_uk_g1,
  496. + set_us_g0,
  497. + set_us_g1,
  498. + set_g0_special,
  499. + set_g1_special,
  500. + set_g0_alternate,
  501. + set_g1_alternate,
  502. + set_g0_graph,
  503. + set_g1_graph,
  504. + set_single_shift2,
  505. + set_single_shift3,
  506. + )),
  507. + ))(input)
  508. +}
  509. -named!(
  510. - pub parse_escape<&str, AnsiSequence>,
  511. - do_parse!(
  512. - tag!("\u{1b}") >>
  513. - seq: combined >>
  514. - (seq)
  515. - )
  516. -);
  517. +pub fn parse_escape(input: &str) -> IResult<&str, AnsiSequence> {
  518. + let (input, _) = tag("\u{1b}")(input)?;
  519. + let (input, seq) = combined(input)?;
  520. + Ok((input, seq))
  521. +}
  522. base-commit: 6ed05bfb18ce3c5382393388c259e9c0e95d4a9d
  523. --
  524. 2.30.2