bool InteractiveInterpreter::canEvaluate()
{
    int yyaction = 0;
    int yytoken = -1;
    int yytos = -1;

    setCode(m_code, 1);
    m_tokens.append(T_FEED_JS_PROGRAM);

    do {
        if (++yytos == m_stateStack.size())
            m_stateStack.resize(m_stateStack.size() * 2);

        m_stateStack[yytos] = yyaction;

again:
        if (yytoken == -1 && action_index[yyaction] != -TERMINAL_COUNT) {
            if (m_tokens.isEmpty())
                yytoken = lex();
            else
                yytoken = m_tokens.takeFirst();
        }

        yyaction = t_action(yyaction, yytoken);
        if (yyaction > 0) {
            if (yyaction == ACCEPT_STATE) {
                --yytos;
                return true;
            }
            yytoken = -1;
        } else if (yyaction < 0) {
            const int ruleno = -yyaction - 1;
            yytos -= rhs[ruleno];
            yyaction = nt_action(m_stateStack[yytos], lhs[ruleno] - TERMINAL_COUNT);
        }
    } while (yyaction);

    const int errorState = m_stateStack[yytos];
    if (t_action(errorState, T_AUTOMATIC_SEMICOLON) && canInsertAutomaticSemicolon(yytoken)) {
        yyaction = errorState;
        m_tokens.prepend(yytoken);
        yytoken = T_SEMICOLON;
        goto again;
    }

    if (yytoken != EOF_SYMBOL)
        return true;

    return false;
}
Example #2
0
File: main.cpp Project: Suneal/qt
bool Parser::parse()
{
  const int INITIAL_STATE = 0;

  current_char = 0;
  in_tag = 0;

  int yytoken = -1;
  reallocateStack();

  tos = 0;
  state_stack[++tos] = INITIAL_STATE;

  while (true)
    {
      if (yytoken == -1 && - TERMINAL_COUNT != action_index [state_stack [tos]])
        yytoken = nextToken();

      int act = t_action (state_stack [tos], yytoken);

      if (act == ACCEPT_STATE) {
        return true;
      }

      else if (act > 0)
        {
          if (++tos == stack_size)
            reallocateStack();

          sym_stack [tos].ival = current_char; // ### save the token value here
          state_stack [tos] = act;
          yytoken = -1;
        }

      else if (act < 0)
        {
          int r = - act - 1;

          tos -= rhs [r];
          act = state_stack [tos++];
          consumeRule (r);
          state_stack [tos] = nt_action (act, lhs [r] - TERMINAL_COUNT);
        }

      else
        break;
    }

    return false;
}
SyntaxChecker::Result SyntaxChecker::checkSyntax(const QString &code)
{
  const int INITIAL_STATE = 0;
  QScript::Lexer lexer (/*engine=*/ 0);
  lexer.setCode(code, /*lineNo*/ 1);

  int yytoken = -1;
  int saved_yytoken = -1;
  QString error_message;
  int error_lineno = -1;
  int error_column = -1;
  State checkerState = Valid;

  reallocateStack();

  tos = 0;
  state_stack[++tos] = INITIAL_STATE;

  while (true)
    {
      const int state = state_stack [tos];
      if (yytoken == -1 && - TERMINAL_COUNT != action_index [state])
        {
          if (saved_yytoken == -1)
            yytoken = lexer.lex();
          else
            {
              yytoken = saved_yytoken;
              saved_yytoken = -1;
            }
        }

      int act = t_action (state, yytoken);

      if (act == ACCEPT_STATE) {
          if (lexer.error() == QScript::Lexer::UnclosedComment)
              checkerState = Intermediate;
          else
              checkerState = Valid;
          break;
      } else if (act > 0) {
          if (++tos == stack_size)
            reallocateStack();

          state_stack [tos] = act;
          yytoken = -1;
        }

      else if (act < 0)
        {
          int r = - act - 1;

          tos -= rhs [r];
          act = state_stack [tos++];

          if ((r == Q_SCRIPT_REGEXPLITERAL_RULE1)
              || (r == Q_SCRIPT_REGEXPLITERAL_RULE2)) {
              // Skip the rest of the RegExp literal
              bool rx = lexer.scanRegExp();
              if (!rx) {
                  checkerState = Intermediate;
                  break;
              }
          }

          state_stack [tos] = nt_action (act, lhs [r] - TERMINAL_COUNT);
        }

      else
        {
          if (saved_yytoken == -1 && automatic (&lexer, yytoken) && t_action (state, T_AUTOMATIC_SEMICOLON) > 0)
            {
              saved_yytoken = yytoken;
              yytoken = T_SEMICOLON;
              continue;
            }

          else if ((state == INITIAL_STATE) && (yytoken == 0)) {
              // accept empty input
              yytoken = T_SEMICOLON;
              continue;
          }

          int ers = state;
          int shifts = 0;
          int reduces = 0;
          int expected_tokens [3];
          for (int tk = 0; tk < TERMINAL_COUNT; ++tk)
            {
              int k = t_action (ers, tk);

              if (! k)
                continue;
              else if (k < 0)
                ++reduces;
              else if (spell [tk])
                {
                  if (shifts < 3)
                    expected_tokens [shifts] = tk;
                  ++shifts;
                }
            }

          error_message.clear ();
          if (shifts && shifts < 3)
            {
              bool first = true;

              for (int s = 0; s < shifts; ++s)
                {
                  if (first)
                    error_message += QLatin1String ("Expected ");
                  else
                    error_message += QLatin1String (", ");

                  first = false;
                  error_message += QLatin1Char('`');
                  error_message += QLatin1String (spell [expected_tokens [s]]);
                  error_message += QLatin1Char('\'');
                }
            }

          if (error_message.isEmpty())
              error_message = lexer.errorMessage();

          error_lineno = lexer.startLineNo();
          error_column = lexer.startColumnNo();
          checkerState = Error;
          break;
        }
    }

  if (checkerState == Error) {
      if (lexer.error() == QScript::Lexer::UnclosedComment)
          checkerState = Intermediate;
      else if (yytoken == 0)
          checkerState = Intermediate;
  }
  return Result(checkerState, error_lineno, error_column, error_message);
}
bool SVGPathParser::parse(SVGPathLexer *lexer)
{
  const int INITIAL_STATE = 0;

  int yytoken = -1;

  reallocateStack();

  m_tos = 0;
  m_stateStack[++m_tos] = INITIAL_STATE;

  while (true) {
      const int state = m_stateStack.at(m_tos);
      if (yytoken == -1 && - TERMINAL_COUNT != action_index [state])
        yytoken = lexer->lex();
      int act = t_action (state, yytoken);
      if (act == ACCEPT_STATE)
        return true;

      else if (act > 0) {
          if (++m_tos == m_stateStack.size())
            reallocateStack();
          m_stateStack[m_tos] = act;
          yytoken = -1;
      } else if (act < 0) {
          int r = - act - 1;

          m_tos -= rhs [r];
          act = m_stateStack.at(m_tos++);

          switch (r) {
 case 0: {
    //qDebug() << " got path_data ";
} break;  case 2: {
    //qDebug() << " got moveto_drawto_command_groups ";
} break;  case 4: {
    //qDebug() << " got moveto_drawto_command_group ";
} break;  case 6: {
    //qDebug() << " got drawto_commands  ";
} break;  case 16: {
    //qDebug() << " got drawto_command  ";
} break;  case 17: {
    //qDebug() << "							got moveto ";
} break;  case 20: {
    //qDebug() << " got moveto_argument_sequence ";
} break;  case 21: {
    //qDebug() << "							got lineto ";
} break;  case 24: {
    //qDebug() << " got lineto_argument_sequence  ";
} break;  case 25: {
    //qDebug() << "							got horizontal_lineto ";
} break;  case 28: {
    //qDebug() << " got horizontal_lineto_argument_sequence ";
} break;  case 29: {
    //qDebug() << "							got vertical_lineto ";
} break;  case 32: {
    //qDebug() << " got vertical_lineto_argument_sequence ";
} break;  case 33: {
    //qDebug() << "							got curveto ";
} break;  case 36: {
    //qDebug() << " got curveto_argument_sequence 3 ";
} break;  case 40: {
    //qDebug() << " got curveto_argument ";
} break;  case 41: {
    //qDebug() << "							got smooth_curveto ";
} break;  case 44: {
    //qDebug() << " got smooth_curveto_argument_sequence 3 ";
} break;  case 46: {
    //qDebug() << " got smooth_curveto_argument  ";
} break;  case 47: {
    //qDebug() << "							got quadratic_bezier_curveto ";
} break;  case 50: {
    //qDebug() << " got quadratic_bezier_curveto_argument ";
} break;  case 52: {
    //qDebug() << " got quadratic_bezier_curveto_argument ";
} break;  case 53: {
    //qDebug() << "							got elliptical_arc ";
} break;  case 56: {
    //qDebug() << " got elliptical_arc_argument_sequence ";
} break;  case 57: {
    //qDebug() << " got elliptical_arc_argument ";
} break;  case 58: {
    //qDebug() << "							got smooth_quadratic_bezier_curveto ";
} break;  case 61: {
    //qDebug() << " got smooth_quadratic_bezier_curveto_argument_sequence 3 ";
} break;  case 63: {
    //qDebug() << " got coordinate_pair ";
} break;  case 64: {
    //qDebug() << " got x coordinate ";
} break;  case 65: {
    //qDebug() << " got y coordinate ";
} break;  case 67: {
    //qDebug() << " got comma_wsp 3 ";
} break;  case 68: {
    //qDebug() << " got wspplus ";
} break;  
case 69: {
    //qDebug() << " got coordinate ";
    m_symStack.append(lexer->currentNumber());
} break; 
 
case 70: {
    //qDebug() << " got nonnegative_number ";
    //not presently checking this is non-negative
    m_symStack.append(lexer->currentNumber());
} break; 
 
case 71: {
    //qDebug() << " got number ";
    m_symStack.append(lexer->currentNumber());
} break; 
 
case 72: {
    //qDebug() << " got flag ";
    //not presently checking this is only 0 or 1
    m_symStack.append(lexer->currentNumber());
} break; 
 
case 73: {
    //qDebug() << "							got moveto command ";
    m_symStack.append(lexer->currentCommand());
} break; 
 
case 74: {
    //qDebug() << "							got lineto command ";
    m_symStack.append(lexer->currentCommand());
} break; 
 
case 75: {
    //qDebug() << "							got horizontal_lineto command ";
    m_symStack.append(lexer->currentCommand());
} break; 
 
case 76: {
    //qDebug() << "							got vertical_lineto command ";
    m_symStack.append(lexer->currentCommand());
} break; 
 
case 77: {
    //qDebug() << "							got curveto command ";
    m_symStack.append(lexer->currentCommand());
} break; 
 
case 78: {
    //qDebug() << "							got smooth curveto command ";
    m_symStack.append(lexer->currentCommand());
} break; 
 
case 79: {
    //qDebug() << "							got quadratic_bezier_curveto_command command ";
    m_symStack.append(lexer->currentCommand());
} break; 
 
case 80: {
    //qDebug() << "							got smooth_quadratic_bezier_curveto_command command ";
    m_symStack.append(lexer->currentCommand());
} break; 
 case 81: {
    //qDebug() << "							got elliptical_arc_command ";
    m_symStack.append(lexer->currentCommand());
} break;  case 82: {
    //qDebug() << "							got closepath ";
    m_symStack.append(lexer->currentCommand());
} break;  case 83: {
    //qDebug() << "							got fakeclosepath ";
} break; 
          } // switch

          m_stateStack[m_tos] = nt_action(act, lhs[r] - TERMINAL_COUNT);

      } else {
          int ers = state;
          int shifts = 0;
          int reduces = 0;
          int expected_tokens[3];
          for (int tk = 0; tk < TERMINAL_COUNT; ++tk) {
              int k = t_action(ers, tk);

              if (! k)
                continue;
              else if (k < 0)
                ++reduces;
              else if (spell[tk]) {
                  if (shifts < 3)
                    expected_tokens[shifts] = tk;
                  ++shifts;
              }
          }

          m_errorMessage.clear();
          if (shifts && shifts < 3) {
              bool first = true;

              for (int s = 0; s < shifts; ++s) {
                  if (first)
                    m_errorMessage += QLatin1String("Expected ");
                  else
                    m_errorMessage += QLatin1String(", ");

                  first = false;
                  m_errorMessage += QLatin1String("`");
                  m_errorMessage += QLatin1String(spell[expected_tokens[s]]);
                  m_errorMessage += QLatin1String("'");
              }
          }

          return false;
        }
    }

    return false;
}
Example #5
0
bool ExpressionParser::parse(AbstractLexer *lexer)
{
  const int INITIAL_STATE = 0;
  int yytoken = -1;

  reallocateStack();

  m_tos = 0;
  m_stateStack[++m_tos] = INITIAL_STATE;

  for(;;) {
      const int state = m_stateStack.at(m_tos);
      if (yytoken == -1 && - TERMINAL_COUNT != action_index [state]) {
        yytoken = lexer->lex();
        while(yytoken==tComment) {
            //TODO: Add mathml comment
            m_comments.append(lexer->current.val);
            yytoken = lexer->lex();
        }
        
        if(!lexer->error().isEmpty()) {
            m_err += lexer->error();
            return false;
        }
      }
      int act = t_action (state, yytoken);
      if (act == ACCEPT_STATE)
        return true;
      else if (act > 0) {
          if (++m_tos == m_stateStack.size())
            reallocateStack();
          m_stateStack[m_tos] = act;
          yytoken = -1;
      } else if (act < 0) {
          int r = - act - 1;

          m_tos -= rhs [r];
          act = m_stateStack.at(m_tos++);
          switch (r) {

#line 209 "exp.g"
 case 0: 
#line 210 "exp.g"
 case 1: 
#line 212 "exp.g"

case 2:
    m_exp = "<math>"+sym(1)+"</math>";
    break;

#line 218 "exp.g"
 case 3: sym(1) = "<declare><ci>"+sym(1)+"</ci>"+sym(3)+"</declare>"; break; 
#line 221 "exp.g"
 case 4: 
#line 222 "exp.g"
 case 5: 
#line 223 "exp.g"
 case 6: 
#line 225 "exp.g"

case 7:
    sym(1) = lexer->current.val;
    break;

#line 234 "exp.g"

case 10:
    sym(1) = "<ci>"+sym(1)+"</ci>";
    break;

#line 243 "exp.g"

case 13:
    sym(1) = "<apply><selector />"+sym(3)+sym(1)+"</apply>";
    break;

#line 250 "exp.g"

case 14:
    sym(1) = sym(2);
    break;

#line 260 "exp.g"
 case 19: sym(1)=sym(2); break; 
#line 261 "exp.g"
 case 20: sym(1)=funcToTag(sym(1)); break; 
#line 263 "exp.g"
 case 21: sym(1) = "<apply>"+sym(1)+sym(2)+"</apply>"; break; 
#line 264 "exp.g"
 case 22: sym(1) = "<apply>"+sym(1)+sym(3)+"</apply>"; break; 
#line 265 "exp.g"
 case 23: sym(1) = "<apply>"+sym(1)+       "</apply>"; break; 
#line 267 "exp.g"
 case 24: sym(1) = "<apply>"+sym(3)+sym(1)+"</apply>"; break; 
#line 268 "exp.g"
 case 25: sym(1) = "<apply>"+sym(3)+sym(1)+"</apply>"; break; 
#line 275 "exp.g"

case 29:
    sym(1).prepend(sym(3));
    break;

#line 282 "exp.g"

case 30:
    sym(1)=sym(3)+sym(5)+sym(1);
    break;

#line 289 "exp.g"

case 31:
    sym(1)=sym(3)+"<domainofapplication>"+sym(5)+"</domainofapplication>"+sym(1);
    break;

#line 297 "exp.g"

case 32:
    sym(1) = '<'+sym(1)+" />";
    break;

#line 304 "exp.g"

case 33:
    sym(1) = '<'+sym(1)+'>'+sym(3)+"</"+sym(1)+'>';
    break;

#line 312 "exp.g"

case 34:
    sym(1) = "<lambda>"+sym(1)+sym(3)+"</lambda>";
    break;

#line 320 "exp.g"

case 35:
    sym(1) = "<apply><minus />"+sym(2)+"</apply>";
    break;

#line 327 "exp.g"

case 36:
    sym(1) = "<otherwise>"+sym(2)+"</otherwise>";
    break;

#line 334 "exp.g"
 case 37: sym(1) = "<apply><plus />"  +sym(1)+sym(3)+"</apply>"; break; 
#line 335 "exp.g"
 case 38: sym(1) = "<apply><minus />" +sym(1)+sym(3)+"</apply>"; break; 
#line 336 "exp.g"
 case 39: sym(1) = "<apply><times />" +sym(1)+sym(3)+"</apply>"; break; 
#line 337 "exp.g"
 case 40: sym(1) = "<apply><divide />"+sym(1)+sym(3)+"</apply>"; break; 
#line 338 "exp.g"
 case 41: sym(1) = "<apply><power />" +sym(1)+sym(3)+"</apply>"; break; 
#line 339 "exp.g"
 case 42: sym(1) = "<apply><eq />"    +sym(1)+sym(3)+"</apply>"; break; 
#line 340 "exp.g"
 case 43: sym(1) = "<apply><leq />"   +sym(1)+sym(3)+"</apply>"; break; 
#line 341 "exp.g"
 case 44: sym(1) = "<apply><geq />"   +sym(1)+sym(3)+"</apply>"; break; 
#line 342 "exp.g"
 case 45: sym(1) = "<apply><lt />"    +sym(1)+sym(3)+"</apply>"; break; 
#line 343 "exp.g"
 case 46: sym(1) = "<apply><gt />"    +sym(1)+sym(3)+"</apply>"; break; 
#line 344 "exp.g"
 case 47: sym(1) = "<apply><neq />"   +sym(1)+sym(3)+"</apply>"; break; 
#line 345 "exp.g"
 case 48: sym(1) = "<apply><times />" +sym(1)+sym(2)+"</apply>"; break; 
#line 346 "exp.g"
 case 49: sym(1) = "<apply><power />" +sym(1)+sym(2)+"</apply>"; break; 
#line 348 "exp.g"
 case 50: sym(1) = "<piece>"+sym(3)+sym(1)+"</piece>"; break; 
#line 353 "exp.g"

case 52:
    sym(1) += sym(3);
    break;

#line 362 "exp.g"

case 54:
    sym(1) = sym(2);
    break;

#line 369 "exp.g"

case 55:
    sym(1) = "<bvar><ci>"+sym(1)+"</ci></bvar>";
    break;

#line 376 "exp.g"

case 56:
    sym(1) += sym(3);
    break;

#line 383 "exp.g"

case 57:
    sym(1) += sym(3);
    break;

#line 390 "exp.g"

case 58:
    sym(1) = "<uplimit>"+sym(3)+"</uplimit><downlimit>"+sym(1)+"</downlimit>";
    break;

#line 396 "exp.g"

        } // switch
        m_stateStack[m_tos] = nt_action(act, lhs[r] - TERMINAL_COUNT);
    } else {
        int ers = state;
        int shifts = 0;
        int reduces = 0;
        int expected_tokens[3];
        for (int tk = 0; tk < TERMINAL_COUNT; ++tk) {
            int k = t_action(ers, tk);

            if (! k)
                continue;
            else if (k < 0)
                ++reduces;
            else if (spell[tk]) {
                if (shifts < 3)
                expected_tokens[shifts] = tk;
                ++shifts;
            }
        }

        m_errorLineNumber = lexer->lineNumber();
        int tokFoundType=lexer->current.type;
        QString error;
        
        if (shifts && shifts<3) {
            QString tokFound(spell[tokFoundType]);
            QStringList expectedTokens;
            for (int s = 0; s < shifts; ++s) {
                expectedTokens += '\''+QLatin1String(spell[expected_tokens[s]])+'\'';
            }
            error=QCoreApplication::translate("error message", "Expected %1 instead of '%2'").arg(expectedTokens.join(QCoreApplication::tr(", ")), tokFound);
        } else if(tokFoundType==tLpr) {
            error=QCoreApplication::tr("Missing right parenthesis");
        } else if(tokFoundType==tRpr || tokFoundType==tRcb) {
            error=QCoreApplication::tr("Unbalanced right parenthesis");
        } else
            if(tokFoundType==tId)
                error=QCoreApplication::tr("Unexpected token identifier: %1").arg(lexer->current.val);
            else
                error=QCoreApplication::tr("Unexpected token %1").arg(spell[tokFoundType]);
        m_err.append(error);
        return false;
        }
    }

    return false;
}