create.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. var List = require('../common/List');
  2. var SyntaxError = require('../common/SyntaxError');
  3. var TokenStream = require('../common/TokenStream');
  4. var Lexer = require('../lexer/Lexer');
  5. var definitionSyntax = require('../definition-syntax');
  6. var tokenize = require('../tokenizer');
  7. var createParser = require('../parser/create');
  8. var createGenerator = require('../generator/create');
  9. var createConvertor = require('../convertor/create');
  10. var createWalker = require('../walker/create');
  11. var clone = require('../utils/clone');
  12. var names = require('../utils/names');
  13. var mix = require('./config/mix');
  14. function assign(dest, src) {
  15. for (var key in src) {
  16. dest[key] = src[key];
  17. }
  18. return dest;
  19. }
  20. function createSyntax(config) {
  21. var parse = createParser(config);
  22. var walk = createWalker(config);
  23. var generate = createGenerator(config);
  24. var convert = createConvertor(walk);
  25. var syntax = {
  26. List: List,
  27. SyntaxError: SyntaxError,
  28. TokenStream: TokenStream,
  29. Lexer: Lexer,
  30. vendorPrefix: names.vendorPrefix,
  31. keyword: names.keyword,
  32. property: names.property,
  33. isCustomProperty: names.isCustomProperty,
  34. definitionSyntax: definitionSyntax,
  35. lexer: null,
  36. createLexer: function(config) {
  37. return new Lexer(config, syntax, syntax.lexer.structure);
  38. },
  39. tokenize: tokenize,
  40. parse: parse,
  41. walk: walk,
  42. generate: generate,
  43. find: walk.find,
  44. findLast: walk.findLast,
  45. findAll: walk.findAll,
  46. clone: clone,
  47. fromPlainObject: convert.fromPlainObject,
  48. toPlainObject: convert.toPlainObject,
  49. createSyntax: function(config) {
  50. return createSyntax(mix({}, config));
  51. },
  52. fork: function(extension) {
  53. var base = mix({}, config); // copy of config
  54. return createSyntax(
  55. typeof extension === 'function'
  56. ? extension(base, assign)
  57. : mix(base, extension)
  58. );
  59. }
  60. };
  61. syntax.lexer = new Lexer({
  62. generic: true,
  63. types: config.types,
  64. properties: config.properties,
  65. node: config.node
  66. }, syntax);
  67. return syntax;
  68. };
  69. exports.create = function(config) {
  70. return createSyntax(mix({}, config));
  71. };