css-declarations-parser.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. /*
  2. * The code in this file is licensed under the CC0 license.
  3. *
  4. * http://creativecommons.org/publicdomain/zero/1.0/
  5. *
  6. * It is free to use for any purpose. No attribution, permission, or reproduction of this license is require
  7. */
  8. // Modified by Gildas Lormeau (ES5 -> ES6, removed unused code)
  9. // https://github.com/tabatkins/parse-css
  10. this.parseCss = this.parseCss || (() => {
  11. const BAD_STRING_TOKEN_TYPE = "BADSTRING";
  12. const BAD_URL_TOKEN_TYPE = "BADURL";
  13. const WHITESPACE_TOKEN_TYPE = "WHITESPACE";
  14. const CDO_TOKEN_TYPE = "CDO";
  15. const CDC_TOKEN_TYPE = "CDO";
  16. const COLON_TOKEN_TYPE = ":";
  17. const SEMICOLON_TOKEN_TYPE = ";";
  18. const COMMA_TOKEN_TYPE = ",";
  19. const OPEN_CURLY_TOKEN_TYPE = "{";
  20. const CLOSE_CURLY_TOKEN_TYPE = "}";
  21. const OPEN_SQUARE_TOKEN_TYPE = "[";
  22. const CLOSE_SQUARE_TOKEN_TYPE = "]";
  23. const OPEN_PAREN_TOKEN_TYPE = "(";
  24. const CLOSE_PAREN_TOKEN_TYPE = ";";
  25. const INCLUDE_MATCH_TOKEN_TYPE = "~=";
  26. const DASH_MATCH_TOKEN_TYPE = "|=";
  27. const PREFIX_MATCH_TOKEN_TYPE = "^=";
  28. const SUFFIX_MATCH_TOKEN_TYPE = "$=";
  29. const SUBSTRING_MATCH_TOKEN_TYPE = "*=";
  30. const COLUMN_TOKEN_TYPE = "||";
  31. const EOF_TOKEN_TYPE = "EOF";
  32. const DELIM_TOKEN_TYPE = "DELIM";
  33. const IDENT_TOKEN_TYPE = "IDENT";
  34. const FUNCTION_TOKEN_TYPE = "FUNCTION";
  35. const HASH_TOKEN_TYPE = "HASH";
  36. const STRING_TOKEN_TYPE = "STRING";
  37. const URL_TOKEN_TYPE = "URL";
  38. const NUMBER_TOKEN_TYPE = "NUMBER";
  39. const PERCENTAGE_TOKEN_TYPE = "PERCENTAGE";
  40. const DIMENSION_TOKEN_TYPE = "DIMENSION";
  41. const DECLARATION_TYPE = "DECLARATION";
  42. const FUNCTION_TYPE = "FUNCTION";
  43. function between(num, first, last) { return num >= first && num <= last; }
  44. function digit(code) { return between(code, 0x30, 0x39); }
  45. function hexdigit(code) { return digit(code) || between(code, 0x41, 0x46) || between(code, 0x61, 0x66); }
  46. function uppercaseletter(code) { return between(code, 0x41, 0x5a); }
  47. function lowercaseletter(code) { return between(code, 0x61, 0x7a); }
  48. function letter(code) { return uppercaseletter(code) || lowercaseletter(code); }
  49. function nonascii(code) { return code >= 0x80; }
  50. function namestartchar(code) { return letter(code) || nonascii(code) || code == 0x5f; }
  51. function namechar(code) { return namestartchar(code) || digit(code) || code == 0x2d; }
  52. function nonprintable(code) { return between(code, 0, 8) || code == 0xb || between(code, 0xe, 0x1f) || code == 0x7f; }
  53. function newline(code) { return code == 0xa; }
  54. function whitespace(code) { return newline(code) || code == 9 || code == 0x20; }
  55. const maximumallowedcodepoint = 0x10ffff;
  56. function preprocess(str) {
  57. // Turn a string into an array of code points,
  58. // following the preprocessing cleanup rules.
  59. const codepoints = [];
  60. for (let i = 0; i < str.length; i++) {
  61. let code = str.charCodeAt(i);
  62. if (code == 0xd && str.charCodeAt(i + 1) == 0xa) {
  63. code = 0xa; i++;
  64. }
  65. if (code == 0xd || code == 0xc) code = 0xa;
  66. if (code == 0x0) code = 0xfffd;
  67. if (between(code, 0xd800, 0xdbff) && between(str.charCodeAt(i + 1), 0xdc00, 0xdfff)) {
  68. // Decode a surrogate pair into an astral codepoint.
  69. const lead = code - 0xd800;
  70. const trail = str.charCodeAt(i + 1) - 0xdc00;
  71. code = Math.pow(2, 20) + lead * Math.pow(2, 10) + trail;
  72. i++;
  73. }
  74. codepoints.push(code);
  75. }
  76. return codepoints;
  77. }
  78. function stringFromCode(code) {
  79. if (code <= 0xffff) return String.fromCharCode(code);
  80. // Otherwise, encode astral char as surrogate pair.
  81. code -= Math.pow(2, 20);
  82. const lead = Math.floor(code / Math.pow(2, 10)) + 0xd800;
  83. const trail = code % Math.pow(2, 10) + 0xdc00;
  84. return String.fromCharCode(lead) + String.fromCharCode(trail);
  85. }
  86. function tokenize(str) {
  87. str = preprocess(str);
  88. let i = -1;
  89. const tokens = [];
  90. let code;
  91. // Line number information.
  92. let line = 0;
  93. let column = 0;
  94. // The only use of lastLineLength is in reconsume().
  95. let lastLineLength = 0;
  96. const incrLineno = function () {
  97. line += 1;
  98. lastLineLength = column;
  99. column = 0;
  100. };
  101. const locStart = { line: line, column: column };
  102. const codepoint = function (i) {
  103. if (i >= str.length) {
  104. return -1;
  105. }
  106. return str[i];
  107. };
  108. const next = function (num) {
  109. if (num === undefined)
  110. num = 1;
  111. if (num > 3)
  112. throw "Spec Error: no more than three codepoints of lookahead.";
  113. return codepoint(i + num);
  114. };
  115. const consume = function (num) {
  116. if (num === undefined)
  117. num = 1;
  118. i += num;
  119. code = codepoint(i);
  120. if (newline(code)) incrLineno();
  121. else column += num;
  122. //console.log('Consume '+i+' '+String.fromCharCode(code) + ' 0x' + code.toString(16));
  123. return true;
  124. };
  125. const reconsume = function () {
  126. i -= 1;
  127. if (newline(code)) {
  128. line -= 1;
  129. column = lastLineLength;
  130. } else {
  131. column -= 1;
  132. }
  133. locStart.line = line;
  134. locStart.column = column;
  135. return true;
  136. };
  137. const eof = function (codepoint) {
  138. if (codepoint === undefined) codepoint = code;
  139. return codepoint == -1;
  140. };
  141. const donothing = function () { };
  142. const parseerror = function () { throw new Error("Parse error at index " + i + ", processing codepoint 0x" + code.toString(16) + "."); };
  143. const consumeAToken = function () {
  144. consumeComments();
  145. consume();
  146. if (whitespace(code)) {
  147. while (whitespace(next())) consume();
  148. return new Token(WHITESPACE_TOKEN_TYPE);
  149. }
  150. else if (code == 0x22) return consumeAStringToken();
  151. else if (code == 0x23) {
  152. if (namechar(next()) || areAValidEscape(next(1), next(2))) {
  153. const token = new Token(HASH_TOKEN_TYPE);
  154. if (wouldStartAnIdentifier(next(1), next(2), next(3))) token.type = "id";
  155. token.value = consumeAName();
  156. return token;
  157. } else {
  158. return new Token(DELIM_TOKEN_TYPE, stringFromCode(code));
  159. }
  160. }
  161. else if (code == 0x24) {
  162. if (next() == 0x3d) {
  163. consume();
  164. return new Token(SUFFIX_MATCH_TOKEN_TYPE);
  165. } else {
  166. return new Token(DELIM_TOKEN_TYPE, stringFromCode(code));
  167. }
  168. }
  169. else if (code == 0x27) return consumeAStringToken();
  170. else if (code == 0x28) return new Token(OPEN_PAREN_TOKEN_TYPE);
  171. else if (code == 0x29) return new Token(CLOSE_PAREN_TOKEN_TYPE);
  172. else if (code == 0x2a) {
  173. if (next() == 0x3d) {
  174. consume();
  175. return new Token(SUBSTRING_MATCH_TOKEN_TYPE);
  176. } else {
  177. return new Token(DELIM_TOKEN_TYPE, stringFromCode(code));
  178. }
  179. }
  180. else if (code == 0x2b) {
  181. if (startsWithANumber()) {
  182. reconsume();
  183. return consumeANumericToken();
  184. } else {
  185. return new Token(DELIM_TOKEN_TYPE, stringFromCode(code));
  186. }
  187. }
  188. else if (code == 0x2c) return new Token(COMMA_TOKEN_TYPE);
  189. else if (code == 0x2d) {
  190. if (startsWithANumber()) {
  191. reconsume();
  192. return consumeANumericToken();
  193. } else if (next(1) == 0x2d && next(2) == 0x3e) {
  194. consume(2);
  195. return new Token(CDC_TOKEN_TYPE);
  196. } else if (startsWithAnIdentifier()) {
  197. reconsume();
  198. return consumeAnIdentlikeToken();
  199. } else {
  200. return new Token(DELIM_TOKEN_TYPE, stringFromCode(code));
  201. }
  202. }
  203. else if (code == 0x2e) {
  204. if (startsWithANumber()) {
  205. reconsume();
  206. return consumeANumericToken();
  207. } else {
  208. return new Token(DELIM_TOKEN_TYPE, stringFromCode(code));
  209. }
  210. }
  211. else if (code == 0x3a) return new Token(COLON_TOKEN_TYPE);
  212. else if (code == 0x3b) return new Token(SEMICOLON_TOKEN_TYPE);
  213. else if (code == 0x3c) {
  214. if (next(1) == 0x21 && next(2) == 0x2d && next(3) == 0x2d) {
  215. consume(3);
  216. return new Token(CDO_TOKEN_TYPE);
  217. } else {
  218. return new Token(DELIM_TOKEN_TYPE, stringFromCode(code));
  219. }
  220. }
  221. else if (code == 0x40) {
  222. return new Token(DELIM_TOKEN_TYPE, stringFromCode(code));
  223. }
  224. else if (code == 0x5b) new Token(OPEN_SQUARE_TOKEN_TYPE);
  225. else if (code == 0x5c) {
  226. if (startsWithAValidEscape()) {
  227. reconsume();
  228. return consumeAnIdentlikeToken();
  229. } else {
  230. parseerror();
  231. return new Token(DELIM_TOKEN_TYPE, stringFromCode(code));
  232. }
  233. }
  234. else if (code == 0x5d) new Token(CLOSE_SQUARE_TOKEN_TYPE);
  235. else if (code == 0x5e) {
  236. if (next() == 0x3d) {
  237. consume();
  238. return new Token(PREFIX_MATCH_TOKEN_TYPE);
  239. } else {
  240. return new Token(DELIM_TOKEN_TYPE, stringFromCode(code));
  241. }
  242. }
  243. else if (code == 0x7b) return new Token(OPEN_CURLY_TOKEN_TYPE);
  244. else if (code == 0x7c) {
  245. if (next() == 0x3d) {
  246. consume();
  247. return new Token(DASH_MATCH_TOKEN_TYPE);
  248. } else if (next() == 0x7c) {
  249. consume();
  250. return new Token(COLUMN_TOKEN_TYPE);
  251. } else {
  252. return new Token(DELIM_TOKEN_TYPE, stringFromCode(code));
  253. }
  254. }
  255. else if (code == 0x7d) return new Token(CLOSE_CURLY_TOKEN_TYPE);
  256. else if (code == 0x7e) {
  257. if (next() == 0x3d) {
  258. consume();
  259. return new Token(INCLUDE_MATCH_TOKEN_TYPE);
  260. } else {
  261. return new Token(DELIM_TOKEN_TYPE, stringFromCode(code));
  262. }
  263. }
  264. else if (digit(code)) {
  265. reconsume();
  266. return consumeANumericToken();
  267. }
  268. else if (namestartchar(code)) {
  269. reconsume();
  270. return consumeAnIdentlikeToken();
  271. }
  272. else if (eof()) return new Token(EOF_TOKEN_TYPE);
  273. else return new Token(DELIM_TOKEN_TYPE, stringFromCode(code));
  274. };
  275. const consumeComments = function () {
  276. while (next(1) == 0x2f && next(2) == 0x2a) {
  277. consume(2);
  278. while (true) { // eslint-disable-line no-constant-condition
  279. consume();
  280. if (code == 0x2a && next() == 0x2f) {
  281. consume();
  282. break;
  283. } else if (eof()) {
  284. parseerror();
  285. return;
  286. }
  287. }
  288. }
  289. };
  290. const consumeANumericToken = function () {
  291. const num = consumeANumber();
  292. if (wouldStartAnIdentifier(next(1), next(2), next(3))) {
  293. const token = new Token(DIMENSION_TOKEN_TYPE, num.value);
  294. token.repr = num.repr;
  295. token.type = num.type;
  296. token.unit = consumeAName();
  297. return token;
  298. } else if (next() == 0x25) {
  299. consume();
  300. const token = new Token(PERCENTAGE_TOKEN_TYPE, num.value);
  301. token.repr = num.repr;
  302. return token;
  303. } else {
  304. const token = new Token(NUMBER_TOKEN_TYPE, num.value);
  305. token.type = "integer";
  306. token.repr = num.repr;
  307. token.type = num.type;
  308. return token;
  309. }
  310. };
  311. const consumeAnIdentlikeToken = function () {
  312. const str = consumeAName();
  313. if (str.toLowerCase() == "url" && next() == 0x28) {
  314. consume();
  315. while (whitespace(next(1)) && whitespace(next(2))) consume();
  316. if (next() == 0x22 || next() == 0x27) {
  317. return new Token(FUNCTION_TOKEN_TYPE, str);
  318. } else if (whitespace(next()) && (next(2) == 0x22 || next(2) == 0x27)) {
  319. return new Token(FUNCTION_TOKEN_TYPE, str);
  320. } else {
  321. return consumeAURLToken();
  322. }
  323. } else if (next() == 0x28) {
  324. consume();
  325. return new Token(FUNCTION_TOKEN_TYPE, str);
  326. } else {
  327. return new Token(IDENT_TOKEN_TYPE, str);
  328. }
  329. };
  330. const consumeAStringToken = function (endingCodePoint) {
  331. if (endingCodePoint === undefined) endingCodePoint = code;
  332. let string = "";
  333. while (consume()) {
  334. if (code == endingCodePoint || eof()) {
  335. return new Token(STRING_TOKEN_TYPE, string);
  336. } else if (newline(code)) {
  337. parseerror();
  338. reconsume();
  339. return new Token(BAD_STRING_TOKEN_TYPE);
  340. } else if (code == 0x5c) {
  341. if (eof(next())) {
  342. donothing();
  343. } else if (newline(next())) {
  344. consume();
  345. } else {
  346. string += stringFromCode(consumeEscape());
  347. }
  348. } else {
  349. string += stringFromCode(code);
  350. }
  351. }
  352. };
  353. const consumeAURLToken = function () {
  354. const token = new Token(URL_TOKEN_TYPE, "");
  355. while (whitespace(next())) consume();
  356. if (eof(next())) return token;
  357. while (consume()) {
  358. if (code == 0x29 || eof()) {
  359. return token;
  360. } else if (whitespace(code)) {
  361. while (whitespace(next())) consume();
  362. if (next() == 0x29 || eof(next())) {
  363. consume();
  364. return token;
  365. } else {
  366. consumeTheRemnantsOfABadURL();
  367. return new Token(BAD_URL_TOKEN_TYPE);
  368. }
  369. } else if (code == 0x22 || code == 0x27 || code == 0x28 || nonprintable(code)) {
  370. parseerror();
  371. consumeTheRemnantsOfABadURL();
  372. return new Token(BAD_URL_TOKEN_TYPE);
  373. } else if (code == 0x5c) {
  374. if (startsWithAValidEscape()) {
  375. token.value += stringFromCode(consumeEscape());
  376. } else {
  377. parseerror();
  378. consumeTheRemnantsOfABadURL();
  379. return new Token(BAD_URL_TOKEN_TYPE);
  380. }
  381. } else {
  382. token.value += stringFromCode(code);
  383. }
  384. }
  385. };
  386. const consumeEscape = function () {
  387. // Assume the the current character is the \
  388. // and the next code point is not a newline.
  389. consume();
  390. if (hexdigit(code)) {
  391. // Consume 1-6 hex digits
  392. const digits = [code];
  393. for (let total = 0; total < 5; total++) {
  394. if (hexdigit(next())) {
  395. consume();
  396. digits.push(code);
  397. } else {
  398. break;
  399. }
  400. }
  401. if (whitespace(next())) consume();
  402. let value = parseInt(digits.map(function (x) { return String.fromCharCode(x); }).join(""), 16);
  403. if (value > maximumallowedcodepoint) value = 0xfffd;
  404. return value;
  405. } else if (eof()) {
  406. return 0xfffd;
  407. } else {
  408. return code;
  409. }
  410. };
  411. const areAValidEscape = function (c1, c2) {
  412. if (c1 != 0x5c) return false;
  413. if (newline(c2)) return false;
  414. return true;
  415. };
  416. const startsWithAValidEscape = function () {
  417. return areAValidEscape(code, next());
  418. };
  419. const wouldStartAnIdentifier = function (c1, c2, c3) {
  420. if (c1 == 0x2d) {
  421. return namestartchar(c2) || c2 == 0x2d || areAValidEscape(c2, c3);
  422. } else if (namestartchar(c1)) {
  423. return true;
  424. } else if (c1 == 0x5c) {
  425. return areAValidEscape(c1, c2);
  426. } else {
  427. return false;
  428. }
  429. };
  430. const startsWithAnIdentifier = function () {
  431. return wouldStartAnIdentifier(code, next(1), next(2));
  432. };
  433. const wouldStartANumber = function (c1, c2, c3) {
  434. if (c1 == 0x2b || c1 == 0x2d) {
  435. if (digit(c2)) return true;
  436. if (c2 == 0x2e && digit(c3)) return true;
  437. return false;
  438. } else if (c1 == 0x2e) {
  439. if (digit(c2)) return true;
  440. return false;
  441. } else if (digit(c1)) {
  442. return true;
  443. } else {
  444. return false;
  445. }
  446. };
  447. const startsWithANumber = function () {
  448. return wouldStartANumber(code, next(1), next(2));
  449. };
  450. const consumeAName = function () {
  451. let result = "";
  452. while (consume()) {
  453. if (namechar(code)) {
  454. result += stringFromCode(code);
  455. } else if (startsWithAValidEscape()) {
  456. result += stringFromCode(consumeEscape());
  457. } else {
  458. reconsume();
  459. return result;
  460. }
  461. }
  462. };
  463. const consumeANumber = function () {
  464. let repr = [];
  465. let type = "integer";
  466. if (next() == 0x2b || next() == 0x2d) {
  467. consume();
  468. repr += stringFromCode(code);
  469. }
  470. while (digit(next())) {
  471. consume();
  472. repr += stringFromCode(code);
  473. }
  474. if (next(1) == 0x2e && digit(next(2))) {
  475. consume();
  476. repr += stringFromCode(code);
  477. consume();
  478. repr += stringFromCode(code);
  479. type = "number";
  480. while (digit(next())) {
  481. consume();
  482. repr += stringFromCode(code);
  483. }
  484. }
  485. const c1 = next(1), c2 = next(2), c3 = next(3);
  486. if ((c1 == 0x45 || c1 == 0x65) && digit(c2)) {
  487. consume();
  488. repr += stringFromCode(code);
  489. consume();
  490. repr += stringFromCode(code);
  491. type = "number";
  492. while (digit(next())) {
  493. consume();
  494. repr += stringFromCode(code);
  495. }
  496. } else if ((c1 == 0x45 || c1 == 0x65) && (c2 == 0x2b || c2 == 0x2d) && digit(c3)) {
  497. consume();
  498. repr += stringFromCode(code);
  499. consume();
  500. repr += stringFromCode(code);
  501. consume();
  502. repr += stringFromCode(code);
  503. type = "number";
  504. while (digit(next())) {
  505. consume();
  506. repr += stringFromCode(code);
  507. }
  508. }
  509. const value = convertAStringToANumber(repr);
  510. return { type: type, value: value, repr: repr };
  511. };
  512. const convertAStringToANumber = function (string) {
  513. // CSS's number rules are identical to JS, afaik.
  514. return Number(string);
  515. };
  516. const consumeTheRemnantsOfABadURL = function () {
  517. while (consume()) {
  518. if (code == 0x29 || eof()) {
  519. return;
  520. } else if (startsWithAValidEscape()) {
  521. consumeEscape();
  522. donothing();
  523. } else {
  524. donothing();
  525. }
  526. }
  527. };
  528. let iterationCount = 0;
  529. while (!eof(next())) {
  530. tokens.push(consumeAToken());
  531. iterationCount++;
  532. if (iterationCount > str.length * 2) return "I'm infinite-looping!";
  533. }
  534. return tokens;
  535. }
  536. class Token {
  537. constructor(tokenType, value) {
  538. this.tokenType = tokenType;
  539. this.value = value;
  540. this.repr = null;
  541. this.type = null;
  542. this.unit = null;
  543. }
  544. }
  545. // ---
  546. class TokenStream {
  547. constructor(tokens) {
  548. // Assume that tokens is an array.
  549. this.tokens = tokens;
  550. this.i = -1;
  551. }
  552. tokenAt(i) {
  553. if (i < this.tokens.length)
  554. return this.tokens[i];
  555. return new Token(EOF_TOKEN_TYPE);
  556. }
  557. consume(num) {
  558. if (num === undefined)
  559. num = 1;
  560. this.i += num;
  561. this.token = this.tokenAt(this.i);
  562. //console.log(this.i, this.token);
  563. return true;
  564. }
  565. next() {
  566. return this.tokenAt(this.i + 1);
  567. }
  568. reconsume() {
  569. this.i--;
  570. }
  571. }
  572. function parseerror(s, msg) {
  573. throw new Error("Parse error at token " + s.i + ": " + s.token + ".\n" + msg);
  574. }
  575. function donothing() { return true; }
  576. function consumeAListOfDeclarations(s) {
  577. const decls = [];
  578. while (s.consume()) {
  579. if (s.token.tokenType == WHITESPACE_TOKEN_TYPE || s.token.tokenType == SEMICOLON_TOKEN_TYPE) {
  580. donothing();
  581. } else if (s.token.tokenType == EOF_TOKEN_TYPE) {
  582. return decls;
  583. } else if (s.token.tokenType == IDENT_TOKEN_TYPE) {
  584. const temp = [s.token];
  585. while (!(s.next().tokenType == SEMICOLON_TOKEN_TYPE || s.next().tokenType == EOF_TOKEN_TYPE))
  586. temp.push(consumeAComponentValue(s));
  587. let decl = consumeADeclaration(new TokenStream(temp));
  588. if (decl) decls.push(decl);
  589. } else {
  590. parseerror(s);
  591. s.reconsume();
  592. while (!(s.next().tokenType == SEMICOLON_TOKEN_TYPE || s.next().tokenType == EOF_TOKEN_TYPE))
  593. consumeAComponentValue(s);
  594. }
  595. }
  596. }
  597. function consumeADeclaration(s) {
  598. // Assumes that the next input token will be an ident token.
  599. s.consume();
  600. const decl = new Declaration(s.token.value);
  601. while (s.next().tokenType == WHITESPACE_TOKEN_TYPE) s.consume();
  602. if (!(s.next().tokenType == COLON_TOKEN_TYPE)) {
  603. parseerror(s);
  604. return;
  605. } else {
  606. s.consume();
  607. }
  608. while (!(s.next().tokenType == EOF_TOKEN_TYPE)) {
  609. decl.value.push(consumeAComponentValue(s));
  610. }
  611. let foundImportant = false;
  612. for (let i = decl.value.length - 1; i >= 0; i--) {
  613. if (decl.value[i].tokenType == WHITESPACE_TOKEN_TYPE) {
  614. continue;
  615. } else if (decl.value[i].tokenType == IDENT_TOKEN_TYPE && decl.value[i].value.toLowerCase() == "important") {
  616. foundImportant = true;
  617. } else if (foundImportant && decl.value[i].tokenType == DELIM_TOKEN_TYPE && decl.value[i].value == "!") {
  618. decl.value.splice(i, decl.value.length);
  619. decl.important = true;
  620. break;
  621. } else {
  622. break;
  623. }
  624. }
  625. return decl;
  626. }
  627. function consumeAComponentValue(s) {
  628. s.consume();
  629. if (s.token.tokenType == FUNCTION_TOKEN_TYPE)
  630. return consumeAFunction(s);
  631. return s.token;
  632. }
  633. function consumeAFunction(s) {
  634. const func = new Func(s.token.value);
  635. while (s.consume()) {
  636. if (s.token.tokenType == EOF_TOKEN_TYPE || s.token.tokenType == CLOSE_PAREN_TOKEN_TYPE)
  637. return func;
  638. else {
  639. s.reconsume();
  640. func.value.push(consumeAComponentValue(s));
  641. }
  642. }
  643. }
  644. function normalizeInput(input) {
  645. if (typeof input == "string")
  646. return new TokenStream(tokenize(input));
  647. else throw SyntaxError(input);
  648. }
  649. function parseAListOfDeclarations(s) {
  650. s = normalizeInput(s);
  651. return consumeAListOfDeclarations(s);
  652. }
  653. class Declaration {
  654. constructor(name) {
  655. this.name = name;
  656. this.value = [];
  657. this.important = false;
  658. this.type = DECLARATION_TYPE;
  659. }
  660. }
  661. class Func {
  662. constructor(name) {
  663. this.name = name;
  664. this.value = [];
  665. this.type = FUNCTION_TYPE;
  666. }
  667. }
  668. // Exportation.
  669. return {
  670. parseAListOfDeclarations
  671. };
  672. })();