Parser.cpp 21 KB

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