Kea  1.5.0
master_lexer.h
Go to the documentation of this file.
1 // Copyright (C) 2012-2015 Internet Systems Consortium, Inc. ("ISC")
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 
7 #ifndef MASTER_LEXER_H
8 #define MASTER_LEXER_H 1
9 
10 #include <dns/exceptions.h>
11 
12 #include <istream>
13 #include <string>
14 
15 #include <stdint.h>
16 
17 #include <boost/noncopyable.hpp>
18 
19 namespace isc {
20 namespace dns {
21 namespace master_lexer_internal {
22 class State;
23 }
24 
39 class MasterToken {
40 public:
47  enum Type {
50  INITIAL_WS,
51  // for detecting it)
54  STRING,
59  ERROR
60  };
61 
63  enum ErrorCode {
75  };
77 
95  struct StringRegion {
96  const char* beg;
97  size_t len;
98  };
99 
105  explicit MasterToken(Type type) : type_(type) {
106  if (type > NOVALUE_TYPE_MAX) {
107  isc_throw(InvalidParameter, "Token per-type constructor "
108  "called with invalid type: " << type);
109  }
110  }
111 
127  MasterToken(const char* str_beg, size_t str_len, bool quoted = false) :
128  type_(quoted ? QSTRING : STRING)
129  {
130  val_.str_region_.beg = str_beg;
131  val_.str_region_.len = str_len;
132  }
133 
138  explicit MasterToken(uint32_t number) : type_(NUMBER) {
139  val_.number_ = number;
140  }
141 
146  explicit MasterToken(ErrorCode error_code) : type_(ERROR) {
147  if (!(error_code < MAX_ERROR_CODE)) {
148  isc_throw(InvalidParameter, "Invalid master lexer error code: "
149  << error_code);
150  }
151  val_.error_code_ = error_code;
152  }
153 
157  Type getType() const { return (type_); }
158 
164  const StringRegion& getStringRegion() const {
165  if (type_ != STRING && type_ != QSTRING) {
167  "Token::getStringRegion() for non string-variant type");
168  }
169  return (val_.str_region_);
170  }
171 
183  std::string getString() const {
184  std::string ret;
185  getString(ret);
186  return (ret);
187  }
188 
205  void getString(std::string& ret) const {
206  if (type_ != STRING && type_ != QSTRING) {
208  "Token::getString() for non string-variant type");
209  }
210  ret.assign(val_.str_region_.beg,
211  val_.str_region_.beg + val_.str_region_.len);
212  }
213 
218  uint32_t getNumber() const {
219  if (type_ != NUMBER) {
221  "Token::getNumber() for non number type");
222  }
223  return (val_.number_);
224  }
225 
231  if (type_ != ERROR) {
233  "Token::getErrorCode() for non error type");
234  }
235  return (val_.error_code_);
236  };
237 
247  std::string getErrorText() const;
248 
249 private:
250  Type type_; // this is not const so the class can be assignable
251 
252  // We use a union to represent different types of token values via the
253  // unified Token class. The class integrity should ensure valid operation
254  // on the union; getter methods should only refer to the member set at
255  // the construction.
256  union {
258  uint32_t number_;
260  } val_;
261 };
262 
301 class MasterLexer : public boost::noncopyable {
303 public:
306  class ReadError : public Unexpected {
307  public:
308  ReadError(const char* file, size_t line, const char* what) :
309  Unexpected(file, line, what)
310  {}
311  };
312 
321  public:
322  LexerError(const char* file, size_t line, MasterToken error_token) :
323  isc::dns::Exception(file, line, error_token.getErrorText().c_str()),
324  token_(error_token)
325  {}
327  };
328 
337  static const size_t SOURCE_SIZE_UNKNOWN;
338 
343  enum Options {
344  NONE = 0,
345  INITIAL_WS = 1,
346  QSTRING = 2,
348  NUMBER = 4
349  };
350 
355 
360 
385  bool pushSource(const char* filename, std::string* error = NULL);
386 
417  void pushSource(std::istream& input);
418 
431  void popSource();
432 
436  size_t getSourceCount() const;
437 
453  std::string getSourceName() const;
454 
469  size_t getSourceLine() const;
470 
497  size_t getTotalSourceSize() const;
498 
536  size_t getPosition() const;
537 
566 
638  bool eol_ok = false);
639 
655  void ungetToken();
656 
657 private:
658  struct MasterLexerImpl;
659  MasterLexerImpl* impl_;
660 };
661 
668  return (static_cast<MasterLexer::Options>(
669  static_cast<unsigned>(o1) | static_cast<unsigned>(o2)));
670 }
671 
672 } // namespace dns
673 } // namespace isc
674 #endif // MASTER_LEXER_H
675 
676 // Local Variables:
677 // mode: c++
678 // End:
isc::dns::MasterLexer::getPosition
size_t getPosition() const
Return the position of lexer in the pushed sources so far.
isc::dns::MasterToken::str_region_
StringRegion str_region_
Definition: master_lexer.h:257
isc::Unexpected
A generic exception that is thrown when an unexpected error condition occurs.
Definition: exceptions/exceptions.h:153
isc::dns::MasterToken
Tokens for MasterLexer.
Definition: master_lexer.h:39
isc::dns::MasterToken::getErrorCode
ErrorCode getErrorCode() const
Return the error code of a error type token.
Definition: master_lexer.h:230
isc::dns::MasterToken::getStringRegion
const StringRegion & getStringRegion() const
Return the value of a string-variant token.
Definition: master_lexer.h:164
isc::dns::MasterToken::END_OF_LINE
@ END_OF_LINE
End of line detected.
Definition: master_lexer.h:48
isc::dns::MasterLexer::LexerError::LexerError
LexerError(const char *file, size_t line, MasterToken error_token)
Definition: master_lexer.h:322
isc::dns::MasterToken::MasterToken
MasterToken(uint32_t number)
Constructor for number type of token.
Definition: master_lexer.h:138
isc::dns::MasterLexer
Tokenizer for parsing DNS master files.
Definition: master_lexer.h:301
isc::dns::MasterToken::END_OF_FILE
@ END_OF_FILE
End of file detected.
Definition: master_lexer.h:49
isc::dns::MasterToken::STRING
@ STRING
A single string.
Definition: master_lexer.h:56
isc::dns::MasterLexer::getSourceLine
size_t getSourceLine() const
Return the input source line number.
isc::dns::MasterToken::NUMBER_OUT_OF_RANGE
@ NUMBER_OUT_OF_RANGE
Number was out of range.
Definition: master_lexer.h:71
isc::dns::MasterToken::Type
Type
Enumeration for token types.
Definition: master_lexer.h:47
isc::dns::MasterToken::UNBALANCED_QUOTES
@ UNBALANCED_QUOTES
Unbalanced quotations detected.
Definition: master_lexer.h:68
isc::dns::MasterLexer::getNextToken
const MasterToken & getNextToken(MasterToken::Type expect, bool eol_ok=false)
Parse the input for the expected type of token.
isc::dns::MasterToken::error_code_
ErrorCode error_code_
Definition: master_lexer.h:259
isc::dns::MasterLexer::MasterLexer
MasterLexer()
The constructor.
isc::dns::MasterToken::StringRegion::beg
const char * beg
The start address of the string.
Definition: master_lexer.h:96
isc::dns::MasterToken::getType
Type getType() const
Return the token type.
Definition: master_lexer.h:157
isc::dns::MasterToken::NOVALUE_TYPE_MAX
@ NOVALUE_TYPE_MAX
Max integer corresponding to no-value (type only) types.
Definition: master_lexer.h:53
isc::dns::MasterToken::MasterToken
MasterToken(ErrorCode error_code)
Constructor for error type of token.
Definition: master_lexer.h:146
isc::dns::MasterToken::getString
std::string getString() const
Return the value of a string-variant token as a string object.
Definition: master_lexer.h:183
isc::dns::MasterToken::ErrorCode
ErrorCode
Enumeration for lexer error codes.
Definition: master_lexer.h:63
exceptions.h
isc::dns::MasterLexer::LexerError::token_
const MasterToken token_
Definition: master_lexer.h:326
isc::dns::MasterLexer::INITIAL_WS
@ INITIAL_WS
recognize begin-of-line spaces after an end-of-line
Definition: master_lexer.h:345
isc::dns::MasterLexer::ungetToken
void ungetToken()
Return the last token back to the lexer.
isc::dns::MasterToken::UNEXPECTED_QUOTES
@ UNEXPECTED_QUOTES
Unexpected quotes character detected.
Definition: master_lexer.h:73
isc::dns::MasterToken::UNBALANCED_PAREN
@ UNBALANCED_PAREN
Unbalanced parentheses detected.
Definition: master_lexer.h:65
isc::dns::MasterLexer::ReadError::ReadError
ReadError(const char *file, size_t line, const char *what)
Definition: master_lexer.h:308
isc
Defines the logger used by the top-level component of kea-dhcp-ddns.
Definition: agent_parser.cc:144
isc::dns::MasterToken::getNumber
uint32_t getNumber() const
Return the value of a string-variant token as a string object.
Definition: master_lexer.h:218
isc::dns::MasterToken::MasterToken
MasterToken(const char *str_beg, size_t str_len, bool quoted=false)
Constructor for string and quoted-string types of token.
Definition: master_lexer.h:127
isc::dns::MasterToken::BAD_NUMBER
@ BAD_NUMBER
Number is expected but not recognized.
Definition: master_lexer.h:72
isc::dns::master_lexer_internal::State
Tokenization state for MasterLexer.
Definition: master_lexer_state.h:50
isc::Exception::what
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Definition: exceptions/exceptions.cc:32
isc::dns::MasterLexer::LexerError
Exception thrown from a wrapper version of MasterLexer::getNextToken() for non fatal errors.
Definition: master_lexer.h:320
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition: exceptions/exceptions.h:192
isc::dns::MasterLexer::getNextToken
const MasterToken & getNextToken(Options options=NONE)
Parse and return another token from the input.
isc::InvalidOperation
A generic exception that is thrown if a function is called in a prohibited way.
Definition: exceptions/exceptions.h:143
isc::dns::MasterLexer::getSourceName
std::string getSourceName() const
Return the name of the current input source name.
isc::InvalidParameter
A generic exception that is thrown if a parameter given to a method or function is considered invalid...
Definition: exceptions/exceptions.h:124
isc::dns::MasterToken::getErrorText
std::string getErrorText() const
Return a textual description of the error of a error type token.
isc::dns::MasterLexer::getTotalSourceSize
size_t getTotalSourceSize() const
Return the total size of pushed sources.
isc::dns::MasterToken::ERROR
@ ERROR
Error detected in getting a token.
Definition: master_lexer.h:59
isc::dns::Exception
Definition: dns/exceptions.h:25
isc::dns::MasterLexer::ReadError
Exception thrown when we fail to read from the input stream or file.
Definition: master_lexer.h:306
isc::dns::MasterToken::MasterToken
MasterToken(Type type)
Constructor for non-value type of token.
Definition: master_lexer.h:105
isc::dns::MasterLexer::NONE
@ NONE
No option.
Definition: master_lexer.h:344
isc::dns::operator|
MasterLexer::Options operator|(MasterLexer::Options o1, MasterLexer::Options o2)
Operator to combine MasterLexer options.
Definition: master_lexer.h:667
isc::dns::MasterToken::StringRegion
A simple representation of a range of a string.
Definition: master_lexer.h:95
isc::dns::MasterToken::NO_TOKEN_PRODUCED
@ NO_TOKEN_PRODUCED
No token was produced.
Definition: master_lexer.h:69
isc::dns::MasterToken::UNEXPECTED_END
@ UNEXPECTED_END
The lexer reaches the end of line or file unexpectedly.
Definition: master_lexer.h:66
isc::dns::MasterLexer::getSourceCount
size_t getSourceCount() const
Get number of sources inside the lexer.
isc::dns::MasterLexer::popSource
void popSource()
Stop using the most recently opened input source (file or stream).
isc::dns::MasterLexer::pushSource
void pushSource(std::istream &input)
Make the given stream the current input source of MasterLexer.
isc::dns::MasterToken::NOT_STARTED
@ NOT_STARTED
The lexer is just initialized and has no token.
Definition: master_lexer.h:64
isc::dns::MasterToken::number_
uint32_t number_
Definition: master_lexer.h:258
isc::dns::MasterLexer::NUMBER
@ NUMBER
recognize numeric text as integer
Definition: master_lexer.h:348
isc::dns::MasterToken::getString
void getString(std::string &ret) const
Fill in a string with the value of a string-variant token.
Definition: master_lexer.h:205
isc::dns::MasterLexer::SOURCE_SIZE_UNKNOWN
static const size_t SOURCE_SIZE_UNKNOWN
Special value for input source size meaning "unknown".
Definition: master_lexer.h:337
isc::dns::MasterLexer::pushSource
bool pushSource(const char *filename, std::string *error=NULL)
Open a file and make it the current input source of MasterLexer.
isc::dns::MasterLexer::QSTRING
@ QSTRING
recognize quoted string
Definition: master_lexer.h:347
isc::dns::MasterToken::MAX_ERROR_CODE
@ MAX_ERROR_CODE
Max integer corresponding to valid error codes.
Definition: master_lexer.h:74
isc::dns::MasterToken::NUMBER
@ NUMBER
A decimal number (unsigned 32-bit)
Definition: master_lexer.h:58
isc::dns::MasterToken::INITIAL_WS
@ INITIAL_WS
White spaces at the beginning of a line after an end of line or at the beginning of file (if asked.
Definition: master_lexer.h:50
isc::dns::MasterToken::StringRegion::len
size_t len
The length of the string in bytes.
Definition: master_lexer.h:97
isc::dns::MasterLexer::~MasterLexer
~MasterLexer()
The destructor.
isc::dns::MasterLexer::Options
Options
Options for getNextToken.
Definition: master_lexer.h:343
isc::dns::MasterToken::QSTRING
@ QSTRING
A single string quoted by double-quotes (").
Definition: master_lexer.h:57