Lexer.js 11 KB

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