Пример #1
0
void CodeEditer::indentBlock(QTextDocument *doc, QTextBlock block, QChar typedChar)
{
	Q_UNUSED(typedChar);

	// At beginning: Leave as is.
	if (block == doc->begin())
		return;

	const QTextBlock previous = block.previous();
	const QString previousText = previous.text();
	// Empty line indicates a start of a new paragraph. Leave as is.
	if (previousText.isEmpty() || previousText.trimmed().isEmpty())
		return;

	// Just use previous line.
	// Skip non-alphanumerical characters when determining the indentation
	// to enable writing bulleted lists whose items span several lines.
	int i = 0;
	while (i < previousText.size()) {
		if (previousText.at(i).isLetterOrNumber() ||
			previousText.at(i) == '{' ||
			previousText.at(i) == '}' ||
			previousText.at(i) == '#') {
			//const TextEditor::TabSettings &ts = tabSettings();
			indentLine(block, columnAt(previousText, i));
			break;
		}
		++i;
	}
}
XMLSerializer& XMLSerializer::closeTag(void)
{
    String back =  d_tagStack.back();
    if (! d_error)
    {
        --d_depth;
        if (d_needClose)
        {
            d_stream << "/>";
        }
        else if (! d_lastIsText)
        {
            d_stream << std::endl;
            indentLine();
            d_stream << "</" << back.c_str() << '>';
        }
        else
        {
            d_stream << "</" << back.c_str() << '>';
        }
        d_lastIsText = false;
        d_needClose = false;
        d_tagStack.pop_back();
        d_error = ! d_stream;
    }
    return *this;
}
Пример #3
0
/**
 * additional formatting for line of source code.
 * every line of source code in a source code file should be sent
 *     one after the other to this function.
 * indents event tables
 * unindents the case blocks
 *
 * @param line       the original formatted line will be updated if necessary.
 */
void ASEnhancer::enhance(string &line, bool isInPreprocessor, bool isInSQL)
{
	shouldUnindentLine = true;
	shouldUnindentComment = false;
	lineNumber++;

	// check for beginning of event table
	if (nextLineIsEventIndent)
	{
		isInEventTable = true;
		nextLineIsEventIndent = false;
	}

	// check for beginning of SQL declare section
	if (nextLineIsDeclareIndent)
	{
		isInDeclareSection = true;
		nextLineIsDeclareIndent = false;
	}

	if (line.length() == 0
	        && ! isInEventTable
	        && ! isInDeclareSection
	        && ! emptyLineFill)
		return;

	// test for unindent on attached brackets
	if (unindentNextLine)
	{
		sw.unindentDepth++;
		sw.unindentCase = true;
		unindentNextLine = false;
	}

	// parse characters in the current line
	parseCurrentLine(line, isInPreprocessor, isInSQL);

	if (isInEventTable || isInDeclareSection)
	{
		if (line.length() == 0 || line[0] != '#')
			indentLine(line, 1);
	}

	if (shouldUnindentComment && sw.unindentDepth > 0)
		unindentLine(line, sw.unindentDepth - 1);
	else if (shouldUnindentLine && sw.unindentDepth > 0)
		unindentLine(line, sw.unindentDepth);
}
XMLSerializer& XMLSerializer::openTag(const String& name)
{
    if (! d_error)
    {
        ++d_tagCount;
        if (d_needClose)
        {
            d_stream << '>';	  
        }
        if (!d_lastIsText)
        {
            d_stream << std::endl;
            indentLine();
        }
        d_stream << '<' << name.c_str() << ' ';
        d_tagStack.push_back(name);
        ++d_depth;
        d_needClose = true;
        d_lastIsText = false;
        d_error = ! d_stream;
    }
    return *this;
}
Пример #5
0
void printLlvmIrCode(FILE *fp)
{
	fprintf(fp,"define void @main()\n");
	fprintf(fp,"{\n");
	fprintf(fp,"\tcall i32 @startint();\n");
	fprintf(fp,"\tret void;\n");
	fprintf(fp,"}\n\n");
	for(int i=0;i<functions.size();i++)
	{
		Function *func=functions[i];
		for(int j=0;j<func->name.size();j++)
		{
			if(func->name[j]->var==NULL)
			{
				func->processedFunctionName+=func->name[j]->text;
			}
			else
			{
				func->processedFunctionName+=func->name[j]->var->type->name;
			}
		}
		if(!isalpha(func->processedFunctionName[0]))
			func->processedFunctionName.insert(0,"f");

		for(int j=0;j<func->processedFunctionName.size();j++)
		{
			char c=func->processedFunctionName[j];
			string replacement="Q";
			if(!isalnum(c) && c!='.' && c!='_')
			{
				switch(c)
				{
				case '+':
					replacement="plus";
					break;
				case '*':
					replacement="asterisk";
					break;
				case '=':
					replacement="equal";
					break;
				case '<':
					replacement="less";
					break;
				default:
					warn("No known replacement for %c\n",c);
					break;
				}

				replacement.insert(0,".");
				replacement.push_back('.');
				func->processedFunctionName.erase(j,1);
				func->processedFunctionName.insert(j,replacement);
				j+=replacement.size();
			}
		}
	}


	for(int i=0;i<functions.size();i++)
	{
		Function *func=functions[i];
		fprintf(fp,"define fastcc ");

		if(func->ret==NULL)
			fprintf(fp,"void ");
		else
			fprintf(fp,"%s ",getLlvmVariableType(func->ret).c_str());

		fprintf(fp,"@%s ( ",func->processedFunctionName.c_str());
		for(int j=0;j<func->arguments.size();j++)
		{
			fprintf(fp,"%s %%%s",getLlvmVariableType(func->arguments[j]).c_str(),func->arguments[j]->llvmName().c_str());
			if(j!=func->arguments.size()-1)
				fprintf(fp,", ");
		}
		fprintf(fp,")\n{\n");

		Line *lastLine=NULL;
		for(int iLine=func->firstLine->lineNumber;iLine<func->lastLine->lineNumber;iLine++)
		{
			Line *line=lines[iLine];
			if(lastLine!=NULL && lastLine->level!=line->level)
			{
				if(lastLine->level<line->level)
				{
					for(int i=lastLine->level;i<line->level;i++)
					{
						indentLine(fp,i);
						fprintf(fp,"{\n");
					}
				}
				else if(lastLine->level>line->level)
				{
					for(int i=lastLine->level-1;i>=line->level;i--)
					{
						indentLine(fp,i);
						fprintf(fp,"}\n");
					}
				}
			}
			for(int iCall=0;iCall<line->commands.size();iCall++)
			{
				FunctionCall *call=line->commands[iCall];
				indentLine(fp,line->level);
				fprintf(fp,"%s\n",call->llvmCall().c_str());
			}
			lastLine=line;
		}
		if(lastLine!=NULL && lastLine->level!=1)
			for(int i=lastLine->level-1;i>=1;i--)
			{
				indentLine(fp,i);
				fprintf(fp,"}\n");
			}
		if(func->ret==NULL)
			fprintf(fp,"\tret void\n}\n\n");
		else
			fprintf(fp,"\tret %s %%%s\n}\n\n",getLlvmVariableType(func->ret).c_str(),func->ret->llvmName().c_str());
	}
}
Пример #6
0
static void indent(QTextBlock block, const QStringList &previousCode)
{
    int indent = indentForBottomLine(previousCode, QChar::Null);
    indentLine(block, indent);
}
Пример #7
0
/**
 * additional formatting for line of source code.
 * every line of source code in a source code file should be sent
 *     one after the other to this function.
 * indents event tables
 * unindents the case blocks
 *
 * @param line       the original formatted line will be updated if necessary.
 */
void ASEnhancer::enhance(string& line, bool isInPreprocessor, bool isInSQL)
{
	bool isSpecialChar = false;			// is a backslash escape character
	shouldIndentLine = true;
	lineNumber++;

	// check for beginning of event table
	if (nextLineIsEventIndent)
	{
		isInEventTable = true;
		nextLineIsEventIndent = false;
	}

	// check for beginning of SQL declare section
	if (nextLineIsDeclareIndent)
	{
		isInDeclareSection = true;
		nextLineIsDeclareIndent = false;
	}

	if (line.length() == 0
	        && ! isInEventTable
	        && ! isInDeclareSection
	        && ! emptyLineFill)
		return;

	// test for unindent on attached brackets
	if (unindentNextLine)
	{
		sw.unindentDepth++;
		sw.unindentCase = true;
		unindentNextLine = false;
	}

	// parse characters in the current line.

	for (size_t i = 0; i < line.length(); i++)
	{
		char ch = line[i];

		// bypass whitespace
		if (isWhiteSpace(ch))
			continue;

		// handle special characters (i.e. backslash+character such as \n, \t, ...)
		if (isSpecialChar)
		{
			isSpecialChar = false;
			continue;
		}
		if (!(isInComment) && line.compare(i, 2, "\\\\") == 0)
		{
			i++;
			continue;
		}
		if (!(isInComment) && ch == '\\')
		{
			isSpecialChar = true;
			continue;
		}

		// handle quotes (such as 'x' and "Hello Dolly")
		if (!isInComment && (ch == '"' || ch == '\''))
		{
			if (!isInQuote)
			{
				quoteChar = ch;
				isInQuote = true;
			}
			else if (quoteChar == ch)
			{
				isInQuote = false;
				continue;
			}
		}

		if (isInQuote)
			continue;

		// handle comments

		if (!(isInComment) && line.compare(i, 2, "//") == 0)
		{
			// check for windows line markers
			if (line.compare(i + 2, 1, "\xf0") > 0)
				lineNumber--;
			break;                 // finished with the line
		}
		else if (!(isInComment) && line.compare(i, 2, "/*") == 0)
		{
			isInComment = true;
			i++;
			continue;
		}
		else if ((isInComment) && line.compare(i, 2, "*/") == 0)
		{
			isInComment = false;
			i++;
			continue;
		}

		if (isInComment)
			continue;

		// if we have reached this far then we are NOT in a comment or string of special characters

		if (line[i] == '{')
			bracketCount++;

		if (line[i] == '}')
			bracketCount--;

		bool isPotentialKeyword = isCharPotentialHeader(line, i);

		// ----------------  wxWidgets and MFC macros  ----------------------------------

		if (isPotentialKeyword)
		{
			if (findKeyword(line, i, "BEGIN_EVENT_TABLE")
			        || findKeyword(line, i, "BEGIN_DISPATCH_MAP")
			        || findKeyword(line, i, "BEGIN_EVENT_MAP")
			        || findKeyword(line, i, "BEGIN_MESSAGE_MAP")
			        || findKeyword(line, i, "BEGIN_PROPPAGEIDS"))
			{
				nextLineIsEventIndent = true;
				break;
			}
			if (findKeyword(line, i, "END_EVENT_TABLE")
			        || findKeyword(line, i, "END_DISPATCH_MAP")
			        || findKeyword(line, i, "END_EVENT_MAP")
			        || findKeyword(line, i, "END_MESSAGE_MAP")
			        || findKeyword(line, i, "END_PROPPAGEIDS"))
			{
				isInEventTable = false;
				break;
			}
		}

		// ----------------  process SQL  -----------------------------------------------

		if (isInSQL)
		{
			if (isBeginDeclareSectionSQL(line, i))
				nextLineIsDeclareIndent = true;
			if (isEndDeclareSectionSQL(line, i))
				isInDeclareSection = false;
			break;
		}

		// ----------------  process switch statements  ---------------------------------

		if (isPotentialKeyword && findKeyword(line, i, "switch"))
		{
			switchDepth++;
			switchStack.push_back(sw);                      // save current variables
			sw.switchBracketCount = 0;
			sw.unindentCase = false;                        // don't clear case until end of switch
			i += 5;                                         // bypass switch statement
			continue;
		}

		// just want unindented switch statements from this point

		if (caseIndent
		        || switchDepth == 0
		        || (isInPreprocessor && !preprocessorIndent))
		{
			// bypass the entire word
			if (isPotentialKeyword)
			{
				string name = getCurrentWord(line, i);
				i += name.length() - 1;
			}
			continue;
		}

		i = processSwitchBlock(line, i);

	}   // end of for loop * end of for loop * end of for loop * end of for loop

	if (isInEventTable || isInDeclareSection)
	{
		if (line.length() == 0 || line[0] != '#')
			indentLine(line, 1);
	}

	if (shouldIndentLine && sw.unindentDepth > 0)
		unindentLine(line, sw.unindentDepth);
}
Пример #8
0
/**
 * additional formatting for line of source code.
 * every line of source code in a source code file should be sent
 *     one after the other to this function.
 * indents event tables
 * unindents the case blocks
 *
 * @param line       the original formatted line will be updated if necessary.
 */
void ASEnhancer::enhance(string &line)
{
	bool   isSpecialChar = false;
	size_t  lineLength;                             // length of the line being parsed

	lineNumber++;
	lineLength = line.length();

	// check for beginning of event table
	if (nextLineIsEventTable)
	{
		isInEventTable = true;
		nextLineIsEventTable = false;
	}

	if (lineLength == 0
	        && ! isInEventTable
	        && ! emptyLineFillX)
		return;

	// test for unindent on attached brackets
	if (unindentNextLine)
	{
		sw.unindentDepth++;
		sw.unindentCase = true;
		unindentNextLine = false;
		TRcase(" unindent case ", sw.unindentDepth);
	}

	// parse characters in the current line.

	for (size_t i = 0; i < lineLength; i++)
	{
		char ch = line[i];

		// bypass whitespace
		if (isWhiteSpaceX(ch))
			continue;

		// handle special characters (i.e. backslash+character such as \n, \t, ...)
		if (isSpecialChar)
		{
			isSpecialChar = false;
			continue;
		}
		if (!(isInComment) && line.compare(i, 2, "\\\\") == 0)
		{
			i++;
			continue;
		}
		if (!(isInComment) && ch == '\\')
		{
			isSpecialChar = true;
			continue;
		}

		// handle quotes (such as 'x' and "Hello Dolly")
		if (!(isInComment) && (ch == '"' || ch == '\''))
		{
			if (!isInQuote)
			{
				quoteChar = ch;
				isInQuote = true;
			}
			else if (quoteChar == ch)
			{
				isInQuote = false;
				continue;
			}
		}

		if (isInQuote)
			continue;

		// handle comments

		if (!(isInComment) && line.compare(i, 2, "//") == 0)
		{
			// check for windows line markers
			if (line.compare(i + 2, 1, "\xf0") > 0)
				lineNumber--;
			break;                 // finished with the line
		}
		else if (!(isInComment) && line.compare(i, 2, "/*") == 0)
		{
			isInComment = true;
			i++;
			continue;
		}
		else if ((isInComment) && line.compare(i, 2, "*/") == 0)
		{
			isInComment = false;
			i++;
			continue;
		}

		if (isInComment)
			continue;

		// if we have reached this far then we are NOT in a comment or string of special characters

		if (line[i] == '{')                                 // if open bracket
			bracketCount++;

		if (line[i] == '}')                     // if close bracket
			bracketCount--;

		// ----------------  process event tables  --------------------------------------

		// check for event table begin
		if (findKeyword(line, i, "BEGIN_EVENT_TABLE")
		        || findKeyword(line, i, "BEGIN_MESSAGE_MAP"))
			nextLineIsEventTable = true;

		// check for event table end
		if (findKeyword(line, i, "END_EVENT_TABLE")
		        || findKeyword(line, i, "END_MESSAGE_MAP"))
			isInEventTable = false;

		// ----------------  process switch statements  ---------------------------------

		if (findKeyword(line, i, "switch"))                 // if switch statement
		{
			switchDepth++;                                  // bump switch depth
			TRswitch(" switch ", switchDepth);
			swVector.push_back(sw);                         // save current variables
			sw.switchBracketCount = 0;
			sw.unindentCase = false;                        // don't clear case until end of switch
			i += 5;                                         // bypass switch statement
			continue;
		}

		// just want switch statements from this point

		if (caseIndentX || switchDepth == 0)               // from here just want switch statements
			continue;                                      // get next char

		if (line[i] == '{')                                 // if open bracket
		{
			sw.switchBracketCount++;
			if (lookingForCaseBracket)                      // if 1st after case statement
			{
				sw.unindentCase = true;                     // unindenting this case
				sw.unindentDepth++;                         // bump depth
				lookingForCaseBracket = false;              // not looking now
				TRcase(" unindent case ", sw.unindentDepth);
			}
			continue;
		}

		lookingForCaseBracket = false;                      // no opening bracket, don't indent

		if (line[i] == '}')                                 // if close bracket
		{
			sw.switchBracketCount--;
			if (sw.switchBracketCount == 0)                 // if end of switch statement
			{
				TRswitch("  endsw ", switchDepth);
				switchDepth--;                              // one less switch
				sw = swVector.back();                       // restore sw struct
				swVector.pop_back();                        // remove last entry from stack
			}
			continue;
		}

		// look for case or default header

		if (findKeyword(line, i, "case") || findKeyword(line, i, "default"))
		{
			if (sw.unindentCase)                            // if unindented last case
			{
				sw.unindentCase = false;                    // stop unindenting previous case
				sw.unindentDepth--;                         // reduce depth
			}
			bool isInQuote = false;
			char quoteChar = ' ';
			for (; i < lineLength; i++)                     // find colon
			{
				if (isInQuote)
				{
					if (line[i] == '\\')
					{
						i++;								// bypass next char
						continue;
					}
					else if (line[i] == quoteChar)			// check ending quote
					{
						isInQuote = false;
						quoteChar = ' ';
						continue;
					}
					else
					{
						continue;							// must close quote before continuing
					}
				}
				if (line[i] == '\'' || line[i] == '\"')	// check opening quote
				{
					isInQuote = true;
					quoteChar = line[i];
					continue;
				}
				if (line[i] == ':')
				{
					if ((i + 1 < lineLength) && (line[i + 1] == ':'))
						i++;								// bypass scope resolution operator
					else
						break;								// found it
				}
			}
			i++;
			for (; i < lineLength; i++)                     // bypass whitespace
			{
				if (!(isWhiteSpaceX(line[i])))
					break;
			}
			if (i < lineLength)                             // check for bracket
			{
				if (line[i] == '{')                         // if bracket found
				{
					sw.switchBracketCount++;
					unindentNextLine = true;                // start unindenting on next line
					continue;
				}
			}
			lookingForCaseBracket = true;                   // bracket must be on next line
			i--;                                            // need to check for comments
			continue;
		}
	}   // end of for loop

	if (isInEventTable) 									// if need to indent
		indentLine(line, 1);               					//    do it

	if (sw.unindentDepth > 0)                               // if need to unindent
		unindentLine(line, sw.unindentDepth);               //    do it
}
Пример #9
0
void XmlTreeSerializer::saveTree(const TreeIterator& treeIterator, const OutputStream& outputStream, int indent, const NumberFormat* numberFormat)
{
	TreeIterator iterator = treeIterator;
	if (iterator.hasMetaTag(NON_SERIALIZEABLE) == false)
	{
		if (iterator.isAttribute() == true)
		{
			iterator.moveToParent();
		}

		indentLine(outputStream, indent);
		String nodeName = iterator.getName();
		if (nodeName.findFirst("::") != String::END)
		{
			nodeName.searchAndReplace("::", ":");
		}
		outputStream << '<' << nodeName;

		bool hasData = false;
		const int attributeCount = iterator.getAttributeCount();
		for (int i = 0; i < attributeCount; ++i)
		{
			iterator.moveToAttribute(i);

			if (iterator.hasMetaTag(NON_SERIALIZEABLE) == false && iterator.isDefaultValue() == false)
			{
				if (iterator.getName() == "data")
				{
					hasData = true;
				}
				else
				{
					const String value = iterator.getValue();
					const char8 quote = value.findFirst('\'') == String::END ? '\'' : '"';
					outputStream << ' ' << iterator.getName() << '=' << quote << value << quote;
				}
			}

			iterator.moveToParent();
		}

		const int childCount = iterator.getChildNodeCount();
		if (childCount == 0 && hasData == false)
		{
			outputStream << " />" << newLine;
		}
		else
		{
			outputStream << '>' << newLine;
			if (hasData == true)
			{
				outputStream << iterator.getValueOfAttribute("data");
			}
			for (int i = 0; i < childCount; ++i)
			{
				iterator.moveToChildNode(i);
				saveTree(iterator, outputStream, indent+1, numberFormat);
				iterator.moveToParent();
			}
			indentLine(outputStream, indent);
			outputStream << "</" << nodeName << '>' << newLine;
		}
	}
}