| 1 | /*============================================================================= |
| 2 | Copyright (c) 2001-2011 Hartmut Kaiser |
| 3 | Copyright (c) 2001-2011 Joel de Guzman |
| 4 | |
| 5 | Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 6 | file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 7 | =============================================================================*/ |
| 8 | |
| 9 | #include "lexer.hpp" |
| 10 | |
| 11 | namespace client { namespace lexer |
| 12 | { |
| 13 | template <typename BaseIterator> |
| 14 | conjure_tokens<BaseIterator>::conjure_tokens() |
| 15 | : identifier("[a-zA-Z_][a-zA-Z_0-9]*" , token_ids::identifier) |
| 16 | , lit_uint("[0-9]+" , token_ids::lit_uint) |
| 17 | , true_or_false("true|false" , token_ids::true_or_false) |
| 18 | , add(*this) |
| 19 | { |
| 20 | lex::_pass_type _pass; |
| 21 | |
| 22 | this->self = lit_uint | true_or_false; |
| 23 | |
| 24 | this->add |
| 25 | ("void" ) |
| 26 | ("int" ) |
| 27 | ("if" ) |
| 28 | ("else" ) |
| 29 | ("while" ) |
| 30 | ("return" ) |
| 31 | ("=" , token_ids::assign) |
| 32 | ("\\+=" , token_ids::plus_assign) |
| 33 | ("\\-=" , token_ids::minus_assign) |
| 34 | ("\\*=" , token_ids::times_assign) |
| 35 | ("\\/=" , token_ids::divide_assign) |
| 36 | ("%=" , token_ids::mod_assign) |
| 37 | ("\\&=" , token_ids::bit_and_assign) |
| 38 | ("\\^=" , token_ids::bit_xor_assign) |
| 39 | ("\\|=" , token_ids::bit_or_assign) |
| 40 | ("<<=" , token_ids::shift_left_assign) |
| 41 | (">>=" , token_ids::shift_right_assign) |
| 42 | ("\\|\\|" , token_ids::logical_or) |
| 43 | ("&&" , token_ids::logical_and) |
| 44 | ("\\|" , token_ids::bit_or) |
| 45 | ("\\^" , token_ids::bit_xor) |
| 46 | ("&" , token_ids::bit_and) |
| 47 | ("<<" , token_ids::shift_left) |
| 48 | (">>" , token_ids::shift_right) |
| 49 | ("==" , token_ids::equal) |
| 50 | ("!=" , token_ids::not_equal) |
| 51 | ("<" , token_ids::less) |
| 52 | ("<=" , token_ids::less_equal) |
| 53 | (">" , token_ids::greater) |
| 54 | (">=" , token_ids::greater_equal) |
| 55 | ("\\+" , token_ids::plus) |
| 56 | ("\\-" , token_ids::minus) |
| 57 | ("\\*" , token_ids::times) |
| 58 | ("\\/" , token_ids::divide) |
| 59 | ("%" , token_ids::mod) |
| 60 | ("\\+\\+" , token_ids::plus_plus) |
| 61 | ("\\-\\-" , token_ids::minus_minus) |
| 62 | ("~" , token_ids::compl_) |
| 63 | ("!" , token_ids::not_) |
| 64 | ; |
| 65 | |
| 66 | this->self += lex::char_('(') | ')' | '{' | '}' | ',' | ';'; |
| 67 | |
| 68 | this->self += |
| 69 | identifier |
| 70 | | lex::string("(\\/\\*[^*]*\\*+([^/*][^*]*\\*+)*\\/)|(\\/\\/[^\r\n]*)" , token_ids::comment) |
| 71 | [ |
| 72 | lex::_pass = lex::pass_flags::pass_ignore |
| 73 | ] |
| 74 | | lex::string("[ \t\n\r]+" , token_ids::whitespace) |
| 75 | [ |
| 76 | lex::_pass = lex::pass_flags::pass_ignore |
| 77 | ] |
| 78 | ; |
| 79 | } |
| 80 | |
| 81 | template <typename BaseIterator> |
| 82 | bool conjure_tokens<BaseIterator>::add_( |
| 83 | std::string const& keyword, int id_) |
| 84 | { |
| 85 | // add the token to the lexer |
| 86 | token_ids::type id; |
| 87 | if (id_ == token_ids::invalid) |
| 88 | id = token_ids::type(this->get_next_id()); |
| 89 | else |
| 90 | id = token_ids::type(id_); |
| 91 | |
| 92 | this->self.add(keyword, id); |
| 93 | // store the mapping for later retrieval |
| 94 | std::pair<typename keyword_map_type::iterator, bool> p = |
| 95 | keywords_.insert(x: typename keyword_map_type::value_type(keyword, id)); |
| 96 | |
| 97 | return p.second; |
| 98 | } |
| 99 | }} |
| 100 | |
| 101 | |