master_lexer.cc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. // Copyright (C) 2012 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <exceptions/exceptions.h>
  15. #include <dns/master_lexer.h>
  16. #include <dns/master_lexer_inputsource.h>
  17. #include <dns/master_lexer_state.h>
  18. #include <boost/shared_ptr.hpp>
  19. #include <cassert>
  20. #include <string>
  21. #include <sstream>
  22. #include <vector>
  23. namespace isc {
  24. namespace dns {
  25. namespace {
  26. typedef boost::shared_ptr<master_lexer_internal::InputSource> InputSourcePtr;
  27. }
  28. using namespace master_lexer_internal;
  29. struct MasterLexer::MasterLexerImpl {
  30. MasterLexerImpl() : source_(NULL), paren_count_(0), last_was_eol_(false),
  31. token_(Token::NOT_STARTED)
  32. {}
  33. std::vector<InputSourcePtr> sources_;
  34. InputSource* source_; // current source
  35. size_t paren_count_;
  36. bool last_was_eol_;
  37. Token token_;
  38. };
  39. MasterLexer::MasterLexer() : impl_(new MasterLexerImpl) {
  40. }
  41. MasterLexer::~MasterLexer() {
  42. delete impl_;
  43. }
  44. bool
  45. MasterLexer::pushSource(const char* filename, std::string* error) {
  46. if (filename == NULL) {
  47. isc_throw(InvalidParameter,
  48. "NULL filename for MasterLexer::pushSource");
  49. }
  50. try {
  51. impl_->sources_.push_back(InputSourcePtr(new InputSource(filename)));
  52. } catch (const InputSource::OpenError& ex) {
  53. if (error != NULL) {
  54. *error = ex.what();
  55. }
  56. return (false);
  57. }
  58. impl_->source_ = impl_->sources_.back().get();
  59. return (true);
  60. }
  61. void
  62. MasterLexer::pushSource(std::istream& input) {
  63. impl_->sources_.push_back(InputSourcePtr(new InputSource(input)));
  64. impl_->source_ = impl_->sources_.back().get();
  65. }
  66. void
  67. MasterLexer::popSource() {
  68. if (impl_->sources_.empty()) {
  69. isc_throw(InvalidOperation,
  70. "MasterLexer::popSource on an empty source");
  71. }
  72. impl_->sources_.pop_back();
  73. impl_->source_ = impl_->sources_.empty() ? NULL :
  74. impl_->sources_.back().get();
  75. }
  76. std::string
  77. MasterLexer::getSourceName() const {
  78. if (impl_->sources_.empty()) {
  79. return (std::string());
  80. }
  81. return (impl_->sources_.back()->getName());
  82. }
  83. size_t
  84. MasterLexer::getSourceLine() const {
  85. if (impl_->sources_.empty()) {
  86. return (0);
  87. }
  88. return (impl_->sources_.back()->getCurrentLine());
  89. }
  90. namespace {
  91. const char* const error_text[] = {
  92. "lexer not started", // NOT_STARTED
  93. "unbalanced parentheses", // UNBALANCED_PAREN
  94. "unexpected end of input", // UNEXPECTED_END
  95. "unbalanced quotes" // UNBALANCED_QUOTES
  96. };
  97. const size_t error_text_max_count = sizeof(error_text) / sizeof(error_text[0]);
  98. }
  99. std::string
  100. MasterLexer::Token::getErrorText() const {
  101. if (type_ != ERROR) {
  102. isc_throw(InvalidOperation,
  103. "Token::getErrorText() for non error type");
  104. }
  105. // The class integrity ensures the following:
  106. assert(val_.error_code_ < error_text_max_count);
  107. return (error_text[val_.error_code_]);
  108. }
  109. namespace master_lexer_internal {
  110. typedef MasterLexer::Token Token; // convenience shortcut
  111. bool
  112. State::wasLastEOL(const MasterLexer& lexer) const {
  113. return (lexer.impl_->last_was_eol_);
  114. }
  115. const MasterLexer::Token&
  116. State::getToken(const MasterLexer& lexer) const {
  117. return (lexer.impl_->token_);
  118. }
  119. size_t
  120. State::getParenCount(const MasterLexer& lexer) const {
  121. return (lexer.impl_->paren_count_);
  122. }
  123. class Start : public State {
  124. public:
  125. Start() {}
  126. virtual const State* handle(MasterLexer& lexer,
  127. MasterLexer::Options& options,
  128. MasterLexer::Options orig_options) const;
  129. };
  130. class CRLF : public State {
  131. public:
  132. CRLF() {}
  133. virtual const State* handle(MasterLexer& /*lexer*/,
  134. MasterLexer::Options& /*options*/,
  135. MasterLexer::Options /*orig_options*/) const
  136. {
  137. return (NULL);
  138. }
  139. };
  140. class String : public State {
  141. public:
  142. String() {}
  143. virtual const State* handle(MasterLexer& /*lexer*/,
  144. MasterLexer::Options& /*options*/,
  145. MasterLexer::Options /*orig_options*/) const
  146. {
  147. return (NULL);
  148. }
  149. };
  150. namespace {
  151. const Start START_STATE;
  152. const CRLF CRLF_STATE;
  153. const String STRING_STATE;
  154. }
  155. const State&
  156. State::getInstance(ID state_id) {
  157. switch (state_id) {
  158. case Start:
  159. return (START_STATE);
  160. case CRLF:
  161. return (CRLF_STATE);
  162. case EatLine:
  163. return (CRLF_STATE); // XXX
  164. case String:
  165. return (STRING_STATE); // XXX
  166. }
  167. }
  168. inline void
  169. cancelOptions(MasterLexer::Options& options,
  170. MasterLexer::Options canceled_options)
  171. {
  172. options = static_cast<MasterLexer::Options>(
  173. options & static_cast<MasterLexer::Options>(~canceled_options));
  174. }
  175. const State*
  176. Start::handle(MasterLexer& lexer, MasterLexer::Options& options,
  177. MasterLexer::Options orig_options) const
  178. {
  179. while (true) {
  180. const int c = getLexerImpl(lexer)->source_->getChar();
  181. if (c < 0) {
  182. // TODO: handle unbalance cases
  183. getLexerImpl(lexer)->last_was_eol_ = false;
  184. getLexerImpl(lexer)->token_ = Token(Token::END_OF_FILE);
  185. return (NULL);
  186. } else if (c == ' ' || c == '\t') {
  187. if (getLexerImpl(lexer)->last_was_eol_ &&
  188. (options & MasterLexer::INITIAL_WS) != 0) {
  189. getLexerImpl(lexer)->last_was_eol_ = false;
  190. getLexerImpl(lexer)->token_ = Token(Token::INITIAL_WS);
  191. return (NULL);
  192. }
  193. continue;
  194. } else if (c == '\n') {
  195. getLexerImpl(lexer)->last_was_eol_ = true;
  196. getLexerImpl(lexer)->token_ = Token(Token::END_OF_LINE);
  197. return (NULL);
  198. } else if (c == '(') {
  199. getLexerImpl(lexer)->last_was_eol_ = false;
  200. cancelOptions(options,
  201. MasterLexer::END_OF_LINE | MasterLexer::INITIAL_WS);
  202. ++getLexerImpl(lexer)->paren_count_;
  203. continue;
  204. } else if (c == ')') {
  205. // TBD: unbalanced case
  206. --getLexerImpl(lexer)->paren_count_;
  207. options = orig_options; // TBD: only when count becomes 0
  208. } else {
  209. getLexerImpl(lexer)->last_was_eol_ = false;
  210. return (&STRING_STATE);
  211. }
  212. }
  213. }
  214. } // namespace master_lexer_internal
  215. } // end of namespace dns
  216. } // end of namespace isc