xref: /netbsd-src/external/bsd/elftosb/dist/elftosb2/ElftosbLexer.h (revision 993229b6fea628ff8b1fa09146c80b0cfb2768eb)
1 /*
2  * File:	ElftosbLexer.h
3  *
4  * Copyright (c) Freescale Semiconductor, Inc. All rights reserved.
5  * See included license file for license details.
6  */
7 
8 // This header just wraps the standard flex C++ header to make it easier to include
9 // without having to worry about redefinitions of the class name every time.
10 
11 #if !defined(_ElftosbLexer_h_)
12 #define _ElftosbLexer_h_
13 
14 #include "ElftosbAST.h"
15 #include "FlexLexer.h"
16 #include "elftosb_parser.tab.hpp"
17 #include <vector>
18 #include <string>
19 #include <stdexcept>
20 #include "Blob.h"
21 
22 using namespace std;
23 
24 namespace elftosb
25 {
26 
27 /*!
28  * \brief Exception class for syntax errors.
29  */
30 class syntax_error : public std::runtime_error
31 {
32 public:
syntax_error(const std::string & __arg)33 	explicit syntax_error(const std::string & __arg) : std::runtime_error(__arg) {}
34 };
35 
36 /*!
37  * \brief Exception class for lexical errors.
38  */
39 class lexical_error : public std::runtime_error
40 {
41 public:
lexical_error(const std::string & __arg)42 	explicit lexical_error(const std::string & __arg) : std::runtime_error(__arg) {}
43 };
44 
45 /*!
46  * \brief Lexical scanner class for elftosb command files.
47  *
48  * This class is a subclass of the standard C++ lexer class produced by
49  * Flex. It's primary purpose is to provide a clean way to report values
50  * for symbols, without using the yylval global. This is necessary because
51  * the parser produced by Bison is a "pure" parser.
52  *
53  * In addition, this class manages a list of source names generated by
54  * parsing. The lexer uses this list to determine if an identifier is
55  * a source name or a constant identifier.
56  */
57 class ElftosbLexer : public yyFlexLexer
58 {
59 public:
60 	//! \brief Constructor.
61 	ElftosbLexer(istream & inputStream);
62 
63 	//! \brief Lexer interface. Returns one token.
64 	virtual int yylex();
65 
66 	//! \brief Returns the value for the most recently produced token in \a value.
67 	virtual void getSymbolValue(YYSTYPE * value);
68 
69 	//! \brief Returns the current token's location in \a loc.
getLocation()70 	inline token_loc_t & getLocation() { return m_location; }
71 
72 	//! \name Source names
73 	//@{
74 	void addSourceName(std::string * ident);
75 	bool isSourceName(std::string * ident);
76 	//@}
77 
78 protected:
79 	YYSTYPE m_symbolValue;	//!< Value for the current token.
80 	int m_line;	//!< Current line number.
81 	token_loc_t m_location;	//!< Location for the current token.
82 	Blob * m_blob;	//!< The binary object value as its being constructed.
83 	int m_blobFirstLine;	//!< Line number for the first character of a blob.
84 
85 	typedef std::vector<std::string> string_vector_t;
86 	string_vector_t m_sources;	//!< Vector of source identifiers;
87 
88 	//! \brief Throw an elftosb::lexical_error exception.
89 	virtual void LexerError(const char * msg);
90 
91 	//! \brief Process a string containing escape sequences.
92 	int processStringEscapes(const char * in, char * out);
93 };
94 
95 }; // namespace elftosb
96 
97 #endif // _ElftosbLexer_h_
98