1- #include < cctype>
2- #include < fstream>
31#include < lbd/diagnostics/ContextGuard.hpp>
42#include < lbd/frontend/lexer/Lexer.hpp>
53#include < lbd/source/Range.hpp>
@@ -50,13 +48,13 @@ namespace lbd::frontend::lexer
5048 {
5149 while (std::isspace (static_cast <unsigned char >(getCurrentCharacter ()))) advanceCursor ();
5250
53- const source::Location beginLocation{buffer.id , cursor};
51+ const source::Location beginLocation{buffer.getId () , cursor};
5452
5553 // Adding Context Guard for diagnostics.
5654 diagnostics::ContextGuard contextGuard (
5755 context.getDiagnosticEmitter (),
58- source::Range (beginLocation, {buffer.id , static_cast <source::Offset>(buffer.getSize ())}),
59- fmt::format (" While lexing `{}`." , buffer.name ));
56+ source::Range (beginLocation, {buffer.getId () , static_cast <source::Offset>(buffer.getSize ())}),
57+ fmt::format (" While lexing `{}`." , buffer.getName () ));
6058
6159 if (isEof ()) return {token::TokenKind::END_OF_FILE , " " , {beginLocation, beginLocation}};
6260
@@ -65,8 +63,9 @@ namespace lbd::frontend::lexer
6563 {
6664 const source::Offset beginOffset = cursor;
6765 while (!isEof () && isIdentifier (getCurrentCharacter ())) advanceCursor ();
68- const auto lexeme = buffer.contents .substr (beginOffset, cursor - beginOffset);
69- return {token::TokenKind::IDENTIFIER , lexeme, {beginLocation, {buffer.id , cursor}}};
66+ // TODO: Relook whether Lexer should create a copy of lexeme or refer to buffer.
67+ const auto lexeme = std::string (buffer.getContents ().substr (beginOffset, cursor - beginOffset));
68+ return {token::TokenKind::IDENTIFIER , lexeme, {beginLocation, {buffer.getId (), cursor}}};
7069 }
7170
7271 // Number.
@@ -79,7 +78,7 @@ namespace lbd::frontend::lexer
7978 if (getCurrentCharacter () == ' -' && !std::isdigit (peekNextCurrentCharacter ()))
8079 {
8180 context.getDiagnosticEmitter ().error (
82- {beginLocation, {buffer.id , cursor}},
81+ {beginLocation, {buffer.getId () , cursor}},
8382 diagnostics::DiagnosticId::LEXER_UNEXPECTED_CHARACTER , ' -'
8483 );
8584 }
@@ -91,8 +90,8 @@ namespace lbd::frontend::lexer
9190 advanceCursor (); // Consume '.'
9291 while (!isEof () && std::isdigit (getCurrentCharacter ())) advanceCursor ();
9392 }
94- const auto lexeme = buffer.contents .substr (beginOffset, cursor - beginOffset);
95- return {token::TokenKind::NUMBER , lexeme, {beginLocation, {buffer.id , cursor}}};
93+ const auto lexeme = std::string ( buffer.getContents () .substr (beginOffset, cursor - beginOffset) );
94+ return {token::TokenKind::NUMBER , lexeme, {beginLocation, {buffer.getId () , cursor}}};
9695 }
9796
9897 // String literal.
@@ -104,14 +103,14 @@ namespace lbd::frontend::lexer
104103 if (getCurrentCharacter () != ' "' )
105104 {
106105 context.getDiagnosticEmitter ().error (
107- {beginLocation, {buffer.id , cursor}},
106+ {beginLocation, {buffer.getId () , cursor}},
108107 diagnostics::DiagnosticId::LEXER_UNBALANCED_QUOTE
109108 );
110109 }
111- advanceCursor (); // Consume '"'
110+ advanceCursor (); // Consume double-quote.
112111 // +1 and -1 for excluding the quotes.
113- const auto lexeme = buffer.contents .substr (beginOffset + 1 , cursor - beginOffset - 2 );
114- return {token::TokenKind::STRING , lexeme, {beginLocation, {buffer.id , cursor}}};
112+ const auto lexeme = std::string ( buffer.getContents () .substr (beginOffset + 1 , cursor - beginOffset - 2 ) );
113+ return {token::TokenKind::STRING , lexeme, {beginLocation, {buffer.getId () , cursor}}};
115114 }
116115
117116 LexSymbol:
@@ -128,7 +127,7 @@ namespace lbd::frontend::lexer
128127 {
129128 advanceCursor ();
130129 const auto lexeme = std::string (1 , symbol);
131- return {symbolToTokenKind (symbol), lexeme, {beginLocation, {buffer.id , cursor}}};
130+ return {symbolToTokenKind (symbol), lexeme, {beginLocation, {buffer.getId () , cursor}}};
132131 }
133132 case ' =' :
134133 {
@@ -137,9 +136,9 @@ namespace lbd::frontend::lexer
137136 {
138137 // FAT_ARROW
139138 advanceCursor (); // Consume '>'
140- return {token::TokenKind::FAT_ARROW , " =>" , {beginLocation, {buffer.id , cursor}}};
139+ return {token::TokenKind::FAT_ARROW , " =>" , {beginLocation, {buffer.getId () , cursor}}};
141140 }
142- return {token::TokenKind::ASSIGNMENT , " =" , {beginLocation, {buffer.id , cursor}}};
141+ return {token::TokenKind::ASSIGNMENT , " =" , {beginLocation, {buffer.getId () , cursor}}};
143142 }
144143 case ' -' :
145144 {
@@ -154,7 +153,7 @@ namespace lbd::frontend::lexer
154153 {
155154 // Arrow.
156155 advanceCursor (); // Consume '>'.
157- return {token::TokenKind::ARROW , " ->" , {beginLocation, {buffer.id , cursor}}};
156+ return {token::TokenKind::ARROW , " ->" , {beginLocation, {buffer.getId () , cursor}}};
158157 }
159158 default :
160159 break ;
@@ -164,7 +163,7 @@ namespace lbd::frontend::lexer
164163 break ;
165164 }
166165 context.getDiagnosticEmitter ().error (
167- {beginLocation, {buffer.id , cursor}},
166+ {beginLocation, {buffer.getId () , cursor}},
168167 diagnostics::DiagnosticId::LEXER_UNEXPECTED_CHARACTER , getCurrentCharacter ()
169168 );
170169 }
@@ -205,11 +204,11 @@ namespace lbd::frontend::lexer
205204 case ' (' : return token::TokenKind::OPEN_PARENTHESIS ;
206205 case ' )' : return token::TokenKind::CLOSE_PARENTHESIS ;
207206 default :
208- const source::Location location{buffer.id , cursor};
207+ const source::Location location{buffer.getId () , cursor};
209208 context.getDiagnosticEmitter ().error (
210209 {location, location},
211210 diagnostics::DiagnosticId::LEXER_UNEXPECTED_CHARACTER , symbol
212211 );
213212 }
214213 }
215- }
214+ }
0 commit comments