1 /* $Id: formula_tokenizer.hpp 25713 2008-04-09 18:36:16Z dragonking $ */ 2 /* 3 Copyright (C) 2007 - 2008 by David White <dave@whitevine.net> 4 Part of the Silver Tree Project 5 6 This program is free software; you can redistribute it and/or modify 7 it under the terms of the GNU General Public License version 2 or later. 8 This program is distributed in the hope that it will be useful, 9 but WITHOUT ANY WARRANTY. 10 11 See the COPYING file for more details. 12 */ 13 14 #ifndef FORMULA_TOKENIZER_HPP_INCLUDED 15 #define FORMULA_TOKENIZER_HPP_INCLUDED 16 17 #include <string> 18 #include <vector> 19 20 namespace formula_tokenizer 21 { 22 23 typedef std::string::const_iterator iterator; 24 25 enum FFL_TOKEN_TYPE { TOKEN_OPERATOR, TOKEN_STRING_LITERAL, 26 TOKEN_CONST_IDENTIFIER, 27 TOKEN_IDENTIFIER, TOKEN_INTEGER, TOKEN_DECIMAL, 28 TOKEN_LPARENS, TOKEN_RPARENS, 29 TOKEN_LSQUARE, TOKEN_RSQUARE, 30 TOKEN_LBRACKET, TOKEN_RBRACKET, 31 TOKEN_COMMA, TOKEN_SEMICOLON, TOKEN_COLON, 32 TOKEN_WHITESPACE, TOKEN_KEYWORD, 33 TOKEN_COMMENT, TOKEN_POINTER, TOKEN_LEFT_POINTER, 34 TOKEN_PIPE, 35 TOKEN_INVALID }; 36 37 struct token { 38 FFL_TOKEN_TYPE type; 39 iterator begin, end; 40 }; 41 42 token get_token(iterator& i1, iterator i2); 43 44 struct token_error { 45 token_error(const std::string& m); 46 std::string msg; 47 }; 48 49 //A special interface for searching for and matching tokens. 50 class token_matcher { 51 public: 52 token_matcher(); 53 explicit token_matcher(FFL_TOKEN_TYPE type); 54 token_matcher& add(FFL_TOKEN_TYPE type); 55 token_matcher& add(const std::string& str); 56 57 bool match(const token& t) const; 58 59 //Find the first matching token within the given range and return it. 60 //Does not return tokens that are inside any kinds of brackets. 61 bool find_match(const token*& i1, const token* i2) const; 62 private: 63 std::vector<FFL_TOKEN_TYPE> types_; 64 std::vector<std::string> str_; 65 }; 66 67 } 68 69 #endif 70