Lexer.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. 'use strict';
  2. var SyntaxReferenceError = require('./error').SyntaxReferenceError;
  3. var MatchError = require('./error').MatchError;
  4. var names = require('../utils/names');
  5. var generic = require('./generic');
  6. var parse = require('./grammar/parse');
  7. var generate = require('./grammar/generate');
  8. var walk = require('./grammar/walk');
  9. var astToTokens = require('./ast-to-tokens');
  10. var buildMatchGraph = require('./match-graph').buildMatchGraph;
  11. var matchAsTree = require('./match').matchAsTree;
  12. var trace = require('./trace');
  13. var search = require('./search');
  14. var getStructureFromConfig = require('./structure').getStructureFromConfig;
  15. var cssWideKeywords = buildMatchGraph(parse('inherit | initial | unset'));
  16. var cssWideKeywordsWithExpression = buildMatchGraph(parse('inherit | initial | unset | <expression>'));
  17. function dumpMapSyntax(map, syntaxAsAst) {
  18. var result = {};
  19. for (var name in map) {
  20. if (map[name].syntax) {
  21. result[name] = syntaxAsAst ? map[name].syntax : generate(map[name].syntax);
  22. }
  23. }
  24. return result;
  25. }
  26. function valueHasVar(value) {
  27. var hasVar = false;
  28. this.syntax.walk(value, function(node) {
  29. if (node.type === 'Function' && node.name.toLowerCase() === 'var') {
  30. hasVar = true;
  31. }
  32. });
  33. return hasVar;
  34. }
  35. function buildMatchResult(match, error, iterations) {
  36. return {
  37. matched: match,
  38. iterations: iterations,
  39. error: error,
  40. getTrace: trace.getTrace,
  41. isType: trace.isType,
  42. isProperty: trace.isProperty,
  43. isKeyword: trace.isKeyword
  44. };
  45. }
  46. function matchSyntax(lexer, syntax, node, useCommon) {
  47. if (!node) {
  48. return buildMatchResult(null, new Error('Node is undefined'));
  49. }
  50. if (valueHasVar.call(lexer, node)) {
  51. return buildMatchResult(null, new Error('Matching for a tree with var() is not supported'));
  52. }
  53. var tokens = lexer.syntax.generate(node, astToTokens);
  54. var result;
  55. if (useCommon) {
  56. result = matchAsTree(tokens, lexer.valueCommonSyntax, lexer);
  57. }
  58. if (!useCommon || !result.match) {
  59. result = matchAsTree(tokens, syntax.match, lexer);
  60. if (!result.match) {
  61. return buildMatchResult(
  62. null,
  63. new MatchError(result.reason, lexer, syntax.syntax, node, result),
  64. result.iterations
  65. );
  66. }
  67. }
  68. return buildMatchResult(result.match, null, result.iterations);
  69. }
  70. var Lexer = function(config, syntax, structure) {
  71. this.valueCommonSyntax = cssWideKeywords;
  72. this.syntax = syntax;
  73. this.generic = false;
  74. this.properties = {};
  75. this.types = {};
  76. this.structure = structure || getStructureFromConfig(config);
  77. if (config) {
  78. if (config.generic) {
  79. this.generic = true;
  80. for (var name in generic) {
  81. this.addType_(name, generic[name]);
  82. }
  83. }
  84. if (config.types) {
  85. for (var name in config.types) {
  86. this.addType_(name, config.types[name]);
  87. }
  88. }
  89. if (config.properties) {
  90. for (var name in config.properties) {
  91. this.addProperty_(name, config.properties[name]);
  92. }
  93. }
  94. }
  95. };
  96. Lexer.prototype = {
  97. structure: {},
  98. checkStructure: function(ast) {
  99. function collectWarning(node, message) {
  100. warns.push({
  101. node: node,
  102. message: message
  103. });
  104. }
  105. var structure = this.structure;
  106. var warns = [];
  107. this.syntax.walk(ast, function(node) {
  108. if (structure.hasOwnProperty(node.type)) {
  109. structure[node.type].check(node, collectWarning);
  110. } else {
  111. collectWarning(node, 'Unknown node type `' + node.type + '`');
  112. }
  113. });
  114. return warns.length ? warns : false;
  115. },
  116. createDescriptor: function(syntax, type, name) {
  117. var ref = {
  118. type: type,
  119. name: name
  120. };
  121. var descriptor = {
  122. type: type,
  123. name: name,
  124. syntax: null,
  125. match: null
  126. };
  127. if (typeof syntax === 'function') {
  128. descriptor.match = buildMatchGraph(syntax, ref);
  129. } else {
  130. if (typeof syntax === 'string') {
  131. // lazy parsing on first access
  132. Object.defineProperty(descriptor, 'syntax', {
  133. get: function() {
  134. Object.defineProperty(descriptor, 'syntax', {
  135. value: parse(syntax)
  136. });
  137. return descriptor.syntax;
  138. }
  139. });
  140. } else {
  141. descriptor.syntax = syntax;
  142. }
  143. Object.defineProperty(descriptor, 'match', {
  144. get: function() {
  145. Object.defineProperty(descriptor, 'match', {
  146. value: buildMatchGraph(descriptor.syntax, ref)
  147. });
  148. return descriptor.match;
  149. }
  150. });
  151. }
  152. return descriptor;
  153. },
  154. addProperty_: function(name, syntax) {
  155. this.properties[name] = this.createDescriptor(syntax, 'Property', name);
  156. },
  157. addType_: function(name, syntax) {
  158. this.types[name] = this.createDescriptor(syntax, 'Type', name);
  159. if (syntax === generic.expression) {
  160. this.valueCommonSyntax = cssWideKeywordsWithExpression;
  161. }
  162. },
  163. matchDeclaration: function(node) {
  164. if (node.type !== 'Declaration') {
  165. return buildMatchResult(null, new Error('Not a Declaration node'));
  166. }
  167. return this.matchProperty(node.property, node.value);
  168. },
  169. matchProperty: function(propertyName, value) {
  170. var property = names.property(propertyName);
  171. // don't match syntax for a custom property
  172. if (property.custom) {
  173. return buildMatchResult(null, new Error('Lexer matching doesn\'t applicable for custom properties'));
  174. }
  175. var propertySyntax = property.vendor
  176. ? this.getProperty(property.name) || this.getProperty(property.basename)
  177. : this.getProperty(property.name);
  178. if (!propertySyntax) {
  179. return buildMatchResult(null, new SyntaxReferenceError('Unknown property', propertyName));
  180. }
  181. return matchSyntax(this, propertySyntax, value, true);
  182. },
  183. matchType: function(typeName, value) {
  184. var typeSyntax = this.getType(typeName);
  185. if (!typeSyntax) {
  186. return buildMatchResult(null, new SyntaxReferenceError('Unknown type', typeName));
  187. }
  188. return matchSyntax(this, typeSyntax, value, false);
  189. },
  190. match: function(syntax, value) {
  191. if (!syntax || !syntax.type) {
  192. return buildMatchResult(null, new SyntaxReferenceError('Bad syntax'));
  193. }
  194. if (!syntax.match) {
  195. syntax = this.createDescriptor(syntax);
  196. }
  197. return matchSyntax(this, syntax, value, false);
  198. },
  199. findValueFragments: function(propertyName, value, type, name) {
  200. return search.matchFragments(this, value, this.matchProperty(propertyName, value), type, name);
  201. },
  202. findDeclarationValueFragments: function(declaration, type, name) {
  203. return search.matchFragments(this, declaration.value, this.matchDeclaration(declaration), type, name);
  204. },
  205. findAllFragments: function(ast, type, name) {
  206. var result = [];
  207. this.syntax.walk(ast, {
  208. visit: 'Declaration',
  209. enter: function(declaration) {
  210. result.push.apply(result, this.findDeclarationValueFragments(declaration, type, name));
  211. }.bind(this)
  212. });
  213. return result;
  214. },
  215. getProperty: function(name) {
  216. return this.properties.hasOwnProperty(name) ? this.properties[name] : null;
  217. },
  218. getType: function(name) {
  219. return this.types.hasOwnProperty(name) ? this.types[name] : null;
  220. },
  221. validate: function() {
  222. function validate(syntax, name, broken, descriptor) {
  223. if (broken.hasOwnProperty(name)) {
  224. return broken[name];
  225. }
  226. broken[name] = false;
  227. if (descriptor.syntax !== null) {
  228. walk(descriptor.syntax, function(node) {
  229. if (node.type !== 'Type' && node.type !== 'Property') {
  230. return;
  231. }
  232. var map = node.type === 'Type' ? syntax.types : syntax.properties;
  233. var brokenMap = node.type === 'Type' ? brokenTypes : brokenProperties;
  234. if (!map.hasOwnProperty(node.name) || validate(syntax, node.name, brokenMap, map[node.name])) {
  235. broken[name] = true;
  236. }
  237. }, this);
  238. }
  239. }
  240. var brokenTypes = {};
  241. var brokenProperties = {};
  242. for (var key in this.types) {
  243. validate(this, key, brokenTypes, this.types[key]);
  244. }
  245. for (var key in this.properties) {
  246. validate(this, key, brokenProperties, this.properties[key]);
  247. }
  248. brokenTypes = Object.keys(brokenTypes).filter(function(name) {
  249. return brokenTypes[name];
  250. });
  251. brokenProperties = Object.keys(brokenProperties).filter(function(name) {
  252. return brokenProperties[name];
  253. });
  254. if (brokenTypes.length || brokenProperties.length) {
  255. return {
  256. types: brokenTypes,
  257. properties: brokenProperties
  258. };
  259. }
  260. return null;
  261. },
  262. dump: function(syntaxAsAst) {
  263. return {
  264. generic: this.generic,
  265. types: dumpMapSyntax(this.types, syntaxAsAst),
  266. properties: dumpMapSyntax(this.properties, syntaxAsAst)
  267. };
  268. },
  269. toString: function() {
  270. return JSON.stringify(this.dump());
  271. }
  272. };
  273. module.exports = Lexer;