コード例 #1
0
ファイル: lexer.cpp プロジェクト: mvila/liu
 Token *Lexer::nextToken() {
     while(true) {
         switch(_currentChar.toAscii()) {
         case '\'': return scanCharacter();
         case '"': return scanText();
         case '(': return scan(Token::LeftParenthesis);
         case ')': return scan(Token::RightParenthesis);
         case '[': return scan(Token::LeftBracket);
         case ']': return scanRightBracket();
         case '{': return scan(Token::LeftBrace);
         case '}': return scan(Token::RightBrace);
         case ';': return scan(Token::Semicolon);
         default:
             if(isEof()) return scan(Token::Eof);
             else if(isLineComment()) consumeLineComment();
             else if(isBlockComment()) consumeBlockComment();
             else if(isNewline()) return scanNewline();
             else if(isSpace()) consumeSpaces();
             else if(isName()) return scanName();
             else if(isBackquotedName()) return scanBackquotedName();
             else if(isNumber()) return scanNumber();
             else if(isOperator()) return scanOperator();
             else throw lexerException(QString("invalid character: '%1'").arg(_currentChar));
         }
     }
 }
int main()
{
    std::ifstream inputFile;
    std::ofstream noCommentFile;
    std::ofstream commentFile;
    
    inputFile.open ("code_with_comments.txt", std::ios::in | std::ios::binary);
    commentFile.open ("comments.txt", std::ios::out | std::ios::trunc | std::ios::binary);
    noCommentFile.open ("no_comments.txt", std::ios::out | std::ios::trunc | std::ios::binary);
    char ch;
    std::vector<char> fragment;
    if (inputFile.is_open()) {
        while(!inputFile.eof())
        {
            inputFile.get(ch);
            fragment.push_back(ch);

            if( isBlockComment(fragment) || isLineComment(fragment)) {
                writeToFile(fragment, inputFile, commentFile);
            }

            else if( (!isOpenComment(fragment)) && 
                    (ch != '/') && 
                    (ch != '*') && 
                    (ch != '\n') ) {
                writeToFile(fragment, inputFile, noCommentFile);
            }
            else {
                continue;
            }
        }
    }
    else {
        std::cout <<"Unable to open the file...";
    }

    commentFile.close();
    noCommentFile.close();
    inputFile.close();
    return 0;
}