css-declarations-parser.js 20 KB

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