css-declarations-parser.js 20 KB

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