Lexer.js 15 KB

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