/// <summary>
        /// Initializes a new instance of the CodeModelException class.
        /// </summary>
        /// <param name="sourceCode">The source code document containing the exception.</param>
        /// <param name="message">The exception message.</param>
        internal CodeModelException(Code sourceCode, string message)
            : base(string.Format(CultureInfo.CurrentCulture, Strings.CodeModelErrorWithMessage, sourceCode.Path, message))
        {
            Param.AssertNotNull(sourceCode, "sourceCode");
            Param.AssertValidString(message, "message");

            this.sourceCode = sourceCode;
        }
        /// <summary>
        /// Initializes a new instance of the CodeModelException class.
        /// </summary>
        /// <param name="sourceCode">The source code document containing the exception.</param>
        /// <param name="innerException">The exception within this exception.</param>
        internal CodeModelException(Code sourceCode, Exception innerException)
            : base(string.Format(CultureInfo.CurrentCulture, Strings.CodeModelError, sourceCode.Path), innerException)
        {
            Param.AssertNotNull(sourceCode, "sourceCode");
            Param.Ignore(innerException);

            this.sourceCode = sourceCode;
        }
        /// <summary>
        /// Initializes a new instance of the SyntaxException class.
        /// </summary>
        /// <param name="sourceCode">The source code document containing the exception.</param>
        /// <param name="lineNumber">The line number of the exception.</param>
        internal SyntaxException(Code sourceCode, int lineNumber)
            : base(string.Format(CultureInfo.CurrentCulture, Strings.SyntaxErrorInFile, sourceCode.Path, lineNumber))
        {
            Param.AssertNotNull(sourceCode, "sourceCode");
            Param.AssertGreaterThanZero(lineNumber, "lineNumber");

            this.sourceCode = sourceCode;
            this.lineNumber = lineNumber;
        }
Beispiel #4
0
        /// <summary>
        /// Initializes a new instance of the CodeLexer class.
        /// </summary>
        /// <param name="languageService">The C# language service.</param>
        /// <param name="source">The source to read.</param>
        /// <param name="codeReader">Used for reading the source code.</param>
        internal CodeLexer(CsLanguageService languageService, Code source, CodeReader codeReader)
        {
            Param.AssertNotNull(languageService, "languageService");
            Param.AssertNotNull(source, "source");
            Param.AssertNotNull(codeReader, "codeReader");

            this.languageService = languageService;
            this.source = source;
            this.codeReader = codeReader;
        }
        /// <summary>
        /// Creates a document for the given source code.
        /// </summary>
        /// <param name="sourceCode">The source code to parse.</param>
        /// <returns>Returns the document.</returns>
        /// <exception cref="SyntaxException">Throw a syntax exception if the code cannot be parsed according to the C# specification.</exception>
        internal CsDocument CreateCodeModelInternal(Code sourceCode)
        {
            Param.AssertNotNull(sourceCode, "sourceCode");

            // Create the lexer object for the code string.
            var lexer = new CodeLexer(this, sourceCode, new CodeReader(sourceCode));

            // Parse the document.
            var parser = new CodeParser(this, lexer, this.preprocessorDefinitions);
            parser.ParseCodeModel();

            return parser.Document;
        }
        /// <summary>
        /// Initializes a new instance of the SyntaxException class.
        /// </summary>
        /// <param name="sourceCode">The source code document containing the exception.</param>
        /// <param name="lineNumber">The line number of the exception.</param>
        /// <param name="message">The exception message.</param>
        /// <param name="innerException">The exception within this exception.</param>
        internal SyntaxException(Code sourceCode, int lineNumber, string message, Exception innerException)
            : base(string.Format(CultureInfo.CurrentCulture, Strings.SyntaxErrorInFileWithMessage, sourceCode.Path, lineNumber, message), innerException)
        {
            Param.AssertNotNull(sourceCode, "sourceCode");
            Param.AssertGreaterThanZero(lineNumber, "lineNumber");
            Param.AssertValidString(message, "message");
            Param.Ignore(innerException);

            this.sourceCode = sourceCode;
            this.lineNumber = lineNumber;
        }
Beispiel #7
0
        /// <summary>
        /// Gets an unknown symbol type.
        /// </summary>
        /// <param name="sourceCode">The source code containing the symbols.</param>
        /// <returns>Returns the item.</returns>
        private Symbol GetOtherSymbol(Code sourceCode)
        {
            Param.AssertNotNull(sourceCode, "sourceCode");

            var text = new StringBuilder();
            this.ReadToEndOfOtherSymbol(text);
            if (text.Length == 0)
            {
                throw new SyntaxException(sourceCode, 1);
            }

            string symbolText = text.ToString();

            // Get the token location.
            var location = new CodeLocation(
                this.marker.Index,
                this.marker.Index + text.Length - 1,
                this.marker.IndexOnLine,
                this.marker.IndexOnLine + text.Length - 1,
                this.marker.LineNumber,
                this.marker.LineNumber);

            // Create the symbol.
            var symbol = new Symbol(
                symbolText,
                CodeLexer.GetOtherSymbolType(symbolText),
                location);

            // Reset the marker index.
            this.marker.Index += text.Length;
            this.marker.IndexOnLine += text.Length;

            // Return the symbol.
            return symbol;
        }
Beispiel #8
0
        /// <summary>
        /// Initializes a new instance of the CodeLexer class.
        /// </summary>
        /// <param name="languageService">The C# language service.</param>
        /// <param name="source">The source to read.</param>
        /// <param name="codeReader">Used for reading the source code.</param>
        /// <param name="index">The starting absolute index of the code being parsed.</param>
        /// <param name="indexOnLine">The starting index on line of the code being parsed.</param>
        /// <param name="lineNumber">The starting line number of the code being parsed.</param>
        internal CodeLexer(CsLanguageService languageService, Code source, CodeReader codeReader, int index, int indexOnLine, int lineNumber)
        {
            Param.AssertNotNull(languageService, "languageService");
            Param.AssertNotNull(source, "source");
            Param.AssertNotNull(codeReader, "codeReader");
            Param.AssertGreaterThanOrEqualToZero(index, "index");
            Param.AssertGreaterThanOrEqualToZero(indexOnLine, "indexOnLine");
            Param.AssertGreaterThanZero(lineNumber, "lineNumber");

            this.languageService = languageService;
            this.source = source;
            this.codeReader = codeReader;

            this.marker.Index = index;
            this.marker.IndexOnLine = indexOnLine;
            this.marker.LineNumber = lineNumber;
        }
        /// <summary>
        /// Extracts the body of the given preprocessor directive symbol, parses it, and returns the parsed expression.
        /// </summary>
        /// <param name="document">The parent document.</param>
        /// <param name="code">The source code.</param>
        /// <param name="parentProxy">Represents the parent item.</param>
        /// <param name="languageService">The C# language service.</param>
        /// <param name="preprocessorDefinitions">Optional preprocessor definitions.</param>
        /// <param name="preprocessorSymbol">The preprocessor directive symbol.</param>
        /// <param name="startIndex">The index of the start of the expression body within the text string.</param>
        /// <returns>Returns the expression.</returns>
        internal static Expression GetConditionalPreprocessorBodyExpression(
            CsDocument document, 
            Code code, 
            CodeUnitProxy parentProxy, 
            CsLanguageService languageService, 
            IDictionary<string, object> preprocessorDefinitions,
            Symbol preprocessorSymbol, 
            int startIndex)
        {
            Param.AssertNotNull(document, "document");
            Param.AssertNotNull(code, "code");
            Param.AssertNotNull(parentProxy, "parentProxy");
            Param.AssertNotNull(languageService, "languageService");
            Param.Ignore(preprocessorDefinitions);
            Param.AssertNotNull(preprocessorSymbol, "preprocessorSymbol");
            Param.AssertGreaterThanOrEqualToZero(startIndex, "startIndex");
            CsLanguageService.Debug.Assert(preprocessorSymbol.SymbolType == SymbolType.PreprocessorDirective, "The symbol is not a preprocessor directive.");

            string text = preprocessorSymbol.Text.Substring(startIndex, preprocessorSymbol.Text.Length - startIndex).TrimEnd(null);
            if (text.Length > 0)
            {
                // Trim off the whitespace at the beginning and advance the start index.
                int trimIndex = 0;
                for (int i = 0; i < text.Length; ++i)
                {
                    if (char.IsWhiteSpace(text[i]))
                    {
                        ++trimIndex;
                    }
                    else
                    {
                        break;
                    }
                }

                if (trimIndex > 0)
                {
                    text = text.Substring(trimIndex, text.Length - trimIndex);
                    startIndex += trimIndex;
                }

                if (text.Length > 0)
                {
                    // Extract the symbols within this text.
                    Code preprocessorCode = new Code(text, "Preprocessor", "Preprocessor");
                    
                    var lexer = new CodeLexer(
                        languageService,
                        preprocessorCode,
                        new CodeReader(preprocessorCode),
                        preprocessorSymbol.Location.StartPoint.Index + startIndex,
                        preprocessorSymbol.Location.StartPoint.IndexOnLine + startIndex,
                        preprocessorSymbol.Location.StartPoint.LineNumber);

                    List<Symbol> symbolList = lexer.GetSymbols(document, null);
                    var directiveSymbols = new SymbolManager(symbolList);

                    var preprocessorBodyParser = new CodeParser(languageService, document, directiveSymbols, preprocessorDefinitions);

                    // Parse these symbols to create the body expression.
                    return preprocessorBodyParser.GetNextConditionalPreprocessorExpression(document, parentProxy);
                }
            }

            // The directive has no body.
            return null;
        }
Beispiel #10
0
 /// <summary>
 /// Initializes a new instance of the CodeReader class.
 /// </summary>
 /// <param name="code">Contains the code to read.</param>
 public CodeReader(Code code)
 {
     Param.AssertNotNull(code, "code");
     this.code = code;
 }