private void ApplyLine(string line, CodeContext codeContext, string[] allLines, string fileName, ref int index)
{
bool succeeded = true;
mParserLog.AppendLine("--- " + line + " ---");
if (string.IsNullOrEmpty(line))
{
// do nothing
// This may be empty
// because the split function
// may have returned a line with
// only a newline character. If so
// that becomes an empty line when trim
// is called on it. This could be a line
// that was trimmed which is now empty.
}
else if (line.Trim().StartsWith("//"))
{
// comment, carry on
}
else if (line.Trim().StartsWith("#region") || line.Trim().StartsWith("#endregion"))
{
// we can skip region blocks
}
else if (ConditionalCodeBlock.GetBlockTypeStartingAt(allLines, index) != BlockType.None)
{
ElementRuntime elementRuntime = codeContext.ContainerInstance as ElementRuntime;
IElement element = null;
if (elementRuntime != null)
{
element = elementRuntime.AssociatedIElement;
}
succeeded = ApplyConditionalBlocks(allLines, element, codeContext, fileName, ref index);
if (!succeeded)
{
throw new Exception();
}
}
else
{
// here we want to identify how many lines make up the statement since
// it could be something like
// this.X = 3
// + 4;
int numberOfLinesInStatement = GetNumberOfLinesInRegularStatement(allLines, index);
string combined = CombineLines(allLines, index, numberOfLinesInStatement);
string trimmedCombined = combined.Trim();
if (trimmedCombined.StartsWith("{") && trimmedCombined.EndsWith("}"))
{
combined = trimmedCombined.Substring(1, trimmedCombined.Length - 2);
}
CSharpParser parser = new CSharpParser();
var statements = parser.ParseStatements(combined);
// I can be incremented by this function by the number of lines.
// If this value is incremented, then the calling function is responsible
// for recognizing that in its loop and acting properly. The calling function
// assumes an increment of 1 so we won't do anything if there's only 1 line in this
// statement.
if (numberOfLinesInStatement != 1)
{
index += numberOfLinesInStatement;
}
foreach (var statement in statements)
{
if (statement is ExpressionStatement)
{
Expression expression = ((ExpressionStatement)statement).Expression;
if (expression is InvocationExpression || expression is UnaryOperatorExpression)
{
mExpressionParser.EvaluateExpression(expression, codeContext);
}
else if (expression is AssignmentExpression)
{
succeeded = ApplyAssignmentLine(expression as AssignmentExpression, codeContext);
if (!succeeded)
{
throw new Exception();
}
}
else if (expression is IdentifierExpression || expression is MemberReferenceExpression)
{
// This is probably an incomplete line, so let's tolerate it and move on...
}
else
{
throw new Exception();
}
}
else if (statement is VariableDeclarationStatement)
{
VariableDeclarationStatement vds = statement as VariableDeclarationStatement;
foreach (var child in vds.Children)
{
if (child is VariableInitializer)
{
VariableInitializer variableInitializer = child as VariableInitializer;
ApplyAssignment(variableInitializer, vds, codeContext);
//variableInitializer.
}
}
//ApplyAssignment(codeContext, vds.Variables.First().GetText(), vds.
//vds.E
//ApplyAssignmentLine(statement.ToString(), codeContext);
}
//if ( is AssignmentExpression || result is MemberReferenceExpression)
//{
// ApplyAssignmentLine(combined, codeContext);
//}
//else if (result is InvocationExpression)
//{
// ApplyMethodCall(combined, codeContext);
//}
else
{
AddErrorText("Unknown line(s): " + combined);
}
}
}
}