/[svn]/linuxsampler/trunk/src/ls_instr_script.cpp
ViewVC logotype

Diff of /linuxsampler/trunk/src/ls_instr_script.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2611 by schoenebeck, Mon Jun 9 19:20:37 2014 UTC revision 2974 by schoenebeck, Fri Jul 22 15:51:40 2016 UTC
# Line 1  Line 1 
1  /*  /*
2   * Copyright (c) 2014 Christian Schoenebeck   * Copyright (c) 2014-2016 Christian Schoenebeck
3   *   *
4   * http://www.linuxsampler.org   * http://www.linuxsampler.org
5   *   *
# Line 36  using namespace std; Line 36  using namespace std;
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 << "        --auto-suspend" << endl;
55        cout << "            In contrast to the real sampler, this command line program " << endl;
56        cout << "            disables automatic suspension by the VM by default to ease " << endl;
57        cout << "            i.e. bench marking tasks and the like. By providing this " << endl;
58        cout << "            argument auto suspension will be enabled." << endl;
59        cout << endl;
60      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;
61      cout << "variables and functions are available. However in this particular" << endl;      cout << "variables and functions are available. However in this particular" << endl;
62      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;
# Line 51  static void printUsage() { Line 67  static void printUsage() {
67      cout << endl;      cout << endl;
68  }  }
69    
70    static void printCodeWithSyntaxHighlighting(ScriptVM* vm);
71    static void dumpSyntaxHighlighting(ScriptVM* vm);
72    
73  int main(int argc, char *argv[]) {  int main(int argc, char *argv[]) {
74      if (argc < 2) {      if (argc < 2) {
75          printUsage();          printUsage();
# Line 72  int main(int argc, char *argv[]) { Line 91  int main(int argc, char *argv[]) {
91          printUsage();          printUsage();
92          return -1;          return -1;
93      }      }
94        vm->setAutoSuspendEnabled(false);
95    
96        // validate & parse arguments provided to this program
97        for (int iArg = 2; iArg < argc; ++iArg) {
98            const string opt = argv[iArg];
99            if (opt == "--") { // common for all command line tools: separator between initial option arguments and i.e. subsequent file arguments
100                iArg++;
101                break;
102            }
103            if (opt.substr(0, 1) != "-") break;
104    
105            if (opt == "-s" || opt == "--syntax") {
106                printCodeWithSyntaxHighlighting(vm);
107                return 0;
108            } else if (opt == "-ds" || opt == "--debug-syntax") {
109                dumpSyntaxHighlighting(vm);
110                return 0;
111            } else if (opt == "--auto-suspend") {
112                vm->setAutoSuspendEnabled(true);
113            } else {
114                cerr << "Unknown option '" << opt << "'" << endl;
115                cerr << endl;
116                printUsage();
117                return -1;
118            }
119        }
120    
121      VMParserContext* parserContext = vm->loadScript(&std::cin);      VMParserContext* parserContext = vm->loadScript(&std::cin);
122    
# Line 84  int main(int argc, char *argv[]) { Line 129  int main(int argc, char *argv[]) {
129      } else if (!errors.empty()) {      } else if (!errors.empty()) {
130          CFmt fmt; fmt.red();          CFmt fmt; fmt.red();
131          printf("EOF. Script parse completed with issues (%d errors, %d warnings):\n",          printf("EOF. Script parse completed with issues (%d errors, %d warnings):\n",
132                 errors.size(), warnings.size());                 int(errors.size()), int(warnings.size()));
133      } else {      } else {
134          CFmt fmt; fmt.yellow();          CFmt fmt; fmt.yellow();
135          printf("EOF. Script parse completed with issues (%d errors, %d warnings):\n",          printf("EOF. Script parse completed with issues (%d errors, %d warnings):\n",
136                 errors.size(), warnings.size());                 int(errors.size()), int(warnings.size()));
137      }      }
138      for (int i = 0; i < issues.size(); ++i) {      for (int i = 0; i < issues.size(); ++i) {
139          CFmt fmt;          CFmt fmt;
# Line 148  int main(int argc, char *argv[]) { Line 193  int main(int argc, char *argv[]) {
193    
194      return 0;      return 0;
195  }  }
196    
197    static void printCodeWithSyntaxHighlighting(ScriptVM* vm) {
198        vector<VMSourceToken> tokens = vm->syntaxHighlighting(&std::cin);
199    
200        for (int i = 0; i < tokens.size(); ++i) {
201            const VMSourceToken& token = tokens[i];
202    
203            CFmt fmt;
204            if (token.isKeyword()) {
205                fmt.bold();
206            } else if (token.isVariableName()) {
207                fmt.magenta();
208            } else if (token.isIdentifier()) {
209                if (token.isEventHandlerName()) {
210                    fmt.bold();
211                    fmt.cyan();
212                } else { // a function ...
213                    fmt.cyan();
214                }
215            } else if (token.isNumberLiteral()) {
216                fmt.yellow();
217            } else if (token.isStringLiteral()) {
218                fmt.red();
219            } else if (token.isComment()) {
220                fmt.blue();
221            } else if (token.isPreprocessor()) {
222                fmt.green();
223            } else if (token.isNewLine()) {
224            }
225    
226            printf("%s", token.text().c_str());
227            fflush(stdout);
228        }
229    }
230    
231    static void dumpSyntaxHighlighting(ScriptVM* vm) {
232        vector<VMSourceToken> tokens = vm->syntaxHighlighting(&std::cin);
233    
234        for (int i = 0; i < tokens.size(); ++i) {
235            const VMSourceToken& token = tokens[i];
236            const char* type = "OTHER";
237            if (token.isKeyword()) {
238                type = "KEYWORD";
239            } else if (token.isVariableName()) {
240                type = "VARIABLE";
241            } else if (token.isIdentifier()) {
242                if (token.isEventHandlerName()) {
243                    type = "HANDLER_NAME";
244                } else { // a function ...
245                    type = "FUNCTION";
246                }
247            } else if (token.isNumberLiteral()) {
248                type = "NUMBER";
249            } else if (token.isStringLiteral()) {
250                type = "STRING";
251            } else if (token.isComment()) {
252                type = "COMMENT";
253            } else if (token.isPreprocessor()) {
254                type = "PREPROC";
255            } else if (token.isNewLine()) {
256                type = "NL";
257            }
258            printf("L%d,C%d: %s \"%s\"\n", token.firstLine(), token.firstColumn(), type, token.text().c_str());
259        }
260    }

Legend:
Removed from v.2611  
changed lines
  Added in v.2974

  ViewVC Help
Powered by ViewVC