Commit 24e75cee authored by JINMEI Tatuya's avatar JINMEI Tatuya
Browse files

[2371] implemented initial framework of MasterLexer and some methods

parent 27e86396
......@@ -14,8 +14,71 @@
#include <dns/master_lexer.h>
#include <boost/shared_ptr.hpp>
#include <cassert>
#include <string>
#include <sstream>
#include <vector>
namespace isc {
namespace dns {
namespace master_lexer_internal {
std::string
createStreamName(std::istream& input_stream) {
std::stringstream ss;
ss << "stream-" << &input_stream;
return (ss.str());
}
class InputSource {
public:
InputSource(std::istream& input_stream) :
name_(createStreamName(input_stream))
{}
const std::string& getName() const { return (name_); }
size_t getCurrentLine() const { return (1); }
private:
const std::string name_;
};
typedef boost::shared_ptr<InputSource> InputSourcePtr;
}
using namespace master_lexer_internal;
struct MasterLexer::MasterLexerImpl {
std::vector<InputSourcePtr> sources_;
};
MasterLexer::MasterLexer() : impl_(new MasterLexerImpl) {
}
MasterLexer::~MasterLexer() {
delete impl_;
}
void
MasterLexer::open(std::istream& input) {
impl_->sources_.push_back(InputSourcePtr(new InputSource(input)));
}
std::string
MasterLexer::getSourceName() const {
if (impl_->sources_.empty()) {
return (std::string());
}
return (impl_->sources_.back()->getName());
}
size_t
MasterLexer::getSourceLine() const {
if (impl_->sources_.empty()) {
return (0);
}
return (impl_->sources_.back()->getCurrentLine());
}
namespace {
const char* const error_text[] = {
......@@ -27,9 +90,6 @@ const char* const error_text[] = {
const size_t error_text_max_count = sizeof(error_text) / sizeof(error_text[0]);
}
namespace isc {
namespace dns {
std::string
MasterLexer::Token::getErrorText() const {
if (type_ != ERROR) {
......@@ -42,6 +102,5 @@ MasterLexer::Token::getErrorText() const {
return (error_text[val_.error_code_]);
}
} // end of namespace dns
} // end of namespace isc
......@@ -17,6 +17,7 @@
#include <exceptions/exceptions.h>
#include <istream>
#include <string>
#include <stdint.h>
......@@ -27,6 +28,16 @@ namespace dns {
class MasterLexer {
public:
class Token; // we define it separately for better readability
MasterLexer();
~MasterLexer();
void open(std::istream& input);
std::string getSourceName() const;
size_t getSourceLine() const;
private:
struct MasterLexerImpl;
MasterLexerImpl* impl_;
};
/// \brief Tokens for \c MasterLexer
......
......@@ -25,6 +25,7 @@ run_unittests_SOURCES += edns_unittest.cc
run_unittests_SOURCES += labelsequence_unittest.cc
run_unittests_SOURCES += messagerenderer_unittest.cc
run_unittests_SOURCES += master_lexer_token_unittest.cc
run_unittests_SOURCES += master_lexer_unittest.cc
run_unittests_SOURCES += name_unittest.cc
run_unittests_SOURCES += nsec3hash_unittest.cc
run_unittests_SOURCES += rrclass_unittest.cc rrtype_unittest.cc
......
// Copyright (C) 2012 Internet Systems Consortium, Inc. ("ISC")
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
#include <dns/master_lexer.h>
#include <gtest/gtest.h>
#include <boost/lexical_cast.hpp>
#include <string>
#include <sstream>
using namespace isc::dns;
using std::string;
using std::stringstream;
using boost::lexical_cast;
namespace {
class MasterLexerTest : public ::testing::Test {
protected:
MasterLexerTest() {}
MasterLexer lexer;
stringstream ss;
};
TEST_F(MasterLexerTest, preOPen) {
// Initially sources stack is empty, and getXXX() returns accordingly.
EXPECT_TRUE(lexer.getSourceName().empty());
EXPECT_EQ(0, lexer.getSourceLine());
}
TEST_F(MasterLexerTest, openStream) {
lexer.open(ss);
EXPECT_EQ(string("stream-") + lexical_cast<string>(&ss),
lexer.getSourceName());
// From the point of view of this test, we only have to check (though
// indirectly) getSourceLine calls InputSource::getCurrentLine. It should
// return 1 initially.
EXPECT_EQ(1, lexer.getSourceLine());
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment