1 |
/* |
/* |
2 |
* Copyright (c) 2014-2015 Christian Schoenebeck |
* Copyright (c) 2014-2016 Christian Schoenebeck |
3 |
* |
* |
4 |
* http://www.linuxsampler.org |
* http://www.linuxsampler.org |
5 |
* |
* |
36 |
static void printUsage() { |
static void printUsage() { |
37 |
cout << "ls_instr_script - Parse real-time instrument script from stdin." << endl; |
cout << "ls_instr_script - Parse real-time instrument script from stdin." << endl; |
38 |
cout << endl; |
cout << endl; |
39 |
cout << "Usage: ls_instr_script ENGINE" << endl; |
cout << "Usage: ls_instr_script ENGINE [OPTIONS]" << endl; |
40 |
cout << endl; |
cout << endl; |
41 |
cout << " ENGINE\n"; |
cout << " ENGINE\n"; |
42 |
cout << " Either \"core\", \"gig\", \"sf2\" or \"sfz\"." << endl; |
cout << " Either \"core\", \"gig\", \"sf2\" or \"sfz\"." << endl; |
43 |
cout << endl; |
cout << endl; |
44 |
|
cout << " OPTIONS" << endl; |
45 |
|
cout << " --syntax | -s" << endl; |
46 |
|
cout << " Prints the script to stdout with colored syntax highlighting" << endl; |
47 |
|
cout << " and exits immediately." << endl; |
48 |
|
cout << endl; |
49 |
|
cout << " --debug-syntax | -ds" << endl; |
50 |
|
cout << " Prints a debugging representation (of the syntax" << endl; |
51 |
|
cout << " highlighting backend) of the parsed script to stdout and exits" << endl; |
52 |
|
cout << " immediately." << endl; |
53 |
|
cout << endl; |
54 |
cout << "If you pass \"core\" as argument, only the core language built-in" << endl; |
cout << "If you pass \"core\" as argument, only the core language built-in" << endl; |
55 |
cout << "variables and functions are available. However in this particular" << endl; |
cout << "variables and functions are available. However in this particular" << endl; |
56 |
cout << "mode the program will not just parse the given script, but also" << endl; |
cout << "mode the program will not just parse the given script, but also" << endl; |
61 |
cout << endl; |
cout << endl; |
62 |
} |
} |
63 |
|
|
64 |
|
static void printCodeWithSyntaxHighlighting(ScriptVM* vm); |
65 |
|
static void dumpSyntaxHighlighting(ScriptVM* vm); |
66 |
|
|
67 |
int main(int argc, char *argv[]) { |
int main(int argc, char *argv[]) { |
68 |
if (argc < 2) { |
if (argc < 2) { |
69 |
printUsage(); |
printUsage(); |
86 |
return -1; |
return -1; |
87 |
} |
} |
88 |
|
|
89 |
|
// validate & parse arguments provided to this program |
90 |
|
for (int iArg = 2; iArg < argc; ++iArg) { |
91 |
|
const string opt = argv[iArg]; |
92 |
|
if (opt == "--") { // common for all command line tools: separator between initial option arguments and i.e. subsequent file arguments |
93 |
|
iArg++; |
94 |
|
break; |
95 |
|
} |
96 |
|
if (opt.substr(0, 1) != "-") break; |
97 |
|
|
98 |
|
if (opt == "-s" || opt == "--syntax") { |
99 |
|
printCodeWithSyntaxHighlighting(vm); |
100 |
|
return 0; |
101 |
|
} else if (opt == "-ds" || opt == "--debug-syntax") { |
102 |
|
dumpSyntaxHighlighting(vm); |
103 |
|
return 0; |
104 |
|
} else { |
105 |
|
cerr << "Unknown option '" << opt << "'" << endl; |
106 |
|
cerr << endl; |
107 |
|
printUsage(); |
108 |
|
return -1; |
109 |
|
} |
110 |
|
} |
111 |
|
|
112 |
VMParserContext* parserContext = vm->loadScript(&std::cin); |
VMParserContext* parserContext = vm->loadScript(&std::cin); |
113 |
|
|
114 |
std::vector<ParserIssue> errors = parserContext->errors(); |
std::vector<ParserIssue> errors = parserContext->errors(); |
184 |
|
|
185 |
return 0; |
return 0; |
186 |
} |
} |
187 |
|
|
188 |
|
static void printCodeWithSyntaxHighlighting(ScriptVM* vm) { |
189 |
|
vector<VMSourceToken> tokens = vm->syntaxHighlighting(&std::cin); |
190 |
|
|
191 |
|
for (int i = 0; i < tokens.size(); ++i) { |
192 |
|
const VMSourceToken& token = tokens[i]; |
193 |
|
|
194 |
|
CFmt fmt; |
195 |
|
if (token.isKeyword()) { |
196 |
|
fmt.bold(); |
197 |
|
} else if (token.isVariableName()) { |
198 |
|
fmt.magenta(); |
199 |
|
} else if (token.isIdentifier()) { |
200 |
|
if (token.isEventHandlerName()) { |
201 |
|
fmt.bold(); |
202 |
|
fmt.cyan(); |
203 |
|
} else { // a function ... |
204 |
|
fmt.cyan(); |
205 |
|
} |
206 |
|
} else if (token.isNumberLiteral()) { |
207 |
|
fmt.yellow(); |
208 |
|
} else if (token.isStringLiteral()) { |
209 |
|
fmt.red(); |
210 |
|
} else if (token.isComment()) { |
211 |
|
fmt.blue(); |
212 |
|
} else if (token.isPreprocessor()) { |
213 |
|
fmt.green(); |
214 |
|
} else if (token.isNewLine()) { |
215 |
|
} |
216 |
|
|
217 |
|
printf("%s", token.text().c_str()); |
218 |
|
fflush(stdout); |
219 |
|
} |
220 |
|
} |
221 |
|
|
222 |
|
static void dumpSyntaxHighlighting(ScriptVM* vm) { |
223 |
|
vector<VMSourceToken> tokens = vm->syntaxHighlighting(&std::cin); |
224 |
|
|
225 |
|
for (int i = 0; i < tokens.size(); ++i) { |
226 |
|
const VMSourceToken& token = tokens[i]; |
227 |
|
const char* type = "OTHER"; |
228 |
|
if (token.isKeyword()) { |
229 |
|
type = "KEYWORD"; |
230 |
|
} else if (token.isVariableName()) { |
231 |
|
type = "VARIABLE"; |
232 |
|
} else if (token.isIdentifier()) { |
233 |
|
if (token.isEventHandlerName()) { |
234 |
|
type = "HANDLER_NAME"; |
235 |
|
} else { // a function ... |
236 |
|
type = "FUNCTION"; |
237 |
|
} |
238 |
|
} else if (token.isNumberLiteral()) { |
239 |
|
type = "NUMBER"; |
240 |
|
} else if (token.isStringLiteral()) { |
241 |
|
type = "STRING"; |
242 |
|
} else if (token.isComment()) { |
243 |
|
type = "COMMENT"; |
244 |
|
} else if (token.isPreprocessor()) { |
245 |
|
type = "PREPROC"; |
246 |
|
} else if (token.isNewLine()) { |
247 |
|
type = "NL"; |
248 |
|
} |
249 |
|
printf("L%d,C%d: %s \"%s\"\n", token.firstLine(), token.firstColumn(), type, token.text().c_str()); |
250 |
|
} |
251 |
|
} |