css-declarations-parser.js 20 KB

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