コード例 #1
0
 private static void HandleParserException(ParserException e)
 {
     // It may be possible for the log message to have interpolation
     // values in it. Don't know how the logging framework would
     // handle that correctly but I'll play it safe here by hoping
     // it doesn't recursively interpolate.
     Log.Error("Parsing error: ", e.Message);
     Log.Error(e.OffendingLine);
     Log.Error(e.CaretVisualizer);
 }
コード例 #2
0
        /// <summary>
        /// Performs a full parsing on the text. On success, internal data
        /// structures will be populated. Returns success or failure status.
        /// </summary>
        /// <param name="text">The text to parse.</param>
        /// <returns>True on success, false if any errors occurred. Failure
        /// occurs when the grammar is not correct, or tokenizing fails from
        /// malformed data (ex: string missing an ending quotation mark).
        /// </returns>
        public bool Parse(string text)
        {
            try
            {
                Tokens = Tokenizer.Read(text);

                if (preprocessor)
                {
                    Optional <List <Token> > tokens = preprocessor.Value.Process(Tokens);
                    if (!tokens)
                    {
                        Log.Error("Preprocessor failed, resource cannot be found");
                        return(false);
                    }

                    Tokens = tokens.Value;
                }

                PerformParsing();
                return(true);
            }
            catch (ArgumentOutOfRangeException)
            {
                if (Tokens.Empty())
                {
                    Log.Error("No tokens to parse, cannot read definition file");
                    return(false);
                }

                ParserException exception = new ParserException(CurrentTokenIndex, Tokens, "Text ended too early when parsing");
                HandleParserException(exception);
                return(false);
            }
            catch (ParserException e)
            {
                HandleParserException(e);
                return(false);
            }
            catch (Exception e)
            {
                PrintUnexpectedErrorMessage(e);
                return(false);
            }
        }