| 1 | /*============================================================================= |
| 2 | Copyright (c) 2001-2011 Hartmut Kaiser |
| 3 | |
| 4 | Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 5 | file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 | =============================================================================*/ |
| 7 | |
| 8 | #include "lexer.hpp" |
| 9 | |
| 10 | #include <boost/phoenix/operator/self.hpp> |
| 11 | |
| 12 | namespace client { namespace lexer |
| 13 | { |
| 14 | template <typename BaseIterator> |
| 15 | conjure_tokens<BaseIterator>::conjure_tokens() |
| 16 | : identifier("[a-zA-Z_][a-zA-Z_0-9]*" , token_ids::identifier) |
| 17 | , lit_uint("[0-9]+" , token_ids::lit_uint) |
| 18 | , true_or_false("true|false" , token_ids::true_or_false) |
| 19 | { |
| 20 | lex::_pass_type _pass; |
| 21 | |
| 22 | this->self = lit_uint | true_or_false; |
| 23 | |
| 24 | add_keyword(keyword: "void" ); |
| 25 | add_keyword(keyword: "int" ); |
| 26 | add_keyword(keyword: "if" ); |
| 27 | add_keyword(keyword: "else" ); |
| 28 | add_keyword(keyword: "while" ); |
| 29 | add_keyword(keyword: "return" ); |
| 30 | |
| 31 | this->self.add |
| 32 | ("\\|\\|" , token_ids::logical_or) |
| 33 | ("&&" , token_ids::logical_and) |
| 34 | ("==" , token_ids::equal) |
| 35 | ("!=" , token_ids::not_equal) |
| 36 | ("<" , token_ids::less) |
| 37 | ("<=" , token_ids::less_equal) |
| 38 | (">" , token_ids::greater) |
| 39 | (">=" , token_ids::greater_equal) |
| 40 | ("\\+" , token_ids::plus) |
| 41 | ("\\-" , token_ids::minus) |
| 42 | ("\\*" , token_ids::times) |
| 43 | ("\\/" , token_ids::divide) |
| 44 | ("!" , token_ids::not_) |
| 45 | ; |
| 46 | |
| 47 | this->self += lex::char_('(') | ')' | '{' | '}' | ',' | '=' | ';'; |
| 48 | |
| 49 | this->self += |
| 50 | identifier |
| 51 | | lex::string("\\/\\*[^*]*\\*+([^/*][^*]*\\*+)*\\/" , token_ids::comment) |
| 52 | [ |
| 53 | lex::_pass = lex::pass_flags::pass_ignore |
| 54 | ] |
| 55 | | lex::string("[ \t\n\r]+" , token_ids::whitespace) |
| 56 | [ |
| 57 | lex::_pass = lex::pass_flags::pass_ignore |
| 58 | ] |
| 59 | ; |
| 60 | } |
| 61 | |
| 62 | template <typename BaseIterator> |
| 63 | bool conjure_tokens<BaseIterator>::add_keyword(std::string const& keyword) |
| 64 | { |
| 65 | // add the token to the lexer |
| 66 | token_ids::type id = token_ids::type(this->get_next_id()); |
| 67 | this->self.add(keyword, id); |
| 68 | |
| 69 | // store the mapping for later retrieval |
| 70 | std::pair<typename keyword_map_type::iterator, bool> p = |
| 71 | keywords_.insert(x: typename keyword_map_type::value_type(keyword, id)); |
| 72 | |
| 73 | return p.second; |
| 74 | } |
| 75 | }} |
| 76 | |
| 77 | |