css-declarations-parser.js 20 KB

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