ether-code 0.1.7 → 0.1.9
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/ether-compiler.js +190 -0
- package/ether-parser.js +3059 -0
- package/package.json +3 -2
- package/cli/ether.js +0 -700
- package/cli/watcher.js +0 -106
- package/i18n/i18n-css.json +0 -743
- package/i18n/i18n-graphql.json +0 -1531
- package/i18n/i18n-html.json +0 -572
- package/i18n/i18n-js.json +0 -2790
- package/i18n/i18n-node.json +0 -2442
- package/i18n/i18n-php.json +0 -4306
- package/i18n/i18n-python.json +0 -3080
- package/i18n/i18n-react.json +0 -1784
- package/i18n/i18n-ruby.json +0 -1858
- package/i18n/i18n-sql.json +0 -3466
- package/i18n/i18n-ts.json +0 -442
- package/lexer/ether-lexer.js +0 -869
- package/lexer/tokens.js +0 -292
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) {
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
const fs = require('fs')
|
|
2
|
+
const path = require('path')
|
|
3
|
+
const { EtherLexer, TokenType } = require('./ether-lexer')
|
|
4
|
+
const { EtherParser } = require('./ether-parser')
|
|
5
|
+
const { CSSGenerator } = require('./generators/css-generator')
|
|
6
|
+
const { HTMLGenerator } = require('./generators/html-generator')
|
|
7
|
+
const { JSGenerator } = require('./generators/js-generator')
|
|
8
|
+
const { TSGenerator } = require('./generators/ts-generator')
|
|
9
|
+
const { PHPGenerator } = require('./generators/php-generator')
|
|
10
|
+
const { PythonGenerator } = require('./generators/python-generator')
|
|
11
|
+
const { RubyGenerator } = require('./generators/ruby-generator')
|
|
12
|
+
const { SQLGenerator } = require('./generators/sql-generator')
|
|
13
|
+
const { NodeGenerator } = require('./generators/node-generator')
|
|
14
|
+
const { ReactGenerator } = require('./generators/react-generator')
|
|
15
|
+
const { GraphQLGenerator } = require('./generators/graphql-generator')
|
|
16
|
+
|
|
17
|
+
class EtherCompiler {
|
|
18
|
+
constructor(options = {}) {
|
|
19
|
+
this.i18nDir = options.i18nDir || path.join(__dirname, 'i18n')
|
|
20
|
+
this.parser = new EtherParser({ i18nDir: this.i18nDir })
|
|
21
|
+
this.generators = {}
|
|
22
|
+
this.initGenerators()
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
initGenerators() {
|
|
26
|
+
const generatorConfigs = [
|
|
27
|
+
{ name: 'css', Generator: CSSGenerator, i18n: 'i18n-css.json' },
|
|
28
|
+
{ name: 'html', Generator: HTMLGenerator, i18n: 'i18n-html.json' },
|
|
29
|
+
{ name: 'js', Generator: JSGenerator, i18n: 'i18n-js.json' },
|
|
30
|
+
{ name: 'ts', Generator: TSGenerator, i18n: 'i18n-ts.json' },
|
|
31
|
+
{ name: 'php', Generator: PHPGenerator, i18n: 'i18n-php.json' },
|
|
32
|
+
{ name: 'python', Generator: PythonGenerator, i18n: 'i18n-python.json' },
|
|
33
|
+
{ name: 'ruby', Generator: RubyGenerator, i18n: 'i18n-ruby.json' },
|
|
34
|
+
{ name: 'sql', Generator: SQLGenerator, i18n: 'i18n-sql.json' },
|
|
35
|
+
{ name: 'node', Generator: NodeGenerator, i18n: 'i18n-node.json' },
|
|
36
|
+
{ name: 'react', Generator: ReactGenerator, i18n: 'i18n-react.json' },
|
|
37
|
+
{ name: 'graphql', Generator: GraphQLGenerator, i18n: 'i18n-graphql.json' }
|
|
38
|
+
]
|
|
39
|
+
|
|
40
|
+
for (const config of generatorConfigs) {
|
|
41
|
+
try {
|
|
42
|
+
const i18nPath = path.join(this.i18nDir, config.i18n)
|
|
43
|
+
if (fs.existsSync(i18nPath)) {
|
|
44
|
+
this.generators[config.name] = new config.Generator(i18nPath)
|
|
45
|
+
} else {
|
|
46
|
+
this.generators[config.name] = new config.Generator()
|
|
47
|
+
}
|
|
48
|
+
} catch (e) {
|
|
49
|
+
this.generators[config.name] = null
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
this.generators.javascript = this.generators.js
|
|
54
|
+
this.generators.typescript = this.generators.ts
|
|
55
|
+
this.generators.py = this.generators.python
|
|
56
|
+
this.generators.rb = this.generators.ruby
|
|
57
|
+
this.generators.nodejs = this.generators.node
|
|
58
|
+
this.generators.jsx = this.generators.react
|
|
59
|
+
this.generators.gql = this.generators.graphql
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
compile(source, targetLang = 'auto') {
|
|
63
|
+
const ast = this.parser.parse(source, targetLang)
|
|
64
|
+
const lang = this.detectLanguage(ast, targetLang, source)
|
|
65
|
+
const generator = this.generators[lang]
|
|
66
|
+
|
|
67
|
+
if (!generator) {
|
|
68
|
+
throw new Error(`Générateur non disponible pour: ${lang}`)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return generator.generate(ast)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
detectLanguage(ast, targetLang, source) {
|
|
75
|
+
if (targetLang && targetLang !== 'auto') {
|
|
76
|
+
return targetLang.toLowerCase()
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (ast.type === 'StyleSheet') return 'css'
|
|
80
|
+
if (ast.type === 'Document') return 'html'
|
|
81
|
+
if (ast.type === 'SQLProgram') return 'sql'
|
|
82
|
+
if (ast.type === 'GraphQLProgram') return 'graphql'
|
|
83
|
+
if (ast.lang) return ast.lang
|
|
84
|
+
|
|
85
|
+
return this.parser.detectTargetLanguage(source)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
compileFile(inputPath, outputPath = null, targetLang = 'auto') {
|
|
89
|
+
const source = fs.readFileSync(inputPath, 'utf-8')
|
|
90
|
+
const result = this.compile(source, targetLang)
|
|
91
|
+
|
|
92
|
+
if (outputPath) {
|
|
93
|
+
const dir = path.dirname(outputPath)
|
|
94
|
+
if (!fs.existsSync(dir)) {
|
|
95
|
+
fs.mkdirSync(dir, { recursive: true })
|
|
96
|
+
}
|
|
97
|
+
fs.writeFileSync(outputPath, result)
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return result
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
getExtension(lang) {
|
|
104
|
+
const extensions = {
|
|
105
|
+
css: '.css',
|
|
106
|
+
html: '.html',
|
|
107
|
+
js: '.js',
|
|
108
|
+
javascript: '.js',
|
|
109
|
+
ts: '.ts',
|
|
110
|
+
typescript: '.ts',
|
|
111
|
+
php: '.php',
|
|
112
|
+
python: '.py',
|
|
113
|
+
py: '.py',
|
|
114
|
+
ruby: '.rb',
|
|
115
|
+
rb: '.rb',
|
|
116
|
+
sql: '.sql',
|
|
117
|
+
node: '.js',
|
|
118
|
+
nodejs: '.js',
|
|
119
|
+
react: '.jsx',
|
|
120
|
+
jsx: '.jsx',
|
|
121
|
+
graphql: '.graphql',
|
|
122
|
+
gql: '.graphql'
|
|
123
|
+
}
|
|
124
|
+
return extensions[lang] || '.txt'
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
parse(source, targetLang = 'auto') {
|
|
128
|
+
return this.parser.parse(source, targetLang)
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
generate(ast, lang) {
|
|
132
|
+
const generator = this.generators[lang]
|
|
133
|
+
if (!generator) {
|
|
134
|
+
throw new Error(`Générateur non disponible pour: ${lang}`)
|
|
135
|
+
}
|
|
136
|
+
return generator.generate(ast)
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
tokenize(source) {
|
|
140
|
+
const lexer = new EtherLexer(source)
|
|
141
|
+
return lexer.tokenize()
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
inferTargetFromFile(filePath) {
|
|
145
|
+
const ext = path.extname(filePath).toLowerCase()
|
|
146
|
+
const extMap = {
|
|
147
|
+
'.eth': 'auto',
|
|
148
|
+
'.ether': 'auto',
|
|
149
|
+
'.ecss': 'css',
|
|
150
|
+
'.ehtml': 'html',
|
|
151
|
+
'.ejs': 'js',
|
|
152
|
+
'.ets': 'ts',
|
|
153
|
+
'.ephp': 'php',
|
|
154
|
+
'.epy': 'python',
|
|
155
|
+
'.erb': 'ruby',
|
|
156
|
+
'.esql': 'sql',
|
|
157
|
+
'.egql': 'graphql',
|
|
158
|
+
'.ejsx': 'react'
|
|
159
|
+
}
|
|
160
|
+
return extMap[ext] || 'auto'
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function compile(source, targetLang = 'auto', options = {}) {
|
|
165
|
+
const compiler = new EtherCompiler(options)
|
|
166
|
+
return compiler.compile(source, targetLang)
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function compileFile(inputPath, outputPath = null, targetLang = 'auto', options = {}) {
|
|
170
|
+
const compiler = new EtherCompiler(options)
|
|
171
|
+
return compiler.compileFile(inputPath, outputPath, targetLang)
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function parse(source, targetLang = 'auto', options = {}) {
|
|
175
|
+
const compiler = new EtherCompiler(options)
|
|
176
|
+
return compiler.parse(source, targetLang)
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function tokenize(source, options = {}) {
|
|
180
|
+
const lexer = new EtherLexer(source, options)
|
|
181
|
+
return lexer.tokenize()
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
module.exports = {
|
|
185
|
+
EtherCompiler,
|
|
186
|
+
compile,
|
|
187
|
+
compileFile,
|
|
188
|
+
parse,
|
|
189
|
+
tokenize
|
|
190
|
+
}
|