コード例 #1
0
        /// <summary>
        /// Parses the current file contents to get the AST, tokens,
        /// and parse errors.
        /// </summary>
        private void ParseFileContents()
        {
            ParseError[] parseErrors = null;

            try
            {
                this.ScriptAst =
                    Parser.ParseInput(
                        this.Contents,
                        out this.scriptTokens,
                        out parseErrors);
            }
            catch (RuntimeException ex)
            {
                var parseError =
                    new ParseError(
                        null,
                        ex.ErrorRecord.FullyQualifiedErrorId,
                        ex.Message);

                parseErrors       = new[] { parseError };
                this.scriptTokens = new Token[0];
                this.ScriptAst    = null;
            }

            // Translate parse errors into syntax markers
            this.SyntaxMarkers =
                parseErrors
                .Select(ScriptFileMarker.FromParseError)
                .ToArray();

            //Get all dot sourced referenced files and store  them
            this.ReferencedFiles =
                AstOperations.FindDotSourcedIncludes(this.ScriptAst);
        }
コード例 #2
0
        /// <summary>
        /// Parses the current file contents to get the AST, tokens,
        /// and parse errors.
        /// </summary>
        private void ParseFileContents()
        {
            ParseError[] parseErrors = null;

            try
            {
#if PowerShellv5r2
                // This overload appeared with Windows 10 Update 1
                if (this.powerShellVersion.Major >= 5 &&
                    this.powerShellVersion.Build >= 10586)
                {
                    // Include the file path so that module relative
                    // paths are evaluated correctly
                    this.ScriptAst =
                        Parser.ParseInput(
                            this.Contents,
                            this.FilePath,
                            out this.scriptTokens,
                            out parseErrors);
                }
                else
                {
                    this.ScriptAst =
                        Parser.ParseInput(
                            this.Contents,
                            out this.scriptTokens,
                            out parseErrors);
                }
#else
                this.ScriptAst =
                    Parser.ParseInput(
                        this.Contents,
                        out this.scriptTokens,
                        out parseErrors);
#endif
            }
            catch (RuntimeException ex)
            {
                var parseError =
                    new ParseError(
                        null,
                        ex.ErrorRecord.FullyQualifiedErrorId,
                        ex.Message);

                parseErrors       = new[] { parseError };
                this.scriptTokens = new Token[0];
                this.ScriptAst    = null;
            }

            // Translate parse errors into syntax markers
            this.SyntaxMarkers =
                parseErrors
                .Select(ScriptFileMarker.FromParseError)
                .ToArray();

            //Get all dot sourced referenced files and store  them
            this.ReferencedFiles =
                AstOperations.FindDotSourcedIncludes(this.ScriptAst);
        }
コード例 #3
0
        /// <summary>
        /// Parses the current file contents to get the AST, tokens,
        /// and parse errors.
        /// </summary>
        private void ParseFileContents()
        {
            ParseError[] parseErrors = null;

            // First, get the updated file range
            int lineCount = this.FileLines.Count;

            if (lineCount > 0)
            {
                this.FileRange =
                    new BufferRange(
                        new BufferPosition(1, 1),
                        new BufferPosition(
                            lineCount + 1,
                            this.FileLines[lineCount - 1].Length + 1));
            }
            else
            {
                this.FileRange = BufferRange.None;
            }

            try
            {
                Token[] scriptTokens;

                // This overload appeared with Windows 10 Update 1
                if (this.powerShellVersion.Major >= 6 ||
                    (this.powerShellVersion.Major == 5 && this.powerShellVersion.Build >= 10586))
                {
                    // Include the file path so that module relative
                    // paths are evaluated correctly
                    this.ScriptAst =
                        Parser.ParseInput(
                            this.Contents,
                            this.FilePath,
                            out scriptTokens,
                            out parseErrors);
                }
                else
                {
                    this.ScriptAst =
                        Parser.ParseInput(
                            this.Contents,
                            out scriptTokens,
                            out parseErrors);
                }

                this.ScriptTokens = scriptTokens;
            }
            catch (RuntimeException ex)
            {
                var parseError =
                    new ParseError(
                        null,
                        ex.ErrorRecord.FullyQualifiedErrorId,
                        ex.Message);

                parseErrors       = new[] { parseError };
                this.ScriptTokens = new Token[0];
                this.ScriptAst    = null;
            }

            // Translate parse errors into syntax markers
            this.SyntaxMarkers =
                parseErrors
                .Select(ScriptFileMarker.FromParseError)
                .ToArray();

            // Untitled files have no directory
            // Discussed in https://github.com/PowerShell/PowerShellEditorServices/pull/815.
            // Rather than working hard to enable things for untitled files like a phantom directory,
            // users should save the file.
            if (IsUntitledPath(this.FilePath))
            {
                // Need to initialize the ReferencedFiles property to an empty array.
                this.ReferencedFiles = new string[0];
                return;
            }

            // Get all dot sourced referenced files and store them
            this.ReferencedFiles = AstOperations.FindDotSourcedIncludes(this.ScriptAst, Path.GetDirectoryName(this.FilePath));
        }