ether-code 0.1.8 → 0.2.0
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/LICENSE +1 -1
- package/README.md +27 -89
- package/cli/compiler.js +11 -25
- package/cli/ether.js +21 -187
- package/ether-compiler.js +190 -0
- package/ether-parser.js +3059 -0
- package/package.json +3 -2
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
const fs = require('fs')
|
|
2
|
+
const path = require('path')
|
|
3
|
+
const { EtherLexer, TokenType } = require('./lexer/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
|
+
}
|