Simplified the arithmetic operator code
[fridhskrift.git] / lexer / string.cpp
bloba41d34833446fc3ba85aabfcfd829dc111819365
1 #include <fridh/lexer.hpp>
2 #include <ail/string.hpp>
4 namespace fridh
6 bool lexer::parse_string(line_of_code & output, std::string & error_message, std::string error_prefix)
8 std::string string;
9 char string_character = input[i];
10 i++;
11 std::size_t start = i;
12 for(; i < end; i++)
14 char byte = input[i];
15 switch(byte)
17 case '\\':
19 if(end - i < 2)
21 error_message = lexer_error(error_prefix + "Backslash at the end of the input");
22 return false;
25 i++;
27 char next_byte = input[i];
29 switch(next_byte)
31 case 'r':
32 string.push_back('\r');
33 continue;
35 case 'n':
36 string.push_back('\n');
37 continue;
40 if(ail::is_hex_digit(next_byte))
42 if(end - i < 2)
44 error_message = lexer_error(error_prefix + "Incomplete hex number escape sequence at the end of the input");
45 return false;
47 if(!ail::is_hex_digit(input[i + 1]))
49 error_message = lexer_error(error_prefix + "Invalid hex number escape sequence");
50 return false;
52 std::string hex_string = input.substr(i, 2);
53 i++;
54 char new_byte = ail::string_to_number<char>(hex_string, std::ios_base::hex);
55 string.push_back(new_byte);
57 else
59 error_message = lexer_error(error_prefix + "Invalid escape sequence: " + ail::hex_string_8(static_cast<uchar>(next_byte)));
60 return false;
62 break;
65 case '\n':
66 error_message = lexer_error(error_prefix + "Detected a newline in a string");
67 return false;
69 case '\'':
70 case '"':
71 if(byte == string_character)
73 output.lexemes.push_back(lexeme(string, string));
74 i++;
75 return true;
77 string.push_back(byte);
78 break;
80 default:
81 string.push_back(byte);
82 break;
85 error_message = lexer_error(error_prefix + "String lacks terminator");
86 return false;