/// <summary> /// Parses a macro. /// </summary> /// <param name="inputText">The macro script text.</param> /// <returns>The parse tree, starting with the InstructionSet node.</returns> /// <exception cref="ParseException">Thrown if the text cannot be parsed.</exception> public static ParseTree ParseMacro(string inputText) { if (inputText == null) { throw new ArgumentNullException(); } // Preparse ‘ ’ quotes. Given that macros are entered in Word, and that people get confused, it's easier to just treat them all the same. inputText = inputText.Replace('‘', '\''); inputText = inputText.Replace('’', '\''); // Parse macro ExpressionGrammar grammar = _grammarPool.GetObject(); try { var parser = new Parser(grammar); parser.Parse(inputText); // Check for macro errors ParseTree parseTree = parser.Context.CurrentParseTree; grammar.CheckParseTreeOk(parseTree); return(parseTree); } finally { _grammarPool.PutObject(grammar); } }
/// <summary> /// Singleton instance of grammar. /// Thread-safe. Grammar is verified on first access. /// </summary> private static ExpressionGrammar CreateGrammar() { var grammar = new ExpressionGrammar(true); // Verify the grammar on first load var parser = new Parser(grammar); var errors = parser.Language.Errors; if (errors.Count > 0) { throw new Exception("Internal error: macro grammar contains error(s): " + string.Join("\n", errors)); } return(grammar); }
/// <summary> /// Prewarm the grammar pool with one object. /// </summary> public static void Prewarm() { ExpressionGrammar grammar = _grammarPool.GetObject(); _grammarPool.PutObject(grammar); }