1
0

css-declarations-parser.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  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 = "", code;
  376. while (code = consume()) { // eslint-disable-line no-cond-assign
  377. if (namechar(code)) {
  378. result += String.fromCodePoint(code);
  379. } else if (startsWithAValidEscape(next, code)) {
  380. result += String.fromCodePoint(consumeEscape(consume, next, eof));
  381. } else {
  382. reconsume();
  383. return result;
  384. }
  385. }
  386. }
  387. function consumeANumber(consume, next) {
  388. let repr = [], type = "integer";
  389. let code;
  390. if (next() == 0x2b || next() == 0x2d) {
  391. code = consume();
  392. repr += String.fromCodePoint(code);
  393. }
  394. while (digit(next())) {
  395. code = consume();
  396. repr += String.fromCodePoint(code);
  397. }
  398. if (next(1) == 0x2e && digit(next(2))) {
  399. code = consume();
  400. repr += String.fromCodePoint(code);
  401. code = consume();
  402. repr += String.fromCodePoint(code);
  403. type = "number";
  404. while (digit(next())) {
  405. code = consume();
  406. repr += String.fromCodePoint(code);
  407. }
  408. }
  409. const c1 = next(1), c2 = next(2), c3 = next(3);
  410. if ((c1 == 0x45 || c1 == 0x65) && digit(c2)) {
  411. code = consume();
  412. repr += String.fromCodePoint(code);
  413. code = consume();
  414. repr += String.fromCodePoint(code);
  415. type = "number";
  416. while (digit(next())) {
  417. code = consume();
  418. repr += String.fromCodePoint(code);
  419. }
  420. } else if ((c1 == 0x45 || c1 == 0x65) && (c2 == 0x2b || c2 == 0x2d) && digit(c3)) {
  421. code = consume();
  422. repr += String.fromCodePoint(code);
  423. code = consume();
  424. repr += String.fromCodePoint(code);
  425. code = consume();
  426. repr += String.fromCodePoint(code);
  427. type = "number";
  428. while (digit(next())) {
  429. code = consume();
  430. repr += String.fromCodePoint(code);
  431. }
  432. }
  433. const value = convertAStringToANumber(repr);
  434. return { type, value, repr };
  435. }
  436. function convertAStringToANumber(string) {
  437. // CSS's number rules are identical to JS, afaik.
  438. return Number(string);
  439. }
  440. function consumeTheRemnantsOfABadURL(consume, next, eof, donothing) {
  441. let code;
  442. while (code = consume()) { // eslint-disable-line no-cond-assign
  443. if (code == 0x29 || eof()) {
  444. return;
  445. } else if (startsWithAValidEscape(next, code)) {
  446. consumeEscape(consume, next, eof);
  447. donothing();
  448. } else {
  449. donothing();
  450. }
  451. }
  452. }
  453. function tokenize(str) {
  454. str = preprocess(str);
  455. let i = -1;
  456. const tokens = [];
  457. const strLength = str.length;
  458. let code;
  459. // Line number information.
  460. let line = 0, column = 0;
  461. // The only use of lastLineLength is in reconsume().
  462. let lastLineLength = 0;
  463. const incrLineno = function () {
  464. line += 1;
  465. lastLineLength = column;
  466. column = 0;
  467. };
  468. const locStart = { line, column };
  469. const codepoint = function (i) {
  470. if (i >= strLength) {
  471. return -1;
  472. }
  473. return str[i];
  474. };
  475. const next = function (num) {
  476. if (num === undefined)
  477. num = 1;
  478. if (num > 3)
  479. throw "Spec Error: no more than three codepoints of lookahead.";
  480. return codepoint(i + num);
  481. };
  482. const consume = function (num) {
  483. if (num === undefined)
  484. num = 1;
  485. i += num;
  486. const code = codepoint(i);
  487. if (newline(code)) incrLineno();
  488. else column += num;
  489. //console.log('Consume '+i+' '+String.fromCharCode(code) + ' 0x' + code.toString(16));
  490. return code;
  491. };
  492. const reconsume = function () {
  493. i -= 1;
  494. if (newline(code)) {
  495. line -= 1;
  496. column = lastLineLength;
  497. } else {
  498. column -= 1;
  499. }
  500. locStart.line = line;
  501. locStart.column = column;
  502. return true;
  503. };
  504. const eof = function (codepoint) {
  505. if (codepoint === undefined) codepoint = code;
  506. return codepoint == -1;
  507. };
  508. const donothing = function () { };
  509. const parseerror = function () { throw new Error("Parse error at index " + i + ", processing codepoint 0x" + code.toString(16) + "."); };
  510. let iterationCount = 0;
  511. while (!eof(next())) {
  512. tokens.push(consumeAToken(consume, next, eof, reconsume, parseerror, donothing));
  513. iterationCount++;
  514. if (iterationCount > strLength * 2) return "I'm infinite-looping!";
  515. }
  516. return tokens;
  517. }
  518. class Token {
  519. constructor(tokenType, value) {
  520. this.tokenType = tokenType;
  521. this.value = value;
  522. this.repr = null;
  523. this.type = null;
  524. this.unit = null;
  525. }
  526. }
  527. // ---
  528. class TokenStream {
  529. constructor(tokens) {
  530. // Assume that tokens is an array.
  531. this.tokens = tokens;
  532. this.i = -1;
  533. }
  534. tokenAt(i) {
  535. if (i < this.tokens.length)
  536. return this.tokens[i];
  537. return new Token(EOF_TOKEN_TYPE);
  538. }
  539. consume(num) {
  540. if (num === undefined)
  541. num = 1;
  542. this.i += num;
  543. this.token = this.tokenAt(this.i);
  544. //console.log(this.i, this.token);
  545. return true;
  546. }
  547. next() {
  548. return this.tokenAt(this.i + 1);
  549. }
  550. reconsume() {
  551. this.i--;
  552. }
  553. }
  554. function parseerror(s, msg) {
  555. throw new Error("Parse error at token " + s.i + ": " + s.token + ".\n" + msg);
  556. }
  557. function donothing() { return true; }
  558. function consumeAListOfDeclarations(s) {
  559. const decls = [];
  560. while (s.consume()) {
  561. if (s.token.tokenType == WHITESPACE_TOKEN_TYPE || s.token.tokenType == SEMICOLON_TOKEN_TYPE) {
  562. donothing();
  563. } else if (s.token.tokenType == EOF_TOKEN_TYPE) {
  564. return decls;
  565. } else if (s.token.tokenType == IDENT_TOKEN_TYPE) {
  566. const temp = [s.token];
  567. while (!(s.next().tokenType == SEMICOLON_TOKEN_TYPE || s.next().tokenType == EOF_TOKEN_TYPE))
  568. temp.push(consumeAComponentValue(s));
  569. const decl = consumeADeclaration(new TokenStream(temp));
  570. if (decl) decls.push(decl);
  571. } else {
  572. parseerror(s);
  573. s.reconsume();
  574. while (!(s.next().tokenType == SEMICOLON_TOKEN_TYPE || s.next().tokenType == EOF_TOKEN_TYPE))
  575. consumeAComponentValue(s);
  576. }
  577. }
  578. }
  579. function consumeADeclaration(s) {
  580. // Assumes that the next input token will be an ident token.
  581. s.consume();
  582. const decl = new Declaration(s.token.value);
  583. while (s.next().tokenType == WHITESPACE_TOKEN_TYPE) s.consume();
  584. if (!(s.next().tokenType == COLON_TOKEN_TYPE)) {
  585. parseerror(s);
  586. return;
  587. } else {
  588. s.consume();
  589. }
  590. while (!(s.next().tokenType == EOF_TOKEN_TYPE)) {
  591. decl.value.push(consumeAComponentValue(s));
  592. }
  593. let foundImportant = false;
  594. for (let i = decl.value.length - 1; i >= 0; i--) {
  595. if (decl.value[i].tokenType == WHITESPACE_TOKEN_TYPE) {
  596. continue;
  597. } else if (decl.value[i].tokenType == IDENT_TOKEN_TYPE && decl.value[i].value.toLowerCase() == "important") {
  598. foundImportant = true;
  599. } else if (foundImportant && decl.value[i].tokenType == DELIM_TOKEN_TYPE && decl.value[i].value == "!") {
  600. decl.value.splice(i, decl.value.length);
  601. decl.important = true;
  602. break;
  603. } else {
  604. break;
  605. }
  606. }
  607. return decl;
  608. }
  609. function consumeAComponentValue(s) {
  610. s.consume();
  611. if (s.token.tokenType == FUNCTION_TOKEN_TYPE)
  612. return consumeAFunction(s);
  613. return s.token;
  614. }
  615. function consumeAFunction(s) {
  616. const func = new Func(s.token.value);
  617. while (s.consume()) {
  618. if (s.token.tokenType == EOF_TOKEN_TYPE || s.token.tokenType == CLOSE_PAREN_TOKEN_TYPE)
  619. return func;
  620. else {
  621. s.reconsume();
  622. func.value.push(consumeAComponentValue(s));
  623. }
  624. }
  625. }
  626. function normalizeInput(input) {
  627. if (typeof input == "string")
  628. return new TokenStream(tokenize(input));
  629. else throw SyntaxError(input);
  630. }
  631. function parseAListOfDeclarations(s) {
  632. s = normalizeInput(s);
  633. return consumeAListOfDeclarations(s);
  634. }
  635. class Declaration {
  636. constructor(name) {
  637. this.name = name;
  638. this.value = [];
  639. this.important = false;
  640. this.type = DECLARATION_TYPE;
  641. }
  642. }
  643. class Func {
  644. constructor(name) {
  645. this.name = name;
  646. this.value = [];
  647. this.type = FUNCTION_TYPE;
  648. }
  649. }
  650. // Exportation.
  651. return {
  652. parseAListOfDeclarations
  653. };
  654. })();