I haven't worked on this in ages, illness somewhat interrupted my line of thought...
[fridhskrift.git] / lexer / string.cpp
blob10bfa1186f711ee784e07ca370713aedb22f2ad1
1 #include <fridh/lexer.hpp>
2 #include <ail/string.hpp>
4 namespace fridh
6 void lexer::parse_string(line_of_code & output)
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)
20 lexer_error("Backslash at the end of the input");
22 i++;
24 char next_byte = input[i];
26 switch(next_byte)
28 case 'r':
29 string.push_back('\r');
30 continue;
32 case 'n':
33 string.push_back('\n');
34 continue;
37 if(ail::is_hex_digit(next_byte))
39 if(end - i < 2)
40 lexer_error("Incomplete hex number escape sequence at the end of the input");
42 if(!ail::is_hex_digit(input[i + 1]))
43 lexer_error("Invalid hex number escape sequence");
45 std::string hex_string = input.substr(i, 2);
46 i++;
47 char new_byte = ail::string_to_number<char>(hex_string, std::ios_base::hex);
48 string.push_back(new_byte);
50 else
51 lexer_error("Invalid escape sequence: " + ail::hex_string_8(static_cast<uchar>(next_byte)));
52 break;
55 case '\n':
56 lexer_error("Detected a newline in a string");
57 break;
59 case '\'':
60 case '"':
61 if(byte == string_character)
63 output.lexemes.push_back(lexeme(string));
64 i++;
65 return;
67 string.push_back(byte);
68 break;
70 default:
71 string.push_back(byte);
72 break;
75 lexer_error("String lacks terminator");