Parser.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  1. /*
  2. * Copyright (c) 2021, Matthew Olsson <mattco@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/ScopeGuard.h>
  7. #include <AK/TypeCasts.h>
  8. #include <LibPDF/CommonNames.h>
  9. #include <LibPDF/Document.h>
  10. #include <LibPDF/Filter.h>
  11. #include <LibPDF/Parser.h>
  12. #include <LibTextCodec/Decoder.h>
  13. #include <ctype.h>
  14. #include <math.h>
  15. namespace PDF {
  16. template<typename T, typename... Args>
  17. static NonnullRefPtr<T> make_object(Args... args) requires(IsBaseOf<Object, T>)
  18. {
  19. return adopt_ref(*new T(forward<Args>(args)...));
  20. }
  21. Vector<Command> Parser::parse_graphics_commands(const ReadonlyBytes& bytes)
  22. {
  23. auto parser = adopt_ref(*new Parser(bytes));
  24. return parser->parse_graphics_commands();
  25. }
  26. Parser::Parser(Badge<Document>, const ReadonlyBytes& bytes)
  27. : m_reader(bytes)
  28. {
  29. }
  30. Parser::Parser(const ReadonlyBytes& bytes)
  31. : m_reader(bytes)
  32. {
  33. }
  34. bool Parser::initialize()
  35. {
  36. if (!parse_header())
  37. return {};
  38. m_reader.move_to(m_reader.bytes().size() - 1);
  39. if (!navigate_to_before_eof_marker())
  40. return false;
  41. if (!navigate_to_after_startxref())
  42. return false;
  43. if (m_reader.done())
  44. return false;
  45. m_reader.set_reading_forwards();
  46. auto xref_offset_value = parse_number();
  47. if (!xref_offset_value.is_int())
  48. return false;
  49. auto xref_offset = xref_offset_value.as_int();
  50. m_reader.move_to(xref_offset);
  51. auto xref_table = parse_xref_table();
  52. if (!xref_table.has_value())
  53. return false;
  54. auto trailer = parse_file_trailer();
  55. if (!trailer)
  56. return false;
  57. m_xref_table = xref_table.value();
  58. m_trailer = trailer;
  59. return true;
  60. }
  61. Value Parser::parse_object_with_index(u32 index)
  62. {
  63. VERIFY(m_xref_table.has_object(index));
  64. auto byte_offset = m_xref_table.byte_offset_for_object(index);
  65. m_reader.move_to(byte_offset);
  66. auto indirect_value = parse_indirect_value();
  67. VERIFY(indirect_value);
  68. VERIFY(indirect_value->index() == index);
  69. return indirect_value->value();
  70. }
  71. bool Parser::parse_header()
  72. {
  73. // FIXME: Do something with the version?
  74. m_reader.set_reading_forwards();
  75. m_reader.move_to(0);
  76. if (m_reader.remaining() < 8 || !m_reader.matches("%PDF-"))
  77. return false;
  78. m_reader.move_by(5);
  79. char major_ver = m_reader.read();
  80. if (major_ver != '1' && major_ver != '2')
  81. return false;
  82. if (m_reader.read() != '.')
  83. return false;
  84. char minor_ver = m_reader.read();
  85. if (minor_ver < '0' || major_ver > '7')
  86. return false;
  87. consume_eol();
  88. // Parse optional high-byte comment, which signifies a binary file
  89. // FIXME: Do something with this?
  90. auto comment = parse_comment();
  91. if (!comment.is_empty()) {
  92. auto binary = comment.length() >= 4;
  93. if (binary) {
  94. for (size_t i = 0; i < comment.length() && binary; i++)
  95. binary = static_cast<u8>(comment[i]) > 128;
  96. }
  97. }
  98. return true;
  99. }
  100. Optional<XRefTable> Parser::parse_xref_table()
  101. {
  102. if (!m_reader.matches("xref"))
  103. return {};
  104. m_reader.move_by(4);
  105. if (!consume_eol())
  106. return {};
  107. XRefTable table;
  108. while (true) {
  109. if (m_reader.matches("trailer"))
  110. break;
  111. Vector<XRefEntry> entries;
  112. auto starting_index_value = parse_number();
  113. auto starting_index = starting_index_value.as_int();
  114. auto object_count_value = parse_number();
  115. auto object_count = object_count_value.as_int();
  116. for (int i = 0; i < object_count; i++) {
  117. auto offset_string = String(m_reader.bytes().slice(m_reader.offset(), 10));
  118. m_reader.move_by(10);
  119. if (!consume(' '))
  120. return {};
  121. auto generation_string = String(m_reader.bytes().slice(m_reader.offset(), 5));
  122. m_reader.move_by(5);
  123. if (!consume(' '))
  124. return {};
  125. auto letter = m_reader.read();
  126. if (letter != 'n' && letter != 'f')
  127. return {};
  128. // The line ending sequence can be one of the following:
  129. // SP CR, SP LF, or CR LF
  130. if (m_reader.matches(' ')) {
  131. consume();
  132. auto ch = consume();
  133. if (ch != '\r' && ch != '\n')
  134. return {};
  135. } else {
  136. if (!m_reader.matches("\r\n"))
  137. return {};
  138. m_reader.move_by(2);
  139. }
  140. auto offset = strtol(offset_string.characters(), nullptr, 10);
  141. auto generation = strtol(generation_string.characters(), nullptr, 10);
  142. entries.append({ offset, static_cast<u16>(generation), letter == 'n' });
  143. }
  144. table.add_section({ starting_index, object_count, entries });
  145. }
  146. return table;
  147. }
  148. RefPtr<DictObject> Parser::parse_file_trailer()
  149. {
  150. if (!m_reader.matches("trailer"))
  151. return {};
  152. m_reader.move_by(7);
  153. consume_whitespace();
  154. auto dict = parse_dict();
  155. if (!dict)
  156. return {};
  157. if (!m_reader.matches("startxref"))
  158. return {};
  159. m_reader.move_by(9);
  160. consume_whitespace();
  161. m_reader.move_until([&](auto) { return matches_eol(); });
  162. VERIFY(consume_eol());
  163. if (!m_reader.matches("%%EOF"))
  164. return {};
  165. m_reader.move_by(5);
  166. consume_whitespace();
  167. return dict;
  168. }
  169. bool Parser::navigate_to_before_eof_marker()
  170. {
  171. m_reader.set_reading_backwards();
  172. while (!m_reader.done()) {
  173. m_reader.move_until([&](auto) { return matches_eol(); });
  174. if (m_reader.done())
  175. return false;
  176. consume_eol();
  177. if (!m_reader.matches("%%EOF"))
  178. continue;
  179. m_reader.move_by(5);
  180. if (!matches_eol())
  181. continue;
  182. consume_eol();
  183. return true;
  184. }
  185. return false;
  186. }
  187. bool Parser::navigate_to_after_startxref()
  188. {
  189. m_reader.set_reading_backwards();
  190. while (!m_reader.done()) {
  191. m_reader.move_until([&](auto) { return matches_eol(); });
  192. auto offset = m_reader.offset() + 1;
  193. consume_eol();
  194. if (!m_reader.matches("startxref"))
  195. continue;
  196. m_reader.move_by(9);
  197. if (!matches_eol())
  198. continue;
  199. m_reader.move_to(offset);
  200. return true;
  201. }
  202. return false;
  203. }
  204. bool Parser::sloppy_is_linearized()
  205. {
  206. ScopeGuard guard([&] {
  207. m_reader.move_to(0);
  208. m_reader.set_reading_forwards();
  209. });
  210. auto limit = min(1024ul, m_reader.bytes().size() - 1);
  211. m_reader.move_to(limit);
  212. m_reader.set_reading_backwards();
  213. while (!m_reader.done()) {
  214. m_reader.move_until('/');
  215. if (m_reader.matches("/Linearized"))
  216. return true;
  217. m_reader.move_by(1);
  218. }
  219. return false;
  220. }
  221. String Parser::parse_comment()
  222. {
  223. if (!m_reader.matches('%'))
  224. return {};
  225. consume();
  226. auto comment_start_offset = m_reader.offset();
  227. m_reader.move_until([&](auto) {
  228. return matches_eol();
  229. });
  230. String str = StringView(m_reader.bytes().slice(comment_start_offset, m_reader.offset() - comment_start_offset));
  231. consume_eol();
  232. consume_whitespace();
  233. return str;
  234. }
  235. Value Parser::parse_value()
  236. {
  237. parse_comment();
  238. if (m_reader.matches("null")) {
  239. m_reader.move_by(4);
  240. consume_whitespace();
  241. return Value(Value::NullTag {});
  242. }
  243. if (m_reader.matches("true")) {
  244. m_reader.move_by(4);
  245. consume_whitespace();
  246. return Value(true);
  247. }
  248. if (m_reader.matches("false")) {
  249. m_reader.move_by(5);
  250. consume_whitespace();
  251. return Value(false);
  252. }
  253. if (matches_number())
  254. return parse_possible_indirect_value_or_ref();
  255. if (m_reader.matches('/'))
  256. return parse_name();
  257. if (m_reader.matches("<<")) {
  258. auto dict = parse_dict();
  259. if (!dict)
  260. return {};
  261. if (m_reader.matches("stream"))
  262. return parse_stream(dict.release_nonnull());
  263. return dict;
  264. }
  265. if (m_reader.matches_any('(', '<'))
  266. return parse_string();
  267. if (m_reader.matches('['))
  268. return parse_array();
  269. dbgln("tried to parse value, but found char {} ({}) at offset {}", m_reader.peek(), static_cast<u8>(m_reader.peek()), m_reader.offset());
  270. VERIFY_NOT_REACHED();
  271. }
  272. Value Parser::parse_possible_indirect_value_or_ref()
  273. {
  274. auto first_number = parse_number();
  275. if (!first_number.is_int() || !matches_number())
  276. return first_number;
  277. m_reader.save();
  278. auto second_number = parse_number();
  279. if (!second_number.is_int()) {
  280. m_reader.load();
  281. return first_number;
  282. }
  283. if (m_reader.matches('R')) {
  284. m_reader.discard();
  285. consume();
  286. consume_whitespace();
  287. return Value(first_number.as_int(), second_number.as_int());
  288. }
  289. if (m_reader.matches("obj")) {
  290. m_reader.discard();
  291. return parse_indirect_value(first_number.as_int(), second_number.as_int());
  292. }
  293. m_reader.load();
  294. return first_number;
  295. }
  296. RefPtr<IndirectValue> Parser::parse_indirect_value(int index, int generation)
  297. {
  298. if (!m_reader.matches("obj"))
  299. return {};
  300. m_reader.move_by(3);
  301. if (matches_eol())
  302. consume_eol();
  303. auto value = parse_value();
  304. if (!m_reader.matches("endobj"))
  305. return {};
  306. consume(6);
  307. consume_whitespace();
  308. return make_object<IndirectValue>(index, generation, value);
  309. }
  310. RefPtr<IndirectValue> Parser::parse_indirect_value()
  311. {
  312. auto first_number = parse_number();
  313. if (!first_number.is_int())
  314. return {};
  315. auto second_number = parse_number();
  316. if (!second_number.is_int())
  317. return {};
  318. return parse_indirect_value(first_number.as_int(), second_number.as_int());
  319. }
  320. Value Parser::parse_number()
  321. {
  322. size_t start_offset = m_reader.offset();
  323. bool is_float = false;
  324. if (m_reader.matches('+') || m_reader.matches('-'))
  325. consume();
  326. while (!m_reader.done()) {
  327. if (m_reader.matches('.')) {
  328. if (is_float)
  329. break;
  330. is_float = true;
  331. consume();
  332. } else if (isdigit(m_reader.peek())) {
  333. consume();
  334. } else {
  335. break;
  336. }
  337. }
  338. consume_whitespace();
  339. auto string = String(m_reader.bytes().slice(start_offset, m_reader.offset() - start_offset));
  340. float f = strtof(string.characters(), nullptr);
  341. if (is_float)
  342. return Value(f);
  343. VERIFY(floorf(f) == f);
  344. return Value(static_cast<int>(f));
  345. }
  346. RefPtr<NameObject> Parser::parse_name()
  347. {
  348. if (!consume('/'))
  349. return {};
  350. StringBuilder builder;
  351. while (true) {
  352. if (!matches_regular_character())
  353. break;
  354. if (m_reader.matches('#')) {
  355. int hex_value = 0;
  356. for (int i = 0; i < 2; i++) {
  357. auto ch = consume();
  358. if (!isxdigit(ch))
  359. return {};
  360. hex_value *= 16;
  361. if (ch <= '9') {
  362. hex_value += ch - '0';
  363. } else {
  364. hex_value += ch - 'A' + 10;
  365. }
  366. }
  367. builder.append(static_cast<char>(hex_value));
  368. continue;
  369. }
  370. builder.append(consume());
  371. }
  372. consume_whitespace();
  373. return make_object<NameObject>(builder.to_string());
  374. }
  375. RefPtr<StringObject> Parser::parse_string()
  376. {
  377. ScopeGuard guard([&] { consume_whitespace(); });
  378. String string;
  379. bool is_binary_string;
  380. if (m_reader.matches('(')) {
  381. string = parse_literal_string();
  382. is_binary_string = false;
  383. } else {
  384. string = parse_hex_string();
  385. is_binary_string = true;
  386. }
  387. if (string.is_null())
  388. return {};
  389. if (string.bytes().starts_with(Array<u8, 2> { 0xfe, 0xff })) {
  390. // The string is encoded in UTF16-BE
  391. string = TextCodec::decoder_for("utf-16be")->to_utf8(string.substring(2));
  392. } else if (string.bytes().starts_with(Array<u8, 3> { 239, 187, 191 })) {
  393. // The string is encoded in UTF-8. This is the default anyways, but if these bytes
  394. // are explicitly included, we have to trim them
  395. string = string.substring(3);
  396. }
  397. return make_object<StringObject>(string, is_binary_string);
  398. }
  399. String Parser::parse_literal_string()
  400. {
  401. if (!consume('('))
  402. return {};
  403. StringBuilder builder;
  404. auto opened_parens = 0;
  405. while (true) {
  406. if (m_reader.matches('(')) {
  407. opened_parens++;
  408. builder.append(consume());
  409. } else if (m_reader.matches(')')) {
  410. consume();
  411. if (opened_parens == 0)
  412. break;
  413. opened_parens--;
  414. builder.append(')');
  415. } else if (m_reader.matches('\\')) {
  416. consume();
  417. if (matches_eol()) {
  418. consume_eol();
  419. continue;
  420. }
  421. if (m_reader.done())
  422. return {};
  423. auto ch = consume();
  424. switch (ch) {
  425. case 'n':
  426. builder.append('\n');
  427. break;
  428. case 'r':
  429. builder.append('\r');
  430. break;
  431. case 't':
  432. builder.append('\t');
  433. break;
  434. case 'b':
  435. builder.append('\b');
  436. break;
  437. case 'f':
  438. builder.append('\f');
  439. break;
  440. case '(':
  441. builder.append('(');
  442. break;
  443. case ')':
  444. builder.append(')');
  445. break;
  446. case '\\':
  447. builder.append('\\');
  448. break;
  449. default: {
  450. if (ch >= '0' && ch <= '7') {
  451. int octal_value = ch - '0';
  452. for (int i = 0; i < 2; i++) {
  453. auto octal_ch = consume();
  454. if (octal_ch < '0' || octal_ch > '7')
  455. break;
  456. octal_value = octal_value * 8 + (octal_ch - '0');
  457. }
  458. builder.append(static_cast<char>(octal_value));
  459. } else {
  460. builder.append(ch);
  461. }
  462. }
  463. }
  464. } else if (matches_eol()) {
  465. consume_eol();
  466. builder.append('\n');
  467. } else {
  468. builder.append(consume());
  469. }
  470. }
  471. if (opened_parens != 0)
  472. return {};
  473. return builder.to_string();
  474. }
  475. String Parser::parse_hex_string()
  476. {
  477. if (!consume('<'))
  478. return {};
  479. StringBuilder builder;
  480. while (true) {
  481. if (m_reader.matches('>')) {
  482. consume();
  483. return builder.to_string();
  484. } else {
  485. int hex_value = 0;
  486. for (int i = 0; i < 2; i++) {
  487. auto ch = consume();
  488. if (ch == '>') {
  489. // The hex string contains an odd number of characters, and the last character
  490. // is assumed to be '0'
  491. consume();
  492. hex_value *= 16;
  493. builder.append(static_cast<char>(hex_value));
  494. return builder.to_string();
  495. }
  496. if (!isxdigit(ch))
  497. return {};
  498. hex_value *= 16;
  499. if (ch <= '9') {
  500. hex_value += ch - '0';
  501. } else {
  502. hex_value += ch - 'A' + 10;
  503. }
  504. }
  505. builder.append(static_cast<char>(hex_value));
  506. }
  507. }
  508. }
  509. RefPtr<ArrayObject> Parser::parse_array()
  510. {
  511. if (!consume('['))
  512. return {};
  513. consume_whitespace();
  514. Vector<Value> values;
  515. while (!m_reader.matches(']')) {
  516. auto value = parse_value();
  517. if (!value)
  518. return {};
  519. values.append(value);
  520. }
  521. if (!consume(']'))
  522. return {};
  523. consume_whitespace();
  524. return make_object<ArrayObject>(values);
  525. }
  526. RefPtr<DictObject> Parser::parse_dict()
  527. {
  528. if (!consume('<') || !consume('<'))
  529. return {};
  530. consume_whitespace();
  531. HashMap<FlyString, Value> map;
  532. while (true) {
  533. if (m_reader.matches(">>"))
  534. break;
  535. auto name = parse_name();
  536. if (!name)
  537. return {};
  538. auto value = parse_value();
  539. if (!value)
  540. return {};
  541. map.set(name->name(), value);
  542. }
  543. if (!consume('>') || !consume('>'))
  544. return {};
  545. consume_whitespace();
  546. return make_object<DictObject>(map);
  547. }
  548. RefPtr<DictObject> Parser::conditionally_parse_page_tree_node(u32 object_index, bool& ok)
  549. {
  550. ok = true;
  551. VERIFY(m_xref_table.has_object(object_index));
  552. auto byte_offset = m_xref_table.byte_offset_for_object(object_index);
  553. m_reader.move_to(byte_offset);
  554. parse_number();
  555. parse_number();
  556. if (!m_reader.matches("obj")) {
  557. ok = false;
  558. return {};
  559. }
  560. m_reader.move_by(3);
  561. consume_whitespace();
  562. if (!consume('<') || !consume('<'))
  563. return {};
  564. consume_whitespace();
  565. HashMap<FlyString, Value> map;
  566. while (true) {
  567. if (m_reader.matches(">>"))
  568. break;
  569. auto name = parse_name();
  570. if (!name) {
  571. ok = false;
  572. return {};
  573. }
  574. auto name_string = name->name();
  575. if (!name_string.is_one_of(CommonNames::Type, CommonNames::Parent, CommonNames::Kids, CommonNames::Count)) {
  576. // This is a page, not a page tree node
  577. return {};
  578. }
  579. auto value = parse_value();
  580. if (!value) {
  581. ok = false;
  582. return {};
  583. }
  584. if (name_string == CommonNames::Type) {
  585. if (!value.is_object())
  586. return {};
  587. auto type_object = value.as_object();
  588. if (!type_object->is_name())
  589. return {};
  590. auto type_name = object_cast<NameObject>(type_object);
  591. if (type_name->name() != CommonNames::Pages)
  592. return {};
  593. }
  594. map.set(name->name(), value);
  595. }
  596. if (!consume('>') || !consume('>'))
  597. return {};
  598. consume_whitespace();
  599. return make_object<DictObject>(map);
  600. }
  601. RefPtr<StreamObject> Parser::parse_stream(NonnullRefPtr<DictObject> dict)
  602. {
  603. if (!m_reader.matches("stream"))
  604. return {};
  605. m_reader.move_by(6);
  606. if (!consume_eol())
  607. return {};
  608. ReadonlyBytes bytes;
  609. auto maybe_length = dict->get(CommonNames::Length);
  610. if (maybe_length.has_value()) {
  611. // The PDF writer has kindly provided us with the direct length of the stream
  612. m_reader.save();
  613. auto length = m_document->resolve_to<int>(maybe_length.value());
  614. m_reader.load();
  615. bytes = m_reader.bytes().slice(m_reader.offset(), length);
  616. m_reader.move_by(length);
  617. consume_whitespace();
  618. } else {
  619. // We have to look for the endstream keyword
  620. auto stream_start = m_reader.offset();
  621. while (true) {
  622. m_reader.move_until([&](auto) { return matches_eol(); });
  623. auto potential_stream_end = m_reader.offset();
  624. consume_eol();
  625. if (!m_reader.matches("endstream"))
  626. continue;
  627. bytes = m_reader.bytes().slice(stream_start, potential_stream_end - stream_start);
  628. break;
  629. }
  630. }
  631. m_reader.move_by(9);
  632. consume_whitespace();
  633. if (dict->contains(CommonNames::Filter)) {
  634. auto filter_type = dict->get_name(m_document, CommonNames::Filter)->name();
  635. auto maybe_bytes = Filter::decode(bytes, filter_type);
  636. if (!maybe_bytes.has_value())
  637. return {};
  638. return make_object<EncodedStreamObject>(dict, move(maybe_bytes.value()));
  639. }
  640. return make_object<PlainTextStreamObject>(dict, bytes);
  641. }
  642. Vector<Command> Parser::parse_graphics_commands()
  643. {
  644. Vector<Command> commands;
  645. Vector<Value> command_args;
  646. constexpr static auto is_command_char = [](char ch) {
  647. return isalpha(ch) || ch == '*' || ch == '\'';
  648. };
  649. while (!m_reader.done()) {
  650. auto ch = m_reader.peek();
  651. if (is_command_char(ch)) {
  652. auto command_start = m_reader.offset();
  653. while (is_command_char(ch)) {
  654. consume();
  655. if (m_reader.done())
  656. break;
  657. ch = m_reader.peek();
  658. }
  659. auto command_string = StringView(m_reader.bytes().slice(command_start, m_reader.offset() - command_start));
  660. auto command_type = Command::command_type_from_symbol(command_string);
  661. commands.append(Command(command_type, move(command_args)));
  662. command_args = Vector<Value>();
  663. consume_whitespace();
  664. continue;
  665. }
  666. command_args.append(parse_value());
  667. }
  668. return commands;
  669. }
  670. bool Parser::matches_eol() const
  671. {
  672. return m_reader.matches_any(0xa, 0xd);
  673. }
  674. bool Parser::matches_whitespace() const
  675. {
  676. return matches_eol() || m_reader.matches_any(0, 0x9, 0xc, ' ');
  677. }
  678. bool Parser::matches_number() const
  679. {
  680. if (m_reader.done())
  681. return false;
  682. auto ch = m_reader.peek();
  683. return isdigit(ch) || ch == '-' || ch == '+';
  684. }
  685. bool Parser::matches_delimiter() const
  686. {
  687. return m_reader.matches_any('(', ')', '<', '>', '[', ']', '{', '}', '/', '%');
  688. }
  689. bool Parser::matches_regular_character() const
  690. {
  691. return !matches_delimiter() && !matches_whitespace();
  692. }
  693. bool Parser::consume_eol()
  694. {
  695. if (m_reader.matches("\r\n")) {
  696. consume(2);
  697. return true;
  698. }
  699. auto consumed = consume();
  700. return consumed == 0xd || consumed == 0xa;
  701. }
  702. bool Parser::consume_whitespace()
  703. {
  704. bool consumed = false;
  705. while (matches_whitespace()) {
  706. consumed = true;
  707. consume();
  708. }
  709. return consumed;
  710. }
  711. char Parser::consume()
  712. {
  713. return m_reader.read();
  714. }
  715. void Parser::consume(int amount)
  716. {
  717. for (size_t i = 0; i < static_cast<size_t>(amount); i++)
  718. consume();
  719. }
  720. bool Parser::consume(char ch)
  721. {
  722. return consume() == ch;
  723. }
  724. }