Java Reference
In-Depth Information
puts the current character, ch , back onto the input stream, and decrements
currentLine if the character is a newline. Clearly, putBackChar should be
called at most once between calls to nextChar ; as it is a private routine, we
do not worry about abuse on the part of the class user. Putting characters
back onto the input stream is a commonly used technique in parsing. In
many instances we have read one too many characters, and undoing the read
is useful. In our case this occurs after processing a / . We must determine
whether the next character begins the comment start token; if it does not, we
cannot simply disregard it because it could be an opening or closing symbol
or a quote. Thus we pretend that it is never read.
Next is the routine skipComment , shown in Figure 11.5. Its purpose is to skip
over the characters in the comment and position the input stream so that the next
read is the first character after the comment ends. This technique is complicated
by the fact that comments can either begin with // , in which case the line ends
the comment, or /* , in which case */ ends the comment. 1 In the // case, we con-
tinually get the next character until either the end of file is reached (in which
1 /**
2 * Precondition: We are about to process a comment;
3 * have already seen comment-start token
4 * Postcondition: Stream will be set immediately after
5 * comment-ending token
6 */
7 private void skipComment( int start )
8 {
9 if( start == SLASH_SLASH )
10 {
11 while( nextChar( ) && ( ch != '\n' ) )
12 ;
13 return;
14 }
15
16 // Look for a */ sequence
17 boolean state = false; // True if we have seen *
18
19 while( nextChar( ) )
20 {
21 if( state && ch == '/' )
22 return;
23 state = ( ch == '*' );
24 }
25 errors++;
26 System.out.println( "Unterminated comment!" );
27 }
figure 11.5
The skipComment
routine for moving
past an already
started comment
1. We do not consider deviant cases involving \ , nor /**/ .
 
 
Search WWH ::




Custom Search