Exemplo n.º 1
0
image_buf extract_frame( const media::image_frame &f, const std::vector<std::string> &chans )
{
	size_t nC = chans.size();
	if ( f.size() < nC )
		throw_runtime( "Request for {0} channels/planes, but media image only has {1}", nC, f.size() );

	image_buf r;

	int64_t px = 0, py = 0;
	for ( size_t i = 0, N = nC; i != N; ++i )
	{
		if ( ! f.has_channel( chans[i] ) )
			throw_runtime( "media image does not have channel '{0}'", chans[i] );
		const media::image_buffer &ib = f[chans[i]];
		if ( i == 0 )
		{
			px = ib.width();
			py = ib.height();
		}
		else if ( px != ib.width() )
			throw_runtime( "media image channel {0} width {1} does not match channel 0 width {2}", i, ib.width(), px );
		else if ( py != ib.height() )
			throw_runtime( "media image channel {0} height {1} does not match channel 0 width {2}", i, ib.height(), py );

		// TODO: do we want this?
		//r.add_plane( plane( "m.conv_to_plane", d, f, c[i] ) );
		// need to add hash functions, etc.
		plane p( px, py );
		for ( int64_t y = 0; y < py; ++y )
			ib.get_scanline( y, p.line( static_cast<int>( y ) ), 1 );
		r.add_plane( std::move( p ) );
	}
	return r;
}
Exemplo n.º 2
0
void json::parse_array( std::istream_iterator<char> &it, std::istream_iterator<char> &end, int &line )
{
	clear();

	skip_whitespace( it, end, line );
	if ( *it != '[' )
		throw_runtime( "invalid json value at line {0}", line );
	++it;

	json_array arr;
	json val;
	while ( it != end )
	{
		skip_whitespace( it, end, line );
		if ( *it == ']' )
			break;

		val.parse_value( it, end, line );
		arr.push_back( val );

		skip_whitespace( it, end, line );
		if ( *it == ']' )
			break;

		if ( *it != ',' )
			throw_runtime( "expected ',' or ']' in json array at line {0}", line );
		++it;
	}

	if ( *it != ']' )
		throw_runtime( "unterminated json array at line {0}", line );
	++it;

	set<json_array>( std::move( arr ) );
}
Exemplo n.º 3
0
std::shared_ptr<media::image_frame>
to_frame( const image_buf &i, const std::vector<std::string> &chans, const std::string &type )
{
	if ( i.size() < chans.size() )
		throw_runtime( "image does not have enough channels ({0}) for requested channel list size ({1})", i.size(), chans.size() );

	engine::dimensions d = i.dims();
	std::shared_ptr<media::image_frame> r = std::make_shared<media::image_frame>( d.x, d.y );
	for ( size_t c = 0, nC = chans.size(); c != nC; ++c )
	{
		const plane &p = i[c];
		media::image_buffer ib;
		if ( type == "f16" )
		{
			ib = media::image_buffer::simple_buffer<base::half>( p.width(), p.height() );
		}
		else if ( type == "u16" )
		{
			ib = media::image_buffer::simple_buffer<uint16_t>( p.width(), p.height() );
		}
		else
			throw_runtime( "Unknown/unhandled data type tag" );

		for ( int64_t y = 0, h = p.height(); y < h; ++y )
		{
			const float *pL = p.line( static_cast<int>( y ) );
			ib.set_scanline( y, pL, 1 );
		}
		r->add_channel( chans[c], ib );
	}
	return r;
}
Exemplo n.º 4
0
void
cmd_line::parse( const std::vector<char *> &args )
{
	size_t current = 0;

	while ( current < args.size() )
	{
		char *arg = args[current];

		if ( std::strcmp( arg, "--" ) == 0 )
		{
			++current;
			break;
		}

		// Try each option in order...
		bool matched = false;
		for ( auto &opt: _options )
		{
			if ( opt.match( arg ) )
			{
				matched = opt.call( current, args );
				if ( matched )
					break;
			}
		}

		if ( !matched )
			throw_runtime( "unknown option: {0}", arg );
	}

	// After the "--", no options allowed (only arguments)
	while ( current < args.size() )
	{
		char *arg = args[current];
		bool matched = false;
		for ( auto &opt: _options )
		{
			if ( opt.is_non_option() )
				matched = opt.call( current, args );
		}
		if ( !matched )
			throw_runtime( "extra argument: {0}", arg );
	}

	// Check for required options
	for ( auto &opt: _options )
	{
		if ( opt.required() && !opt )
			throw_runtime( "required option '{0}' missing", opt.name() );
	}
}
Exemplo n.º 5
0
std::string api::get_version( void )
{
	auto str = reinterpret_cast<const char *>( glGetString( GL_VERSION ) );
	if ( str == nullptr )
		throw_runtime( "glGetString( GL_VENDOR ) error" );
	return str;
}
Exemplo n.º 6
0
std::string api::get_renderer( void )
{
	auto str = reinterpret_cast<const char *>( glGetString( GL_RENDERER ) );
	if ( str == nullptr )
		throw_runtime( "glGetString( GL_RENDERER ) error" );
	return str;
}
Exemplo n.º 7
0
void unit_test::run( const std::string &n )
{
	auto it = _tests.find( n );
	if ( it == _tests.end() )
		throw_runtime( "test '{0}' not found", n );
	run( it );
}
Exemplo n.º 8
0
std::string api::get_shading_version( void )
{
	auto str = reinterpret_cast<const char *>( glGetString( GL_SHADING_LANGUAGE_VERSION ) );
	if ( str == nullptr )
		throw_runtime( "glGetString( GL_SHADING_LANGUAGE_VERSION ) error" );
	return str;
}
Exemplo n.º 9
0
const any &
computed_base::compute( void ) const
{
	if ( ! _graph )
		throw_runtime( "No graph to compute with" );
	std::unique_lock<std::mutex> lk( _graph->_value_get_mutex );
	return _graph->get_value( _id );
}
Exemplo n.º 10
0
void json::parse_null( std::istream_iterator<char> &it, std::istream_iterator<char> &end, int &line )
{
	clear();

	const char *next = "null";
	while ( it != end && *next != '\0' )
	{
		if ( *it != *next )
			throw_runtime( "invalid json value at line {0}", line );
		++next;
		++it;
	}

	if ( *next != '\0' )
		throw_runtime( "invalid json value at line {0}", line );

	set<json_null>();
}
Exemplo n.º 11
0
void json::parse_object( std::istream_iterator<char> &it, std::istream_iterator<char> &end, int &line )
{
	clear();

	skip_whitespace( it, end, line );
	if ( *it != '{' )
		throw_runtime( "invalid json value at line {0}", line );
	++it;

	json_object obj;
	json name;
	json val;
	skip_whitespace( it, end, line );
	if ( *it != '}' )
	{
		while ( it != end )
		{
			name.parse_string( it, end, line );
			skip_whitespace( it, end, line );
			if ( *it != ':' )
				throw_runtime( "expected ':' in json object at line {0} (got {1})", line, *it );
			++it;
			skip_whitespace( it, end, line );
			val.parse_value( it, end, line );

			obj[name.get<std::string>()] = std::move( val );

			skip_whitespace( it, end, line );
			if ( *it == '}' )
				break;

			if ( *it != ',' )
				throw_runtime( "expected ',' or '}' in json object at line {0} (got {1})", line, *it );
			++it;
		}
	}

	if ( *it != '}' )
		throw_runtime( "unterminated json object at line {0}", line );
	++it;

	set<json_object>( std::move( obj ) );
}
Exemplo n.º 12
0
const cmd_line::option &
cmd_line::operator[]( const char *n ) const
{
	for ( const auto &opt: _options )
	{
		if ( opt.name().compare( n ) == 0 )
			return opt;
	}
	throw_runtime( "option '{0}' not found", n );
}
Exemplo n.º 13
0
void unix_streambuf::initFD( std::ios_base::openmode m )
{
	if ( _fd == -1 )
	{
		if ( _path.empty() )
			throw_logic( "invalid path to open file" );

		int flags = 0;

		std::ios_base::openmode mnoate = m & ~std::ios_base::ate;

		// under unix, don't care about the binary flag either
		mnoate = mnoate & ~std::ios_base::binary;

		if ( mnoate == std::ios_base::in )
			flags = O_RDONLY;
		else if ( mnoate == std::ios_base::out ||
				  mnoate == (std::ios_base::out|std::ios_base::trunc) )
			flags = O_WRONLY | O_CREAT | O_TRUNC;
		else if ( mnoate == std::ios_base::app ||
				  mnoate == (std::ios_base::out|std::ios_base::app) )
			flags = O_WRONLY | O_APPEND;
		else if ( mnoate == (std::ios_base::in|std::ios_base::out) )
			flags = O_RDWR;
		else if ( mnoate == (std::ios_base::in|std::ios_base::out|std::ios_base::trunc) )
			flags = O_RDWR | O_CREAT | O_TRUNC;
		else if ( mnoate == (std::ios_base::in|std::ios_base::out|std::ios_base::app) ||
				  mnoate == (std::ios_base::in|std::ios_base::app) )
			flags = O_RDWR | O_APPEND | O_CREAT;

		mode_t mode = (S_IRUSR|S_IWUSR) | (S_IRGRP|S_IWGRP) | (S_IROTH|S_IWOTH);
		int nfd = ::open( _path.c_str(), flags, mode );
		if ( nfd < 0 )
			throw_runtime( "unable to open '{0}'", _path );
		_fd = nfd;
	}

	if ( m & std::ios_base::ate )
	{
		if ( this->seek( 0, std::ios_base::end ) == -1 )
			throw_runtime( "unable to seek to the end of the stream" );
	}
}
Exemplo n.º 14
0
void context::init( HWND hwnd )
{
	precondition( _hrc == nullptr, "expect uninitialized context" );
	_hdc = GetDC( hwnd );
	_hrc = doCreate( _hdc, coreContext );

	wglMakeCurrent( _hdc, _hrc );

	if ( !gl3wIsSupported( 3, 3 ) )
		throw_runtime( "OpenGL 3.3 not supported" );
}
Exemplo n.º 15
0
void unit_test::run( std::map<std::string,std::function<void(void)>>::iterator &it )
{
	precondition( it != _tests.end(), "invalid test" );

	// Make sure we are not already running...
	const std::string &n = it->first;
	if ( std::find( _running.begin(), _running.end(), n ) == _running.end() )
		_running.emplace_back( n );
	else
		throw_runtime( "recursive tests not allowed: {0}", infix_separated( ", ", _running ) );
	on_scope_exit { _running.erase( std::remove( _running.begin(), _running.end(), n ), _running.end() ); };

	// Already completed
	if ( _success.find( n ) != _success.end() )
		return;
	if ( _failure.find( n ) != _failure.end() )
		throw_runtime( "test '{0}' has failed", n );

	// Run the test
	size_t failures = _failure.size();
	try
	{
		it->second();
	}
	catch ( std::exception &e )
	{
		std::stringstream tmp;
		base::print_exception( tmp, e );
		std::vector<std::string> lines;
		base::split( tmp.str(), '\n', std::back_inserter( lines ), true );
		for ( auto &l: lines )
			message( l );
		failure( std::string( e.what() ) );
	}

	if ( failures != _failure.size() )
		_failure.insert( n );
	else
		_success.insert( n );
}
Exemplo n.º 16
0
void web_base::read_content( net::tcp_socket &socket )
{
	auto te = _header.find( "Transfer-Encoding" );
	auto cl = _header.find( "Content-Length" );
	if ( te != _header.end() )
	{
		if ( te->second == "chunked" )
		{
			size_t size = 0;
			do
			{
				std::string line = read_line( socket );
				size_t pos = 0;
				size = std::stoul( line, &pos, 16 );
				if ( size > 0 )
				{
					size_t off = _content.size();
					_content.resize( _content.size() + size );
					socket.read( &_content[off], size );
					char c[2];
					socket.read( &c, 2 );
					if ( c[0] != '\r' || c[1] != '\n' )
						throw_runtime( "invalid HTTP chunk" );
				}
			} while ( size > 0 );

		}
		else
			throw_runtime( "unknown Transfer-Encoding: {0}", te->second );
		/// @todo Read trailer headers
	}
	else if ( cl != _header.end() )
	{
		size_t size = std::stoul( cl->second, nullptr, 10 );
		size_t off = _content.size();
		_content.resize( _content.size() + size );
		socket.read( &_content[off], size );
	}
}
Exemplo n.º 17
0
bool
cmd_line::multi( option &opt, size_t &idx, const std::vector<char *> &args )
{
	if ( !opt.is_non_option() )
		++idx;

	if ( idx >= args.size() || args[idx][0] == '-' )
		throw_runtime( "option '{0}' expected at least 1 value", opt.name() );

	opt.add_value( args[idx] );
	++idx;

	return true;
}
Exemplo n.º 18
0
void address::lookup_name( const char *name )
{
	precondition( name, "null lookup name" );
	precondition( name[0] != '\0', "empty lookup name" );

    struct addrinfo hints;
    ::memset( &hints, 0, sizeof(hints) );
    hints.ai_family = AF_INET;

    struct addrinfo *res0 = NULL;
    int err = EAI_AGAIN;
    while ( err == EAI_AGAIN )
        err = getaddrinfo( name, NULL, &hints, &res0 );

    if ( err == 0 )
    {
        struct addrinfo *res = res0;
        for ( ; res; res=res->ai_next )
        {
            if ( res->ai_addrlen == sizeof(struct sockaddr_in) )
                break;
        }

        if ( res )
        {
            struct sockaddr_in *a = reinterpret_cast<struct sockaddr_in *>( res->ai_addr );
            _addr = ntohl( a->sin_addr.s_addr );
        }

        freeaddrinfo( res0 );

        if ( !res )
            throw_runtime( "no valid address found" );
    }
    else
        throw_runtime( "lookup name {0}: {1}", name, gai_strerror( err ) );
}
Exemplo n.º 19
0
void json::parse_number( std::istream_iterator<char> &it, std::istream_iterator<char> &end, int &line )
{
	clear();
	std::string n;
	while ( it != end )
	{
		char c = *it;
		if ( std::isdigit( c ) || c == '.' || c == 'e' || c == 'E' || c == '-' || c == '+' )
			n.push_back( c );
		else
			break;
		++it;
	}

	if ( n.empty() )
		throw_runtime( "invalid json value at line {0} (character {1})", line, *it );

	size_t pos = 0;
	double x = std::stod( n, &pos );
	if ( pos < n.size() )
		throw_runtime( "invalid json number \"{0}\" at line {1}", n, line );

	set<json_number>( std::make_pair( x, n ) );
}
Exemplo n.º 20
0
std::string web_base::read_line( net::tcp_socket &socket )
{
	char c = '\0';
	socket.read( &c, 1 );

	std::string result;
	while ( c != '\r' )
	{
		result.push_back( c );
		socket.read( &c, 1 );
	}
	socket.read( &c, 1 );
	if ( c != '\n' )
		throw_runtime( "invalid HTTP line" );
	return result;
}
Exemplo n.º 21
0
void expr_parser::next_token( void )
{
	if ( _gottoken )
		_it.next();

	if ( !_it )
	{
		_token = std::make_pair( std::u32string(), std::make_shared<end_operator>() );
		return;
	}

	_gottoken = true;
	if ( _it.type() == TOK_OPERATOR )
	{
		std::u32string opname = _it.value();

		auto oplookup = operators.find( opname );
		while ( !opname.empty() && oplookup == operators.end() )
		{
			opname.pop_back();
			oplookup = operators.find( opname );
		}

		if ( oplookup == operators.end() )
			throw_runtime( "unknown operator '{0}'", _it.value() );

		_it.split( opname );

		std::shared_ptr<base_operator> op( oplookup->second );
		_token = std::make_pair( opname, op );
	}
	else
	{
		auto op = std::make_shared<primary_operator>( _primary() );
		_gottoken = false;
		if ( op->expression() )
			_token = std::make_pair( _it.value(), op );
		else
			_token = std::make_pair( std::u32string(), std::make_shared<end_operator>() );
	}
}
Exemplo n.º 22
0
void parser::parse_expr( std::istream &in )
{
	precondition( in.get() == '[', "missing '[' to start expression" );
	int count = 0;
	while ( !in.eof() && in )
	{
		int c = in.get();
		if ( std::char_traits<char>::not_eof( c ) )
		{
			if ( c == ']' )
			{
				if ( count == 0 )
				{
					_func.push_expr();
					break;
				}
				_func.add( static_cast<char>( c ) );
				--count;
			}
			else if ( c == '[' )
			{
				_func.add( static_cast<char>( c ) );
				++count;
			}
			else if ( c == '"' )
			{
				_func.add( static_cast<char>( c ) );
				parse_string( in );
			}
			else
				_func.add( static_cast<char>( c ) );
		}
	}
	if ( in.get() != ']' )
		throw_runtime( "missing ']' in expression" );
}
Exemplo n.º 23
0
void parser::parse_code( std::istream &in )
{
	precondition( in.get() == '%', "missing '%' to start code" );

	// find the first word
	std::string word;
	while ( std::isspace( in.peek() ) )
		in.get();
	while ( std::isalpha( in.peek() ) )
		word.push_back( static_cast<char>( in.get() ) );
	while ( std::isspace( in.peek() ) )
		in.get();

	if ( word == "for" )
	{
		_func.add( "for " );
	}
	else if ( word == "if" )
	{
		_func.add( "if " );
	}
	else if ( word == "else" )
	{
		_func.unindent();
		_func.add( "}\nelse" );
		_func.push_code();
	}
	else if ( word == "code" || word == "endfor" || word == "endif" )
	{
	}
	else
		throw_runtime( "unknown template keyword '{0}'", word );

	int count = 0;
	while ( !in.eof() && in )
	{
		int c = in.get();
		if ( std::char_traits<char>::not_eof( c ) )
		{
			if ( c == ']' )
			{
				if ( count == 0 )
				{
					_func.push_code();
					break;
				}
				_func.add( static_cast<char>( c ) );
				--count;
			}
			else if ( c == '[' )
			{
				_func.add( static_cast<char>( c ) );
				++count;
			}
			else if ( c == '"' )
			{
				_func.add( static_cast<char>( c ) );
				parse_string( in );
			}
			else
				_func.add( static_cast<char>( c ) );
		}
	}

	if ( word == "for" || word == "if" || word == "else" )
	{
		_func.add( "\n{" );
		_func.indent();
	}
	else if ( word == "endfor" || word == "endif" )
	{
		_func.unindent();
		_func.add( '}' );
		_func.push_code();
	}
}
Exemplo n.º 24
0
imgproc::buffer<float,2> png_read( const char *file_name )
{
	FILE *fp = fopen( file_name, "rb" );
	if ( fp == nullptr )
		throw_errno( "error opening file {0}", file_name );
	on_scope_exit
	{
		fclose( fp );
	};

	// read the header
	png_byte header[8];
	fread( header, 1, 8, fp );

	if (png_sig_cmp(header, 0, 8))
		throw_runtime( "error: {0} is not a PNG.\n", file_name );

	png_structp png_ptr = png_create_read_struct( PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr );
	if (!png_ptr)
		throw_runtime( "error: png_create_read_struct returned 0" );
	on_scope_exit
	{
		png_destroy_read_struct( &png_ptr, nullptr, nullptr );
	};

	// create png info struct
	png_infop info_ptr = png_create_info_struct( png_ptr );
	if ( !info_ptr )
		throw_runtime( "error: png_create_info_struct returned 0" );

	// create png info struct
	png_infop end_info = png_create_info_struct(png_ptr);
	if (!end_info)
		throw_runtime( "error: png_create_info_struct returned 0" );

	// the code in this if statement gets called if libpng encounters an error
	if ( setjmp( png_jmpbuf( png_ptr ) ) )
		throw_runtime( "error from libpng" );

	// init png reading
	png_init_io( png_ptr, fp );

	// let libpng know you already read the first 8 bytes
	png_set_sig_bytes( png_ptr, 8 );

	// read all the info up to the image data
	png_read_info( png_ptr, info_ptr );

	// variables to pass to get info
	int bit_depth, color_type;
	png_uint_32 temp_width, temp_height;

	// get info about png
	png_get_IHDR( png_ptr, info_ptr, &temp_width, &temp_height, &bit_depth, &color_type, nullptr, nullptr, nullptr);

	// Update the png info struct.
	png_read_update_info(png_ptr, info_ptr);

	// Row size in bytes.
	int rowbytes = png_get_rowbytes(png_ptr, info_ptr);

	// glTexImage2d requires rows to be 4-byte aligned
	rowbytes += 3 - ((rowbytes-1) % 4);

	// Allocate the image_data as a big block, to be given to opengl
	std::vector<png_byte> image_data;
	image_data.resize( rowbytes * temp_height + 15 );

	// row_pointers is for pointing to image_data for reading the png with libpng
	std::vector<png_bytep> row_pointers;
	row_pointers.resize( temp_height );

	// set the individual row_pointers to point at the correct offsets of image_data
	for ( png_uint_32 i = 0; i < temp_height; i++)
		row_pointers[i] = image_data.data() + i * rowbytes;

	// read the png into image_data through row_pointers
	png_read_image( png_ptr, row_pointers.data() );

	imgproc::buffer<float,2> img( temp_width, temp_height );
	auto *pixs = image_data.data();
	for ( size_t y = 0; y < temp_height; ++y )
	{
		for ( size_t x = 0; x < temp_width; ++x )
		{
			img( x, y ) = float( *pixs ) / float( 255.0 );
			pixs += 4;
		}
	}

	return img;
}
Exemplo n.º 25
0
void iterator::parse_decimal( void )
{
	std::string tmp;
	_type = TOK_INTEGER;

	if ( _value == U"." )
	{
		tmp.push_back( '.' );
		_type = TOK_FLOATING;
	}

	while ( utf::is_number_decimal( _c ) )
	{
		tmp.push_back( '0' + static_cast<char>( utf::integer_value( _c ) ) );
		next_utf();
	}

	if ( _c == '.' )
	{
		tmp.push_back( '.' );
		next_utf();
		while ( utf::is_number_decimal( _c ) )
		{
			tmp.push_back( '0' + static_cast<char>( utf::integer_value( _c ) ) );
			next_utf();
		}
		_type = TOK_FLOATING;
	}

	if ( _c == 'e' || _c == 'E' )
	{
		tmp.push_back( 'e' );
		next_utf();
		if ( _c == '-' )
		{
			tmp.push_back( '-' );
			next_utf();
		}
		if ( _c == '+' )
		{
			tmp.push_back( '+' );
			next_utf();
		}
		while ( utf::is_number_decimal( _c ) )
		{
			tmp.push_back( '0' + static_cast<char>( utf::integer_value( _c ) ) );
			next_utf();
		}
		_type = TOK_FLOATING;
	}

	if ( tmp.empty() )
		tmp.push_back( '0' );
	if ( _type == TOK_INTEGER )
	{
		size_t pos = 0;
		_ival = std::stoull( tmp, &pos );
		if ( pos != tmp.size() )
			throw_runtime( "could not parse floating point number: {0}", tmp );
	}
	else if ( _type == TOK_FLOATING )
	{
		size_t pos = 0;
		_fval = std::stod( tmp, &pos );
		if ( pos != tmp.size() )
			throw_runtime( "could not parse floating point number: {0}", tmp );
	}
}
Exemplo n.º 26
0
void iterator::parse_number( void )
{
	_ival = utf::integer_value( _c );
	if ( _ival == 0 )
	{
		next_utf();
		// Number with different bases
		switch ( _c )
		{
			case 'b':
			case 'B':
				// Binary number
				next_utf();
				while ( utf::is_number_decimal( _c ) )
				{
					uint64_t v = utf::integer_value( _c );
					if ( v > 1 )
						throw_runtime( "expected binary digit" );
					_ival = ( _ival << 1 ) + v;
					next_utf();
				}
				_type = TOK_INTEGER;
				break;

			case 'c':
			case 'C':
				// Octal number
				next_utf();
				while ( utf::is_number_decimal( _c ) )
				{
					uint64_t v = utf::integer_value( _c );
					if ( v > 7 )
						throw_runtime( "expected octal digit" );
					_ival = ( _ival << 3 ) + v;
					next_utf();
				}
				_type = TOK_INTEGER;
				break;

			case 'x':
			case 'X':
				// Hexadecimal number
				next_utf();
				while ( utf::is_hex_digit( _c ) )
				{
					uint64_t v = utf::integer_value( _c );
					_ival = ( _ival << 4 ) + v;
					next_utf();
				}
				_type = TOK_INTEGER;
				break;

			default:
				if ( utf::is_number( _c ) )
					// Unknown base...
					throw_runtime( "0 should be followed by a base letter" );
				else
					// else it's just a 0 by itself, which is okay... continue as a decimal
					parse_decimal();
				break;
		}
	}
	else if ( _ival > 0 )
		parse_decimal();
}
Exemplo n.º 27
0
std::shared_ptr<expr> base_operator::left( expr_parser & /*parser*/, const std::u32string &op, const std::shared_ptr<expr> & /*left*/ )
{
	throw_runtime( "expected operand, got '{0}'", op );
}
Exemplo n.º 28
0
void expr_parser::match( std::u32string &op )
{
	if ( op != _token.first )
		throw_runtime( "expected '{0}', got '{1}'", op, _token.first );
	next_token();
}
Exemplo n.º 29
0
void parser::parse_directive( std::istream &in )
{
	precondition( in.get() == '#', "missing '#' to start code" );

	// find the first word
	std::string word;
	while ( std::isspace( in.peek() ) )
		in.get();
	while ( std::isalpha( in.peek() ) )
		word.push_back( static_cast<char>( in.get() ) );
	while ( std::isspace( in.peek() ) )
		in.get();

	int count = 0;
	std::string rest;
	while ( !in.eof() && in )
	{
		int c = in.get();
		if ( std::char_traits<char>::not_eof( c ) )
		{
			if ( c == ']' )
			{
				if ( count == 0 )
				{
					break;
				}
				rest.push_back( static_cast<char>( c ) );
				--count;
			}
			else if ( c == '[' )
			{
				rest.push_back( static_cast<char>( c ) );
				++count;
			}
			else if ( c == '"' )
			{
				rest.push_back( static_cast<char>( c ) );
				bool escaped = false;
				while ( !in.eof() && in )
				{
					int sc = in.get();
					if ( std::char_traits<char>::not_eof( sc ) )
					{
						rest.push_back( static_cast<char>( sc ) );
						if ( sc == '"' && !escaped )
							break;
						escaped = false;
						if ( sc == '\\' )
							escaped = true;
					}
				}
			}
			else
				rest.push_back( static_cast<char>( c ) );
		}
	}

	if ( word == "include" )
		_includes.push_back( rest );
	else if ( word == "arg" )
		_func.add_arg( rest );
	else if ( word == "function" )
		_func.set_name( rest );
	else
		throw_runtime( "unknown directive '{0}'", word );

	if ( in.peek() == '\n' )
		in.get();
}
Exemplo n.º 30
0
void json::parse_string( std::istream_iterator<char> &it, std::istream_iterator<char> &end, int &line )
{
	clear();

	std::string n;

	if ( *it != '"' )
		throw_runtime( "invalid json value at line {0}", line );
	++it;

	while ( it != end )
	{
		char c = *it;
		if ( c == '"' )
			break;
		if ( c == '\\' )
		{
			++it;
			if ( it == end )
				throw_runtime( "unterminated json string at line {0}", line );

			c = *it;
			switch ( c )
			{
				case 'b': n.push_back( '\b' ); break;
				case 'f': n.push_back( '\f' ); break;
				case 'n': n.push_back( '\n' ); break;
				case 't': n.push_back( '\t' ); break;
				case 'r': n.push_back( '\r' ); break;
				case 'u':
				{
					uint32_t u = 0;
					for ( size_t i = 0; i < 4; ++i )
					{
						++it;
						if ( it == end )
							throw_runtime( "unterminated unicode at end of string" );
						c = static_cast<char>( std::toupper( *it ) );
						if ( !std::isxdigit( c ) )
							throw_runtime( "expected hex digit, got {0}", c );
						u = ( u << 4 ) + uint32_t( (c > '9') ? (c & ~0x20) - 'A' + 10: ( c - '0' ) );
					}
					n.push_back( static_cast<char>( u ) );
					break;
				}

				default:
					n.push_back( c );
					break;
			}
		}
		else
			n.push_back( c );
		++it;
	}

	if ( *it != '"' )
		throw_runtime( "unterminated json string at line {0}", line );
	++it;

	set<json_string>( n );
}