@@ -95,6 +95,14 @@ void App::processTokens(std::string &inputFileContent) {
9595 tokenStreamBuilder.build ();
9696 std::vector<Token> tokens = tokenStreamBuilder.getTokens ();
9797
98+ // if no tokens are generated, throw an exception
99+ if (tokens.empty ()) {
100+ throw std::runtime_error (" No tokens generated. Please check the input file." );
101+ }
102+
103+ // any unknown tokens printed as errors to the console
104+ catchUnkonwnTokens (tokens);
105+
98106 // write tokens to output file if specified
99107 if (hasOutputFile) {
100108 // set color to orange
@@ -112,6 +120,31 @@ void App::processTokens(std::string &inputFileContent) {
112120 }
113121}
114122
123+ void App::catchUnkonwnTokens (std::vector<TINY::SCANNER::Token> &tokens) {
124+ bool hasUnknownTokens = false ;
125+ for (const Token &token : tokens) {
126+ if (token.getType () == TokenType::UNKNOWN) {
127+ // for the first unknown token, set color to red
128+ if (!hasUnknownTokens) {
129+ hasUnknownTokens = true ;
130+ // set color to red
131+ std::cout << " \033 [1;31m" ;
132+ std::cerr << " The input contains unexpected tokens, please check the input file."
133+ << std::endl
134+ << " List of unexpected tokens:"
135+ << std::endl;
136+ }
137+
138+ std::cerr << " -\t Error: unexpected token '" << token.getValue ()
139+ << " ' at line " << token.getLine ()
140+ << " , column " << token.getColumn ()
141+ << std::endl;
142+ }
143+ }
144+ // reset color
145+ std::cout << " \033 [0m" ;
146+ }
147+
115148void App::printTokens (const std::vector<Token> &tokens, bool includePosition) {
116149 // set color to Green
117150 std::cout << " \033 [1;32m" ;
0 commit comments