Example #1
0
void bash_ast::init_parser(const std::string& script_path)
{
  input.reset(antlr3NewAsciiStringInPlaceStream(
    reinterpret_cast<pANTLR3_UINT8>(const_cast<char*>(script.c_str())),
    // We do not support strings longer than the max value of ANTLR3_UNIT32
    boost::numeric_cast<ANTLR3_UINT32>(script.size()),
    NULL));

  if(!input)
    throw libbash::parse_exception("Unable to open file " + script + " due to malloc() failure");

  input->fileName = input->strFactory->newStr(
      input->strFactory,
      reinterpret_cast<pANTLR3_UINT8>(const_cast<char*>(script_path.c_str())));

  lexer.reset(libbashLexerNew(input.get()));
  if(!lexer)
    throw libbash::parse_exception("Unable to create the lexer due to malloc() failure");

  token_stream.reset(antlr3CommonTokenStreamSourceNew(
      ANTLR3_SIZE_HINT, lexer->pLexer->rec->state->tokSource));
  if(!token_stream)
    throw libbash::parse_exception("Out of memory trying to allocate token stream");

  parser.reset(libbashParserNew(token_stream.get()));
  if(!parser)
    throw libbash::parse_exception("Out of memory trying to allocate parser");

  ast = parse(parser.get());
  ast->strFactory->newRaw = &locked_newRaw8;
  ast->strFactory->destroy = &locked_destroy;
  if(parser->pParser->rec->getNumberOfSyntaxErrors(parser->pParser->rec))
    throw libbash::parse_exception("Something wrong happened while parsing");
}
Example #2
0
bool uSQL::SQLParser::parse(const std::string &queryString)
{
  clear();

#if defined(USQL_USE_ANTLR3_STRINGSTREAMNEW)
  pANTLR3_INPUT_STREAM input  = antlr3StringStreamNew(
        (pANTLR3_UINT8)queryString.c_str(), 
        ANTLR3_ENC_UTF8,
        (ANTLR3_UINT32)queryString.length(),
        (pANTLR3_UINT8)"");
#else  
  pANTLR3_INPUT_STREAM input  = antlr3NewAsciiStringInPlaceStream(
        (pANTLR3_UINT8)queryString.c_str(), 
        (ANTLR3_UINT32)queryString.length(),
        (pANTLR3_UINT8)"");
#endif
  
  pSQLLexer lexer = SQLLexerNew(input);
  
  pANTLR3_COMMON_TOKEN_STREAM tokens = antlr3CommonTokenStreamSourceNew(ANTLR3_SIZE_HINT, TOKENSOURCE(lexer));
  pSQLParser parser = SQLParserNew(tokens);
  parser->uSqlParser = this;
  parser->statement_list(parser, this);
  
  bool parserResult = true;
  if (0 < parser->pParser->rec->state->errorCount) {
    parserResult = false;
  }

  parser->free(parser);
  tokens->free(tokens);
  lexer->free(lexer);
  input->close(input);
  
  return parserResult;
}
Example #3
0
char *
rsp_query_parse_sql(const char *rsp_query)
{
  /* Input RSP query, fed to the lexer */
  pANTLR3_INPUT_STREAM query;

  /* Lexer and the resulting token stream, fed to the parser */
  pRSPLexer lxr;
  pANTLR3_COMMON_TOKEN_STREAM tkstream;

  /* Parser and the resulting AST, fed to the tree parser */
  pRSPParser psr;
  RSPParser_query_return qtree;
  pANTLR3_COMMON_TREE_NODE_STREAM nodes;

  /* Tree parser and the resulting SQL query string */
  pRSP2SQL sqlconv;
  pANTLR3_STRING sql;

  char *ret = NULL;

  DPRINTF(E_DBG, L_RSP, "Trying RSP query -%s-\n", rsp_query);

#if ANTLR3C_NEW_INPUT
  query = antlr3StringStreamNew ((pANTLR3_UINT8)rsp_query, ANTLR3_ENC_8BIT, (ANTLR3_UINT64)strlen(rsp_query), (pANTLR3_UINT8)"RSP query");
#else
  query = antlr3NewAsciiStringInPlaceStream ((pANTLR3_UINT8)rsp_query, (ANTLR3_UINT64)strlen(rsp_query), (pANTLR3_UINT8)"RSP query");
#endif
  if (!query)
    {
      DPRINTF(E_DBG, L_RSP, "Could not create input stream\n");
      return NULL;
    }

  lxr = RSPLexerNew(query);
  if (!lxr)
    {
      DPRINTF(E_DBG, L_RSP, "Could not create RSP lexer\n");
      goto lxr_fail;
    }

  tkstream = antlr3CommonTokenStreamSourceNew(ANTLR3_SIZE_HINT, TOKENSOURCE(lxr));
  if (!tkstream)
    {
      DPRINTF(E_DBG, L_RSP, "Could not create RSP token stream\n");
      goto tkstream_fail;
    }

  psr = RSPParserNew(tkstream);
  if (!psr)
    {
      DPRINTF(E_DBG, L_RSP, "Could not create RSP parser\n");
      goto psr_fail;
    }

  qtree = psr->query(psr);

  /* Check for parser errors */
  if (psr->pParser->rec->state->errorCount > 0)
    {
      DPRINTF(E_LOG, L_RSP, "RSP query parser terminated with %d errors\n", psr->pParser->rec->state->errorCount);
      goto psr_error;
    }

  DPRINTF(E_SPAM, L_RSP, "RSP query AST:\n\t%s\n", qtree.tree->toStringTree(qtree.tree)->chars);

  nodes = antlr3CommonTreeNodeStreamNewTree(qtree.tree, ANTLR3_SIZE_HINT);
  if (!nodes)
    {
      DPRINTF(E_DBG, L_RSP, "Could not create node stream\n");
      goto psr_error;
    }

  sqlconv = RSP2SQLNew(nodes);
  if (!sqlconv)
    {
      DPRINTF(E_DBG, L_RSP, "Could not create SQL converter\n");
      goto sql_fail;
    }

  sql = sqlconv->query(sqlconv);

  /* Check for tree parser errors */
  if (sqlconv->pTreeParser->rec->state->errorCount > 0)
    {
      DPRINTF(E_LOG, L_RSP, "RSP query tree parser terminated with %d errors\n", sqlconv->pTreeParser->rec->state->errorCount);
      goto sql_error;
    }

  if (sql)
    {
      DPRINTF(E_DBG, L_RSP, "RSP SQL query: -%s-\n", sql->chars);
      ret = strdup((char *)sql->chars);
    }
  else
    {
      DPRINTF(E_LOG, L_RSP, "Invalid RSP query\n");
      ret = NULL;
    }

 sql_error:
  sqlconv->free(sqlconv);
 sql_fail:
  nodes->free(nodes);
 psr_error:
  psr->free(psr);
 psr_fail:
  tkstream->free(tkstream);
 tkstream_fail:
  lxr->free(lxr);
 lxr_fail:
  query->close(query);

  return ret;
}