Simplified the arithmetic operator code
[fridhskrift.git] / lexer / comment.cpp
blobd13d325731e18e217a726e91fca30514f334769b
1 #include <fridh/lexer.hpp>
3 namespace fridh
5 bool lexer::parse_comment(std::string & error_message)
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)
41 error_message = lexer_error("Unable to find the end of a multi-line comment", start_of_comment);
42 return false;
45 else if(string_match(nested_comment_start))
47 uword comment_depth = 1;
48 for(i += nested_comment_start.size(); comment_depth > 0 && i < end;)
50 char byte = input[i];
51 if(byte == '\n')
53 process_newline();
54 continue;
56 else if(string_match(nested_comment_start))
58 comment_depth++;
59 i += nested_comment_start.size();
60 continue;
62 else if(string_match(nested_comment_end))
64 comment_depth--;
65 i += nested_comment_end.size();
66 continue;
68 i++;
71 if(comment_depth != 0)
73 error_message = lexer_error("Unable to find the end of a nested comment", start_of_comment);
74 return false;
77 else
79 std::size_t offset = input.find('\n', i);
80 if(offset == std::string::npos)
82 error_message = lexer_error("Unable to find the end of a multi-line comment", start_of_comment);
83 return false;
85 i = offset;
86 process_newline();
88 return true;