Implemented the one-liner operators ` ( )
[fridhskrift.git] / lexer / comment.cpp
blobcb71fd6dc792bc442fa3e5366da311977f96ce14
1 #include <fridh/lexer.hpp>
3 namespace fridh
5 void lexer::parse_comment()
7 std::string const
8 multi_line_comment = ";;",
9 nested_comment_start = ";.",
10 nested_comment_end = ".;",
12 multiline_comment_prefix = "In multi-line comment: ",
13 nested_comment_prefix = "In nested comment: ";
15 uword start_of_comment = line;
17 if(string_match(multi_line_comment))
19 bool got_end = false;
20 for(i += multi_line_comment.size(); !got_end && i < end;)
22 char byte = input[i];
23 switch(byte)
25 case '\n':
26 process_newline();
27 continue;
29 case ';':
30 if(string_match(multi_line_comment))
32 got_end = true;
33 i++;
35 break;
37 i++;
39 if(!got_end)
40 lexer_error("Unable to find the end of a multi-line comment", start_of_comment);
42 else if(string_match(nested_comment_start))
44 uword comment_depth = 1;
45 for(i += nested_comment_start.size(); comment_depth > 0 && i < end;)
47 char byte = input[i];
48 if(byte == '\n')
50 process_newline();
51 continue;
53 else if(string_match(nested_comment_start))
55 comment_depth++;
56 i += nested_comment_start.size();
57 continue;
59 else if(string_match(nested_comment_end))
61 comment_depth--;
62 i += nested_comment_end.size();
63 continue;
65 i++;
68 if(comment_depth != 0)
69 lexer_error("Unable to find the end of a nested comment", start_of_comment);
71 else
73 std::size_t offset = input.find('\n', i);
74 if(offset == std::string::npos)
75 lexer_error("Unable to find the end of a multi-line comment", start_of_comment);
76 i = offset;
77 process_newline();