123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- // 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 <exceptions/exceptions.h>
- #include <dns/master_lexer.h>
- #include <dns/master_lexer_inputsource.h>
- #include <boost/shared_ptr.hpp>
- #include <cassert>
- #include <string>
- #include <sstream>
- #include <vector>
- namespace isc {
- namespace dns {
- namespace {
- typedef boost::shared_ptr<master_lexer_internal::InputSource> InputSourcePtr;
- }
- using namespace master_lexer_internal;
- struct MasterLexer::MasterLexerImpl {
- MasterLexerImpl() : token_(Token::NOT_STARTED) {}
- std::vector<InputSourcePtr> sources_;
- Token token_;
- };
- MasterLexer::MasterLexer() : impl_(new MasterLexerImpl) {
- }
- MasterLexer::~MasterLexer() {
- delete impl_;
- }
- void
- MasterLexer::open(const char* filename) {
- if (filename == NULL) {
- isc_throw(InvalidParameter, "NULL filename for MasterLexer::open");
- }
- impl_->sources_.push_back(InputSourcePtr(new InputSource(filename)));
- }
- void
- MasterLexer::open(std::istream& input) {
- impl_->sources_.push_back(InputSourcePtr(new InputSource(input)));
- }
- void
- MasterLexer::close() {
- if (impl_->sources_.empty()) {
- isc_throw(InvalidOperation, "MasterLexer::close on an empty source");
- }
- impl_->sources_.pop_back();
- }
- 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[] = {
- "lexer not started", // NOT_STARTED
- "unbalanced parentheses", // UNBALANCED_PAREN
- "unexpected end of input", // UNEXPECTED_END
- "unbalanced quotes" // UNBALANCED_QUOTES
- };
- const size_t error_text_max_count = sizeof(error_text) / sizeof(error_text[0]);
- }
- std::string
- MasterLexer::Token::getErrorText() const {
- if (type_ != ERROR) {
- isc_throw(InvalidOperation,
- "Token::getErrorText() for non error type");
- }
- // The class integrity ensures the following:
- assert(val_.error_code_ < error_text_max_count);
- return (error_text[val_.error_code_]);
- }
- } // end of namespace dns
- } // end of namespace isc
|