Guest User

Untitled

a guest
Nov 1st, 2012
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. std::unordered_map<std::string, tokentype> keywords = {
  2. { "Ohai", TokenType::Ohai }
  3. };
  4.  
  5. // For tokens which are always exactly one character
  6. std::unordered_map<char, tokentype> punctuation = {
  7. { '}', TokenType::CloseCurlyBrace },
  8. { '{', TokenType::OpenCurlyBrace },
  9. };
  10.  
  11. struct Token {
  12. TokenType t;
  13. Token(TokenType type) : t(type) {}
  14. };
  15.  
  16. std::vector<Token> lex(std::string s) {
  17. std::vector<Token> out;
  18.  
  19. for(auto it = s.begin(); it != s.end(); ++it) {
  20. auto next = [&] { return *(it + 1); };
  21.  
  22. auto single_equals = [&](char c, TokenType single, TokenType equals) -> bool {
  23. if (*it == c) {
  24. if (next() == '=') {
  25. out.push_back(equals);
  26. ++it;
  27. return true;
  28. }
  29. out.push_back(single);
  30. return true;
  31. }
  32. return false;
  33. };
  34.  
  35. auto double_equals = [&](char c, TokenType single, TokenType double, TokenType equals) -> bool {
  36. if (*it == c) {
  37. if (next() == '=') {
  38. out.push_back(equals);
  39. ++it;
  40. return true;
  41. }
  42. if (next() == c) {
  43. out.push_back(double);
  44. ++it;
  45. return true;
  46. }
  47. out.push_back(single);
  48. return true;
  49. }
  50. return false;
  51. };
  52.  
  53. if (*it == '/') {
  54. if (next() == '/') {
  55. while(*it != '\n') {
  56. ++it;
  57. ++it;
  58. continue;
  59. }
  60. if (next() == '*') {
  61. while(*it != '*') {
  62. ++it;
  63. if (next() == '/')
  64. ++it;
  65. break;
  66. }
  67. }
  68. continue;
  69. }
  70. }
  71.  
  72. if (punctuation.find(*it) != punctuation.end()) {
  73. out.push_back(punctuation[*it]);
  74. ++it;
  75. continue;
  76. }
  77. if (single_equals('/', TokenType::Div, TokenType::DivEquals)) continue;
  78. if (single_equals('!', TokenType::Not, TokenType::NotEquals)) continue;
  79. if (double_equals('+', TokenType::Plus, TokenType::Increment, TokenType::PlusEquals)) continue;
  80. if (double_equals('-', TokenType::Sub, TokenType::Decrement, TokenType::SubEquals)) continue;
  81. }
  82. return out;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment