Schnek
tokenlist.hpp
1 /*
2  * tokenlist.hpp
3  *
4  * Created on: 11 Jan 2011
5  * Author: Holger Schmitz
6  * Email: holger@notjustphysics.com
7  *
8  * Copyright 2012 Holger Schmitz
9  *
10  * This file is part of Schnek.
11  *
12  * Schnek is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation, either version 3 of the License, or
15  * (at your option) any later version.
16  *
17  * Schnek is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with Schnek. If not, see <http://www.gnu.org/licenses/>.
24  *
25  */
26 
27 #ifndef SCHNEK_TOKENLIST_HPP_
28 #define SCHNEK_TOKENLIST_HPP_
29 
30 #include <memory>
31 #include <list>
32 #include <string>
33 #include <iostream>
34 #include "deckgrammar.hpp"
35 
36 namespace schnek {
37 
38 /* Stores an individual token.
39  *
40  * Along with the token id and a string value (for identifiers of string literals)
41  * the location of the token is also stored by filename and line.
42  */
43 class Token
44 {
45  public:
46  // default constructor
47  Token() {}
48 
50  Token(std::string filename_, int line_, int token_, std::string str_ = "")
51  : filename(filename_), line(line_), token(token_), str(str_) {
52  // std::cerr << "Creating token " << token << " in line " << line << " ("<<str<<")\n";
53  if (token==PATHIDENTIFIER) token = IDENTIFIER;
54  }
55 
57  Token(const Token &t)
58  : filename(t.filename), line(t.line), token(t.token), str(t.str) {}
59 
60  Token& operator=(const Token &t)
61  {
62  filename = t.filename;
63  line = t.line;
64  token = t.token;
65  str = t.str;
66  return *this;
67  }
68 
70  std::string getFilename() const
71  {
72  return filename;
73  }
74 
76  int getLine() const
77  {
78  return line;
79  }
80 
82  int getToken() const
83  {
84  return token;
85  }
86 
88  std::string getString() const
89  {
90  return str;
91  }
92 
93 private:
95  std::string filename;
97  int line;
99  int token;
101  std::string str;
102 };
103 
104 typedef std::shared_ptr<Token> pToken;
105 
106 /* Contains a list of parser tokens.
107  *
108  * The list is filled by the scanner i.e. DeckScanner and then read by the
109  * parser, i.e. DeckParser
110  *
111  * Tokens are stored together with the line numbers to allow user friendly
112  * error messages.
113  */
115 {
116  private:
117  std::list<Token> tlist;
118  std::string filename;
119  public:
120  typedef std::list<Token>::const_iterator const_iterator;
121 
123  TokenList(std::string filename_) : filename(filename_) {}
124 
126 
127  void reset(std::string filename_)
128  {
129  tlist.clear();
130  filename = filename_;
131  }
132 
134 
135  void reset()
136  {
137  tlist.clear();
138  }
139 
141  void insert(int line, int token)
142  {
143  tlist.push_back(Token(filename, line, token));
144  }
145 
146  /* Insert a token that is associated with a string onto the list.
147  *
148  * This stores tokens such as identifiers or string literals.
149  */
150  void insert(int line, int token, char *str, size_t len)
151  {
152  tlist.push_back(Token(filename, line, token, std::string(str,len)));
153  }
154 
155  /* Insert another list of tokens in front of the current one.
156  *
157  * This is used when encountering include statements
158  */
159  void insert(const TokenList &tokens)
160  {
161  tlist.insert(tlist.begin(), tokens.begin(), tokens.end());
162  }
163 
165  const_iterator begin() const
166  {
167  return tlist.begin();
168  }
169 
171  const_iterator end() const
172  {
173  return tlist.end();
174  }
175 };
176 
177 } // namespace
178 
179 #endif // SCHNEK_TOKENLIST_HPP_
Definition: tokenlist.hpp:114
Token(const Token &t)
Copy constructor.
Definition: tokenlist.hpp:57
std::string getFilename() const
Returns the name of the file from which the token was read.
Definition: tokenlist.hpp:70
void insert(int line, int token)
Insert an individual token onto the list.
Definition: tokenlist.hpp:141
Definition: algo.hpp:30
TokenList(std::string filename_)
Default constructor.
Definition: tokenlist.hpp:123
const_iterator end() const
Returns an iterator to the end of the list.
Definition: tokenlist.hpp:171
int getLine() const
Returns the line number where the token was encountered.
Definition: tokenlist.hpp:76
const_iterator begin() const
Returns an iterator to the beginning of the list.
Definition: tokenlist.hpp:165
std::string getString() const
Returns the string constant associated with the token.
Definition: tokenlist.hpp:88
Definition: tokenlist.hpp:43
void reset()
Empties the list and reassigns the filename.
Definition: tokenlist.hpp:135
int getToken() const
Returns the token id.
Definition: tokenlist.hpp:82
Token(std::string filename_, int line_, int token_, std::string str_="")
Construct a token.
Definition: tokenlist.hpp:50
void reset(std::string filename_)
Empties the list and reassigns the filename.
Definition: tokenlist.hpp:127