Ejemplo n.º 1
0
 /// <summary>
 /// Parse for uses of deprecated commands.
 /// </summary>
 /// <param name="lines">A collection of lines to parse.</param>
 /// <returns>A list of error information.</returns>
 public static List<CMakeErrorInfo> ParseForDeprecatedCommands(
     IEnumerable<string> lines)
 {
     List<CMakeErrorInfo> results = new List<CMakeErrorInfo>();
     CMakeScanner scanner = new CMakeScanner();
     TokenInfo tokenInfo = new TokenInfo();
     int state = 0;
     int lineNum = 0;
     foreach (string line in lines)
     {
         scanner.SetSource(line, 0);
         while (scanner.ScanTokenAndProvideInfoAboutIt(tokenInfo, ref state))
         {
             if (!CMakeScanner.InsideParens(state) &&
                 tokenInfo.Token == (int)CMakeToken.Keyword)
             {
                 string tokenText = line.ExtractToken(tokenInfo);
                 CMakeCommandId id = CMakeKeywords.GetCommandId(tokenText);
                 if (CMakeKeywords.IsDeprecated(id))
                 {
                     results.Add(new CMakeErrorInfo()
                     {
                         ErrorCode = CMakeError.DeprecatedCommand,
                         Span = tokenInfo.ToTextSpan(lineNum),
                         Warning = true
                     });
                 }
             }
         }
         lineNum++;
     }
     return results;
 }
Ejemplo n.º 2
0
 /// <summary>
 /// Parse for bad commands.
 /// </summary>
 /// <param name="lines">A collection of lines to parse.</param>
 /// <returns>A list of error information.</returns>
 public static List<CMakeErrorInfo> ParseForBadCommands(IEnumerable<string> lines)
 {
     List<CMakeErrorInfo> results = new List<CMakeErrorInfo>();
     CMakeScanner scanner = new CMakeScanner();
     TokenInfo tokenInfo = new TokenInfo();
     int scannerState = 0;
     int lineNum = 0;
     BadCommandParseState state = BadCommandParseState.BeforeCommand;
     foreach (string line in lines)
     {
         bool lineHasError = false;
         if (state != BadCommandParseState.InsideParens)
         {
             state = BadCommandParseState.BeforeCommand;
         }
         scanner.SetSource(line, 0);
         while (scanner.ScanTokenAndProvideInfoAboutIt(tokenInfo,
             ref scannerState))
         {
             switch (state)
             {
             case BadCommandParseState.BeforeCommand:
                 if (tokenInfo.Token == (int)CMakeToken.Identifier ||
                     tokenInfo.Token == (int)CMakeToken.Keyword)
                 {
                     state = BadCommandParseState.BeforeParen;
                 }
                 else if (tokenInfo.Token != (int)CMakeToken.WhiteSpace &&
                     tokenInfo.Token != (int)CMakeToken.Comment &&
                     !lineHasError)
                 {
                     results.Add(new CMakeErrorInfo()
                     {
                         ErrorCode = CMakeError.ExpectedCommand,
                         Span = tokenInfo.ToTextSpan(lineNum)
                     });
                     lineHasError = true;
                 }
                 break;
             case BadCommandParseState.BeforeParen:
                 if (tokenInfo.Token == (int)CMakeToken.OpenParen)
                 {
                     state = BadCommandParseState.InsideParens;
                 }
                 else if (tokenInfo.Token != (int)CMakeToken.WhiteSpace)
                 {
                     results.Add(new CMakeErrorInfo()
                     {
                         ErrorCode = CMakeError.ExpectedOpenParen,
                         Span = tokenInfo.ToTextSpan(lineNum)
                     });
                     lineHasError = true;
                 }
                 break;
             case BadCommandParseState.InsideParens:
                 if (!CMakeScanner.InsideParens(scannerState))
                 {
                     state = BadCommandParseState.AfterParen;
                 }
                 break;
             case BadCommandParseState.AfterParen:
                 if (tokenInfo.Token != (int)CMakeToken.WhiteSpace &&
                     tokenInfo.Token != (int)CMakeToken.Comment &&
                     !lineHasError)
                 {
                     results.Add(new CMakeErrorInfo()
                     {
                         ErrorCode = CMakeError.ExpectedEOL,
                         Span = tokenInfo.ToTextSpan(lineNum)
                     });
                     lineHasError = true;
                 }
                 break;
             }
         }
         if (state == BadCommandParseState.BeforeParen)
         {
             // If we reached the end of a line while looking for the opening
             // parenthesis, show an error, since it must appear on the same line
             // as the command name to not have a syntax error.
             if (!lineHasError)
             {
                 results.Add(new CMakeErrorInfo()
                 {
                     ErrorCode = CMakeError.ExpectedOpenParen,
                     Span = new TextSpan()
                     {
                         iStartLine = lineNum,
                         iStartIndex = tokenInfo.EndIndex + 1,
                         iEndLine = lineNum,
                         iEndIndex = tokenInfo.EndIndex + 2
                     }
                 });
             }
             state = BadCommandParseState.BeforeCommand;
         }
         lineNum++;
     }
     return results;
 }
Ejemplo n.º 3
0
 /// <summary>
 /// Parse for unmatched parentheses.
 /// </summary>
 /// <param name="lines">A collection of lines to parse.</param>
 /// <returns>
 /// A list of error information.
 /// </returns>
 public static List<CMakeErrorInfo> ParseForUnmatchedParens(
     IEnumerable<string> lines)
 {
     List<CMakeErrorInfo> results = new List<CMakeErrorInfo>();
     Stack<TextSpan> stack = new Stack<TextSpan>();
     CMakeScanner scanner = new CMakeScanner();
     TokenInfo tokenInfo = new TokenInfo();
     int state = 0;
     int lineNum = 0;
     foreach (string line in lines)
     {
         scanner.SetSource(line, 0);
         while (scanner.ScanTokenAndProvideInfoAboutIt(tokenInfo, ref state))
         {
             switch ((CMakeToken)tokenInfo.Token)
             {
             case CMakeToken.OpenParen:
                 stack.Push(tokenInfo.ToTextSpan(lineNum));
                 break;
             case CMakeToken.CloseParen:
                 if (stack.Count > 0)
                 {
                     stack.Pop();
                 }
                 else
                 {
                     results.Add(new CMakeErrorInfo()
                     {
                         ErrorCode = CMakeError.UnmatchedParen,
                         Span = tokenInfo.ToTextSpan(lineNum)
                     });
                 }
                 break;
             }
         }
         lineNum++;
     }
     while (stack.Count > 0)
     {
         results.Add(new CMakeErrorInfo()
         {
             ErrorCode = CMakeError.UnmatchedParen,
             Span = stack.Pop()
         });
     }
     return results;
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Parse for syntax errors in variable references.
 /// </summary>
 /// <param name="lines">A collection of lines to parse.</param>
 /// <returns>
 /// A list of error information.
 /// </returns>
 public static List<CMakeErrorInfo> ParseForBadVariableRefs(IEnumerable<string> lines)
 {
     List<CMakeErrorInfo> results = new List<CMakeErrorInfo>();
     CMakeScanner scanner = new CMakeScanner();
     TokenInfo tokenInfo = new TokenInfo();
     BadVariableRefParseState state = BadVariableRefParseState.NeedStart;
     Stack<TextSpan> stack = new Stack<TextSpan>();
     int scannerState = 0;
     int lineNum = 0;
     foreach (string line in lines)
     {
         scanner.SetSource(line, 0);
         while (scanner.ScanTokenAndProvideInfoAboutIt(tokenInfo,
             ref scannerState))
         {
             switch ((CMakeToken)tokenInfo.Token)
             {
             case CMakeToken.VariableStart:
             case CMakeToken.VariableStartEnv:
             case CMakeToken.VariableStartCache:
             case CMakeToken.VariableStartSetEnv:
                 stack.Push(tokenInfo.ToTextSpan(lineNum));
                 state = BadVariableRefParseState.NeedName;
                 break;
             case CMakeToken.Variable:
             case CMakeToken.Identifier:
                 if (state == BadVariableRefParseState.NeedName)
                 {
                     state = BadVariableRefParseState.NeedEnd;
                 }
                 break;
             case CMakeToken.VariableEnd:
                 if (state == BadVariableRefParseState.NeedEnd)
                 {
                     stack.Pop();
                     state = stack.Count > 0 ? BadVariableRefParseState.NeedEnd :
                         BadVariableRefParseState.NeedStart;
                 }
                 else if (state == BadVariableRefParseState.NeedStart)
                 {
                     results.Add(new CMakeErrorInfo()
                     {
                         ErrorCode = CMakeError.InvalidVariableRef,
                         Span = tokenInfo.ToTextSpan(lineNum)
                     });
                 }
                 else if (state == BadVariableRefParseState.NeedName)
                 {
                     TextSpan span = stack.Pop();
                     span.iEndIndex = tokenInfo.EndIndex + 1;
                     results.Add(new CMakeErrorInfo()
                     {
                         ErrorCode = CMakeError.InvalidVariableRef,
                         Span = span
                     });
                 }
                 break;
             default:
                 HandlePossibleBadVariableRef(ref state, tokenInfo.StartIndex,
                     stack, results);
                 break;
             }
         }
         HandlePossibleBadVariableRef(ref state, line.Length, stack, results);
         lineNum++;
     }
     return results;
 }
Ejemplo n.º 5
0
 /// <summary>
 /// Parse for pairs of matching braces denoting generator expressions.
 /// </summary>
 /// <param name="lines">A collection of lines to parse.</param>
 /// <param name="lineNum">
 /// The number of the line on which to find the generator expression.
 /// </param>
 /// <returns>A list of pairs of matching braces.</returns>
 public static List<SpanPair> ParseForGeneratorBraces(IEnumerable<string> lines,
     int lineNum)
 {
     List<SpanPair> pairs = new List<SpanPair>();
     Stack<TextSpan> stack = new Stack<TextSpan>();
     CMakeScanner scanner = new CMakeScanner();
     TokenInfo tokenInfo = new TokenInfo();
     int state = 0;
     int i = 0;
     foreach (string line in lines)
     {
         scanner.SetSource(line, 0);
         if (i < lineNum)
         {
             while (scanner.ScanTokenAndProvideInfoAboutIt(tokenInfo, ref state));
         }
         else
         {
             while (scanner.ScanTokenAndProvideInfoAboutIt(tokenInfo, ref state))
             {
                 switch ((CMakeToken)tokenInfo.Token)
                 {
                 case CMakeToken.GeneratorStart:
                     stack.Push(tokenInfo.ToTextSpan(i));
                     break;
                 case CMakeToken.GeneratorEnd:
                     if (stack.Count > 0)
                     {
                         pairs.Add(new SpanPair()
                         {
                             First = stack.Pop(),
                             Second = tokenInfo.ToTextSpan(i)
                         });
                     }
                     break;
                 case CMakeToken.Colon:
                 case CMakeToken.FileName:
                 case CMakeToken.Identifier:
                 case CMakeToken.Variable:
                 case CMakeToken.VariableEnd:
                 case CMakeToken.VariableStart:
                 case CMakeToken.VariableStartCache:
                 case CMakeToken.VariableStartEnv:
                     break;
                 default:
                     stack.Clear();
                     break;
                 }
             }
         }
         i++;
     }
     return pairs;
 }
Ejemplo n.º 6
0
 /// <summary>
 /// Parse for pairs of matching parentheses.
 /// </summary>
 /// <param name="lines">A collection of lines to parse.</param>
 /// <returns>A list of pairs of matching parentheses.</returns>
 public static List<SpanPair> ParseForParens(IEnumerable<string> lines)
 {
     List<SpanPair> pairs = new List<SpanPair>();
     Stack<TextSpan> stack = new Stack<TextSpan>();
     CMakeScanner scanner = new CMakeScanner();
     TokenInfo tokenInfo = new TokenInfo();
     int state = 0;
     int i = 0;
     foreach (string line in lines)
     {
         scanner.SetSource(line, 0);
         while (scanner.ScanTokenAndProvideInfoAboutIt(tokenInfo, ref state))
         {
             if (tokenInfo.Token == (int)CMakeToken.OpenParen)
             {
                 stack.Push(tokenInfo.ToTextSpan(i));
             }
             else if (tokenInfo.Token == (int)CMakeToken.CloseParen &&
                 stack.Count > 0)
             {
                 pairs.Add(new SpanPair()
                 {
                     First = stack.Pop(),
                     Second = tokenInfo.ToTextSpan(i)
                 });
             }
         }
         i++;
     }
     return pairs;
 }