//////////////////////////////////////////////////////////////////////////
//
// Skips spaces, tabs, newlines, and comments
//
void Tokenizer::SkipWhiteSpace() {
  while (isspace(CurrentCh) && CurrentCh ) {
    GetCh();
  }

  if( '/' == CurrentCh )  // Look for comments
  {
    GetCh();
    if( '/' == CurrentCh )
    {
      // Throw out everything until the end of the line
      while( '\n' != CurrentCh )
      {
        GetCh();
      }
    }
    else if ( '*' == CurrentCh )
    {
      int startLine = CurLine();
      while( true )
      {
        GetCh();
        if( '*' == CurrentCh )
        {
          GetCh();
          if( CondReadCh( '/' ) )
            break;
          else if ( buffer.isEOF() )
          {
            std::ostringstream ost;
            ost << "Unterminated comment in line ";
            ost << startLine;
            throw SyntaxErrorException( ost.str(), *this );
          }
        }
        else if ( buffer.isEOF() )
        {
          std::ostringstream ost;
          ost << "Unterminated comment in line ";
          ost << startLine;
          throw SyntaxErrorException( ost.str(), *this );
        }
      }
    }
    else
    {
      std::ostringstream ost;
      ost << "unexpected character: '" << CurrentCh << "'";
	  throw SyntaxErrorException( ost.str(), *this );
    }

    SkipWhiteSpace();  // We may need to throw out
                       //  more white space/comments
                       // This is admittedly tail recursion...
  }
}
auto_ptr<Token> Tokenizer::Read(SYMBOL kind) {
  auto_ptr<Token> T( Get() );
  if (T->kind() != kind) {
    string msg( getNameForToken( kind ) );
    msg.append( " expected, " );
	msg.append(getNameForToken( T->kind() ));
	msg.append(" found instead!");
    throw SyntaxErrorException(msg, *this);
  }
  return T;
}
Token* Tokenizer::GetQuotedIdent() {
  GetCh();   // Throw out beginning '"'

  std::ostringstream ident;
  while ( '"' != CurrentCh ) {
    if( '\n' == CurrentCh )
      throw SyntaxErrorException( "Unterminated string constant", *this );

    ident << CurrentCh;
    GetCh();
  }
  GetCh();
  return new IdentToken( ident.str() );
}
Ejemplo n.º 4
0
void AmqpException::Throw(const amqp_connection_close_t& reply)
{
  std::ostringstream what;
  const char* method_name = amqp_method_name(((reply.class_id << 16) | reply.method_id));

  std::string reply_text;
  if (reply.reply_text.bytes != NULL)
  {
    reply_text = std::string((char*)reply.reply_text.bytes, reply.reply_text.len);
  }

  if (method_name != NULL)
  {
    what << "connection error: " << reply.reply_code << ": " << method_name << " caused: " << reply_text;
  }
  else
  {
    what << "connection error: " << reply.reply_code << ": " << reply_text;
  }

  switch (reply.reply_code)
  {
  case ConnectionForcedException::REPLY_CODE:
    throw ConnectionForcedException(what.str(), reply_text, reply.class_id, reply.method_id);
  case InvalidPathException::REPLY_CODE:
    throw InvalidPathException(what.str(), reply_text, reply.class_id, reply.method_id);
  case FrameErrorException::REPLY_CODE:
    throw FrameErrorException(what.str(), reply_text, reply.class_id, reply.method_id);
  case SyntaxErrorException::REPLY_CODE:
    throw SyntaxErrorException(what.str(), reply_text, reply.class_id, reply.method_id);
  case CommandInvalidException::REPLY_CODE:
    throw CommandInvalidException(what.str(), reply_text, reply.class_id, reply.method_id);
  case ChannelErrorException::REPLY_CODE:
    throw ChannelErrorException(what.str(), reply_text, reply.class_id, reply.method_id);
  case UnexpectedFrameException::REPLY_CODE:
    throw UnexpectedFrameException(what.str(), reply_text, reply.class_id, reply.method_id);
  case ResourceErrorException::REPLY_CODE:
    throw ResourceErrorException(what.str(), reply_text, reply.class_id, reply.method_id);
  case NotAllowedException::REPLY_CODE:
    throw NotAllowedException(what.str(), reply_text, reply.class_id, reply.method_id);
  case NotImplementedException::REPLY_CODE:
    throw NotImplementedException(what.str(), reply_text, reply.class_id, reply.method_id);
  case InternalErrorException::REPLY_CODE:
    throw InternalErrorException(what.str(), reply_text, reply.class_id, reply.method_id);
  default:
    throw std::logic_error(std::string("Programming error: unknown connection reply code: ").append(boost::lexical_cast<std::string>(reply.reply_code)));
  }
}
Ejemplo n.º 5
0
/** Load Lua string.
 * Loads the Lua string and places it as a function on top of the stack.
 * @param s string to load
 */
void
LuaContext::load_string(const char *s)
{
  int err;
  if ( (err = luaL_loadstring(__L, s)) != 0 ) {
    std::string errmsg = lua_tostring(__L, -1);
    lua_pop(__L, 1);
    switch (err) {
    case LUA_ERRSYNTAX:
      throw SyntaxErrorException("Lua syntax error in string '%s': %s",
				 s, errmsg.c_str());

    case LUA_ERRMEM:
      throw OutOfMemoryException("Could not load Lua string '%s'", s);
    }
  }
}
Ejemplo n.º 6
0
/** Execute file on a specific Lua state.
 * @param L Lua state to execute the file in.
 * @param filename filet to load and excute.
 */
void
LuaContext::do_file(lua_State *L, const char *filename)
{
  // Load initialization code
  int err = 0;
  std::string errmsg;
  if ( (err = luaL_loadfile(L, filename)) != 0) {
    errmsg = lua_tostring(L, -1);
    lua_pop(L, 1);
    switch (err) {
    case LUA_ERRSYNTAX:
      throw SyntaxErrorException("Lua syntax error in file %s: %s", filename, errmsg.c_str());

    case LUA_ERRMEM:
      throw OutOfMemoryException("Could not load Lua file %s", filename);

    case LUA_ERRFILE:
      throw CouldNotOpenFileException(filename, errmsg.c_str());
    }
  }

  int errfunc = __enable_tracebacks ? 1 : 0;
  if ( (err = lua_pcall(L, 0, LUA_MULTRET, errfunc)) != 0 ) {
    // There was an error while executing the initialization file
    errmsg = lua_tostring(L, -1);
    lua_pop(L, 1);
    switch (err) {
    case LUA_ERRRUN:
      throw LuaRuntimeException("do_file", errmsg.c_str());

    case LUA_ERRMEM:
      throw OutOfMemoryException("Could not execute Lua file %s", filename);

    case LUA_ERRERR:
      throw LuaErrorException("do_file", errmsg.c_str());

    default:
      throw LuaErrorException("do_file/unknown error", errmsg.c_str());
    }
  }

}
Token* Tokenizer::GetPunct() {
  Token* T;

  switch (CurrentCh) {
  case '(':  GetCh(); T = new Token(LPAREN);     break;
  case ')':  GetCh(); T = new Token(RPAREN);     break;
  case '{':  GetCh(); T = new Token(LBRACE);     break;
  case '}':  GetCh(); T = new Token(RBRACE);     break;
  case ',':  GetCh(); T = new Token(COMMA);      break;
  case '=':  GetCh(); T = new Token(EQUALS);     break;
  case ';':  GetCh(); T = new Token(SEMICOLON);  break;

  default:
    std::ostringstream ost;
    ost << "unexpected character: '" << CurrentCh << "'";
    throw SyntaxErrorException(ost.str(), *this);
  }

  return T;
}
Ejemplo n.º 8
0
void neo::LuaContext::executeCode(std::istream& code) {
	std::lock_guard<std::mutex> stateLock(_stateMutex);

	// since the lua_load function requires a static function, we use this structure
	// the Reader structure is at the same time an object storing an istream and a buffer,
	//   and a static function provider
	struct Reader {
		Reader(std::istream& str) : stream(str) {}
		std::istream&		stream;
		char				buffer[512];

		// read function ; "data" must be an instance of Reader
		static const char* read(lua_State* l, void* data, size_t* size) {
			assert(size != nullptr);
			assert(data != nullptr);
			Reader& me = *((Reader*)data);
			if (me.stream.eof())	{ *size = 0; return nullptr; }

			me.stream.read(me.buffer, sizeof(me.buffer));
			*size = size_t(me.stream.gcount());		// gcount could return a value larger than a size_t, but its maximum is sizeof(me.buffer) so there's no problem
			return me.buffer;
		}
	};

	// we create an instance of Reader, and we call lua_load
	std::unique_ptr<Reader> reader(new Reader(code));
	auto loadReturnValue = lua_load(_state, &Reader::read, reader.get(), "chunk");

	// now we have to check return value
	if (loadReturnValue != 0) {
		// there was an error during loading, an error message was pushed on the stack
		const char* errorMsg = lua_tostring(_state, -1);
		lua_pop(_state, 1);
		if (loadReturnValue == LUA_ERRMEM)			throw(std::bad_alloc());
		else if (loadReturnValue == LUA_ERRSYNTAX)	throw(SyntaxErrorException(std::string(errorMsg)));

	} else {
		// calling the loaded function
		_call<std::tuple<>>(std::tuple<>());
	}
}