1 |
|
2 | (function() {
|
3 | var CONFIG_NAME, Scope, compile, config, convOptions, defaults, escodegen, esprima, findup, fs, kit, path, root, transform, util;
|
4 |
|
5 | util = require("util");
|
6 |
|
7 | esprima = require("esprima");
|
8 |
|
9 | escodegen = require("escodegen");
|
10 |
|
11 | config = require("./config");
|
12 |
|
13 | kit = require("./kit");
|
14 |
|
15 | Scope = require("./scope").Scope;
|
16 |
|
17 | require("./stmt");
|
18 |
|
19 | require("./par");
|
20 |
|
21 | fs = require("fs");
|
22 |
|
23 | root = module.exports;
|
24 |
|
25 | findup = require("findup").sync;
|
26 |
|
27 | path = require("path");
|
28 |
|
29 | defaults = {
|
30 | loc: true,
|
31 | range: true,
|
32 | token: true,
|
33 | comment: true
|
34 | };
|
35 |
|
36 | root.FILE_EXTENSIONS = {
|
37 | ".mjs": {
|
38 | profile: "minimal"
|
39 | },
|
40 | ".json": false
|
41 | };
|
42 |
|
43 | defaults = {
|
44 | parser: {
|
45 | loc: true,
|
46 | range: true,
|
47 | token: true,
|
48 | comment: true
|
49 | },
|
50 | transform: {}
|
51 | };
|
52 |
|
53 | CONFIG_NAME = "mfjs-config.json";
|
54 |
|
55 | convOptions = function(v, fn) {
|
56 | var e, error, f;
|
57 | if (v == null) {
|
58 | v = {};
|
59 | }
|
60 | if (v === true) {
|
61 | v = {};
|
62 | }
|
63 | if (v.substr != null) {
|
64 | v = {
|
65 | profile: v
|
66 | };
|
67 | }
|
68 | v = kit.merge({}, defaults, v);
|
69 | if (fn == null) {
|
70 | fn = v.transform.filename;
|
71 | }
|
72 | if (fn != null) {
|
73 | v.parser.source = v.transform.filename = fn;
|
74 | try {
|
75 | f = findup(path.dirname(fn), CONFIG_NAME);
|
76 | if (config.versbose) {
|
77 | console.log('using config #{f}');
|
78 | }
|
79 | } catch (error) {
|
80 | e = error;
|
81 | if (config.verbose) {
|
82 | console.log('looking for #{CONFIG_NAME} failed: #{e}');
|
83 | }
|
84 | }
|
85 | if (f != null) {
|
86 | kit.merge(v, require(path.join(f, CONFIG_NAME)));
|
87 | }
|
88 | }
|
89 | return v;
|
90 | };
|
91 |
|
92 | root.compile = compile = function(str, opts, fn) {
|
93 | var ast, nast;
|
94 | opts = convOptions(opts, fn);
|
95 | ast = esprima.parse(str, opts.parser);
|
96 | nast = transform(ast, opts);
|
97 | return escodegen.generate(nast, opts.printer);
|
98 | };
|
99 |
|
100 | root.compileFile = function(fn, opts) {
|
101 | var txt;
|
102 | txt = fs.readFileSync(fn, "utf8");
|
103 | opts = convOptions(opts, fn);
|
104 | if (opts.parse == null) {
|
105 | opts.parse = {};
|
106 | }
|
107 | return compile(txt, opts);
|
108 | };
|
109 |
|
110 | root.parse = function(str, opts) {
|
111 | return esprima.parse(str, opts);
|
112 | };
|
113 |
|
114 | root.print = function(str) {
|
115 | var ast;
|
116 | ast = root.parse(str);
|
117 | return this.pretty(ast);
|
118 | };
|
119 |
|
120 | root.pretty = function(ast, opts) {
|
121 | return escodegen.generate(ast, convOptions(opts).printer);
|
122 | };
|
123 |
|
124 | root.transform = transform = function(ast, opts) {
|
125 | var ctx, i, oldConfig, res;
|
126 | oldConfig = kit.merge({}, config);
|
127 | if (opts == null) {
|
128 | opts = {};
|
129 | }
|
130 | try {
|
131 | if (opts.transform != null) {
|
132 | kit.merge(config, opts.transform);
|
133 | }
|
134 | ctx = new Scope();
|
135 | res = ctx.prog(ast, opts);
|
136 | res.type = "Program";
|
137 | return res;
|
138 | } finally {
|
139 | for (i in config) {
|
140 | delete config[i];
|
141 | }
|
142 | kit.merge(config, oldConfig);
|
143 | }
|
144 | };
|
145 |
|
146 | }).call(this);
|