void ScanPreProcessorElseIf(ref int i)
            {
                DocumentLine line      = doc.GetLineByOffset(i);
                int          length    = line.Offset + line.Length - i;
                string       parameter = doc.GetTextAt(i + 5, length - 5);
                AstNode      expr      = new CSharpParser().ParseExpression(parameter);
                bool         result;

                if (expr != null && !expr.IsNull)
                {
                    var visitResult = expr.AcceptVisitor(new ConditinalExpressionEvaluator(doc, Defines), null);
                    result = visitResult != null ? (bool)visitResult : false;
                }
                else
                {
                    result = false;
                }

                IfBlockSpan containingIf = null;

                if (result)
                {
                    bool previousResult = false;
                    foreach (Span span in spanStack)
                    {
                        if (span is IfBlockSpan)
                        {
                            containingIf   = (IfBlockSpan)span;
                            previousResult = ((IfBlockSpan)span).IsValid;
                            break;
                        }
                        if (span is ElseIfBlockSpan)
                        {
                            previousResult |= ((ElseIfBlockSpan)span).IsValid;
                        }
                    }

                    result = !previousResult;
                }

                var elseIfBlockSpan = new ElseIfBlockSpan(result);

                if (containingIf != null)
                {
                    elseIfBlockSpan.Disabled = containingIf.Disabled;
                }

                FoundSpanBegin(elseIfBlockSpan, i, 0);

                // put pre processor eol span on stack, so that '#elif' gets the correct highlight
                var preprocessorSpan = CreatePreprocessorSpan();

                FoundSpanBegin(preprocessorSpan, i, 0);
            }
Example #2
0
            void ScanPreProcessorIf(int textOffset, ref int i)
            {
                int     length    = CurText.Length - textOffset;
                string  parameter = CurText.Substring(textOffset + 3, length - 3);
                AstNode expr;

                using (var reader = new StringReader(parameter)) {
                    expr = new CSharpParser().ParseExpression(reader);
                }
                bool result = false;

                if (expr != null && !expr.IsNull)
                {
                    object o = expr.AcceptVisitor(new ConditinalExpressionEvaluator(doc, Defines), null);
                    if (o is bool)
                    {
                        result = (bool)o;
                    }
                }

                foreach (Span span in spanStack)
                {
                    if (span is IfBlockSpan)
                    {
                        result &= ((IfBlockSpan)span).IsValid;
                    }
                    if (span is ElseIfBlockSpan)
                    {
                        result &= ((ElseIfBlockSpan)span).IsValid;
                    }
                }

                var ifBlockSpan = new IfBlockSpan(result);

                foreach (Span span in spanStack)
                {
                    if (span is AbstractBlockSpan)
                    {
                        var parentBlock = (AbstractBlockSpan)span;
                        ifBlockSpan.Disabled = parentBlock.Disabled || !parentBlock.IsValid;
                        break;
                    }
                }

                FoundSpanBegin(ifBlockSpan, i, length);
                i += length - 1;
            }
            void ScanPreProcessorIf(int textOffset, ref int i)
            {
                var end = CurText.Length;
                int idx = 0;

                while ((idx = CurText.IndexOf('/', idx)) >= 0 && idx + 1 < CurText.Length)
                {
                    var next = CurText [idx + 1];
                    if (next == '/')
                    {
                        end = idx - 1;
                        break;
                    }
                    idx++;
                }

                int     length    = end - textOffset;
                string  parameter = CurText.Substring(textOffset + 3, length - 3);
                AstNode expr      = new CSharpParser().ParseExpression(parameter);
                bool    result    = false;

                if (expr != null && !expr.IsNull)
                {
                    object o = expr.AcceptVisitor(new ConditinalExpressionEvaluator(doc, Defines), null);
                    if (o is bool)
                    {
                        result = (bool)o;
                    }
                }

                foreach (Span span in spanStack)
                {
                    if (span is ElseBlockSpan)
                    {
                        result &= ((ElseBlockSpan)span).IsValid;
                        break;
                    }
                    if (span is IfBlockSpan)
                    {
                        result &= ((IfBlockSpan)span).IsValid;
                        break;
                    }
                    if (span is ElseIfBlockSpan)
                    {
                        result &= ((ElseIfBlockSpan)span).IsValid;
                        break;
                    }
                }

                var ifBlockSpan = new IfBlockSpan(result);

                foreach (Span span in spanStack)
                {
                    if (span is AbstractBlockSpan)
                    {
                        var parentBlock = (AbstractBlockSpan)span;
                        ifBlockSpan.Disabled = parentBlock.Disabled || !parentBlock.IsValid;
                        break;
                    }
                }

                FoundSpanBegin(ifBlockSpan, i, length);
                i += length - 1;
            }
Example #4
0
			protected override void ScanSpan (ref int i)
			{
				if (CSharpSyntaxMode.DisableConditionalHighlighting) {
					base.ScanSpan (ref i);
					return;
				}
				int textOffset = i - StartOffset;
				if (CurText.IsAt (textOffset, "#else")) {
					if (!spanStack.Any (s => s is IfBlockSpan || s is ElseIfBlockSpan)) {
						base.ScanSpan (ref i);
						return;
					}
					bool previousResult = false;
					foreach (Span span in spanStack) {
						if (span is IfBlockSpan) {
							previousResult = ((IfBlockSpan)span).IsValid;
						}
						if (span is ElseIfBlockSpan) {
							previousResult |= ((ElseIfBlockSpan)span).IsValid;
						}
					}
//					LineSegment line = doc.GetLineByOffset (i);
//					int length = line.Offset + line.EditableLength - i;
					while (spanStack.Count > 0 && !(CurSpan is IfBlockSpan || CurSpan is ElseIfBlockSpan)) {
						spanStack.Pop ();
					}
					IfBlockSpan ifBlock = CurSpan as IfBlockSpan;
					var elseIfBlock = CurSpan as ElseIfBlockSpan;
					var elseBlockSpan = new ElseBlockSpan (!previousResult);
					if (ifBlock != null) {
						elseBlockSpan.Disabled = ifBlock.Disabled;
					} else if (elseIfBlock != null) {
						elseBlockSpan.Disabled = elseIfBlock.Disabled;
					}
					FoundSpanBegin (elseBlockSpan, i, "#else".Length);
					i += "#else".Length;
					
					// put pre processor eol span on stack, so that '#elif' gets the correct highlight
					Span preprocessorSpan = CreatePreprocessorSpan ();
					FoundSpanBegin (preprocessorSpan, i, 0);
					return;
				}
				if (CurRule.Name == "<root>" && CurText.IsAt (textOffset, "#if")) {
					int length = CurText.Length - textOffset;
					string parameter = CurText.Substring (textOffset + 3, length - 3);
					ICSharpCode.OldNRefactory.Parser.CSharp.Lexer lexer = new ICSharpCode.OldNRefactory.Parser.CSharp.Lexer (new System.IO.StringReader (parameter));
					ICSharpCode.OldNRefactory.Ast.Expression expr = lexer.PPExpression ();
					bool result = false;
					if (expr != null && !expr.IsNull) {
						object o = expr.AcceptVisitor (new ConditinalExpressionEvaluator (doc), null);
						if (o is bool)
							result = (bool)o;
					}
					IfBlockSpan ifBlockSpan = new IfBlockSpan (result);
					
					foreach (Span span in spanStack) {
						if (span is AbstractBlockSpan) {
							var parentBlock = (AbstractBlockSpan)span;
							ifBlockSpan.Disabled = parentBlock.Disabled || !parentBlock.IsValid;
							break;
						}
					}
					
					FoundSpanBegin (ifBlockSpan, i, length);
					i += length - 1;
					return;
				}
				if (CurText.IsAt (textOffset, "#elif") && spanStack.Any (span => span is IfBlockSpan)) {
					LineSegment line = doc.GetLineByOffset (i);
					int length = line.Offset + line.EditableLength - i;
					string parameter = doc.GetTextAt (i + 5, length - 5);
					
					ICSharpCode.OldNRefactory.Parser.CSharp.Lexer lexer = new ICSharpCode.OldNRefactory.Parser.CSharp.Lexer (new System.IO.StringReader (parameter));
					ICSharpCode.OldNRefactory.Ast.Expression expr = lexer.PPExpression ();
				
					bool result = !expr.IsNull ? (bool)expr.AcceptVisitor (new ConditinalExpressionEvaluator (doc), null) : false;
					
					IfBlockSpan containingIf = null;
					if (result) {
						bool previousResult = false;
						foreach (Span span in spanStack) {
							if (span is IfBlockSpan) {
								containingIf = (IfBlockSpan)span;
								previousResult = ((IfBlockSpan)span).IsValid;
								break;
							}
							if (span is ElseIfBlockSpan) {
								previousResult |= ((ElseIfBlockSpan)span).IsValid;
							}
						}
						
						result = !previousResult;
					}
					
					ElseIfBlockSpan elseIfBlockSpan = new ElseIfBlockSpan (result);
					if (containingIf != null)
						elseIfBlockSpan.Disabled = containingIf.Disabled;
					
					FoundSpanBegin (elseIfBlockSpan, i, 0);
					
					// put pre processor eol span on stack, so that '#elif' gets the correct highlight
					var preprocessorSpan = CreatePreprocessorSpan ();
					FoundSpanBegin (preprocessorSpan, i, 0);
					//i += length - 1;
					return;
				}
				if (CurRule.Name == "<root>" &&  CurText[textOffset] == '#') {
					var preprocessorSpan = CreatePreprocessorSpan ();
					FoundSpanBegin (preprocessorSpan, i, 1);
				}
				base.ScanSpan (ref i);
			}
Example #5
0
            protected override void ScanSpan(ref int i)
            {
                if (CSharpSyntaxMode.DisableConditionalHighlighting)
                {
                    base.ScanSpan(ref i);
                    return;
                }
                int textOffset = i - StartOffset;

                if (CurText.IsAt(textOffset, "#else"))
                {
                    if (!spanStack.Any(s => s is IfBlockSpan  ||  s is ElseIfBlockSpan))
                    {
                        base.ScanSpan(ref i);
                        return;
                    }
                    bool previousResult = false;
                    foreach (Span span in spanStack)
                    {
                        if (span is IfBlockSpan)
                        {
                            previousResult = ((IfBlockSpan)span).IsValid;
                        }
                        if (span is ElseIfBlockSpan)
                        {
                            previousResult |= ((ElseIfBlockSpan)span).IsValid;
                        }
                    }
//					LineSegment line = doc.GetLineByOffset (i);
//					int length = line.Offset + line.EditableLength - i;
                    while (spanStack.Count > 0 && !(CurSpan is IfBlockSpan  ||  CurSpan is ElseIfBlockSpan))
                    {
                        spanStack.Pop();
                    }
                    IfBlockSpan     ifBlock       = CurSpan as IfBlockSpan;
                    ElseIfBlockSpan elseIfBlock   = CurSpan as ElseIfBlockSpan;
                    ElseBlockSpan   elseBlockSpan = new ElseBlockSpan(!previousResult);
                    if (ifBlock != null)
                    {
                        elseBlockSpan.Disabled = ifBlock.Disabled;
                    }
                     else if (elseIfBlock != null)
                    {
                        elseBlockSpan.Disabled = elseIfBlock.Disabled;
                    }
                    FoundSpanBegin(elseBlockSpan, i, "#else".Length);
                    i += "#else".Length;

                    // put pre processor eol span on stack, so that '#elif' gets the correct highlight
                    Span preprocessorSpan = CreatePreprocessorSpan();
                    FoundSpanBegin(preprocessorSpan, i, 0);
                    return;
                }
                if (CurRule.Name == "<root>" && CurText.IsAt(textOffset, "#if"))
                {
                    int    length    = CurText.Length - textOffset;
                    string parameter = CurText.Substring(textOffset + 3, length - 3);
                    ICSharpCode.NRefactory.Parser.CSharp.Lexer lexer = new ICSharpCode.NRefactory.Parser.CSharp.Lexer(new System.IO.StringReader(parameter));
                    ICSharpCode.NRefactory.Ast.Expression      expr  = lexer.PPExpression();
                    bool result = false;
                    if (expr != null && !expr.IsNull)
                    {
                        object o = expr.AcceptVisitor(new ConditinalExpressionEvaluator(doc), null);
                        if (o is bool)
                        {
                            result = (bool)o;
                        }
                    }
                    IfBlockSpan ifBlockSpan = new IfBlockSpan(result);

                    foreach (Span span in spanStack)
                    {
                        if (span is AbstractBlockSpan)
                        {
                            AbstractBlockSpan parentBlock = (AbstractBlockSpan)span;
                            ifBlockSpan.Disabled = parentBlock.Disabled || !parentBlock.IsValid;
                            break;
                        }
                    }

                    FoundSpanBegin(ifBlockSpan, i, length);
                    i += length - 1;
                    return;
                }
                if (CurText.IsAt(textOffset, "#elif") && spanStack.Any(span => span is IfBlockSpan))
                {
                    LineSegment line      = doc.GetLineByOffset(i);
                    int         length    = line.Offset + line.EditableLength - i;
                    string      parameter = doc.GetTextAt(i + 5, length - 5);

                    ICSharpCode.NRefactory.Parser.CSharp.Lexer lexer = new ICSharpCode.NRefactory.Parser.CSharp.Lexer(new System.IO.StringReader(parameter));
                    ICSharpCode.NRefactory.Ast.Expression      expr  = lexer.PPExpression();

                    bool result = !expr.IsNull ? (bool)expr.AcceptVisitor(new ConditinalExpressionEvaluator(doc), null) : false;

                    IfBlockSpan containingIf = null;
                    if (result)
                    {
                        bool previousResult = false;
                        foreach (Span span in spanStack)
                        {
                            if (span is IfBlockSpan)
                            {
                                containingIf   = (IfBlockSpan)span;
                                previousResult = ((IfBlockSpan)span).IsValid;
                                break;
                            }
                            if (span is ElseIfBlockSpan)
                            {
                                previousResult |= ((ElseIfBlockSpan)span).IsValid;
                            }
                        }

                        result = !previousResult;
                    }

                    ElseIfBlockSpan elseIfBlockSpan = new ElseIfBlockSpan(result);
                    if (containingIf != null)
                    {
                        elseIfBlockSpan.Disabled = containingIf.Disabled;
                    }

                    FoundSpanBegin(elseIfBlockSpan, i, 0);

                    // put pre processor eol span on stack, so that '#elif' gets the correct highlight
                    Span preprocessorSpan = CreatePreprocessorSpan();
                    FoundSpanBegin(preprocessorSpan, i, 0);
                    //i += length - 1;
                    return;
                }
                if (CurRule.Name == "<root>" && CurText[textOffset] == '#')
                {
                    Span preprocessorSpan = CreatePreprocessorSpan();
                    FoundSpanBegin(preprocessorSpan, i, 1);
                }
                base.ScanSpan(ref i);
            }
Example #6
0
            protected override void ScanSpan(ref int i)
            {
                if (FSharpSyntaxMode.DisableConditionalHighlighting) {
                    base.ScanSpan (ref i);
                    return;
                }
                if (i + 5 < doc.Length && doc.GetTextAt (i, 5) == "#else") {
                    LineSegment line = doc.GetLineByOffset (i);

                    bool previousResult = false;

                    foreach (Span span in spanStack.ToArray ().Reverse ()) {
                        if (span is IfBlockSpan) {
                            previousResult = ((IfBlockSpan)span).IsValid;
                        }
                        if (span is ElseIfBlockSpan) {
                            previousResult |= ((ElseIfBlockSpan)span).IsValid;
                        }
                    }

                    int length = line.Offset + line.EditableLength - i;
                    while (spanStack.Count > 0 && !(CurSpan is IfBlockSpan)) {
                        spanStack.Pop ();
                    }
                //					IfBlockSpan ifBlock = (IfBlockSpan)CurSpan;
                    ElseBlockSpan elseBlockSpan = new ElseBlockSpan (!previousResult);
                    OnFoundSpanBegin (elseBlockSpan, i, 0);

                    spanStack.Push (elseBlockSpan);
                    ruleStack.Push (GetRule (elseBlockSpan));

                    // put pre processor eol span on stack, so that '#else' gets the correct highlight
                    OnFoundSpanBegin (preprocessorSpan, i, 1);
                    spanStack.Push (preprocessorSpan);
                    ruleStack.Push (preprocessorRule);
                    i += length - 1;
                    return;
                }

                if (CurRule.Name == "text.preprocessor" && i >= 3 && doc.GetTextAt (i - 3, 3) == "#if") {
                    LineSegment line = doc.GetLineByOffset (i);
                    int length = line.Offset + line.EditableLength - i;
                    string parameter = doc.GetTextAt (i, length);
                    IFSharpCode.NRefactory.Parser.FSharp.Lexer lexer = new IFSharpCode.NRefactory.Parser.FSharp.Lexer (new System.IO.StringReader (parameter));
                    IFSharpCode.NRefactory.Ast.Expression expr = lexer.PPExpression ();
                    bool result = false;
                    if (expr != null && !expr.IsNull) {
                        object o = expr.AcceptVisitor (new ConditinalExpressionEvaluator (doc), null);
                        if (o is bool)
                            result = (bool)o;
                    }
                    IfBlockSpan ifBlockSpan = new IfBlockSpan (result);
                    OnFoundSpanBegin (ifBlockSpan, i, length);
                    i += length - 1;
                    spanStack.Push (ifBlockSpan);
                    ruleStack.Push (GetRule (ifBlockSpan));
                    return;
                }
                if (i + 5 < doc.Length && doc.GetTextAt (i, 5) == "#elif" && spanStack.Any (span => span is IfBlockSpan)) {
                    LineSegment line = doc.GetLineByOffset (i);
                    int length = line.Offset + line.EditableLength - i;
                    string parameter = doc.GetTextAt (i + 5, length - 5);

                    IFSharpCode.NRefactory.Parser.FSharp.Lexer lexer = new IFSharpCode.NRefactory.Parser.FSharp.Lexer (new System.IO.StringReader (parameter));
                    IFSharpCode.NRefactory.Ast.Expression expr = lexer.PPExpression ();

                    bool result = !expr.IsNull ? (bool)expr.AcceptVisitor (new ConditinalExpressionEvaluator (doc), null) : false;

                    if (result) {
                        bool previousResult = false;
                        foreach (Span span in spanStack.ToArray ().Reverse ()) {
                            if (span is IfBlockSpan) {
                                previousResult = ((IfBlockSpan)span).IsValid;
                            }
                            if (span is ElseIfBlockSpan) {
                                previousResult |= ((ElseIfBlockSpan)span).IsValid;
                            }
                        }

                        result = !previousResult;
                    }

                    ElseIfBlockSpan elseIfBlockSpan = new ElseIfBlockSpan (result);
                    OnFoundSpanBegin (elseIfBlockSpan, i, 0);

                    spanStack.Push (elseIfBlockSpan);
                    ruleStack.Push (GetRule (elseIfBlockSpan));

                    // put pre processor eol span on stack, so that '#elif' gets the correct highlight
                    OnFoundSpanBegin (preprocessorSpan, i, 1);
                    spanStack.Push (preprocessorSpan);
                    ruleStack.Push (preprocessorRule);
                    //i += length - 1;
                    return;
                }
                base.ScanSpan (ref i);
            }