--- linuxsampler/trunk/src/network/lscp.y 2014/01/23 04:00:26 2510 +++ linuxsampler/trunk/src/network/lscp.y 2014/03/09 21:34:03 2534 @@ -36,13 +36,14 @@ #include "lscpevent.h" #include "lscpsymbols.h" #include +#include "lscp.h" namespace LinuxSampler { // to save us typing work in the rules action definitions #define LSCPSERVER ((yyparse_param_t*) yyparse_param)->pServer #define SESSION_PARAM ((yyparse_param_t*) yyparse_param) -#define INCREMENT_LINE { SESSION_PARAM->iLine++; SESSION_PARAM->iColumn = 0; sParsed.clear(); } +#define INCREMENT_LINE { SESSION_PARAM->onNextLine(); sParsed.clear(); } // clears input buffer void restart(yyparse_param_t* pparam, int& yychar); @@ -66,6 +67,16 @@ return (c < 0); } +// returns true if the given character is between between a to z. +inline bool isLowerCaseAlphaChar(const char c) { + return c >= 'a' && c <= 'z'; +} + +// converts the given (expected) lower case character to upper case +inline char alphaCharToUpperCase(const char c) { + return (c - 'a') + 'A'; +} + // custom scanner function which reads from the socket // (bison expects it to return the numerical ID of the next // "recognized token" from the input stream) @@ -169,7 +180,7 @@ %parse-param {void* yyparse_param} // After entering the yyparse() function, store references to the parser's -// state stack, so that we can create more helpful syntax error messages than +// symbol stack, so that we can create more helpful syntax error messages than // Bison (2.x) could do. %initial-action { yyparse_param_t* p = (yyparse_param_t*) yyparse_param; @@ -185,7 +196,7 @@ %type char char_base alpha_char digit digit_oct digit_hex escape_seq escape_seq_octal escape_seq_hex %type real dotnum volume_value boolean control_value %type number sampler_channel instrument_index fx_send_id audio_channel_index device_index effect_index effect_instance effect_chain chain_pos input_control midi_input_channel_index midi_input_port_index midi_map midi_bank midi_prog midi_ctrl -%type string string_escaped text text_escaped text_escaped_base stringval stringval_escaped digits param_val_list param_val query_val filename module effect_system db_path map_name entry_name fx_send_name effect_name engine_name command add_instruction create_instruction destroy_instruction get_instruction list_instruction load_instruction send_instruction set_chan_instruction load_instr_args load_engine_args audio_output_type_name midi_input_type_name remove_instruction unmap_instruction set_instruction subscribe_event unsubscribe_event map_instruction reset_instruction clear_instruction find_instruction move_instruction copy_instruction scan_mode edit_instruction format_instruction append_instruction insert_instruction +%type string string_escaped text text_escaped text_escaped_base stringval stringval_escaped digits param_val_list param_val query_val filename module effect_system db_path map_name entry_name fx_send_name effect_name engine_name line statement command add_instruction create_instruction destroy_instruction get_instruction list_instruction load_instruction send_instruction set_chan_instruction load_instr_args load_engine_args audio_output_type_name midi_input_type_name remove_instruction unmap_instruction set_instruction subscribe_event unsubscribe_event map_instruction reset_instruction clear_instruction find_instruction move_instruction copy_instruction scan_mode edit_instruction format_instruction append_instruction insert_instruction %type buffer_size_type %type key_val_list query_val_list %type instr_load_mode @@ -207,14 +218,17 @@ // GRAMMAR_BNF_BEGIN - do NOT delete or modify this line !!! -input : line LF - | line CR LF +input : line { INCREMENT_LINE; if (!$1.empty()) LSCPSERVER->AnswerClient($1); return LSCP_DONE; } + | error { INCREMENT_LINE; LSCPSERVER->AnswerClient("ERR:0:" + sLastError + "\r\n"); RESTART; return LSCP_SYNTAX_ERROR; } + ; + +line : statement LF { $$ = $1; } + | statement CR LF { $$ = $1; } ; -line : /* epsilon (empty line ignored) */ { INCREMENT_LINE; return LSCP_DONE; } - | comment { INCREMENT_LINE; return LSCP_DONE; } - | command { INCREMENT_LINE; LSCPSERVER->AnswerClient($1); return LSCP_DONE; } - | error { INCREMENT_LINE; LSCPSERVER->AnswerClient("ERR:0:" + sLastError + "\r\n"); RESTART; return LSCP_SYNTAX_ERROR; } +statement : /* epsilon (empty statement/line ignored) */ { $$ = ""; } + | comment { $$ = ""; } + | command ; comment : '#' @@ -428,6 +442,9 @@ | DB_INSTRUMENT SP DESCRIPTION SP db_path SP stringval_escaped { $$ = LSCPSERVER->SetDbInstrumentDescription($5,$7); } | DB_INSTRUMENT SP FILE_PATH SP filename SP filename { $$ = LSCPSERVER->SetDbInstrumentFilePath($5,$7); } | ECHO SP boolean { $$ = LSCPSERVER->SetEcho((yyparse_param_t*) yyparse_param, $3); } + | SHELL SP INTERACT SP boolean { $$ = LSCPSERVER->SetShellInteract((yyparse_param_t*) yyparse_param, $5); } + | SHELL SP AUTO_CORRECT SP boolean { $$ = LSCPSERVER->SetShellAutoCorrect((yyparse_param_t*) yyparse_param, $5); } + | SHELL SP DOC SP boolean { $$ = LSCPSERVER->SetShellDoc((yyparse_param_t*) yyparse_param, $5); } | VOLUME SP volume_value { $$ = LSCPSERVER->SetGlobalVolume($3); } | VOICES SP number { $$ = LSCPSERVER->SetGlobalMaxVoices($3); } | STREAMS SP number { $$ = LSCPSERVER->SetGlobalMaxStreams($3); } @@ -920,6 +937,15 @@ SET : 'S''E''T' ; +SHELL : 'S''H''E''L''L' + ; + +INTERACT : 'I''N''T''E''R''A''C''T' + ; + +AUTO_CORRECT : 'A''U''T''O''_''C''O''R''R''E''C''T' + ; + APPEND : 'A''P''P''E''N''D' ; @@ -1256,75 +1282,353 @@ ECHO : 'E''C''H''O' ; +DOC : 'D''O''C' + ; + QUIT : 'Q''U''I''T' ; %% +// TODO: actually would be fine to have the following bunch of source code in a separate file, however those functions are a) accessing private Bison tables like yytable and b) including the functions from another file here would make the line numbers incorrect on compile errors in auto generated lscpparser.cpp + +/** + * Additional informations of a grammar symbol. + */ +struct BisonSymbolInfo { + bool isTerminalSymbol; ///< Whether the symbol is a terminal or non-termianl symbol. NOTE: Read comment regarding this in _isRuleTerminalSymbol() !! + String nextExpectedChars; ///< According to current parser position: sequence of characters expected next for satisfying this grammar symbol. +}; + +#if HAVE_BISON_MAJ >= 3 // Bison 3.x or younger ... + +/** + * Must ONLY be called just before a so called "reduce" parser action: + * Returns true if the grammar rule, which is just about to be "reduced", is a + * terminal symbol (in *our* terms). + * + * Please note that the term "terminal symbol" is a bit confusingly used in + * this source code here around. In Bison's terms, "terminal symbols" are (more + * or less) just the numbers returned by the YYLEX function. Since we decided + * though to use a convenient solution without a separate lexer, and all its + * caveats, all numbers by the yylex() function here are just the ASCII + * numbers of the individual characters received. Based on that however, one + * single character is not what one would intuitively expect of being a + * "terminal symbol", because it is simply too primitive. + * + * So in this LSCP parser source code a "terminal symbol" rather means a + * keyword like "CREATE" or "GET". In the grammal definition above, those are + * however defined as grammar rules (non-terminals in Bison's terms). So this + * function decides like this: if the given grammar rule just contains + * individual characters on the right side of its grammar rule, then it is a + * "terminal symbol" in *our* terms. + * + * @param rule - Bison grammar rule number + * @param stack - reflecting current Bison parser state + */ +inline static bool _isRuleTerminalSymbol(int rule, const std::vector& stack) { + int nrhs = yyr2[rule]; + for (int i = 0; i < nrhs; ++i) + if (yystos[*(stack.end() - nrhs + i)] >= YYNTOKENS) return false; + return true; +} + +/** + * Must ONLY be called just before a so called "reduce" parser action: Returns + * additional informations to the given grammar rule that is about to be + * "reduced". + * + * @param rule - Bison grammar rule number + * @param stack - reflecting current Bison parser state + * @param nextExpectedChars - must already be filled with the characters + * expected to be coming next + */ +inline static BisonSymbolInfo _symbolInfoForRule(int rule, const std::vector& stack, const String& nextExpectedChars) { + BisonSymbolInfo info; + info.isTerminalSymbol = _isRuleTerminalSymbol(rule, stack); + if (info.isTerminalSymbol) info.nextExpectedChars = nextExpectedChars; + return info; +} + +#else // Bison 2.x or older ... + +//TODO: The Bison 2.x code below can probably soon just be deleted. Most Bisonx 2.x versions should be able to compile successfully with the Bison 3.x code above as well (just requires the existence of table yystos[] in the auto generated lscpparser.cpp). + +/** + * Returns true if the given grammar @a rule is a terminal symbol (in *our* + * terms). + * + * Please note that the term "terminal symbol" is a bit confusingly used in + * this source code here around. In Bison's terms, "terminal symbols" are (more + * or less) just the numbers returned by the YYLEX function. Since we decided + * though to use a convenient solution without a separate lexer, and all its + * caveats, all numbers by the yylex() function here are just the ASCII + * numbers of the individual characters received. Based on that however, one + * single character is not what one would intuitively expect of being a + * "terminal symbol", because it is simply too primitive. + * + * So in this LSCP parser source code a "terminal symbol" rather means a + * keyword like "CREATE" or "GET". In the grammal definition above, those are + * however defined as grammar rules (non-terminals in Bison's terms). So this + * function decides like this: if the given grammar rule just contains + * individual characters on the right side of its grammar rule, then it is a + * "terminal symbol" in *our* terms. + * + * @param rule - Bison grammar rule number + */ +inline static bool _isRuleTerminalSymbol(int rule) { + for (int i = yyprhs[rule]; yyrhs[i] != -1; ++i) + if (yyrhs[i] >= YYNTOKENS) return false; + return true; +} + +/** + * Returns additional informations to the given grammar @a rule. + * + * @param rule - grammar rule index to retrieve informations about + * @param nextExpectedChars - must already be filled with the characters + * expected to be coming next + */ +inline static BisonSymbolInfo _symbolInfoForRule(int rule, const String& nextExpectedChars) { + BisonSymbolInfo info; + info.isTerminalSymbol = _isRuleTerminalSymbol(rule); + if (info.isTerminalSymbol) info.nextExpectedChars = nextExpectedChars; + return info; +} + +#endif // HAVE_BISON_MAJ >= 3 + +/** + * Returns the human readable name of the given @a token. + */ +inline static String _tokenName(int token) { + String s = yytname[token]; + // remove leading and trailing apostrophes that Bison usually adds to + // ASCII characters used directly in grammar rules + if (s.empty()) return s; + if (s[0] == '\'') s.erase(0, 1); + if (s.empty()) return s; + if (s[s.size() - 1] == '\'') s.erase(s.size() - 1); + return s; +} + +/** + * Assumes the given @a token is exactly one character and returns that + * character. This must be changed in future, i.e. in case Unicode characters + * will be introduced in the LSCP grammar one day. + */ +inline static char _tokenChar(int token) { + String s = _tokenName(token); + if (s == "\\n") return '\n'; + if (s == "\\r") return '\r'; + return _tokenName(token)[0]; +} + +/** + * Implements Bison's so called "reduce" action, according to Bison's LALR(1) + * parser algorithm. + */ +inline static int _yyReduce(std::vector& stack, const int& rule) { + if (stack.empty()) throw 1; // severe error + const int len = yyr2[rule]; + stack.resize(stack.size() - len); + YYTYPE_INT16 newState = yypgoto[yyr1[rule] - YYNTOKENS] + stack.back(); + if (0 <= newState && newState <= YYLAST && yycheck[newState] == stack.back()) + newState = yytable[newState]; + else + newState = yydefgoto[yyr1[rule] - YYNTOKENS]; + stack.push_back(newState); + return newState; +} + +/** + * Implements Bison's so called "default reduce" action, according to Bison's + * LALR(1) parser algorithm. + */ +inline static int _yyDefaultReduce(std::vector& stack) { + if (stack.empty()) throw 2; // severe error + int rule = yydefact[stack.back()]; + if (rule <= 0 || rule >= YYNRULES) throw 3; // no rule, something is wrong + return _yyReduce(stack, rule); +} + +static bool yyValid(std::vector& stack, char ch); + +/** + * A set of parser symbol stacks. This type is used for the recursive algorithms + * in a) yyAutoComplete() and b) walkAndFillExpectedSymbols() for detecting + * endless recursions. + * + * This unique container is used to keep track of all previous parser states + * (stacks), for detecting a parser symbol stack that has already been + * encountered before. Because if yyAutoComplete() or + * walkAndFillExpectedSymbols() reach the exactly same parser symbol stack + * again, that means there is an endless recursion in that part of the grammar + * tree branch and shall not be evaluated any further, since it would end up in + * an endless loop of the algorithm otherwise. + * + * This solution consumes a lot of memory, but unfortunately there is no other + * easy way to solve it. With our grammar and today's usual memory heap size & + * memory stack size in modern devices, it should be fine though. + */ +typedef std::set< std::vector > YYStackHistory; + #define DEBUG_BISON_SYNTAX_ERROR_WALKER 0 /** - * Internal function, only called by yyExpectedSymbols(). It is given a Bison - * parser state stack, reflecting the parser's entire state at a certain point, - * i.e. when a syntax error occured. This function will then walk ahead the - * potential parse tree starting from the current head of the given state - * stack. This function will call itself recursively to scan the individual - * parse tree branches. As soon as it hits on the next non-terminal grammar - * symbol in one parse tree branch, it adds the found non-terminal symbol to - * @a expectedSymbols and aborts scanning the respective tree branch further. - * If any local parser state is reached a second time, the respective parse - * tree is aborted to avoid any endless recursion. + * Tries to find the next expected grammar symbols according to the given + * precise parse position & state represented by @a stack, according to Bison's + * LALR(1) parser algorithm. + * + * This function is given a Bison parser symbol stack, reflecting the parser's + * entire state at a certain point, i.e. when a syntax error occured. This + * function will then walk ahead the potential parse tree starting from the + * current head of the given symbol stack. This function will call itself + * recursively to scan the individual parse tree branches. As soon as it hits + * on the next non-terminal grammar symbol in one parse tree branch, it adds the + * found non-terminal symbol to @a expectedSymbols and aborts scanning the + * respective tree branch further. If any local parser state is reached a second + * time, the respective parse tree is aborted to avoid any endless recursion. * - * @param stack - Bison (yacc) state stack + * @param stack - current Bison (yacc) symbol stack to be examined * @param expectedSymbols - will be filled with next expected grammar symbols - * @param depth - just for internal debugging purposes + * @param nextExpectedChars - just for internal purpose, due to the recursive + * implementation of this function, do supply an + * empty string for this argument + * @param history - only for internal purpose, keeps a history of all previous + * parser symbol stacks (just for avoiding endless recursion in + * this recursive algorithm), do supply an empty history + * @param depth - just for internal debugging purposes, do not supply it */ -static void walkAndFillExpectedSymbols(std::vector& stack, std::set& expectedSymbols, int depth = 0) { +static void walkAndFillExpectedSymbols( + std::vector& stack, + std::map& expectedSymbols, + String& nextExpectedChars, YYStackHistory& history, int depth = 0) +{ #if DEBUG_BISON_SYNTAX_ERROR_WALKER printf("\n"); for (int i = 0; i < depth; ++i) printf("\t"); - printf("State stack:"); + printf("Symbol stack:"); for (int i = 0; i < stack.size(); ++i) { printf(" %d", stack[i]); } printf("\n"); #endif + startLabel: - if (stack.empty()) return; + // detect endless recursion + if (history.count(stack)) return; + history.insert(stack); + + if (stack.empty()) { +#if DEBUG_BISON_SYNTAX_ERROR_WALKER + for (int i = 0; i < depth; ++i) printf("\t"); + printf("(EMPTY STACK)\n"); +#endif + return; + } int state = stack[stack.size() - 1]; int n = yypact[state]; if (n == YYPACT_NINF) { // default reduction required ... // get default reduction rule for this state n = yydefact[state]; - if (n <= 0 || n >= YYNRULES) return; // no rule, something is wrong - // return the new resolved expected symbol (left-hand symbol of grammar - // rule), then we're done in this state - expectedSymbols.insert(yytname[yyr1[n]]); + if (n <= 0 || n >= YYNRULES) { +#if DEBUG_BISON_SYNTAX_ERROR_WALKER + for (int i = 0; i < depth; ++i) printf("\t"); + printf("(EMPTY RULE)\n"); +#endif + return; // no rule, something is wrong + } +#if DEBUG_BISON_SYNTAX_ERROR_WALKER + for (int i = 0; i < depth; ++i) printf("\t"); + printf("(default reduction)\n"); +#endif + #if HAVE_BISON_MAJ >= 3 + if (!nextExpectedChars.empty() || !_isRuleTerminalSymbol(n, stack)) { + #else + if (!nextExpectedChars.empty() || !_isRuleTerminalSymbol(n)) { + #endif + // Return the new resolved expected symbol (left-hand symbol of grammar + // rule), then we're done in this state. (If the same symbol can be + // matched on different ways, then it is non-terminal symbol.) + bool ambigious = + expectedSymbols.count(yytname[yyr1[n]]) && + expectedSymbols[yytname[yyr1[n]]].nextExpectedChars != nextExpectedChars; + #if HAVE_BISON_MAJ >= 3 + expectedSymbols[yytname[yyr1[n]]] = _symbolInfoForRule(n, stack, nextExpectedChars); + #else + expectedSymbols[yytname[yyr1[n]]] = _symbolInfoForRule(n, nextExpectedChars); + #endif + if (ambigious) + expectedSymbols[yytname[yyr1[n]]].isTerminalSymbol = false; +#if DEBUG_BISON_SYNTAX_ERROR_WALKER + for (int i = 0; i < depth; ++i) printf("\t"); + printf("(empty expectedChars. sym = %s)\n", yytname[yyr1[n]]); +#endif + return; + } + _yyReduce(stack, n); + goto startLabel; + } + if (!(YYPACT_NINF < n && n <= YYLAST)) { +#if DEBUG_BISON_SYNTAX_ERROR_WALKER + for (int i = 0; i < depth; ++i) printf("\t"); + printf("(invalid action B)\n"); +#endif return; } - if (!(YYPACT_NINF < n && n <= YYLAST)) return; + + // Check for duplicate states, if duplicates exist return + // (this check is necessary since the introduction of the yyValid() call + // below, which does not care about duplicates). + for (int i = 0; i < stack.size(); ++i) + for (int k = i + 1; k < stack.size(); ++k) + if (stack[i] == stack[k]) + return; #if DEBUG_BISON_SYNTAX_ERROR_WALKER for (int i = 0; i < depth; ++i) printf("\t"); printf("Expected tokens:"); #endif int begin = n < 0 ? -n : 0; - int checklim = YYLAST - n + 1; - int end = checklim < YYNTOKENS ? checklim : YYNTOKENS; - int rule, action, stackSize; + //int checklim = YYLAST - n + 1; + int end = YYNTOKENS;//checklim < YYNTOKENS ? checklim : YYNTOKENS; + int rule, action, stackSize, nextExpectedCharsLen; for (int token = begin; token < end; ++token) { - if (token == YYTERROR || yycheck[n + token] != token) continue; + if (token <= YYTERROR) continue; + if (yytname[token] == String("$undefined")) continue; + if (yytname[token] == String("EXT_ASCII_CHAR")) continue; + //if (yycheck[n + token] != token) goto default_reduction; + if (yycheck[n + token] != token) { // default reduction suggested ... + // If we are here, it means the current token in the loop would not + // cause a "shift", however we don't already know whether this token + // is valid or not. Because there might be several reductions + // involved until one can determine whether the token causes an + // error or is valid. So we use this heavy check instead: + std::vector stackCopy = stack; // copy required, since reduction will take place + if (!yyValid(stackCopy, _tokenChar(token))) continue; // invalid token #if DEBUG_BISON_SYNTAX_ERROR_WALKER - printf(" %s", yytname[token]); + printf(" ETdr(%s)", yytname[token]); +#endif + // the token is valid, "stackCopy" has been reduced accordingly + // and now do recurse ... + nextExpectedChars += _tokenName(token); + nextExpectedCharsLen = nextExpectedChars.size(); + walkAndFillExpectedSymbols( //FIXME: could cause stack overflow (should be a loop instead), is probably fine with our current grammar though + stackCopy, expectedSymbols, nextExpectedChars, history, depth + 1 + ); + nextExpectedChars.resize(nextExpectedCharsLen); // restore 'nextExpectedChars' + continue; + } +#if DEBUG_BISON_SYNTAX_ERROR_WALKER + printf(" ET(%s)", yytname[token]); #endif - - //if (yycheck[n + token] != token) goto default_reduction; action = yytable[n + token]; if (action == 0 || action == YYTABLE_NINF) { #if DEBUG_BISON_SYNTAX_ERROR_WALKER - printf(" (invalid action) "); fflush(stdout); + printf(" (invalid action A) "); fflush(stdout); #endif continue; // error, ignore } @@ -1335,21 +1639,33 @@ rule = -action; goto reduce; } - if (action == YYFINAL) continue; // "accept" state, we don't care about it here + if (action == YYFINAL) { +#if DEBUG_BISON_SYNTAX_ERROR_WALKER + printf(" (ACCEPT) "); fflush(stdout); +#endif + continue; // "accept" state, we don't care about it here + } // "shift" required ... - if (std::find(stack.begin(), stack.end(), action) != stack.end()) + if (std::find(stack.begin(), stack.end(), action) != stack.end()) { +#if DEBUG_BISON_SYNTAX_ERROR_WALKER + printf(" (duplicate state %d) ", action); fflush(stdout); +#endif continue; // duplicate state, ignore it to avoid endless recursions + } - // "shift" / push the new state on the state stack and call this + // "shift" / push the new state on the symbol stack and call this // function recursively, and restore the stack after the recurse return stackSize = stack.size(); + nextExpectedCharsLen = nextExpectedChars.size(); stack.push_back(action); + nextExpectedChars += _tokenName(token); walkAndFillExpectedSymbols( //FIXME: could cause stack overflow (should be a loop instead), is probably fine with our current grammar though - stack, expectedSymbols, depth + 1 + stack, expectedSymbols, nextExpectedChars, history, depth + 1 ); stack.resize(stackSize); // restore stack + nextExpectedChars.resize(nextExpectedCharsLen); // restore 'nextExpectedChars' continue; //default_reduction: // resolve default reduction for this state @@ -1360,9 +1676,24 @@ #if DEBUG_BISON_SYNTAX_ERROR_WALKER printf(" (reduce by %d) ", rule); fflush(stdout); #endif - if (rule == 0 || rule >= YYNRULES) continue; // invalid rule, something is wrong - // store the left-hand symbol of the grammar rule - expectedSymbols.insert(yytname[yyr1[rule]]); + if (rule == 0 || rule >= YYNRULES) { +#if DEBUG_BISON_SYNTAX_ERROR_WALKER + printf(" (invalid rule) "); fflush(stdout); +#endif + continue; // invalid rule, something is wrong + } + // Store the left-hand symbol of the grammar rule. (If the same symbol + // can be matched on different ways, then it is non-terminal symbol.) + bool ambigious = + expectedSymbols.count(yytname[yyr1[rule]]) && + expectedSymbols[yytname[yyr1[rule]]].nextExpectedChars != nextExpectedChars; + #if HAVE_BISON_MAJ >= 3 + expectedSymbols[yytname[yyr1[rule]]] = _symbolInfoForRule(rule, stack, nextExpectedChars); + #else + expectedSymbols[yytname[yyr1[rule]]] = _symbolInfoForRule(rule, nextExpectedChars); + #endif + if (ambigious) + expectedSymbols[yytname[yyr1[n]]].isTerminalSymbol = false; #if DEBUG_BISON_SYNTAX_ERROR_WALKER printf(" (SYM %s) ", yytname[yyr1[rule]]); fflush(stdout); #endif @@ -1373,28 +1704,429 @@ } /** + * Just a convenience wrapper on top of the actual walkAndFillExpectedSymbols() + * implementation above, which can be called with less parameters than the + * implementing function above actually requires. + */ +static void walkAndFillExpectedSymbols( + std::vector& stack, + std::map& expectedSymbols) +{ + String nextExpectedChars; + YYStackHistory history; + + walkAndFillExpectedSymbols( + stack, expectedSymbols, nextExpectedChars, history + ); +} + +#define DEBUG_PUSH_PARSE 0 + +/** + * Implements parsing exactly one character (given by @a ch), continueing at the + * parser position reflected by @a stack. The @a stack will hold the new parser + * state after this call. + * + * This function is implemented according to Bison's LALR(1) parser algorithm. + */ +static bool yyPushParse(std::vector& stack, char ch) { + startLabel: + +#if DEBUG_PUSH_PARSE + //printf("\n"); + //for (int i = 0; i < depth; ++i) printf("\t"); + printf("Symbol stack:"); + for (int i = 0; i < stack.size(); ++i) { + printf(" %d", stack[i]); + } + printf(" char='%c'(%d)\n", ch, (int)ch); +#endif + + if (stack.empty()) return false; + + int state = stack.back(); + int n = yypact[state]; + if (n == YYPACT_NINF) { // default reduction required ... +#if DEBUG_PUSH_PARSE + printf("(def reduce 1)\n"); +#endif + state = _yyDefaultReduce(stack); + goto startLabel; + } + if (!(YYPACT_NINF < n && n <= YYLAST)) return false; + + YYTYPE_INT16 token = (ch == YYEOF) ? YYEOF : yytranslate[ch]; + n += token; + if (n < 0 || YYLAST < n || yycheck[n] != token) { +#if DEBUG_PUSH_PARSE + printf("(def reduce 2) n=%d token=%d\n", n, token); +#endif + state = _yyDefaultReduce(stack); + goto startLabel; + } + int action = yytable[n]; // yytable[yypact[state] + token] + if (action == 0 || action == YYTABLE_NINF) throw 4; + if (action < 0) { +#if DEBUG_PUSH_PARSE + printf("(reduce)\n"); +#endif + int rule = -action; + state = _yyReduce(stack, rule); + goto startLabel; + } + if (action == YYFINAL) return true; // final state reached + +#if DEBUG_PUSH_PARSE + printf("(push)\n"); +#endif + // push new state + state = action; + stack.push_back(state); + return true; +} + +/** + * Returns true if parsing ahead with given character @a ch is syntactically + * valid according to the LSCP grammar, it returns false if it would create a + * parse error. + * + * The @a stack will reflect the new parser state after this call. + * + * This is just a wrapper ontop of yyPushParse() which converts parser + * exceptions thrown by yyPushParse() into @c false return value. + */ +static bool yyValid(std::vector& stack, char ch) { + try { + return yyPushParse(stack, ch); + } catch (int i) { +#if DEBUG_PUSH_PARSE + printf("exception %d\n", i); +#endif + return false; + } catch (...) { + return false; + } +} + +/** + * Returns the amount of correct characters of given @a line from the left, + * according to the LSCP grammar. + * + * @param stack - a Bison symbol stack to work with + * @param line - the input line to check + * @param bAutoCorrect - if true: try to correct obvious, trivial syntax errors + */ +static int yyValidCharacters(std::vector& stack, String& line, bool bAutoCorrect) { + int i; + for (i = 0; i < line.size(); ++i) { + // since we might check the same parser state twice against the current + // char here below, and since the symbol stack might be altered + // (i.e. shifted or reduced) on syntax errors, we have to backup the + // current symbol stack and restore it on syntax errors below + std::vector stackCopy = stack; + if (yyValid(stackCopy, line[i])) { + stack = stackCopy; + continue; + } + if (bAutoCorrect) { + // try trivial corrections, i.e. upper case character instead of + // lower case, subline instead of space and vice versa + char c; + if (line[i] == ' ') c = '_'; + else if (line[i] == '_') c = ' '; + else if (isLowerCaseAlphaChar(line[i])) + c = alphaCharToUpperCase(line[i]); + else return i; + if (yyValid(stack, c)) { + line[i] = c; + continue; + } + } + return i; + } + return i; +} + +/** * Should only be called on syntax errors: returns a set of non-terminal * symbols expected to appear now/next, just at the point where the syntax * error appeared. + * + * @returns names of the non-terminal symbols expected at this parse position */ static std::set yyExpectedSymbols() { - std::set result; + std::map expectedSymbols; yyparse_param_t* param = GetCurrentYaccSession(); YYTYPE_INT16* ss = (*param->ppStackBottom); YYTYPE_INT16* sp = (*param->ppStackTop); int iStackSize = sp - ss + 1; - // copy and wrap parser's state stack into a convenient STL container + // copy and wrap parser's symbol stack into a convenient STL container std::vector stack; for (int i = 0; i < iStackSize; ++i) { stack.push_back(ss[i]); } // do the actual parser work - walkAndFillExpectedSymbols(stack, result); + walkAndFillExpectedSymbols(stack, expectedSymbols); + + // convert expectedSymbols to the result set + std::set result; + for (std::map::const_iterator it = expectedSymbols.begin(); + it != expectedSymbols.end(); ++it) result.insert(it->first); return result; } +#define DEBUG_YY_AUTO_COMPLETE 0 + +/** + * Generates and returns an auto completion string for the current parser + * state given by @a stack. That means, this function will return the longest + * sequence of characters that is uniqueley expected to be sent next by the LSCP + * client. Or in other words, if the LSCP client would send any other + * character(s) than returned here, it would result in a syntax error. + * + * This function takes a Bison symbol @a stack as argument, reflecting the + * current Bison parser state, and evaluates the individual grammar tree + * branches starting from that particular position. It walks along the grammar + * tree as long as there is only one possible tree branch and assembles a string + * of input characters that would lead to that walk through the grammar tree. As + * soon as a position in the grammar tree is reached where there are multiple + * possible tree branches, this algorithm will stop, since the user could have + * multiple possible valid characters he could type at that point, thus auto + * completion would no longer be unique at that point. + * + * Regarding @a history argument: read the description on YYStackHistory for the + * purpose behind this argument. + * + * @param stack - current Bison (yacc) symbol stack to create auto completion for + * @param history - only for internal purpose, keeps a history of all previous + * parser symbol stacks (just for avoiding endless recursion in + * this auto completion algorithm), do supply an empty history + * @param depth - just for internal debugging purposes, do not supply anything + * @returns auto completion for current, given parser state + */ +static String yyAutoComplete(std::vector& stack, YYStackHistory& history, int depth = 0) { + std::map expectedSymbols; + walkAndFillExpectedSymbols(stack, expectedSymbols); + if (expectedSymbols.size() == 1) { + String name = expectedSymbols.begin()->first; + BisonSymbolInfo info = expectedSymbols.begin()->second; +#if DEBUG_YY_AUTO_COMPLETE + for (int q = 0; q < depth; ++q) printf(" "); + printf("(%d) Suggested Sub Completion (sz=%d): type=%s %s -> '%s'\n", depth, expectedSymbols.size(), (info.isTerminalSymbol) ? "T" : "NT", name.c_str(), info.nextExpectedChars.c_str()); +#endif + if (info.nextExpectedChars.empty() || !info.isTerminalSymbol) return ""; + // parse forward with the suggested auto completion + std::vector stackCopy = stack; + yyValidCharacters(stackCopy, info.nextExpectedChars, false); + // detect endless recursion + if (history.count(stackCopy)) return ""; + history.insert(stackCopy); + // recurse and return the expanded auto completion with maximum length + return info.nextExpectedChars + yyAutoComplete(stackCopy, history, depth + 1); + } else if (expectedSymbols.size() == 0) { +#if DEBUG_YY_AUTO_COMPLETE + for (int q = 0; q < depth; ++q) printf(" "); + printf("(%d) No sub suggestion.\n", depth); +#endif + return ""; + } else if (expectedSymbols.size() > 1) { +#if DEBUG_YY_AUTO_COMPLETE + for (int q = 0; q < depth; ++q) printf(" "); + printf("(%d) Multiple sub possibilities (before expansion):", depth); + for (std::map::const_iterator it = expectedSymbols.begin(); + it != expectedSymbols.end(); ++it) + { + printf(" %s (..%s)", it->first.c_str(), it->second.nextExpectedChars.c_str()); + } + printf("\n"); +#endif + // check if any of the possibilites is a non-terminal symbol, if so, we + // have no way for auto completion at this point + for (std::map::const_iterator it = expectedSymbols.begin(); + it != expectedSymbols.end(); ++it) + { + if (!it->second.isTerminalSymbol) { +#if DEBUG_YY_AUTO_COMPLETE + for (int q = 0; q < depth; ++q) printf(" "); + printf("(%d) Non-terminal exists. Stop.", depth); +#endif + return ""; + } + } +#if 0 // commented out for now, since practically irrelevant and VERY slow ... + // all possibilities are terminal symbols, so expand all possiblities to + // maximum length with a recursive call for each possibility + for (std::map::iterator it = expectedSymbols.begin(); + it != expectedSymbols.end(); ++it) + { + if (it->second.nextExpectedChars.empty() || !it->second.isTerminalSymbol) continue; + // parse forward with this particular suggested auto completion + std::vector stackCopy = stack; + yyValidCharacters(stackCopy, it->second.nextExpectedChars, false); + // detect endless recursion + if (history.count(stackCopy)) continue; + history.insert(stackCopy); + // recurse and return the total possible auto completion for this + // grammar tree branch + it->second.nextExpectedChars += yyAutoComplete(stackCopy, history, depth + 1); + } +#endif + // try to find the longest common string all possibilities start with + // (from the left) + String sCommon; + for (int i = 0; true; ++i) { + char c; + for (std::map::const_iterator it = expectedSymbols.begin(); + it != expectedSymbols.end(); ++it) + { + if (i >= it->second.nextExpectedChars.size()) + goto commonSearchEndLabel; + if (it == expectedSymbols.begin()) + c = it->second.nextExpectedChars[i]; + if (c != it->second.nextExpectedChars[i]) + goto commonSearchEndLabel; + if (it == --expectedSymbols.end()) + sCommon += c; + } + } + commonSearchEndLabel: +#if DEBUG_YY_AUTO_COMPLETE + for (int q = 0; q < depth; ++q) printf(" "); + printf("(%d) Multiple sub possibilities (after expansion):", depth); + for (std::map::const_iterator it = expectedSymbols.begin(); + it != expectedSymbols.end(); ++it) + { + printf(" %s (..%s)", it->first.c_str(), it->second.nextExpectedChars.c_str()); + } + printf("\n"); + for (int q = 0; q < depth; ++q) printf(" "); + printf("(%d) Common sub possibility: '%s'\n", depth, sCommon.c_str()); +#endif + return sCommon; + } + return ""; // just pro forma, should never happen though +} + +/** + * Just a convenience wrapper on top of the actual yyAutoComplete() + * implementation. See its description above for details. + */ +static String yyAutoComplete(std::vector& stack) { + YYStackHistory history; + return yyAutoComplete(stack, history); +} + namespace LinuxSampler { +#define DEBUG_SHELL_INTERACTION 0 + +/** + * If LSCP shell mode is enabled for the respective LSCP client connection, then + * this function is called on every new byte received from that client. It will + * check the current total input line and reply to the LSCP shell with a + * specially crafted string, which allows the shell to provide colored syntax + * highlighting and potential auto completion in the shell. + * + * It also performs auto correction of obvious & trivial syntax mistakes if + * requested. + * + * The return value of this function will be sent to the client. It contains one + * line specially formatted for the LSCP shell application, which can easily be + * processed by the client/shell for extracting its necessary informations like + * which part of the current command line is syntactically correct, which part + * is incorrect, what could be auto completed right now, etc. So all the heavy + * grammar evaluation tasks are peformed by the LSCP server for the LSCP shell + * application (which is desgined as a thin client), so the LSCP shell + * application will only have to show the results of the LSCP server's + * evaluation to the user on the screen. + * + * @param line - the current command line to be evaluated by LSCP parser + * @param param = reentrant parser session parameters + * @param possibilities - whether all possibilities shall be shown + * @returns LSCP shell response line to be returned to the client + */ +String lscpParserProcessShellInteraction(String& line, yyparse_param_t* param, bool possibilities) { + // first, determine how many characters (starting from the left) of the + // given input line are already syntactically correct + std::vector stack; + stack.push_back(0); // every Bison symbol stack starts with state zero + String l = line + '\n'; // '\n' to pretend ENTER as if the line was now complete + int n = yyValidCharacters(stack, l, param->bShellAutoCorrect); + + // if auto correction is enabled, apply the auto corrected string to + // intput/output string 'line' + if (param->bShellAutoCorrect) { + int nMin = (n < line.length()) ? n : line.length(); + line.replace(0, nMin, l.substr(0, nMin)); + } + + size_t cursorPos = line.size() + param->iCursorOffset; + if (cursorPos < 0) cursorPos = 0; + + // generate an info string that will be sent to the LSCP shell for letting + // it know which part is correct, which one is wrong, where is the cursor, etc. + String result = line; + result.insert(n <= result.length() ? n : result.length(), LSCP_SHK_GOOD_FRONT); + result.insert(cursorPos <= n ? cursorPos : cursorPos + String(LSCP_SHK_GOOD_FRONT).length(), LSCP_SHK_CURSOR); + int code = (n > line.length()) ? LSCP_SHU_COMPLETE : (n < line.length()) ? + LSCP_SHU_SYNTAX_ERR : LSCP_SHU_INCOMPLETE; + result = "SHU:" + ToString(code) + ":" + result; + //if (n > line.length()) result += " [OK]"; + + // get a clean parser stack to the last valid parse position + // (due to the appended '\n' character above, and on syntax errors, the + // symbol stack might be in undesired, i.e. reduced state) + stack.clear(); + stack.push_back(0); // every Bison symbol stack starts with state zero + l = line.substr(0, n); + if (!l.empty()) yyValidCharacters(stack, l, param->bShellAutoCorrect); + + // generate auto completion suggestion (based on the current parser stack) + std::vector stackCopy = stack; // make a copy, since yyAutoComplete() might alter the stack + String sSuggestion = yyAutoComplete(stackCopy); + if (!sSuggestion.empty()) result += LSCP_SHK_SUGGEST_BACK + sSuggestion; + + if (possibilities) { + // append all possible terminals and non-terminals according to + // current parser state + std::map expectedSymbols; + walkAndFillExpectedSymbols(stack, expectedSymbols); + + // pretend to LSCP shell that the following terminal symbols were + // non-terminal symbols (since they are not human visible for auto + // completion on the shell's screen) + std::set specialNonTerminals; + specialNonTerminals.insert("SP"); + specialNonTerminals.insert("CR"); + specialNonTerminals.insert("LF"); + + String sPossibilities; + int iNonTerminals = 0; + int iTerminals = 0; + for (std::map::const_iterator it = expectedSymbols.begin(); + it != expectedSymbols.end(); ++it) + { + if (!sPossibilities.empty()) sPossibilities += " | "; + if (it->second.isTerminalSymbol && !specialNonTerminals.count(it->first)) { + sPossibilities += it->first; + iTerminals++; + } else { + sPossibilities += "<" + it->first + ">"; + iNonTerminals++; + } + } + if (!sPossibilities.empty() && (iNonTerminals || iTerminals > 1)) { + result += LSCP_SHK_POSSIBILITIES_BACK + sPossibilities; + } + } + +#if DEBUG_SHELL_INTERACTION + printf("%s\n", result.c_str()); +#endif + + return result; +} + /** * Clears input buffer. */