static void skipSingleLineComment(SrcFile *sf)
{
  int c;
  while( (c = getChar_(sf)) != EOF){
    if(c == '\n') break;
  }
}
static int skipBlockComment(SrcFile *sf){
  int startLine = sf->curLine;
  int numCommentedLFs = 0;
  int c = getChar_(sf);

  while( c != EOF){
    int nc = getChar_(sf);
    if(c == '*' && nc == '/'){
      return numCommentedLFs;
    }
    if(c == '\n') numCommentedLFs++;
    c = nc;
  }

  //reached EOF
  fprintf(stderr, "error: %s: line %d, unterminated comment\n", sf->filename, startLine);
  exit(EXIT_FAILURE);
}
//return char except comments
static int getChar(SrcFile *sf)
{
  int c = getChar_(sf);

  if(c == '\\'){
    int nc = getChar_(sf);
    if(nc == '"'){ //escaped double-quotation
      sf->inStrLiteral = !sf->inStrLiteral;
    }
    ungetChar_(nc, sf);
  }else if(sf->inStrLiteral){
    if(c == '"'){
      sf->inStrLiteral = 0;
    }
  }else{
    if(c == '/'){
      int nc = getChar_(sf);
      if(nc == '*'){ // block comment
	sf->numCommentedLFs += skipBlockComment(sf);
	c = ' ';
      }else if(nc == '/'){ // single-line comment
	skipSingleLineComment(sf);
	c = '\n';
      }else{
	ungetChar_(nc, sf);
      }
    }else if(c == '"'){
      sf->inStrLiteral = 1;
    }
  }

  if(c == '\n'){
    while(sf->numCommentedLFs > 0){
      ungetChar_('\n', sf);
      sf->numCommentedLFs--;
    }
  }
  return c;
}
示例#4
0
char StringEscaper::getChar(){
   char x = getChar_();
   pos++;
   return x;
};