1 #include "source_file/tokenized_source_file.h"
2 #include "source_file/token.h"
4 using namespace tanlang;
6 TokenizedSourceFile::TokenizedSourceFile(str filename, vector<Token *> tokens)
7 : _filename(std::move(filename)), _tokens(std::move(tokens)) {
11 _tokens.push_back(
new Token(TokenType::COMMENTS, 0, 0,
"", f));
15 Token *TokenizedSourceFile::get_token(uint32_t loc)
const {
16 TAN_ASSERT(loc < _tokens.size());
20 uint32_t TokenizedSourceFile::get_line(uint32_t loc)
const {
return get_token(loc)->get_line() + 1; }
22 uint32_t TokenizedSourceFile::get_col(uint32_t loc)
const {
return get_token(loc)->get_col() + 1; }
24 str TokenizedSourceFile::get_token_str(uint32_t loc)
const {
return get_token(loc)->get_value(); }
26 Token *TokenizedSourceFile::get_last_token()
const {
return _tokens.back(); }
28 bool TokenizedSourceFile::is_eof(uint32_t loc)
const {
return loc >= _tokens.size(); }
30 str TokenizedSourceFile::get_source_code(uint32_t start, uint32_t end)
const {
33 Token *start_tok = get_token(start);
34 SrcLoc start_loc = Token::GetSrcLoc(start_tok);
39 SrcLoc end_loc = start_loc;
40 if (end >= _tokens.size()) {
43 end_loc = Token::GetSrcLoc(get_token(end + 1));
46 ret = src->
substr(start_loc, end_loc);
50 for (i = (
int)ret.length() - 1; i >= 0; --i) {
51 if (!isspace(ret[(
size_t)i]))
55 return ret.substr(0, (
size_t)i + 1);
58 str TokenizedSourceFile::get_filename()
const {
return _filename; }
60 str TokenizedSourceFile::get_src_location_str(uint32_t loc)
const {
61 return get_filename() +
":" + std::to_string(get_line(loc));
64 SourceFile *TokenizedSourceFile::src()
const {
return _tokens[0]->src(); }
str substr(const SrcLoc &start) const
Get a substring from start to the end of the current line.
SrcLoc end() const
The end of source file (exclusive).