src/lexer.h

changeset 75
bf8c57437231
parent 74
007fbadfa7f9
child 79
2425fa6a4f21
equal deleted inserted replaced
74:007fbadfa7f9 75:bf8c57437231
1 /* 1 /*
2 Copyright (c) 2013-2014, Santeri Piippo 2 Copyright (c) 2014, Santeri Piippo
3 All rights reserved. 3 All rights reserved.
4 4
5 Redistribution and use in source and binary forms, with or without 5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met: 6 modification, are permitted provided that the following conditions are met:
7 7
37 class lexer 37 class lexer
38 { 38 {
39 types: 39 types:
40 struct token 40 struct token
41 { 41 {
42 e_token type; 42 e_token type;
43 string text; 43 string text;
44 string file; 44 string file;
45 int line; 45 int line;
46 int column; 46 int column;
47 }; 47 };
48 48
49 using token_list = list<token>; 49 using token_list = list<token>;
50 using iterator = token_list::iterator; 50 using iterator = token_list::iterator;
51 51
53 lexer(); 53 lexer();
54 ~lexer(); 54 ~lexer();
55 55
56 void process_file (string file_name); 56 void process_file (string file_name);
57 bool get_next (e_token req = tk_any); 57 bool get_next (e_token req = tk_any);
58 void must_get_next (e_token tok); 58 void must_get_next (e_token tok = tk_any);
59 void must_get_any_of (const list<e_token>& toks); 59 void must_get_any_of (const list<e_token>& toks);
60 int get_one_symbol (const string_list& syms); 60 int get_one_symbol (const string_list& syms);
61 void must_be (e_token tok); 61 void must_be (e_token tok);
62 bool peek_next (token* tk = null); 62 bool peek_next (token* tk = null);
63 63
64 inline bool has_valid_token() const
65 {
66 return (is_at_end() == false && m_token_position != m_tokens.begin());
67 }
68
64 inline token* get_token() const 69 inline token* get_token() const
65 { 70 {
66 assert (is_at_end() == false); 71 assert (has_valid_token() == true);
67 return & (*m_token_position); 72 return &(*m_token_position);
68 } 73 }
69 74
70 inline bool is_at_end() const 75 inline bool is_at_end() const
71 { 76 {
72 return m_token_position == m_tokens.end(); 77 return m_token_position == m_tokens.end();
73 } 78 }
74 79
75 inline token get_token_type() const 80 inline e_token get_token_type() const
76 { 81 {
77 return get_token()->type; 82 return get_token()->type;
78 } 83 }
79 84
80 // If @tok is given, describes the token. If not, describes @tok_type. 85 // If @tok is given, describes the token. If not, describes @tok_type.
93 inline void skip (int a = 1) 98 inline void skip (int a = 1)
94 { 99 {
95 m_token_position += a; 100 m_token_position += a;
96 } 101 }
97 102
98 string peek_next_string (int a); 103 string peek_next_string (int a = 1);
99 104
100 private: 105 private:
101 token_list m_tokens; 106 token_list m_tokens;
102 iterator m_token_position; 107 iterator m_token_position;
103 108

mercurial