You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

322 lines
9.9 KiB

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