Gyoji Compiler
Loading...
Searching...
No Matches
token-stream.hpp
1/* Copyright 2025 Jonathan S. Arney
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * https://github.com/jarney/gyoji/blob/master/LICENSE
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15#pragma once
16#include <string>
17#include <vector>
18#include <map>
19#include <algorithm>
20#include <gyoji-misc/pointers.hpp>
21#include <gyoji-context/source-reference.hpp>
22
23namespace Gyoji::context {
24 class Token;
25 class TokenStream;
26
27 typedef unsigned int TokenID;
28
55 class Token {
56 public:
63 Token(
64 TokenID _typestr,
65 std::string _value,
66 const std::string & _filename,
67 size_t _line,
68 size_t _column,
69 size_t _length
70 );
74 ~Token();
84 const TokenID & get_type() const;
90 const std::string & get_value() const;
91
92 const SourceReference & get_source_ref() const;
93
94 friend TokenStream;
95 private:
100 void append(std::string & value);
101 TokenID typestr;
102 std::string value;
103 SourceReference src_ref;
104 };
105
121 public:
125 TokenStream();
129 ~TokenStream();
130
137
145
151 std::string get_line(size_t _line) const;
152
159 const Token & add_token(
160 TokenID _typestr,
161 std::string _value,
162 const std::string & _filename,
163 size_t _line,
164 size_t _column
165 );
166
175 std::vector<std::pair<size_t, std::string>> context(size_t line_start, size_t line_end) const;
176
187 void append_token(std::string _value);
188
189 static const SourceReference & get_zero_source_ref();
190
191 private:
192
194 std::map<size_t, std::vector<Token*>> tokens_by_lineno;
195 };
196};
References a location in the source-file.
Definition source-reference.hpp:30
Stream of tokens read by the parser to provide context for errors.
Definition token-stream.hpp:120
std::string get_line(size_t _line) const
Definition token-stream.cpp:56
~TokenStream()
Definition token-stream.cpp:31
const SourceReference & get_current_source_ref() const
Definition token-stream.cpp:47
const std::vector< Gyoji::owned< Token > > & get_tokens() const
Definition token-stream.cpp:35
void append_token(std::string _value)
Definition token-stream.cpp:102
std::vector< std::pair< size_t, std::string > > context(size_t line_start, size_t line_end) const
Definition token-stream.cpp:71
TokenStream()
Definition token-stream.cpp:28
const Token & add_token(TokenID _typestr, std::string _value, const std::string &_filename, size_t _line, size_t _column)
Definition token-stream.cpp:86
Represents a token read from the input stream.
Definition token-stream.hpp:55
const std::string & get_value() const
Definition token-stream.cpp:132
~Token()
Definition token-stream.cpp:122
const TokenID & get_type() const
Definition token-stream.cpp:128
The context namespace deals with objects that should last the entire scope of compilation.
Definition gyoji-context.hpp:30