Lexer.cjs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. 'use strict';
  2. const error = require('./error.cjs');
  3. const names = require('../utils/names.cjs');
  4. const genericConst = require('./generic-const.cjs');
  5. const generic = require('./generic.cjs');
  6. const units = require('./units.cjs');
  7. const prepareTokens = require('./prepare-tokens.cjs');
  8. const matchGraph = require('./match-graph.cjs');
  9. const match = require('./match.cjs');
  10. const trace = require('./trace.cjs');
  11. const search = require('./search.cjs');
  12. const structure = require('./structure.cjs');
  13. const parse = require('../definition-syntax/parse.cjs');
  14. const generate = require('../definition-syntax/generate.cjs');
  15. const walk = require('../definition-syntax/walk.cjs');
  16. function dumpMapSyntax(map, compact, syntaxAsAst) {
  17. const result = {};
  18. for (const name in map) {
  19. if (map[name].syntax) {
  20. result[name] = syntaxAsAst
  21. ? map[name].syntax
  22. : generate.generate(map[name].syntax, { compact });
  23. }
  24. }
  25. return result;
  26. }
  27. function dumpAtruleMapSyntax(map, compact, syntaxAsAst) {
  28. const result = {};
  29. for (const [name, atrule] of Object.entries(map)) {
  30. result[name] = {
  31. prelude: atrule.prelude && (
  32. syntaxAsAst
  33. ? atrule.prelude.syntax
  34. : generate.generate(atrule.prelude.syntax, { compact })
  35. ),
  36. descriptors: atrule.descriptors && dumpMapSyntax(atrule.descriptors, compact, syntaxAsAst)
  37. };
  38. }
  39. return result;
  40. }
  41. function valueHasVar(tokens) {
  42. for (let i = 0; i < tokens.length; i++) {
  43. if (tokens[i].value.toLowerCase() === 'var(') {
  44. return true;
  45. }
  46. }
  47. return false;
  48. }
  49. function syntaxHasTopLevelCommaMultiplier(syntax) {
  50. const singleTerm = syntax.terms[0];
  51. return (
  52. syntax.explicit === false &&
  53. syntax.terms.length === 1 &&
  54. singleTerm.type === 'Multiplier' &&
  55. singleTerm.comma === true
  56. );
  57. }
  58. function buildMatchResult(matched, error, iterations) {
  59. return {
  60. matched,
  61. iterations,
  62. error,
  63. ...trace
  64. };
  65. }
  66. function matchSyntax(lexer, syntax, value, useCssWideKeywords) {
  67. const tokens = prepareTokens(value, lexer.syntax);
  68. let result;
  69. if (valueHasVar(tokens)) {
  70. return buildMatchResult(null, new Error('Matching for a tree with var() is not supported'));
  71. }
  72. if (useCssWideKeywords) {
  73. result = match.matchAsTree(tokens, lexer.cssWideKeywordsSyntax, lexer);
  74. }
  75. if (!useCssWideKeywords || !result.match) {
  76. result = match.matchAsTree(tokens, syntax.match, lexer);
  77. if (!result.match) {
  78. return buildMatchResult(
  79. null,
  80. new error.SyntaxMatchError(result.reason, syntax.syntax, value, result),
  81. result.iterations
  82. );
  83. }
  84. }
  85. return buildMatchResult(result.match, null, result.iterations);
  86. }
  87. class Lexer {
  88. constructor(config, syntax, structure$1) {
  89. this.cssWideKeywords = genericConst.cssWideKeywords;
  90. this.syntax = syntax;
  91. this.generic = false;
  92. this.units = { ...units };
  93. this.atrules = Object.create(null);
  94. this.properties = Object.create(null);
  95. this.types = Object.create(null);
  96. this.structure = structure$1 || structure.getStructureFromConfig(config);
  97. if (config) {
  98. if (config.cssWideKeywords) {
  99. this.cssWideKeywords = config.cssWideKeywords;
  100. }
  101. if (config.units) {
  102. for (const group of Object.keys(units)) {
  103. if (Array.isArray(config.units[group])) {
  104. this.units[group] = config.units[group];
  105. }
  106. }
  107. }
  108. if (config.types) {
  109. for (const [name, type] of Object.entries(config.types)) {
  110. this.addType_(name, type);
  111. }
  112. }
  113. if (config.generic) {
  114. this.generic = true;
  115. for (const [name, value] of Object.entries(generic.createGenericTypes(this.units))) {
  116. this.addType_(name, value);
  117. }
  118. }
  119. if (config.atrules) {
  120. for (const [name, atrule] of Object.entries(config.atrules)) {
  121. this.addAtrule_(name, atrule);
  122. }
  123. }
  124. if (config.properties) {
  125. for (const [name, property] of Object.entries(config.properties)) {
  126. this.addProperty_(name, property);
  127. }
  128. }
  129. }
  130. this.cssWideKeywordsSyntax = matchGraph.buildMatchGraph(this.cssWideKeywords.join(' | '));
  131. }
  132. checkStructure(ast) {
  133. function collectWarning(node, message) {
  134. warns.push({ node, message });
  135. }
  136. const structure = this.structure;
  137. const warns = [];
  138. this.syntax.walk(ast, function(node) {
  139. if (structure.hasOwnProperty(node.type)) {
  140. structure[node.type].check(node, collectWarning);
  141. } else {
  142. collectWarning(node, 'Unknown node type `' + node.type + '`');
  143. }
  144. });
  145. return warns.length ? warns : false;
  146. }
  147. createDescriptor(syntax, type, name, parent = null) {
  148. const ref = {
  149. type,
  150. name
  151. };
  152. const descriptor = {
  153. type,
  154. name,
  155. parent,
  156. serializable: typeof syntax === 'string' || (syntax && typeof syntax.type === 'string'),
  157. syntax: null,
  158. match: null,
  159. matchRef: null // used for properties when a syntax referenced as <'property'> in other syntax definitions
  160. };
  161. if (typeof syntax === 'function') {
  162. descriptor.match = matchGraph.buildMatchGraph(syntax, ref);
  163. } else {
  164. if (typeof syntax === 'string') {
  165. // lazy parsing on first access
  166. Object.defineProperty(descriptor, 'syntax', {
  167. get() {
  168. Object.defineProperty(descriptor, 'syntax', {
  169. value: parse.parse(syntax)
  170. });
  171. return descriptor.syntax;
  172. }
  173. });
  174. } else {
  175. descriptor.syntax = syntax;
  176. }
  177. // lazy graph build on first access
  178. Object.defineProperty(descriptor, 'match', {
  179. get() {
  180. Object.defineProperty(descriptor, 'match', {
  181. value: matchGraph.buildMatchGraph(descriptor.syntax, ref)
  182. });
  183. return descriptor.match;
  184. }
  185. });
  186. if (type === 'Property') {
  187. Object.defineProperty(descriptor, 'matchRef', {
  188. get() {
  189. const syntax = descriptor.syntax;
  190. const value = syntaxHasTopLevelCommaMultiplier(syntax)
  191. ? matchGraph.buildMatchGraph({
  192. ...syntax,
  193. terms: [syntax.terms[0].term]
  194. }, ref)
  195. : null;
  196. Object.defineProperty(descriptor, 'matchRef', {
  197. value
  198. });
  199. return value;
  200. }
  201. });
  202. }
  203. }
  204. return descriptor;
  205. }
  206. addAtrule_(name, syntax) {
  207. if (!syntax) {
  208. return;
  209. }
  210. this.atrules[name] = {
  211. type: 'Atrule',
  212. name: name,
  213. prelude: syntax.prelude ? this.createDescriptor(syntax.prelude, 'AtrulePrelude', name) : null,
  214. descriptors: syntax.descriptors
  215. ? Object.keys(syntax.descriptors).reduce(
  216. (map, descName) => {
  217. map[descName] = this.createDescriptor(syntax.descriptors[descName], 'AtruleDescriptor', descName, name);
  218. return map;
  219. },
  220. Object.create(null)
  221. )
  222. : null
  223. };
  224. }
  225. addProperty_(name, syntax) {
  226. if (!syntax) {
  227. return;
  228. }
  229. this.properties[name] = this.createDescriptor(syntax, 'Property', name);
  230. }
  231. addType_(name, syntax) {
  232. if (!syntax) {
  233. return;
  234. }
  235. this.types[name] = this.createDescriptor(syntax, 'Type', name);
  236. }
  237. checkAtruleName(atruleName) {
  238. if (!this.getAtrule(atruleName)) {
  239. return new error.SyntaxReferenceError('Unknown at-rule', '@' + atruleName);
  240. }
  241. }
  242. checkAtrulePrelude(atruleName, prelude) {
  243. const error = this.checkAtruleName(atruleName);
  244. if (error) {
  245. return error;
  246. }
  247. const atrule = this.getAtrule(atruleName);
  248. if (!atrule.prelude && prelude) {
  249. return new SyntaxError('At-rule `@' + atruleName + '` should not contain a prelude');
  250. }
  251. if (atrule.prelude && !prelude) {
  252. if (!matchSyntax(this, atrule.prelude, '', false).matched) {
  253. return new SyntaxError('At-rule `@' + atruleName + '` should contain a prelude');
  254. }
  255. }
  256. }
  257. checkAtruleDescriptorName(atruleName, descriptorName) {
  258. const error$1 = this.checkAtruleName(atruleName);
  259. if (error$1) {
  260. return error$1;
  261. }
  262. const atrule = this.getAtrule(atruleName);
  263. const descriptor = names.keyword(descriptorName);
  264. if (!atrule.descriptors) {
  265. return new SyntaxError('At-rule `@' + atruleName + '` has no known descriptors');
  266. }
  267. if (!atrule.descriptors[descriptor.name] &&
  268. !atrule.descriptors[descriptor.basename]) {
  269. return new error.SyntaxReferenceError('Unknown at-rule descriptor', descriptorName);
  270. }
  271. }
  272. checkPropertyName(propertyName) {
  273. if (!this.getProperty(propertyName)) {
  274. return new error.SyntaxReferenceError('Unknown property', propertyName);
  275. }
  276. }
  277. matchAtrulePrelude(atruleName, prelude) {
  278. const error = this.checkAtrulePrelude(atruleName, prelude);
  279. if (error) {
  280. return buildMatchResult(null, error);
  281. }
  282. const atrule = this.getAtrule(atruleName);
  283. if (!atrule.prelude) {
  284. return buildMatchResult(null, null);
  285. }
  286. return matchSyntax(this, atrule.prelude, prelude || '', false);
  287. }
  288. matchAtruleDescriptor(atruleName, descriptorName, value) {
  289. const error = this.checkAtruleDescriptorName(atruleName, descriptorName);
  290. if (error) {
  291. return buildMatchResult(null, error);
  292. }
  293. const atrule = this.getAtrule(atruleName);
  294. const descriptor = names.keyword(descriptorName);
  295. return matchSyntax(this, atrule.descriptors[descriptor.name] || atrule.descriptors[descriptor.basename], value, false);
  296. }
  297. matchDeclaration(node) {
  298. if (node.type !== 'Declaration') {
  299. return buildMatchResult(null, new Error('Not a Declaration node'));
  300. }
  301. return this.matchProperty(node.property, node.value);
  302. }
  303. matchProperty(propertyName, value) {
  304. // don't match syntax for a custom property at the moment
  305. if (names.property(propertyName).custom) {
  306. return buildMatchResult(null, new Error('Lexer matching doesn\'t applicable for custom properties'));
  307. }
  308. const error = this.checkPropertyName(propertyName);
  309. if (error) {
  310. return buildMatchResult(null, error);
  311. }
  312. return matchSyntax(this, this.getProperty(propertyName), value, true);
  313. }
  314. matchType(typeName, value) {
  315. const typeSyntax = this.getType(typeName);
  316. if (!typeSyntax) {
  317. return buildMatchResult(null, new error.SyntaxReferenceError('Unknown type', typeName));
  318. }
  319. return matchSyntax(this, typeSyntax, value, false);
  320. }
  321. match(syntax, value) {
  322. if (typeof syntax !== 'string' && (!syntax || !syntax.type)) {
  323. return buildMatchResult(null, new error.SyntaxReferenceError('Bad syntax'));
  324. }
  325. if (typeof syntax === 'string' || !syntax.match) {
  326. syntax = this.createDescriptor(syntax, 'Type', 'anonymous');
  327. }
  328. return matchSyntax(this, syntax, value, false);
  329. }
  330. findValueFragments(propertyName, value, type, name) {
  331. return search.matchFragments(this, value, this.matchProperty(propertyName, value), type, name);
  332. }
  333. findDeclarationValueFragments(declaration, type, name) {
  334. return search.matchFragments(this, declaration.value, this.matchDeclaration(declaration), type, name);
  335. }
  336. findAllFragments(ast, type, name) {
  337. const result = [];
  338. this.syntax.walk(ast, {
  339. visit: 'Declaration',
  340. enter: (declaration) => {
  341. result.push.apply(result, this.findDeclarationValueFragments(declaration, type, name));
  342. }
  343. });
  344. return result;
  345. }
  346. getAtrule(atruleName, fallbackBasename = true) {
  347. const atrule = names.keyword(atruleName);
  348. const atruleEntry = atrule.vendor && fallbackBasename
  349. ? this.atrules[atrule.name] || this.atrules[atrule.basename]
  350. : this.atrules[atrule.name];
  351. return atruleEntry || null;
  352. }
  353. getAtrulePrelude(atruleName, fallbackBasename = true) {
  354. const atrule = this.getAtrule(atruleName, fallbackBasename);
  355. return atrule && atrule.prelude || null;
  356. }
  357. getAtruleDescriptor(atruleName, name) {
  358. return this.atrules.hasOwnProperty(atruleName) && this.atrules.declarators
  359. ? this.atrules[atruleName].declarators[name] || null
  360. : null;
  361. }
  362. getProperty(propertyName, fallbackBasename = true) {
  363. const property = names.property(propertyName);
  364. const propertyEntry = property.vendor && fallbackBasename
  365. ? this.properties[property.name] || this.properties[property.basename]
  366. : this.properties[property.name];
  367. return propertyEntry || null;
  368. }
  369. getType(name) {
  370. return hasOwnProperty.call(this.types, name) ? this.types[name] : null;
  371. }
  372. validate() {
  373. function syntaxRef(name, isType) {
  374. return isType ? `<${name}>` : `<'${name}'>`;
  375. }
  376. function validate(syntax, name, broken, descriptor) {
  377. if (broken.has(name)) {
  378. return broken.get(name);
  379. }
  380. broken.set(name, false);
  381. if (descriptor.syntax !== null) {
  382. walk.walk(descriptor.syntax, function(node) {
  383. if (node.type !== 'Type' && node.type !== 'Property') {
  384. return;
  385. }
  386. const map = node.type === 'Type' ? syntax.types : syntax.properties;
  387. const brokenMap = node.type === 'Type' ? brokenTypes : brokenProperties;
  388. if (!hasOwnProperty.call(map, node.name)) {
  389. errors.push(`${syntaxRef(name, broken === brokenTypes)} used missed syntax definition ${syntaxRef(node.name, node.type === 'Type')}`);
  390. broken.set(name, true);
  391. } else if (validate(syntax, node.name, brokenMap, map[node.name])) {
  392. errors.push(`${syntaxRef(name, broken === brokenTypes)} used broken syntax definition ${syntaxRef(node.name, node.type === 'Type')}`);
  393. broken.set(name, true);
  394. }
  395. }, this);
  396. }
  397. }
  398. const errors = [];
  399. let brokenTypes = new Map();
  400. let brokenProperties = new Map();
  401. for (const key in this.types) {
  402. validate(this, key, brokenTypes, this.types[key]);
  403. }
  404. for (const key in this.properties) {
  405. validate(this, key, brokenProperties, this.properties[key]);
  406. }
  407. const brokenTypesArray = [...brokenTypes.keys()].filter(name => brokenTypes.get(name));
  408. const brokenPropertiesArray = [...brokenProperties.keys()].filter(name => brokenProperties.get(name));
  409. if (brokenTypesArray.length || brokenPropertiesArray.length) {
  410. return {
  411. errors,
  412. types: brokenTypesArray,
  413. properties: brokenPropertiesArray
  414. };
  415. }
  416. return null;
  417. }
  418. dump(syntaxAsAst, pretty) {
  419. return {
  420. generic: this.generic,
  421. cssWideKeywords: this.cssWideKeywords,
  422. units: this.units,
  423. types: dumpMapSyntax(this.types, !pretty, syntaxAsAst),
  424. properties: dumpMapSyntax(this.properties, !pretty, syntaxAsAst),
  425. atrules: dumpAtruleMapSyntax(this.atrules, !pretty, syntaxAsAst)
  426. };
  427. }
  428. toString() {
  429. return JSON.stringify(this.dump());
  430. }
  431. }
  432. exports.Lexer = Lexer;