ISO8601.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  1. /*
  2. * Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/CharacterTypes.h>
  7. #include <LibJS/Runtime/Temporal/ISO8601.h>
  8. namespace JS::Temporal {
  9. namespace Detail {
  10. // https://tc39.es/proposal-temporal/#prod-DecimalDigit
  11. bool ISO8601Parser::parse_decimal_digit()
  12. {
  13. // DecimalDigit : one of
  14. // 0 1 2 3 4 5 6 7 8 9
  15. if (m_state.lexer.next_is(is_ascii_digit)) {
  16. m_state.lexer.consume();
  17. return true;
  18. }
  19. return false;
  20. }
  21. // https://tc39.es/proposal-temporal/#prod-NonZeroDigit
  22. bool ISO8601Parser::parse_non_zero_digit()
  23. {
  24. // NonZeroDigit : one of
  25. // 1 2 3 4 5 6 7 8 9
  26. if (m_state.lexer.next_is(is_ascii_digit) && !m_state.lexer.next_is('0')) {
  27. m_state.lexer.consume();
  28. return true;
  29. }
  30. return false;
  31. }
  32. // https://tc39.es/proposal-temporal/#prod-ASCIISign
  33. bool ISO8601Parser::parse_ascii_sign()
  34. {
  35. // ASCIISign : one of
  36. // + -
  37. return m_state.lexer.consume_specific('+')
  38. || m_state.lexer.consume_specific('-');
  39. }
  40. // https://tc39.es/proposal-temporal/#prod-Sign
  41. bool ISO8601Parser::parse_sign()
  42. {
  43. // Sign :
  44. // ASCIISign
  45. // U+2212
  46. StateTransaction transaction { *this };
  47. auto success = parse_ascii_sign()
  48. || m_state.lexer.consume_specific("\xE2\x88\x92"sv);
  49. if (!success)
  50. return false;
  51. m_state.parse_result.sign = transaction.parsed_string_view();
  52. transaction.commit();
  53. return true;
  54. }
  55. // https://tc39.es/proposal-temporal/#prod-Hour
  56. bool ISO8601Parser::parse_hour()
  57. {
  58. // Hour :
  59. // 0 DecimalDigit
  60. // 1 DecimalDigit
  61. // 20
  62. // 21
  63. // 22
  64. // 23
  65. StateTransaction transaction { *this };
  66. if (m_state.lexer.consume_specific('0') || m_state.lexer.consume_specific('1')) {
  67. if (!parse_decimal_digit())
  68. return false;
  69. } else {
  70. auto success = m_state.lexer.consume_specific("20"sv)
  71. || m_state.lexer.consume_specific("21"sv)
  72. || m_state.lexer.consume_specific("22"sv)
  73. || m_state.lexer.consume_specific("23"sv);
  74. if (!success)
  75. return false;
  76. }
  77. transaction.commit();
  78. return true;
  79. }
  80. // https://tc39.es/proposal-temporal/#prod-MinuteSecond
  81. bool ISO8601Parser::parse_minute_second()
  82. {
  83. // MinuteSecond :
  84. // 0 DecimalDigit
  85. // 1 DecimalDigit
  86. // 2 DecimalDigit
  87. // 3 DecimalDigit
  88. // 4 DecimalDigit
  89. // 5 DecimalDigit
  90. StateTransaction transaction { *this };
  91. auto success = m_state.lexer.consume_specific('0')
  92. || m_state.lexer.consume_specific('1')
  93. || m_state.lexer.consume_specific('2')
  94. || m_state.lexer.consume_specific('3')
  95. || m_state.lexer.consume_specific('4')
  96. || m_state.lexer.consume_specific('5');
  97. if (!success)
  98. return false;
  99. if (!parse_decimal_digit())
  100. return false;
  101. transaction.commit();
  102. return true;
  103. }
  104. // https://tc39.es/proposal-temporal/#prod-DecimalSeparator
  105. bool ISO8601Parser::parse_decimal_separator()
  106. {
  107. // DecimalSeparator : one of
  108. // . ,
  109. return m_state.lexer.consume_specific('.')
  110. || m_state.lexer.consume_specific(',');
  111. }
  112. // https://tc39.es/proposal-temporal/#prod-DateTimeSeparator
  113. bool ISO8601Parser::parse_date_time_separator()
  114. {
  115. // DateTimeSeparator :
  116. // <SP>
  117. // T
  118. // t
  119. return m_state.lexer.consume_specific(' ')
  120. || m_state.lexer.consume_specific('T')
  121. || m_state.lexer.consume_specific('t');
  122. }
  123. // https://tc39.es/proposal-temporal/#prod-UTCDesignator
  124. bool ISO8601Parser::parse_utc_designator()
  125. {
  126. // UTCDesignator : one of
  127. // Z z
  128. StateTransaction transaction { *this };
  129. auto success = m_state.lexer.consume_specific('Z')
  130. || m_state.lexer.consume_specific('z');
  131. if (!success)
  132. return false;
  133. m_state.parse_result.utc_designator = transaction.parsed_string_view();
  134. transaction.commit();
  135. return true;
  136. }
  137. // https://tc39.es/proposal-temporal/#prod-DateYear
  138. bool ISO8601Parser::parse_date_year()
  139. {
  140. // DateFourDigitYear :
  141. // DecimalDigit DecimalDigit DecimalDigit DecimalDigit
  142. // DateExtendedYear :
  143. // Sign DecimalDigit DecimalDigit DecimalDigit DecimalDigit DecimalDigit DecimalDigit
  144. // DateYear :
  145. // DateFourDigitYear
  146. // DateExtendedYear
  147. StateTransaction transaction { *this };
  148. if (parse_sign()) {
  149. for (size_t i = 0; i < 6; ++i) {
  150. if (!parse_decimal_digit())
  151. return false;
  152. }
  153. } else {
  154. for (size_t i = 0; i < 4; ++i) {
  155. if (!parse_decimal_digit())
  156. return false;
  157. }
  158. }
  159. m_state.parse_result.date_year = transaction.parsed_string_view();
  160. transaction.commit();
  161. return true;
  162. }
  163. // https://tc39.es/proposal-temporal/#prod-DateMonth
  164. bool ISO8601Parser::parse_date_month()
  165. {
  166. // DateMonth :
  167. // 0 NonZeroDigit
  168. // 10
  169. // 11
  170. // 12
  171. StateTransaction transaction { *this };
  172. if (m_state.lexer.consume_specific('0')) {
  173. if (!parse_non_zero_digit())
  174. return false;
  175. } else {
  176. auto success = m_state.lexer.consume_specific("10"sv)
  177. || m_state.lexer.consume_specific("11"sv)
  178. || m_state.lexer.consume_specific("12"sv);
  179. if (!success)
  180. return false;
  181. }
  182. m_state.parse_result.date_month = transaction.parsed_string_view();
  183. transaction.commit();
  184. return true;
  185. }
  186. // https://tc39.es/proposal-temporal/#prod-DateDay
  187. bool ISO8601Parser::parse_date_day()
  188. {
  189. // DateDay :
  190. // 0 NonZeroDigit
  191. // 1 DecimalDigit
  192. // 2 DecimalDigit
  193. // 30
  194. // 31
  195. StateTransaction transaction { *this };
  196. if (m_state.lexer.consume_specific('0')) {
  197. if (!parse_non_zero_digit())
  198. return false;
  199. } else if (m_state.lexer.consume_specific('1') || m_state.lexer.consume_specific('2')) {
  200. if (!parse_decimal_digit())
  201. return false;
  202. } else {
  203. auto success = m_state.lexer.consume_specific("30"sv)
  204. || m_state.lexer.consume_specific("31"sv);
  205. if (!success)
  206. return false;
  207. }
  208. m_state.parse_result.date_day = transaction.parsed_string_view();
  209. transaction.commit();
  210. return true;
  211. }
  212. // https://tc39.es/proposal-temporal/#prod-DateSpecYearMonth
  213. bool ISO8601Parser::parse_date_spec_year_month()
  214. {
  215. // DateSpecYearMonth :
  216. // DateYear -[opt] DateMonth
  217. StateTransaction transaction { *this };
  218. if (!parse_date_year())
  219. return false;
  220. m_state.lexer.consume_specific('-');
  221. if (!parse_date_month())
  222. return false;
  223. transaction.commit();
  224. return true;
  225. }
  226. // https://tc39.es/proposal-temporal/#prod-DateSpecMonthDay
  227. bool ISO8601Parser::parse_date_spec_month_day()
  228. {
  229. // TwoDashes :
  230. // --
  231. // DateSpecMonthDay :
  232. // TwoDashes[opt] DateMonth -[opt] DateDay
  233. StateTransaction transaction { *this };
  234. m_state.lexer.consume_specific("--"sv);
  235. if (!parse_date_month())
  236. return false;
  237. m_state.lexer.consume_specific('-');
  238. if (!parse_date_day())
  239. return false;
  240. transaction.commit();
  241. return true;
  242. }
  243. // https://tc39.es/proposal-temporal/#prod-Date
  244. bool ISO8601Parser::parse_date()
  245. {
  246. // Date :
  247. // DateYear - DateMonth - DateDay
  248. // DateYear DateMonth DateDay
  249. StateTransaction transaction { *this };
  250. if (!parse_date_year())
  251. return false;
  252. auto with_dashes = m_state.lexer.consume_specific('-');
  253. if (!parse_date_month())
  254. return false;
  255. if (with_dashes && !m_state.lexer.consume_specific('-'))
  256. return false;
  257. if (!parse_date_day())
  258. return false;
  259. transaction.commit();
  260. return true;
  261. }
  262. // https://tc39.es/proposal-temporal/#prod-TimeHour
  263. bool ISO8601Parser::parse_time_hour()
  264. {
  265. // TimeHour :
  266. // Hour
  267. StateTransaction transaction { *this };
  268. if (!parse_hour())
  269. return false;
  270. m_state.parse_result.time_hour = transaction.parsed_string_view();
  271. transaction.commit();
  272. return true;
  273. }
  274. // https://tc39.es/proposal-temporal/#prod-TimeMinute
  275. bool ISO8601Parser::parse_time_minute()
  276. {
  277. // TimeMinute :
  278. // MinuteSecond
  279. StateTransaction transaction { *this };
  280. if (!parse_minute_second())
  281. return false;
  282. m_state.parse_result.time_minute = transaction.parsed_string_view();
  283. transaction.commit();
  284. return true;
  285. }
  286. // https://tc39.es/proposal-temporal/#prod-TimeSecond
  287. bool ISO8601Parser::parse_time_second()
  288. {
  289. // TimeSecond :
  290. // MinuteSecond
  291. // 60
  292. StateTransaction transaction { *this };
  293. auto success = parse_minute_second()
  294. || m_state.lexer.consume_specific("60"sv);
  295. if (!success)
  296. return false;
  297. m_state.parse_result.time_second = transaction.parsed_string_view();
  298. transaction.commit();
  299. return true;
  300. }
  301. // https://tc39.es/proposal-temporal/#prod-FractionalPart
  302. bool ISO8601Parser::parse_fractional_part()
  303. {
  304. // FractionalPart :
  305. // DecimalDigit DecimalDigit[opt] DecimalDigit[opt] DecimalDigit[opt] DecimalDigit[opt] DecimalDigit[opt] DecimalDigit[opt] DecimalDigit[opt] DecimalDigit[opt]
  306. if (!parse_decimal_digit())
  307. return false;
  308. for (size_t i = 0; i < 8; ++i) {
  309. if (!parse_decimal_digit())
  310. break;
  311. }
  312. return true;
  313. }
  314. // https://tc39.es/proposal-temporal/#prod-TimeFractionalPart
  315. bool ISO8601Parser::parse_time_fractional_part()
  316. {
  317. // TimeFractionalPart :
  318. // FractionalPart
  319. StateTransaction transaction { *this };
  320. if (!parse_fractional_part())
  321. return false;
  322. m_state.parse_result.time_fractional_part = transaction.parsed_string_view();
  323. transaction.commit();
  324. return true;
  325. }
  326. // https://tc39.es/proposal-temporal/#prod-Fraction
  327. bool ISO8601Parser::parse_fraction()
  328. {
  329. // Fraction :
  330. // DecimalSeparator TimeFractionalPart
  331. StateTransaction transaction { *this };
  332. if (!parse_decimal_separator())
  333. return false;
  334. if (!parse_time_fractional_part())
  335. return false;
  336. transaction.commit();
  337. return true;
  338. }
  339. // https://tc39.es/proposal-temporal/#prod-TimeFraction
  340. bool ISO8601Parser::parse_time_fraction()
  341. {
  342. // TimeFraction :
  343. // Fraction
  344. return parse_fraction();
  345. }
  346. // https://tc39.es/proposal-temporal/#prod-TimeZoneUTCOffsetSign
  347. bool ISO8601Parser::parse_time_zone_utc_offset_sign()
  348. {
  349. // TimeZoneUTCOffsetSign :
  350. // Sign
  351. StateTransaction transaction { *this };
  352. if (!parse_sign())
  353. return false;
  354. m_state.parse_result.time_zone_utc_offset_sign = transaction.parsed_string_view();
  355. transaction.commit();
  356. return true;
  357. }
  358. // https://tc39.es/proposal-temporal/#prod-TimeZoneUTCOffsetHour
  359. bool ISO8601Parser::parse_time_zone_utc_offset_hour()
  360. {
  361. // TimeZoneUTCOffsetHour :
  362. // Hour
  363. StateTransaction transaction { *this };
  364. if (!parse_hour())
  365. return false;
  366. m_state.parse_result.time_zone_utc_offset_hour = transaction.parsed_string_view();
  367. transaction.commit();
  368. return true;
  369. }
  370. // https://tc39.es/proposal-temporal/#prod-TimeZoneUTCOffsetMinute
  371. bool ISO8601Parser::parse_time_zone_utc_offset_minute()
  372. {
  373. // TimeZoneUTCOffsetMinute :
  374. // MinuteSecond
  375. StateTransaction transaction { *this };
  376. if (!parse_minute_second())
  377. return false;
  378. m_state.parse_result.time_zone_utc_offset_minute = transaction.parsed_string_view();
  379. transaction.commit();
  380. return true;
  381. }
  382. // https://tc39.es/proposal-temporal/#prod-TimeZoneUTCOffsetSecond
  383. bool ISO8601Parser::parse_time_zone_utc_offset_second()
  384. {
  385. // TimeZoneUTCOffsetSecond :
  386. // MinuteSecond
  387. StateTransaction transaction { *this };
  388. if (!parse_minute_second())
  389. return false;
  390. m_state.parse_result.time_zone_utc_offset_second = transaction.parsed_string_view();
  391. transaction.commit();
  392. return true;
  393. }
  394. // https://tc39.es/proposal-temporal/#prod-TimeZoneUTCOffsetFractionalPart
  395. bool ISO8601Parser::parse_time_zone_utc_offset_fractional_part()
  396. {
  397. // TimeZoneUTCOffsetFractionalPart :
  398. // FractionalPart
  399. StateTransaction transaction { *this };
  400. if (!parse_fractional_part())
  401. return false;
  402. m_state.parse_result.time_zone_utc_offset_fractional_part = transaction.parsed_string_view();
  403. transaction.commit();
  404. return true;
  405. }
  406. // https://tc39.es/proposal-temporal/#prod-TimeZoneUTCOffsetFraction
  407. bool ISO8601Parser::parse_time_zone_utc_offset_fraction()
  408. {
  409. // TimeZoneUTCOffsetFraction :
  410. // DecimalSeparator TimeZoneUTCOffsetFractionalPart
  411. StateTransaction transaction { *this };
  412. if (!parse_decimal_separator())
  413. return false;
  414. if (!parse_time_zone_utc_offset_fractional_part())
  415. return false;
  416. transaction.commit();
  417. return true;
  418. }
  419. // https://tc39.es/proposal-temporal/#prod-TimeZoneNumericUTCOffset
  420. bool ISO8601Parser::parse_time_zone_numeric_utc_offset()
  421. {
  422. // TimeZoneNumericUTCOffset :
  423. // TimeZoneUTCOffsetSign TimeZoneUTCOffsetHour
  424. // TimeZoneUTCOffsetSign TimeZoneUTCOffsetHour : TimeZoneUTCOffsetMinute
  425. // TimeZoneUTCOffsetSign TimeZoneUTCOffsetHour TimeZoneUTCOffsetMinute
  426. // TimeZoneUTCOffsetSign TimeZoneUTCOffsetHour : TimeZoneUTCOffsetMinute : TimeZoneUTCOffsetSecond TimeZoneUTCOffsetFraction[opt]
  427. // TimeZoneUTCOffsetSign TimeZoneUTCOffsetHour TimeZoneUTCOffsetMinute TimeZoneUTCOffsetSecond TimeZoneUTCOffsetFraction[opt]
  428. StateTransaction transaction { *this };
  429. if (!parse_time_zone_utc_offset_sign())
  430. return false;
  431. if (!parse_time_zone_utc_offset_hour())
  432. return false;
  433. if (m_state.lexer.consume_specific(':')) {
  434. if (!parse_time_zone_utc_offset_minute())
  435. return false;
  436. if (m_state.lexer.consume_specific(':')) {
  437. if (!parse_time_zone_utc_offset_second())
  438. return false;
  439. (void)parse_time_zone_utc_offset_fraction();
  440. }
  441. } else if (parse_time_zone_utc_offset_minute()) {
  442. if (parse_time_zone_utc_offset_second())
  443. (void)parse_time_zone_utc_offset_fraction();
  444. }
  445. transaction.commit();
  446. return true;
  447. }
  448. // https://tc39.es/proposal-temporal/#prod-TimeZoneUTCOffset
  449. bool ISO8601Parser::parse_time_zone_utc_offset()
  450. {
  451. // TimeZoneUTCOffset :
  452. // TimeZoneNumericUTCOffset
  453. // UTCDesignator
  454. return parse_time_zone_numeric_utc_offset()
  455. || parse_utc_designator();
  456. }
  457. // https://tc39.es/proposal-temporal/#prod-TimeZoneUTCOffsetName
  458. bool ISO8601Parser::parse_time_zone_utc_offset_name()
  459. {
  460. // TimeZoneUTCOffsetName :
  461. // Sign Hour
  462. // Sign Hour : MinuteSecond
  463. // Sign Hour MinuteSecond
  464. // Sign Hour : MinuteSecond : MinuteSecond Fraction[opt]
  465. // Sign Hour MinuteSecond MinuteSecond Fraction[opt]
  466. StateTransaction transaction { *this };
  467. if (!parse_sign())
  468. return false;
  469. if (!parse_hour())
  470. return false;
  471. if (m_state.lexer.consume_specific(':')) {
  472. if (!parse_minute_second())
  473. return false;
  474. if (m_state.lexer.consume_specific(':')) {
  475. if (!parse_minute_second())
  476. return false;
  477. (void)parse_fraction();
  478. }
  479. } else if (parse_minute_second()) {
  480. if (parse_minute_second())
  481. (void)parse_fraction();
  482. }
  483. transaction.commit();
  484. return true;
  485. }
  486. // https://tc39.es/proposal-temporal/#prod-TimeZoneIANAName
  487. bool ISO8601Parser::parse_time_zone_iana_name()
  488. {
  489. // TZLeadingChar :
  490. // Alpha
  491. // .
  492. // _
  493. // TZChar :
  494. // Alpha
  495. // .
  496. // -
  497. // _
  498. // TimeZoneIANANameComponent :
  499. // TZLeadingChar TZChar[opt] TZChar[opt] TZChar[opt] TZChar[opt] TZChar[opt] TZChar[opt] TZChar[opt] TZChar[opt] TZChar[opt] TZChar[opt] TZChar[opt] TZChar[opt] TZChar[opt] but not one of . or ..
  500. // TimeZoneIANANameTail :
  501. // TimeZoneIANANameComponent
  502. // TimeZoneIANANameComponent / TimeZoneIANANameTail
  503. // TimeZoneIANAName :
  504. // TimeZoneIANANameTail
  505. StateTransaction transaction { *this };
  506. // TODO: Implement the full production. Currently, anything other than "UTC" would get rejected as unknown anyway.
  507. auto success = (m_state.lexer.consume_specific('U') || m_state.lexer.consume_specific('u'))
  508. && (m_state.lexer.consume_specific('T') || m_state.lexer.consume_specific('t'))
  509. && (m_state.lexer.consume_specific('C') || m_state.lexer.consume_specific('c'));
  510. if (!success)
  511. return false;
  512. m_state.parse_result.time_zone_iana_name = transaction.parsed_string_view();
  513. transaction.commit();
  514. return true;
  515. }
  516. // https://tc39.es/proposal-temporal/#prod-TimeZoneBracketedName
  517. bool ISO8601Parser::parse_time_zone_bracketed_name()
  518. {
  519. // TimeZoneBracketedName :
  520. // TimeZoneIANAName
  521. // Etc/GMT ASCIISign Hour
  522. // TimeZoneUTCOffsetName
  523. StateTransaction transaction { *this };
  524. if (parse_time_zone_iana_name()) {
  525. // no-op.
  526. } else if (m_state.lexer.consume_specific("Etc/GMT"sv)) {
  527. if (!parse_ascii_sign())
  528. return false;
  529. if (!parse_hour())
  530. return false;
  531. } else if (!parse_time_zone_utc_offset_name()) {
  532. return false;
  533. }
  534. transaction.commit();
  535. return true;
  536. }
  537. // https://tc39.es/proposal-temporal/#prod-TimeZoneBracketedAnnotation
  538. bool ISO8601Parser::parse_time_zone_bracketed_annotation()
  539. {
  540. // TimeZoneBracketedAnnotation :
  541. // [ TimeZoneBracketedName ]
  542. StateTransaction transaction { *this };
  543. if (!m_state.lexer.consume_specific('['))
  544. return false;
  545. if (!parse_time_zone_bracketed_name())
  546. return false;
  547. if (!m_state.lexer.consume_specific(']'))
  548. return false;
  549. transaction.commit();
  550. return true;
  551. }
  552. // https://tc39.es/proposal-temporal/#prod-TimeZoneOffsetRequired
  553. bool ISO8601Parser::parse_time_zone_offset_required()
  554. {
  555. // TimeZoneOffsetRequired :
  556. // TimeZoneUTCOffset TimeZoneBracketedAnnotation[opt]
  557. StateTransaction transaction { *this };
  558. if (!parse_time_zone_utc_offset())
  559. return false;
  560. (void)parse_time_zone_bracketed_annotation();
  561. transaction.commit();
  562. return true;
  563. }
  564. // https://tc39.es/proposal-temporal/#prod-TimeZoneNameRequired
  565. bool ISO8601Parser::parse_time_zone_name_required()
  566. {
  567. // TimeZoneNameRequired :
  568. // TimeZoneUTCOffset[opt] TimeZoneBracketedAnnotation
  569. StateTransaction transaction { *this };
  570. (void)parse_time_zone_utc_offset();
  571. if (!parse_time_zone_bracketed_annotation())
  572. return false;
  573. transaction.commit();
  574. return true;
  575. }
  576. // https://tc39.es/proposal-temporal/#prod-TimeZone
  577. bool ISO8601Parser::parse_time_zone()
  578. {
  579. // TimeZone :
  580. // TimeZoneOffsetRequired
  581. // TimeZoneNameRequired
  582. return parse_time_zone_offset_required()
  583. || parse_time_zone_name_required();
  584. }
  585. // https://tc39.es/proposal-temporal/#prod-CalendarName
  586. bool ISO8601Parser::parse_calendar_name()
  587. {
  588. // CalChar :
  589. // Alpha
  590. // DecimalDigit
  591. // CalendarNameComponent :
  592. // CalChar CalChar CalChar CalChar[opt] CalChar[opt] CalChar[opt] CalChar[opt] CalChar[opt]
  593. // CalendarNameTail :
  594. // CalendarNameComponent
  595. // CalendarNameComponent - CalendarNameTail
  596. // CalendarName :
  597. // CalendarNameTail
  598. auto parse_calendar_name_component = [&] {
  599. for (size_t i = 0; i < 8; ++i) {
  600. if (!m_state.lexer.next_is(is_ascii_alphanumeric))
  601. return i > 2;
  602. m_state.lexer.consume();
  603. }
  604. return true;
  605. };
  606. StateTransaction transaction { *this };
  607. do {
  608. if (!parse_calendar_name_component())
  609. return false;
  610. } while (m_state.lexer.consume_specific('-'));
  611. m_state.parse_result.calendar_name = transaction.parsed_string_view();
  612. transaction.commit();
  613. return true;
  614. }
  615. // https://tc39.es/proposal-temporal/#prod-Calendar
  616. bool ISO8601Parser::parse_calendar()
  617. {
  618. // Calendar :
  619. // [u-ca= CalendarName ]
  620. StateTransaction transaction { *this };
  621. if (!m_state.lexer.consume_specific("[u-ca="sv))
  622. return false;
  623. if (!parse_calendar_name())
  624. return false;
  625. if (!m_state.lexer.consume_specific(']'))
  626. return false;
  627. transaction.commit();
  628. return true;
  629. }
  630. // https://tc39.es/proposal-temporal/#prod-TimeSpec
  631. bool ISO8601Parser::parse_time_spec()
  632. {
  633. // TimeSpec :
  634. // TimeHour
  635. // TimeHour : TimeMinute
  636. // TimeHour TimeMinute
  637. // TimeHour : TimeMinute : TimeSecond TimeFraction[opt]
  638. // TimeHour TimeMinute TimeSecond TimeFraction[opt]
  639. StateTransaction transaction { *this };
  640. if (!parse_time_hour())
  641. return false;
  642. if (m_state.lexer.consume_specific(':')) {
  643. if (!parse_time_minute())
  644. return false;
  645. if (m_state.lexer.consume_specific(':')) {
  646. if (!parse_time_second())
  647. return false;
  648. (void)parse_time_fraction();
  649. }
  650. } else if (parse_time_minute()) {
  651. if (parse_time_second())
  652. (void)parse_time_fraction();
  653. }
  654. transaction.commit();
  655. return true;
  656. }
  657. // https://tc39.es/proposal-temporal/#prod-Time
  658. bool ISO8601Parser::parse_time()
  659. {
  660. // Time :
  661. // TimeSpec TimeZone[opt]
  662. if (!parse_time_spec())
  663. return false;
  664. (void)parse_time_zone();
  665. return true;
  666. }
  667. // https://tc39.es/proposal-temporal/#prod-TimeSpecSeparator
  668. bool ISO8601Parser::parse_time_spec_separator()
  669. {
  670. // TimeSpecSeparator :
  671. // DateTimeSeparator TimeSpec
  672. StateTransaction transaction { *this };
  673. if (!parse_date_time_separator())
  674. return false;
  675. if (!parse_time_spec())
  676. return false;
  677. transaction.commit();
  678. return true;
  679. }
  680. // https://tc39.es/proposal-temporal/#prod-DateTime
  681. bool ISO8601Parser::parse_date_time()
  682. {
  683. // DateTime :
  684. // Date TimeSpecSeparator[opt] TimeZone[opt]
  685. if (!parse_date())
  686. return false;
  687. (void)parse_time_spec_separator();
  688. (void)parse_time_zone();
  689. return true;
  690. }
  691. // https://tc39.es/proposal-temporal/#prod-CalendarDateTime
  692. bool ISO8601Parser::parse_calendar_date_time()
  693. {
  694. // CalendarDateTime :
  695. // DateTime Calendar[opt]
  696. if (!parse_date_time())
  697. return false;
  698. (void)parse_calendar();
  699. return true;
  700. }
  701. // https://tc39.es/proposal-temporal/#prod-TemporalInstantString
  702. bool ISO8601Parser::parse_temporal_instant_string()
  703. {
  704. // TemporalInstantString :
  705. // Date TimeZoneOffsetRequired
  706. // Date DateTimeSeparator TimeSpec TimeZoneOffsetRequired
  707. StateTransaction transaction { *this };
  708. if (!parse_date())
  709. return false;
  710. if (!parse_time_zone_offset_required()) {
  711. if (!parse_date_time_separator())
  712. return false;
  713. if (!parse_time_spec())
  714. return false;
  715. if (!parse_time_zone_offset_required())
  716. return false;
  717. }
  718. transaction.commit();
  719. return true;
  720. }
  721. // https://tc39.es/proposal-temporal/#prod-TemporalDateString
  722. bool ISO8601Parser::parse_temporal_date_string()
  723. {
  724. // TemporalDateString :
  725. // CalendarDateTime
  726. return parse_calendar_date_time();
  727. }
  728. // https://tc39.es/proposal-temporal/#prod-TemporalDateTimeString
  729. bool ISO8601Parser::parse_temporal_date_time_string()
  730. {
  731. // TemporalDateTimeString :
  732. // CalendarDateTime
  733. return parse_calendar_date_time();
  734. }
  735. // https://tc39.es/proposal-temporal/#prod-TemporalMonthDayString
  736. bool ISO8601Parser::parse_temporal_month_day_string()
  737. {
  738. // TemporalMonthDayString :
  739. // DateSpecMonthDay
  740. // DateTime
  741. // NOTE: Reverse order here because `DateSpecMonthDay` can be a subset of `DateTime`,
  742. // so we'd not attempt to parse that but may not exhaust the input string.
  743. return parse_date_time()
  744. || parse_date_spec_month_day();
  745. }
  746. // https://tc39.es/proposal-temporal/#prod-TemporalTimeString
  747. bool ISO8601Parser::parse_temporal_time_string()
  748. {
  749. // TemporalTimeString :
  750. // Time
  751. // DateTime
  752. // NOTE: Reverse order here because `Time` can be a subset of `DateTime`,
  753. // so we'd not attempt to parse that but may not exhaust the input string.
  754. return parse_date_time()
  755. || parse_time();
  756. }
  757. // https://tc39.es/proposal-temporal/#prod-TemporalTimeZoneIdentifier
  758. bool ISO8601Parser::parse_temporal_time_zone_identifier()
  759. {
  760. // TemporalTimeZoneIdentifier :
  761. // TimeZoneNumericUTCOffset
  762. // TimeZoneIANAName
  763. return parse_time_zone_numeric_utc_offset()
  764. || parse_time_zone_iana_name();
  765. }
  766. // https://tc39.es/proposal-temporal/#prod-TemporalTimeZoneString
  767. bool ISO8601Parser::parse_temporal_time_zone_string()
  768. {
  769. // TemporalTimeZoneString :
  770. // TemporalTimeZoneIdentifier
  771. // Date TimeSpecSeparator[opt] TimeZone Calendar[opt]
  772. StateTransaction transaction { *this };
  773. if (!parse_temporal_time_zone_identifier()) {
  774. if (!parse_date())
  775. return false;
  776. (void)parse_time_spec_separator();
  777. if (!parse_time_zone())
  778. return false;
  779. (void)parse_calendar();
  780. }
  781. transaction.commit();
  782. return true;
  783. }
  784. // https://tc39.es/proposal-temporal/#prod-TemporalYearMonthString
  785. bool ISO8601Parser::parse_temporal_year_month_string()
  786. {
  787. // TemporalYearMonthString :
  788. // DateSpecYearMonth
  789. // DateTime
  790. // NOTE: Reverse order here because `DateSpecYearMonth` can be a subset of `DateTime`,
  791. // so we'd not attempt to parse that but may not exhaust the input string.
  792. return parse_date_time()
  793. || parse_date_spec_year_month();
  794. }
  795. // https://tc39.es/proposal-temporal/#prod-TemporalZonedDateTimeString
  796. bool ISO8601Parser::parse_temporal_zoned_date_time_string()
  797. {
  798. // TemporalZonedDateTimeString :
  799. // Date TimeSpecSeparator[opt] TimeZoneNameRequired Calendar[opt]
  800. StateTransaction transaction { *this };
  801. if (!parse_date())
  802. return false;
  803. (void)parse_time_spec_separator();
  804. if (!parse_time_zone_name_required())
  805. return false;
  806. (void)parse_calendar();
  807. transaction.commit();
  808. return true;
  809. }
  810. // https://tc39.es/proposal-temporal/#prod-TemporalCalendarString
  811. bool ISO8601Parser::parse_temporal_calendar_string()
  812. {
  813. // TemporalCalendarString :
  814. // CalendarName
  815. // TemporalInstantString
  816. // CalendarDateTime
  817. // Time
  818. // DateSpecYearMonth
  819. // DateSpecMonthDay
  820. return parse_calendar_name()
  821. // TODO: || parse_temporal_instant_string()
  822. || parse_calendar_date_time()
  823. || parse_date_spec_year_month()
  824. || parse_date_spec_month_day()
  825. || parse_time();
  826. }
  827. // https://tc39.es/proposal-temporal/#prod-TemporalRelativeToString
  828. bool ISO8601Parser::parse_temporal_relative_to_string()
  829. {
  830. // TemporalRelativeToString :
  831. // TemporalDateTimeString
  832. return parse_temporal_date_time_string();
  833. }
  834. }
  835. #define JS_ENUMERATE_ISO8601_PRODUCTION_PARSERS \
  836. __JS_ENUMERATE(TemporalInstantString, parse_temporal_instant_string) \
  837. __JS_ENUMERATE(TemporalDateString, parse_temporal_date_string) \
  838. __JS_ENUMERATE(TemporalDateTimeString, parse_temporal_date_time_string) \
  839. __JS_ENUMERATE(TemporalMonthDayString, parse_temporal_month_day_string) \
  840. __JS_ENUMERATE(TemporalTimeString, parse_temporal_time_string) \
  841. __JS_ENUMERATE(TemporalTimeZoneString, parse_temporal_time_zone_string) \
  842. __JS_ENUMERATE(TemporalYearMonthString, parse_temporal_year_month_string) \
  843. __JS_ENUMERATE(TemporalZonedDateTimeString, parse_temporal_zoned_date_time_string) \
  844. __JS_ENUMERATE(TemporalCalendarString, parse_temporal_calendar_string) \
  845. __JS_ENUMERATE(TemporalRelativeToString, parse_temporal_relative_to_string)
  846. Optional<ParseResult> parse_iso8601(Production production, StringView input)
  847. {
  848. auto parser = Detail::ISO8601Parser { input };
  849. switch (production) {
  850. #define __JS_ENUMERATE(ProductionName, parse_production) \
  851. case Production::ProductionName: \
  852. if (!parser.parse_production()) \
  853. return {}; \
  854. break;
  855. JS_ENUMERATE_ISO8601_PRODUCTION_PARSERS
  856. #undef __JS_ENUMERATE
  857. default:
  858. VERIFY_NOT_REACHED();
  859. }
  860. // If we parsed successfully but didn't reach the end, the string doesn't match the given production.
  861. if (!parser.lexer().is_eof())
  862. return {};
  863. return parser.parse_result();
  864. }
  865. }