ether-code 0.1.7 → 0.1.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cli/compiler.js +9 -53
- package/cli/ether.js +1 -1
- package/package.json +1 -1
package/cli/compiler.js
CHANGED
|
@@ -2,6 +2,7 @@ const fs = require('fs')
|
|
|
2
2
|
const path = require('path')
|
|
3
3
|
|
|
4
4
|
const { EtherLexer } = require('../lexer/ether-lexer')
|
|
5
|
+
const { EtherParser } = require('../ether-parser')
|
|
5
6
|
|
|
6
7
|
const { CSSGenerator } = require('../generators/css-generator')
|
|
7
8
|
const { HTMLGenerator } = require('../generators/html-generator')
|
|
@@ -67,9 +68,10 @@ class EtherCompiler {
|
|
|
67
68
|
|
|
68
69
|
this.lexer = new EtherLexer()
|
|
69
70
|
this.generators = {}
|
|
70
|
-
this.
|
|
71
|
+
this.parser = null
|
|
71
72
|
|
|
72
73
|
this.initGenerators()
|
|
74
|
+
this.initParser()
|
|
73
75
|
}
|
|
74
76
|
|
|
75
77
|
initGenerators() {
|
|
@@ -86,6 +88,11 @@ class EtherCompiler {
|
|
|
86
88
|
}
|
|
87
89
|
}
|
|
88
90
|
|
|
91
|
+
initParser() {
|
|
92
|
+
const i18nDir = path.join(__dirname, '..', 'i18n')
|
|
93
|
+
this.parser = new EtherParser(i18nDir)
|
|
94
|
+
}
|
|
95
|
+
|
|
89
96
|
normalizeTarget(target) {
|
|
90
97
|
const mappings = {
|
|
91
98
|
javascript: 'js',
|
|
@@ -127,58 +134,7 @@ class EtherCompiler {
|
|
|
127
134
|
|
|
128
135
|
parse(content, target) {
|
|
129
136
|
const normalizedTarget = this.normalizeTarget(target)
|
|
130
|
-
|
|
131
|
-
if (normalizedTarget === 'html') {
|
|
132
|
-
return this.parseHTML(content)
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
const lexer = new EtherLexer(content)
|
|
136
|
-
const tokens = lexer.tokenize()
|
|
137
|
-
|
|
138
|
-
try {
|
|
139
|
-
const ParserClass = this.loadParser(normalizedTarget)
|
|
140
|
-
if (ParserClass) {
|
|
141
|
-
const parser = new ParserClass(tokens, this.config)
|
|
142
|
-
return parser.parse()
|
|
143
|
-
}
|
|
144
|
-
} catch (err) {
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
return this.buildGenericAST(tokens, target)
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
parseHTML(content) {
|
|
151
|
-
const { HTMLParser } = require('../parsers/html-parser')
|
|
152
|
-
const i18nPath = path.join(__dirname, '..', 'i18n', 'i18n-html.json')
|
|
153
|
-
const parser = new HTMLParser(i18nPath)
|
|
154
|
-
return parser.parse(content)
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
loadParser(target) {
|
|
158
|
-
try {
|
|
159
|
-
const parserPath = path.join(__dirname, '..', 'parsers', `${target}-parser.js`)
|
|
160
|
-
if (fs.existsSync(parserPath)) {
|
|
161
|
-
const module = require(parserPath)
|
|
162
|
-
const className = `${target.charAt(0).toUpperCase()}${target.slice(1)}Parser`
|
|
163
|
-
return module[className] || module.default || Object.values(module)[0]
|
|
164
|
-
}
|
|
165
|
-
} catch (err) {
|
|
166
|
-
}
|
|
167
|
-
return null
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
buildGenericAST(tokens, target) {
|
|
171
|
-
return {
|
|
172
|
-
type: 'Program',
|
|
173
|
-
target,
|
|
174
|
-
body: tokens.map(token => ({
|
|
175
|
-
type: 'Token',
|
|
176
|
-
tokenType: token.type,
|
|
177
|
-
value: token.value,
|
|
178
|
-
line: token.line,
|
|
179
|
-
column: token.column
|
|
180
|
-
}))
|
|
181
|
-
}
|
|
137
|
+
return this.parser.parse(content, normalizedTarget)
|
|
182
138
|
}
|
|
183
139
|
|
|
184
140
|
generate(ast, target) {
|
package/cli/ether.js
CHANGED