-
Notifications
You must be signed in to change notification settings - Fork 20
/
language.go
254 lines (221 loc) · 6.83 KB
/
language.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*
* ZEUS - An Electrifying Build System
* Copyright (c) 2017 Philipp Mieden <dreadl0ck [at] protonmail [dot] ch>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"errors"
"sync"
)
var (
commandChainSeparator = "->"
// global language store
ls = &languageStore{
items: map[string]*Language{
"bash": bashLanguage(),
"python": pythonLanguage(),
"javascript": javaScriptLanguage(),
"ruby": rubyLanguage(),
"lua": luaLanguage(),
"sh": shellLanguage(),
"zsh": zshellLanguage(),
"perl": perlLanguage(),
"go": goLanguage(),
},
}
// ErrUnknownLanguage means there's no syntax definition for the desired language
ErrUnknownLanguage = errors.New("unknown language")
)
// thread safe store for all languages
type languageStore struct {
items map[string]*Language
sync.Mutex
}
func (langStore *languageStore) getLang(name string) (*Language, error) {
langStore.Lock()
defer langStore.Unlock()
if lang, ok := langStore.items[name]; ok {
// return language instance
return lang, nil
}
return nil, ErrUnknownLanguage
}
// Language describes interpreter and syntactic elements of a scripting language
type Language struct {
Name string `yaml:"name"`
// extra arguments
Args []string `yaml:"args"`
// path to Interpreter
Interpreter string `yaml:"interpreter"`
// identifier for script type
Bang string `yaml:"bang"`
// Comment identifier
Comment string `yaml:"comment"`
// prefix when declaring variables i.e. 'var' keyword
VariableKeyword string `yaml:"variableKeyword"`
// assigment operator i.e. '='
AssignmentOperator string `yaml:"assignmentOperator"`
// line delimiter i.e. ';'
LineDelimiter string `yaml:"lineDelimiter"`
// flag for stopping execution after an error
FlagStopOnError string `yaml:"flagStopOnError"`
// flag for passing a script on the commandline
FlagEvaluateScript string `yaml:"flagEvaluateScript"`
// some interpreters (i.e. osascript) don't allow passing a multiline script for evaluation on the commandline
// in this case a temporary script is generated on disk and passed to the interpreter for execution
UseTempFile bool `yaml:"useTempFile"`
ExecOpPrefix string `yaml:"execOpPrefix"`
ExecOpSuffix string `yaml:"execOpSuffix"`
// extension for filetype
FileExtension string `yaml:"fileExtension"`
CorrectErrLineNumber bool `yaml:"correctErrLineNumber"`
ErrLineNumberSymbol string `yaml:"errLineNumberSymbol"`
}
func bashLanguage() *Language {
return &Language{
Name: "bash",
Interpreter: "/bin/bash",
Bang: "#!/bin/bash",
Comment: "#",
AssignmentOperator: "=",
FlagStopOnError: "-e",
FlagEvaluateScript: "-c",
FileExtension: ".sh",
CorrectErrLineNumber: false,
ErrLineNumberSymbol: "line",
}
}
func shellLanguage() *Language {
return &Language{
Name: "sh",
Interpreter: "/bin/sh",
Bang: "#!/bin/sh",
Comment: "#",
AssignmentOperator: "=",
FlagStopOnError: "-e",
FlagEvaluateScript: "-c",
FileExtension: ".sh",
CorrectErrLineNumber: false,
ErrLineNumberSymbol: "line",
}
}
func zshellLanguage() *Language {
return &Language{
Name: "zsh",
Interpreter: "/bin/zsh",
Bang: "#!/bin/zsh",
Comment: "#",
AssignmentOperator: "=",
FlagStopOnError: "-e",
FlagEvaluateScript: "-c",
FileExtension: ".zsh",
CorrectErrLineNumber: false,
ErrLineNumberSymbol: "", // TODO: no symbol for that, allow to use a regex for this task
}
}
func pythonLanguage() *Language {
return &Language{
Name: "python",
Interpreter: "/usr/bin/python",
Bang: "#!/usr/bin/python",
Comment: "#",
AssignmentOperator: " = ",
FlagEvaluateScript: "-c",
FileExtension: ".py",
ExecOpPrefix: "import os; os.system(\"",
ExecOpSuffix: "\")",
CorrectErrLineNumber: true,
ErrLineNumberSymbol: "line",
}
}
func javaScriptLanguage() *Language {
return &Language{
Name: "javascript",
Interpreter: "/usr/bin/osascript",
Bang: "#!/usr/bin/osascript -l JavaScript",
Comment: "//",
AssignmentOperator: " = ",
VariableKeyword: "var ",
UseTempFile: true,
FileExtension: ".js",
ExecOpPrefix: "ObjC.import('stdlib'); $.system(\"",
ExecOpSuffix: "\");",
CorrectErrLineNumber: false,
ErrLineNumberSymbol: "line",
}
}
func rubyLanguage() *Language {
return &Language{
Name: "ruby",
Interpreter: "/usr/bin/ruby",
Bang: "#!/usr/bin/ruby",
Comment: "#",
AssignmentOperator: " = ",
VariableKeyword: "$",
FlagEvaluateScript: "-e",
FileExtension: ".rb",
ExecOpPrefix: "`",
ExecOpSuffix: "`",
CorrectErrLineNumber: true,
ErrLineNumberSymbol: "-e:",
}
}
func luaLanguage() *Language {
return &Language{
Name: "lua",
Interpreter: "/usr/local/bin/lua",
//Bang: "#!/usr/local/bin/lua",
Comment: "--",
AssignmentOperator: " = ",
VariableKeyword: "local ",
FlagEvaluateScript: "-e",
FileExtension: ".lua",
ExecOpPrefix: "os.execute(\"",
ExecOpSuffix: "\")",
CorrectErrLineNumber: true,
ErrLineNumberSymbol: "line",
}
}
func perlLanguage() *Language {
return &Language{
Name: "perl",
Interpreter: "/usr/bin/perl",
Bang: "#!/usr/bin/perl",
Comment: "#",
AssignmentOperator: " = ",
LineDelimiter: ";",
VariableKeyword: "$",
FlagEvaluateScript: "-e",
FileExtension: ".pl",
ExecOpPrefix: "system(\"",
ExecOpSuffix: "\")",
CorrectErrLineNumber: true,
ErrLineNumberSymbol: "line",
}
}
func goLanguage() *Language {
return &Language{
Name: "go",
Interpreter: "go",
Args: []string{"run"},
Comment: "//",
AssignmentOperator: " = ",
VariableKeyword: "var ",
FileExtension: ".go",
CorrectErrLineNumber: true,
ErrLineNumberSymbol: "line",
}
}