Beispiel #1
0
 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;
}
Beispiel #3
0
/**
	@brief	説明、引数、戻り値はMonapi2リファレンス参照。
	@date	2005/08/20	junjunn 作成
*/
void IniManager::parse(cpchar1 cszContent)
{
	m_strCurDir.empty();

//行に分割
	StringDivide SDLine(cszContent,"\n");

//行を巡回	
	for (uint i=0;i<SDLine.getCount();i++)
	{
//現在の行。
		cpchar1 pLine = SDLine.getAt(i);
//コメント行ならスキップ
		if (isLineComment(pLine))		continue;

//ディレクトリ指定の行らしい
		if (pLine[0]=='[')
		{
//ディレクトリ名の初めと終わり。
			cpchar1 pStart = pLine+1;
			cpchar1 pEnd = StringFn::find(pLine,']');
			if (pEnd==NULL)	pEnd = StringFn::getEnd(pStart);	//閉じカッコがないのはおかしいがなんとか修正する。
			m_strCurDir.copy(pStart,pEnd-pStart);
		}
//値
		else
		{
//Key=Value形式を区切る。
			StringDivide SDKeyValue(pLine,"=");
//キー名
			String strKey=SDKeyValue.getAt(0);
//先頭の空白を取り除く。
			strKey.remove(" ");		//空白
			strKey.remove("	");		//タブ

//値
			String strValue;
			if (SDKeyValue.getCount()>=2)
			{
//	strTest= "a=b";
//など値にも=が入ってしまっている困ったケースを考慮して残りを一つにくっつける。
				String strValueUnified=SDKeyValue.getAt(1);
				for (uint i=2;i<SDKeyValue.getCount();i++)
				{
					strValueUnified+="=";
					strValueUnified+=SDKeyValue.getAt(i);
				}

//先頭を見つける。
				cpchar1 pStart = strValueUnified;
//先頭の空白をスキップ。
				while (*pStart==' ' || *pStart=='	')	pStart++;

//最後を見つける。
				cpchar1 pEnd=pStart;
//="〜"形式だったら対応する"を見つける。
				if (*pStart=='"')
				{
					pStart++;
					pEnd=StringFn::find(pStart,'"');
					if (pEnd==NULL)	pEnd = StringFn::getEnd(pStart+1);	//閉じカッコがないのはおかしいがなんとか修正する。
				}
//=〜形式だったら;か行の終わりを見つける。
				else
				{
					while (! (*pEnd==';' || *pEnd=='\0'))	pEnd++;

				}

//値を取り出す。
				int iLen=pEnd-pStart;
				pchar1 pBuffer=(pchar1)strValue.extendBuffer(iLen+1);
				StringFn::copy(pBuffer,pStart,iLen);
			}

//ディレクトリ+キー名で作るフルキー名。
			String strKeyWhole;
			strKeyWhole.format("%s/%s",m_strCurDir.getString(),strKey.getString());
			m_strstrmap.setAt(strKeyWhole,strValue);
		}
	}
}