Exemple #1
0
 private void Parse(string script)
 {
     try
     {
         Parser parser2 = new Parser {
             ProduceV2Tokens = true
         };
         parser2.Parse(null, script, this.tokenList, out this.errors);
     }
     catch (Exception exception)
     {
         CommandProcessorBase.CheckForSevereException(exception);
     }
 }
        internal static ScriptBlock Create(Parser parser, string fileName, string fileContents)
        {
            var scriptBlock = TryGetCachedScriptBlock(fileName, fileContents);
            if (scriptBlock != null)
            {
                return scriptBlock;
            }

            ParseError[] errors;
            var ast = parser.Parse(fileName, fileContents, null, out errors, ParseMode.Default);
            if (errors.Length != 0)
            {
                throw new ParseException(errors);
            }

            var result = new ScriptBlock(ast, isFilter: false);
            CacheScriptBlock(result, fileName, fileContents);

            // The value returned will potentially be bound to a session state.  We don't want
            // the cached script block to end up being bound to any session state, so clone
            // the return value to ensure the cached value has no session state.
            return result.Clone();
        }
Exemple #3
0
 internal static ScriptBlock Create(Parser parser, string fileName, string fileContents)
 {
     ParseError[] parseErrorArray = null;
     ScriptBlock scriptBlock = ScriptBlock.TryGetCachedScriptBlock(fileName, fileContents);
     if (scriptBlock == null)
     {
         ScriptBlockAst scriptBlockAst = parser.Parse(fileName, fileContents, null, out parseErrorArray);
         if ((int)parseErrorArray.Length == 0)
         {
             ScriptBlock scriptBlock1 = new ScriptBlock(scriptBlockAst, false);
             ScriptBlock.CacheScriptBlock(scriptBlock1, fileName, fileContents);
             return scriptBlock1.Clone(false);
         }
         else
         {
             throw new ParseException(parseErrorArray);
         }
     }
     else
     {
         return scriptBlock;
     }
 }
Exemple #4
0
 internal ScriptBlockAst GetScriptBlockAst()
 {
     var scriptContents = ScriptContents;
     if (_scriptBlock == null)
     {
         this.ScriptBlock = ScriptBlock.TryGetCachedScriptBlock(_path, scriptContents);
     }
     if (_scriptBlock != null)
     {
         return (ScriptBlockAst)_scriptBlock.Ast;
     }
     if (_scriptBlockAst == null)
     {
         ParseError[] errors;
         Parser parser = new Parser();
         _scriptBlockAst = parser.Parse(_path, ScriptContents, null, out errors, ParseMode.Default);
         if (errors.Length == 0)
         {
             this.ScriptBlock = new ScriptBlock(_scriptBlockAst, isFilter: false);
             ScriptBlock.CacheScriptBlock(_scriptBlock.Clone(), _path, scriptContents);
         }
     }
     return _scriptBlockAst;
 }
Exemple #5
0
 private void Parse(string script)
 {
     try
     {
         var parser = new Language.Parser { ProduceV2Tokens = true };
         parser.Parse(null, script, _tokenList, out _errors, ParseMode.Default);
     }
     catch (Exception e)
     {
         // Catch everything, die on fatal exceptions, otherwise ignore
         CommandProcessorBase.CheckForSevereException(e);
     }
 }
Exemple #6
0
 internal static ScriptBlockAst ParseInput(string input, string fileName, out Token[] tokens, out ParseError[] errors)
 {
     ScriptBlockAst scriptBlockAst;
     Parser parser = new Parser();
     List<Token> tokens1 = new List<Token>();
     try
     {
         scriptBlockAst = parser.Parse(fileName, input, tokens1, out errors);
     }
     catch (Exception exception1)
     {
         Exception exception = exception1;
         throw new ParseException(ParserStrings.UnrecoverableParserError, exception);
     }
     tokens = tokens1.ToArray();
     return scriptBlockAst;
 }
Exemple #7
0
 public static ScriptBlockAst ParseFile(string fileName, out Token[] tokens, out ParseError[] errors)
 {
     ScriptBlockAst scriptBlockAst;
     Parser parser = new Parser();
     ExternalScriptInfo externalScriptInfo = new ExternalScriptInfo(fileName, fileName);
     List<Token> tokens1 = new List<Token>();
     try
     {
         scriptBlockAst = parser.Parse(fileName, externalScriptInfo.ScriptContents, tokens1, out errors);
     }
     catch (Exception exception1)
     {
         Exception exception = exception1;
         throw new ParseException(ParserStrings.UnrecoverableParserError, exception);
     }
     tokens = tokens1.ToArray();
     return scriptBlockAst;
 }
Exemple #8
0
        /// <summary>
        /// Parse input from the specified file.
        /// </summary>
        /// <param name="fileName">The name of the file to parse.</param>
        /// <param name="tokens">Returns the tokens from parsing the script.</param>
        /// <param name="errors">Returns errors, if any, discovered while parsing the script.</param>
        /// <returns>The <see cref="ScriptBlockAst"/> that represents the input script file.</returns>
        public static ScriptBlockAst ParseFile(string fileName, out Token[] tokens, out ParseError[] errors)
        {
            const string scriptSchemaExtension = ".schema.psm1";
            var parseDscResource = false;
            // If the file has the 'schema.psm1' extension, then it is a 'DSC module file' however we don't actually load the
            // module at parse time so we can't use the normal mechanisms to bind the module name for configuration commands.
            // As an alternative, we extract the base name of the module file and use that as the module name for any keywords exported by this file.
            var parser = new Parser();
            if (!string.IsNullOrEmpty(fileName) && fileName.Length > scriptSchemaExtension.Length && fileName.EndsWith(scriptSchemaExtension, StringComparison.OrdinalIgnoreCase))
            {
                parser._keywordModuleName = Path.GetFileName(fileName.Substring(0, fileName.Length - scriptSchemaExtension.Length));
                parseDscResource = true;
            }

            string scriptContents;
            try
            {
                var esi = new ExternalScriptInfo(fileName, fileName);
                scriptContents = esi.ScriptContents;
            }
            catch (Exception e)
            {
                var emptyExtent = new EmptyScriptExtent();
                var errorMsg = string.Format(CultureInfo.CurrentCulture, ParserStrings.FileReadError, e.Message);
                errors = new[] { new ParseError(emptyExtent, "FileReadError", errorMsg) };
                tokens = Utils.EmptyArray<Token>();
                return new ScriptBlockAst(emptyExtent, null, new StatementBlockAst(emptyExtent, null, null), false);
            }

            var tokenList = new List<Token>();
            ScriptBlockAst result;
            try
            {
                if (!parseDscResource)
                {
                    DynamicKeyword.Push();
                }
                result = parser.Parse(fileName, scriptContents, tokenList, out errors, ParseMode.Default);
            }
            catch (Exception e)
            {
                throw new ParseException(ParserStrings.UnrecoverableParserError, e);
            }
            finally
            {
                if (!parseDscResource)
                {
                    DynamicKeyword.Pop();
                }
            }

            tokens = tokenList.ToArray();
            return result;
        }
Exemple #9
0
        /// <summary>
        /// Parse input that does not come from a file.
        /// </summary>
        /// <param name="input">The input to parse.</param>
        /// <param name="fileName">The fileName if present or null.</param>
        /// <param name="tokens">Returns the tokens from parsing the script.</param>
        /// <param name="errors">Returns errors, if any, discovered while parsing the script.</param>
        /// <returns>The <see cref="ScriptBlockAst"/> that represents the input script file.</returns>
        public static ScriptBlockAst ParseInput(string input, string fileName, out Token[] tokens, out ParseError[] errors)
        {
            Parser parser = new Parser();
            List<Token> tokenList = new List<Token>();
            ScriptBlockAst result;
            try
            {
                result = parser.Parse(fileName, input, tokenList, out errors, ParseMode.Default);
            }
            catch (Exception e)
            {
                throw new ParseException(ParserStrings.UnrecoverableParserError, e);
            }

            tokens = tokenList.ToArray();
            return result;
        }