Пример #1
0
            protected override bool ScanSpanEnd(Span cur, ref int i)
            {
                if (cur is IfBlockSpan || cur is ElseIfBlockSpan || cur is ElseBlockSpan)
                {
                    int  textOffset = i - StartOffset;
                    bool end        = CurText.IsAt(textOffset, "#endif");
                    if (end)
                    {
                        FoundSpanEnd(cur, i, 6);                          // put empty end tag in

                        // if we're in a complex span stack pop it up to the if block
                        if (spanStack.Count > 0)
                        {
                            var prev = spanStack.Peek();

                            if ((cur is ElseIfBlockSpan || cur is ElseBlockSpan) && (prev is ElseIfBlockSpan || prev is IfBlockSpan))
                            {
                                PopCurrentIfBlock();
                            }
                        }
                    }
                    return(end);
                }
                return(base.ScanSpanEnd(cur, ref i));
            }
Пример #2
0
            protected override bool ScanSpanEnd(Span cur, ref int i)
            {
                int textOffset = i - StartOffset;

                if (cur.End != null && (cur.Rule == "Table" || cur.Rule == "TableHeader"))
                {
                    if (cur.Rule == "Table")
                    {
                        if (CurText [textOffset] != cur.End.Pattern [0] || CurText.Skip(textOffset + 1).Any(e => e == cur.End.Pattern [0]))
                        {
                            return(false);
                        }
                    }

                    if (cur.Rule == "TableHeader")
                    {
                        if (textOffset != 0 || (!string.IsNullOrWhiteSpace(CurText) && CurText.First(ch => !char.IsWhiteSpace(ch)) == cur.End.Pattern [0]))
                        {
                            return(false);
                        }
                    }

                    FoundSpanEnd(cur, i, 1);
                    return(true);
                }

                if (cur.End != null)
                {
                    RegexMatch match = cur.End.TryMatch(CurText, textOffset);
                    if (match.Success)
                    {
                        FoundSpanEnd(cur, i, match.Length);
                        i += Math.Max(0, match.Length - 1);
                        return(true);
                    }
                }

                if (cur.Exit != null)
                {
                    RegexMatch match = cur.Exit.TryMatch(CurText, textOffset);
                    if (match.Success)
                    {
                        FoundSpanExit(cur, i, match.Length);
                        i += -1;
                        return(true);
                    }
                }

                return(false);
            }
Пример #3
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;
            }
Пример #4
0
            protected override void ScanSpan(ref int i)
            {
                if (CSharpSyntaxMode.DisableConditionalHighlighting)
                {
                    base.ScanSpan(ref i);
                    return;
                }
                int textOffset = i - StartOffset;

                if (textOffset < CurText.Length && CurText [textOffset] == '#' && IsFirstNonWsChar(textOffset))
                {
                    if (CurText.IsAt(textOffset, "#define"))
                    {
                        int    length    = CurText.Length - textOffset;
                        string parameter = CurText.Substring(textOffset + "#define".Length, length - "#define".Length).Trim();

                        var defineSpan = new DefineSpan(parameter);
                        FoundSpanBegin(defineSpan, i, 0);
                    }

                    if (CurText.IsAt(textOffset, "#else"))
                    {
                        ScanPreProcessorElse(ref i);
                        return;
                    }

                    if (CurText.IsAt(textOffset, "#if"))
                    {
                        ScanPreProcessorIf(textOffset, ref i);
                        return;
                    }

                    if (CurText.IsAt(textOffset, "#elif") && spanStack != null && spanStack.Any(span => span is IfBlockSpan))
                    {
                        ScanPreProcessorElseIf(ref i);
                        return;
                    }

                    var preprocessorSpan = CreatePreprocessorSpan();
                    FoundSpanBegin(preprocessorSpan, i, 1);
                    return;
                }

                base.ScanSpan(ref i);
            }
Пример #5
0
            protected override bool ScanSpan(ref int i)
            {
                if (CSharpSyntaxMode.DisableConditionalHighlighting)
                {
                    return(base.ScanSpan(ref i));
                }
                int textOffset = i - StartOffset;

                if (textOffset < CurText.Length && CurRule.Name != "Comment" && CurRule.Name != "String" && CurRule.Name != "VerbatimString" && CurText [textOffset] == '#' && IsFirstNonWsChar(textOffset))
                {
                    if (CurText.IsAt(textOffset, "#define") && (spanStack == null || !spanStack.Any(span => span is IfBlockSpan && !((IfBlockSpan)span).IsValid)))
                    {
                        int    length     = CurText.Length - textOffset;
                        string parameter  = CurText.Substring(textOffset + "#define".Length, length - "#define".Length).Trim();
                        var    defineSpan = new DefineSpan(parameter);
                        FoundSpanBegin(defineSpan, i, 0);
                    }

                    if (CurText.IsAt(textOffset, "#else"))
                    {
                        ScanPreProcessorElse(ref i);
                        return(true);
                    }

                    if (CurText.IsAt(textOffset, "#if"))
                    {
                        ScanPreProcessorIf(textOffset, ref i);
                        return(true);
                    }

                    if (CurText.IsAt(textOffset, "#elif") && spanStack != null && spanStack.Any(span => span is IfBlockSpan))
                    {
                        ScanPreProcessorElseIf(ref i);
                        return(true);
                    }
                }

                return(base.ScanSpan(ref i));
            }
Пример #6
0
 protected override bool ScanSpanEnd(Mono.TextEditor.Highlighting.Span cur, ref int i)
 {
     if (cur is IfBlockSpan || cur is ElseIfBlockSpan || cur is ElseBlockSpan)
     {
         int  textOffset = i - StartOffset;
         bool end        = CurText.IsAt(textOffset, "#endif");
         if (end)
         {
             FoundSpanEnd(cur, i, 6);                          // put empty end tag in
             while (spanStack.Count > 0 && (spanStack.Peek() is IfBlockSpan || spanStack.Peek() is ElseIfBlockSpan || spanStack.Peek() is ElseBlockSpan))
             {
                 spanStack.Pop();
                 if (ruleStack.Count > 1)                             // rulStack[1] is always syntax mode
                 {
                     ruleStack.Pop();
                 }
             }
         }
         return(end);
     }
     return(base.ScanSpanEnd(cur, ref i));
 }
Пример #7
0
            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;
            }
Пример #8
0
            protected virtual bool ScanSpan(ref int i)
            {
                int textOffset = i - StartOffset;

                Span[] spans = CurRule.Spans;
                for (int j = 0; j < spans.Length; j++)
                {
                    Span span = spans [j];

                    if ((span.BeginFlags & SpanBeginFlags.StartsLine) == SpanBeginFlags.StartsLine)
                    {
                        if (textOffset != 0)
                        {
                            char ch = CurText [textOffset - 1];
                            if (ch != '\n' && ch != '\r')
                            {
                                continue;
                            }
                        }
                    }

                    RegexMatch match = span.Begin.TryMatch(CurText, textOffset);
                    if (!match.Success)
                    {
                        continue;
                    }
                    // scan for span exit which cancels the span.
                    if ((span.ExitFlags & SpanExitFlags.CancelSpan) == SpanExitFlags.CancelSpan && span.Exit != null)
                    {
                        bool foundEnd = false;
                        for (int k = i + match.Length; k < CurText.Length; k++)
                        {
                            if (span.Exit.TryMatch(CurText, k - StartOffset).Success)
                            {
                                return(false);
                            }
                            if (span.End.TryMatch(CurText, k - StartOffset).Success)
                            {
                                foundEnd = true;
                                break;
                            }
                        }
                        if (!foundEnd)
                        {
                            return(false);
                        }
                    }

                    bool mismatch = false;
                    if ((span.BeginFlags & SpanBeginFlags.FirstNonWs) == SpanBeginFlags.FirstNonWs)
                    {
                        mismatch = CurText.Take(i).Any(ch => !char.IsWhiteSpace(ch));
                    }
                    if ((span.BeginFlags & SpanBeginFlags.NewWord) == SpanBeginFlags.NewWord)
                    {
                        if (textOffset - 1 > 0 && textOffset - 1 < CurText.Length)
                        {
                            mismatch = !char.IsWhiteSpace(CurText [textOffset - 1]);
                        }
                    }
                    if (mismatch)
                    {
                        continue;
                    }

                    FoundSpanBegin(span, i, match.Length);
                    i += System.Math.Max(0, match.Length - 1);
                    return(true);
                }
                return(false);
            }
Пример #9
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);
            }
Пример #10
0
            protected override bool ScanSpan(ref int i)
            {
                int textOffset = i - StartOffset;

                Span[] spans = CurRule.Spans;
                for (int j = 0; j < spans.Length; j++)
                {
                    Span span = spans [j];

                    if ((span.BeginFlags & SpanBeginFlags.StartsLine) == SpanBeginFlags.StartsLine)
                    {
                        if (textOffset != 0)
                        {
                            char ch = CurText [textOffset - 1];
                            if (ch != '\n' && ch != '\r')
                            {
                                continue;
                            }
                        }
                    }

                    RegexMatch match = span.Begin.TryMatch(CurText, textOffset);
                    if (!match.Success || ((span.Rule == "Table" || span.Rule == "TableHeader") && CurText[textOffset] != span.Begin.Pattern[0]))
                    {
                        continue;
                    }

                    // scan for span exit which cancels the span.
                    if ((span.ExitFlags & SpanExitFlags.CancelSpan) == SpanExitFlags.CancelSpan && span.Exit != null)
                    {
                        bool foundEnd = false;
                        for (int k = i + match.Length; k < CurText.Length; k++)
                        {
                            if (span.Exit.TryMatch(CurText, k - StartOffset).Success)
                            {
                                return(false);
                            }
                            if (span.End.TryMatch(CurText, k - StartOffset).Success)
                            {
                                foundEnd = true;
                                break;
                            }
                        }
                        if (!foundEnd)
                        {
                            return(false);
                        }
                    }

                    bool mismatch = false;

                    if (span.Rule == "Table" || span.Rule == "TableHeader")
                    {
                        bool found = false;

                        for (int k = textOffset + 1; k < CurText.Length; k++)
                        {
                            if (CurText [k] == span.End.Pattern [0] && (k == 0 || CurText [k - 1] != '\\'))
                            {
                                found = true;
                                break;
                            }
                        }

                        mismatch = !found || CurText.Take(textOffset).Any(ch => !char.IsWhiteSpace(ch));
                    }

                    if ((span.BeginFlags & SpanBeginFlags.FirstNonWs) == SpanBeginFlags.FirstNonWs)
                    {
                        mismatch = CurText.Take(textOffset).Any(ch => !char.IsWhiteSpace(ch));
                    }
                    if ((span.BeginFlags & SpanBeginFlags.NewWord) == SpanBeginFlags.NewWord)
                    {
                        if (textOffset - 1 > 0 && textOffset - 1 < CurText.Length)
                        {
                            mismatch = !char.IsWhiteSpace(CurText [textOffset - 1]);
                        }
                        else if (textOffset + match.Length < CurText.Length)
                        {
                            mismatch = !char.IsWhiteSpace(CurText [textOffset + match.Length]);
                        }
                    }
                    if (mismatch)
                    {
                        continue;
                    }
                    if (span.Rule == "Table" || span.Rule == "TableHeader")
                    {
                        FoundSpanBegin(span, i, 1);
                    }
                    else
                    {
                        FoundSpanBegin(span, i, match.Length);
                        i += System.Math.Max(0, match.Length - 1);
                    }
                    return(true);
                }
                return(false);
            }