-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess-comments.cc
More file actions
95 lines (82 loc) · 2.52 KB
/
process-comments.cc
File metadata and controls
95 lines (82 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include "buffered-input.h"
#include "lex.h"
/*
* If a # is encountered, begin processing line as line comment
*
* Comments are not Tokens, and
* all characters in a comment will be skipped
*
* @return Error NCC_OK if no errors occur
* NCC_UNEXPECTED_EOF if EOF is reached before \n
*/
Error process_line_comment()
{
char c;
// advance buffer location until EOF or \n is encountered
while (buffer_get_next_char(c) != -1 && c != '\n');
// if EOF is reached before \n, it is an error
if (lex_eof())
return Error { NCC_UNEXPECTED_EOF, src_line_no, src_col_no };
return Error { NCC_OK, src_line_no, src_col_no };
}
/*
* If a <<- is encountered, begin processing following chars as a block comment
* until ->> is encountered
*
* Comments are not Tokens, and
* all characters in a comment will be skipped
*
* @return Error NCC_OK if no errors occur
* NCC_UNEXPECTED_EOF if EOF is reached before ->>
*/
Error process_block_comment()
{
char c;
// assume <<- is true
bool block_start_token = true;
// retrieve next character
buffer_get_next_char(c);
// if not <<, then this is not block comment
if (c != '<')
{
// <<- is not true
block_start_token = false;
// shift back 2 characters to process < as Token
buffer_back_char();
buffer_back_char();
return Error { NCC_OK, src_line_no, src_col_no };
}
// retrieve next character
buffer_get_next_char(c);
// if not <<-, then this is not block comment
if (block_start_token && c != '-')
{
// <<- is not true
block_start_token = false;
// shift back 3 characters to process < as Token
buffer_back_char();
buffer_back_char();
buffer_back_char();
return Error { NCC_OK, src_line_no, src_col_no };
}
// retrieve characters until EOF
while (buffer_get_next_char(c) != -1)
{
// - part of ->> is found
if (c == '-')
{
// if not ->, this is not the end of a block comment
buffer_get_next_char(c);
if (c != '>')
continue; // start loop from beginning
// if not ->>, this is not the end of a block comment
buffer_get_next_char(c);
if (c == '>')
break; // exit out of loop
}
}
// EOF encountered before ->>
if (lex_eof())
return Error { NCC_UNEXPECTED_EOF, src_line_no, src_col_no };
return Error { NCC_OK, src_line_no, src_col_no };
}