Boost-Graph
view release on metacpan or search on metacpan
include/boost/wave/cpplexer/validate_universal_char.hpp view on Meta::CPAN
/*=============================================================================
Boost.Wave: A Standard compliant C++ preprocessor library
Grammar for universal character validation (see C++ standard: Annex E)
http://www.boost.org/
Copyright (c) 2001-2005 Hartmut Kaiser. Distributed under the Boost
Software License, Version 1.0. (See accompanying file
LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
=============================================================================*/
#if !defined(VALIDATE_UNIVERSAL_CHAR_HPP_55F1B811_CD76_4C72_8344_CBC69CF3B339_INCLUDED)
#define VALIDATE_UNIVERSAL_CHAR_HPP_55F1B811_CD76_4C72_8344_CBC69CF3B339_INCLUDED
#include <boost/assert.hpp>
#include <boost/wave/util/file_position.hpp>
#include <boost/wave/cpplexer/cpplexer_exceptions.hpp>
///////////////////////////////////////////////////////////////////////////////
namespace boost {
namespace wave {
namespace cpplexer {
namespace impl {
enum universal_char_type {
universal_char_type_valid = 0,
universal_char_type_invalid = 1,
universal_char_type_base_charset = 2,
universal_char_type_not_allowed_for_identifiers = 3
};
namespace {
///////////////////////////////////////////////////////////////////////////
//
// is_range is a helper function for the classification by brute force
// below
//
///////////////////////////////////////////////////////////////////////////
inline bool
in_range(unsigned long ch, unsigned long l, unsigned long u)
{
return (l <= ch && ch <= u);
}
}
///////////////////////////////////////////////////////////////////////////////
//
// classify_universal_char
//
// This function classifies an universal character value into 4 subranges:
// universal_char_type_valid
// the universal character value is valid for identifiers
// universal_char_type_invalid
// the universal character value is not valid for its usage inside
// identifiers (see C++ Standard: 2.2.2 [lex.charset])
// universal_char_type_base_charset
// the universal character value designates a character from the base
// character set
// universal_char_type_not_allowed_for_identifiers
// the universal character value is not allowed in an identifier
//
// Implementation note:
// This classification isn't implemented very effectively here. This
// function should be rewritten with some range run matching algorithm.
//
///////////////////////////////////////////////////////////////////////////////
inline universal_char_type
classify_universal_char (unsigned long ch)
{
// test for invalid characters
if (ch <= 0x0020 || in_range(ch, 0x007f, 0x009f))
include/boost/wave/cpplexer/validate_universal_char.hpp view on Meta::CPAN
}
}
///////////////////////////////////////////////////////////////////////////////
//
// validate_literal
//
// The validate_literal function tests a given string or character literal
// for its validity with regard to eventually contained universal
// characters. These should be in valid ranges (see the function
// classify_universal_char above).
//
// If the string or character literal contains invalid or not allowed
// universal characters a corresponding lexing_exception is thrown.
//
///////////////////////////////////////////////////////////////////////////////
template <typename StringT>
inline void
validate_literal (StringT const &name, int line, int column,
StringT const &file_name)
{
using namespace std; // some systems have strtoul in namespace std::
typename StringT::size_type pos = name.find_first_of('\\');
while (StringT::npos != pos) {
// the literal contains a backslash (may be universal char)
if ('u' == name[pos+1] || 'U' == name[pos+1]) {
StringT uchar_val(name.substr(pos+2, ('u' == name[pos+1]) ? 4 : 8));
universal_char_type type =
classify_universal_char(strtoul(uchar_val.c_str(), 0, 16));
if (universal_char_type_valid != type &&
universal_char_type_not_allowed_for_identifiers != type)
{
// an invalid char was found, so throw an exception
StringT error_uchar(name.substr(pos, ('u' == name[pos+1]) ? 6 : 10));
if (universal_char_type_invalid == type) {
BOOST_WAVE_LEXER_THROW(lexing_exception, universal_char_invalid,
error_uchar, line, column, file_name.c_str());
}
else {
BOOST_WAVE_LEXER_THROW(lexing_exception, universal_char_base_charset,
error_uchar, line, column, file_name.c_str());
}
}
}
// find next universal char (if appropriate)
pos = name.find_first_of('\\', pos+2);
}
}
///////////////////////////////////////////////////////////////////////////////
} // namespace impl
} // namespace cpplexer
} // namespace wave
} // namespace boost
#endif // !defined(VALIDATE_UNIVERSAL_CHAR_HPP_55F1B811_CD76_4C72_8344_CBC69CF3B339_INCLUDED)
( run in 2.611 seconds using v1.01-cache-2.11-cpan-e1769b4cff6 )