Lexer.js 9.8 KB

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