public TokenDefinition(string regex, TokenEnum token, LintType lint)
 {
     Matcher = new RegexMatcher(regex, RegexOptions.CultureInvariant);
     Token   = token;
     Lint    = lint;
 }
Beispiel #2
0
        private int LookAhead(out TokenEnum token, out string content, out int position, out LintType lint)
        {
            if (lineRemaining == null)
            {
                token    = 0;
                content  = "";
                position = Position;
                lint     = LintType.NONE;
                return(0);
            }
            foreach (var def in m_tokenDefinitions)
            {
                var matched = def.Matcher.Match(lineRemaining);
                if (matched > 0)
                {
                    position = Position + matched;
                    token    = def.Token;
                    lint     = def.Lint;
                    content  = lineRemaining.Substring(0, matched);

                    // special case for linting for type
                    if (lint == LintType.VARIABLE_OR_TYPE && token == TokenEnum.VARIABLE)
                    {
                        if (Parser.TypeTokens.Contains(content))
                        {
                            lint = LintType.TYPE;
                        }
                        else
                        {
                            lint = LintType.VARIABLE;
                        }
                    }

                    // whitespace elimination
                    if (content.Trim().Length == 0)
                    {
                        DoNext(matched, token, content, position);
                        return(LookAhead(out token, out content, out position, out lint));
                    }

                    // comment elimination
                    if (token == TokenEnum.COMMENT)
                    {
                        Linter.Add(new LintElement(LineNumber, Position, LintType.COMMENT));
                        nextLine();
                        return(LookAhead(out token, out content, out position, out lint));
                    }

                    return(matched);
                }
            }
            throw new Exception(Resource.Strings.Error_Lexer_InvalidToken.F(LineNumber, Position, lineRemaining));
        }
Beispiel #3
0
        public string MakeRTF(string[] lines, SortedList <Pair <int, int>, LintType> lints)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033{\\fonttbl{\\f0\\fnil\\fcharset0 Consolas;}}\r\n");

            Color[] colors = LintColors.GetValues();
            sb.Append("{\\colortbl ;");
            foreach (Color c in colors)
            {
                sb.Append("\\red{0}\\green{1}\\blue{2};".F(c.R, c.G, c.B));
            }
            sb.Append("}\r\n");

            sb.Append("\\viewkind4\\uc1\\pard\\cf0\\f0\\fs17");

            List <LintType> lintdef = new List <LintType>(LintColors.GetKeys());

            LintType    prev    = LintType.NONE;
            LintElement current = new LintElement(0, 0, LintType.NONE);

            foreach (Pair <int, int> pos in lints.Keys)
            {
                LintElement lint = new LintElement(pos.t, pos.u, lints[pos]);
                if (current.Line > 0)
                {
                    if (current.Line > lint.Line || (current.Line == lint.Line && current.Position >= lint.Position))
                    {
                        continue;
                    }

                    while (current.Line < lint.Line)
                    {
                        int index = lintdef.IndexOf(current.Lint);
                        index++;
                        sb.Append("\\cf" + index + " ");
                        sb.Append(lines[current.Line - 1].Substring(current.Position)
                                  .Replace("{", "\\{")
                                  .Replace("}", "\\}")
                                  .Replace("\t", "\\tab")
                                  .Replace("\r\n", "\\par\r\n")
                                  .Replace("\r", "\\par\r\n")
                                  .Replace("\n", "\\par\r\n"));
                        sb.Append("\\par\r\n");

                        current = new LintElement(current.Line + 1, 0, LintType.NONE);
                    }

                    if (current.Line == lint.Line && current.Position < lint.Position)
                    {
                        if (current.Lint != prev)
                        {
                            int index = lintdef.IndexOf(current.Lint);
                            index++;
                            sb.Append("\\cf" + index + " ");
                            sb.Append(lines[current.Line - 1].Substring(current.Position, lint.Position - current.Position)
                                      .Replace("{", "\\{")
                                      .Replace("}", "\\}")
                                      .Replace("\t", "\\tab ")
                                      .Replace("\r\n", "\\par\r\n")
                                      .Replace("\r", "\\par\r\n")
                                      .Replace("\n", "\\par\r\n")
                                      );
                        }
                        else
                        {
                            sb.Append(lines[current.Line - 1].Substring(current.Position, lint.Position - current.Position)
                                      .Replace("{", "\\{")
                                      .Replace("}", "\\}")
                                      .Replace("\t", "\\tab ")
                                      .Replace("\r\n", "\\par\r\n")
                                      .Replace("\r", "\\par\r\n")
                                      .Replace("\n", "\\par\r\n")
                                      );
                        }
                    }
                }
                prev    = current.Lint;
                current = lint;
            }
            for (int i = current.Line; i < lines.Length + 1; i++)
            {
                sb.Append("\\par\r\n");
            }

            sb.Append("}\r\n");

            return(sb.ToString());
        }
Beispiel #4
0
 /// <summary>Specifies a linter element at a specific line and position</summary>
 public LintElement(int line, int position, LintType lint)
 {
   Line = line;
   Position = position;
   Lint = lint;
 }