コード例 #1
0
ファイル: Parser.cs プロジェクト: nickchal/pash
 private RedirectionAst RedirectionRule(RedirectionToken redirectionToken, RedirectionAst[] redirections, ref IScriptExtent extent)
 {
     RedirectionAst fileRedirectionAst;
     string allStream;
     FileRedirectionToken fileRedirectionToken = redirectionToken as FileRedirectionToken;
     if (fileRedirectionToken != null || redirectionToken as InputRedirectionToken != null)
     {
         ExpressionAst singleCommandArgument = this.GetSingleCommandArgument(Parser.CommandArgumentContext.FileName);
         if (singleCommandArgument == null)
         {
             this.ReportError(Parser.After(redirectionToken), ParserStrings.MissingFileSpecification, new object[0]);
             singleCommandArgument = new ErrorExpressionAst(redirectionToken.Extent, null);
         }
         if (fileRedirectionToken != null)
         {
             fileRedirectionAst = new FileRedirectionAst(Parser.ExtentOf(fileRedirectionToken, singleCommandArgument), fileRedirectionToken.FromStream, singleCommandArgument, fileRedirectionToken.Append);
         }
         else
         {
             object[] text = new object[1];
             text[0] = redirectionToken.Text;
             this.ReportError(redirectionToken.Extent, ParserStrings.RedirectionNotSupported, text);
             extent = Parser.ExtentOf(redirectionToken, singleCommandArgument);
             return null;
         }
     }
     else
     {
         MergingRedirectionToken mergingRedirectionToken = (MergingRedirectionToken)redirectionToken;
         RedirectionStream fromStream = mergingRedirectionToken.FromStream;
         RedirectionStream toStream = mergingRedirectionToken.ToStream;
         if (toStream == RedirectionStream.Output)
         {
             if (fromStream == toStream)
             {
                 object[] objArray = new object[1];
                 objArray[0] = mergingRedirectionToken.Text;
                 this.ReportError(redirectionToken.Extent, ParserStrings.RedirectionNotSupported, objArray);
             }
         }
         else
         {
             object[] text1 = new object[1];
             text1[0] = mergingRedirectionToken.Text;
             this.ReportError(redirectionToken.Extent, ParserStrings.RedirectionNotSupported, text1);
             toStream = RedirectionStream.Output;
         }
         fileRedirectionAst = new MergingRedirectionAst(mergingRedirectionToken.Extent, mergingRedirectionToken.FromStream, toStream);
     }
     if (redirections[(int)fileRedirectionAst.FromStream] != null)
     {
         RedirectionStream redirectionStream = fileRedirectionAst.FromStream;
         if (redirectionStream == RedirectionStream.All)
         {
             allStream = ParserStrings.AllStream;
         }
         else if (redirectionStream == RedirectionStream.Output)
         {
             allStream = ParserStrings.OutputStream;
         }
         else if (redirectionStream == RedirectionStream.Error)
         {
             allStream = ParserStrings.ErrorStream;
         }
         else if (redirectionStream == RedirectionStream.Warning)
         {
             allStream = ParserStrings.WarningStream;
         }
         else if (redirectionStream == RedirectionStream.Verbose)
         {
             allStream = ParserStrings.VerboseStream;
         }
         else if (redirectionStream == RedirectionStream.Debug)
         {
             allStream = ParserStrings.DebugStream;
         }
         else if (redirectionStream == RedirectionStream.Host)
         {
             allStream = ParserStrings.HostStream;
         }
         else
         {
             throw PSTraceSource.NewArgumentOutOfRangeException("result.FromStream", (object)fileRedirectionAst.FromStream);
         }
         object[] objArray1 = new object[1];
         objArray1[0] = allStream;
         this.ReportError(fileRedirectionAst.Extent, ParserStrings.StreamAlreadyRedirected, objArray1);
     }
     else
     {
         redirections[(int)fileRedirectionAst.FromStream] = fileRedirectionAst;
     }
     extent = fileRedirectionAst.Extent;
     return fileRedirectionAst;
 }
コード例 #2
0
ファイル: Parser.cs プロジェクト: dfinke/powershell
        private RedirectionAst RedirectionRule(RedirectionToken redirectionToken, RedirectionAst[] redirections, ref IScriptExtent extent)
        {
            //G  redirections:
            //G      redirection
            //G      redirections   redirection
            //G  redirection:
            //G      merging-redirection-operator
            //G      file-redirection-operator   redirected-file-name
            //G  redirected-file-name:
            //G      command-argument
            //G      primary-expression

            RedirectionAst result;

            var fileRedirectionToken = redirectionToken as FileRedirectionToken;
            if (fileRedirectionToken != null || (redirectionToken is InputRedirectionToken))
            {
                // get location
                var filename = GetSingleCommandArgument(CommandArgumentContext.FileName);
                if (filename == null)
                {
                    // ErrorRecovery: Just pretend we have a filename and continue parsing.

                    ReportError(After(redirectionToken), () => ParserStrings.MissingFileSpecification);
                    filename = new ErrorExpressionAst(redirectionToken.Extent);
                }

                if (fileRedirectionToken == null)
                {
                    // Must be an input redirection
                    ReportError(redirectionToken.Extent, () => ParserStrings.RedirectionNotSupported, redirectionToken.Text);
                    extent = ExtentOf(redirectionToken, filename);
                    return null;
                }

                result = new FileRedirectionAst(ExtentOf(fileRedirectionToken, filename), fileRedirectionToken.FromStream,
                                                filename, fileRedirectionToken.Append);
            }
            else
            {
                var mergingRedirectionToken = (MergingRedirectionToken)redirectionToken;

                RedirectionStream fromStream = mergingRedirectionToken.FromStream;
                RedirectionStream toStream = mergingRedirectionToken.ToStream;
                if (toStream != RedirectionStream.Output)
                {
                    // Have we seen something like 1>&2 or 2>&3
                    // ErrorRecovery: This is just a semantic error, so no special recovery.

                    ReportError(redirectionToken.Extent, () => ParserStrings.RedirectionNotSupported, mergingRedirectionToken.Text);
                    toStream = RedirectionStream.Output;
                }
                else if (fromStream == toStream)
                {
                    // Make sure 1>&1, 2>&2, etc. is an error.
                    // ErrorRecovery: This is just a semantic error, so no special recovery.

                    ReportError(redirectionToken.Extent, () => ParserStrings.RedirectionNotSupported, mergingRedirectionToken.Text);
                }

                result = new MergingRedirectionAst(mergingRedirectionToken.Extent, mergingRedirectionToken.FromStream, toStream);
            }

            if (redirections[(int)result.FromStream] == null)
            {
                redirections[(int)result.FromStream] = result;
            }
            else
            {
                string errorStream;
                switch (result.FromStream)
                {
                    case RedirectionStream.All: errorStream = ParserStrings.AllStream; break;
                    case RedirectionStream.Output: errorStream = ParserStrings.OutputStream; break;
                    case RedirectionStream.Error: errorStream = ParserStrings.ErrorStream; break;
                    case RedirectionStream.Warning: errorStream = ParserStrings.WarningStream; break;
                    case RedirectionStream.Verbose: errorStream = ParserStrings.VerboseStream; break;
                    case RedirectionStream.Debug: errorStream = ParserStrings.DebugStream; break;
                    case RedirectionStream.Information: errorStream = ParserStrings.InformationStream; break;
                    default:
                        throw PSTraceSource.NewArgumentOutOfRangeException("result.FromStream", result.FromStream);
                }
                ReportError(result.Extent, () => ParserStrings.StreamAlreadyRedirected, errorStream);
            }

            extent = result.Extent;
            return result;
        }