예제 #1
0
        public static RuleSetParsingResult Parse(string text, bool withLog = false)
        {
            Initialize();

            var parser = _defParserFabric.CreateTreeParser();

            parser.EnableLog = withLog;

            var reader        = new StringSourceTextReader(text);
            var parsingResult = parser.Parse(reader);

            if (!parsingResult.Successed)
            {
                throw new InvalidOperationException("Invalid grammar definition!");
            }

            // return new RuleSetParsingResult(null, parsingResult, parser.ParsingStatistics);

            var tree = ((IParsingTreeGroup)((IParsingTreeGroup)parsingResult.Tree).Childs.First()).Childs.First();

            var ctx   = _map.Translate(tree, new TranslationContext(reader));
            var rules = ctx.Result as RuleSetBase[];

            if (rules == null)
            {
                throw new InvalidOperationException("RuleSet mapping failed unexpectedly!");
            }

            return(new RuleSetParsingResult(rules, parsingResult, parser.ParsingStatistics));
        }
예제 #2
0
        private void btnParse_OnClick(object sender, RoutedEventArgs e)
        {
            try
            {
                var textReader = new StringSourceTextReader(txtText.Text);
                var parser     = _fabric.CreateTreeParser();
                parser.EnableLog = this.EnableTextParsingLog;
                parser.MaterializeOmittedFragments = this.MaterializeOmitFragments;
                parser.UseDelayedStates            = false;
                parser.RestoreRewritedRecursion    = this.RestoreRecursion;
                var result = parser.Parse(textReader);

                this.AppendLog(result.GetDebugInfo());
                this.AppendLog("Parsed in " + parser.ParsingStatistics);

                this.SetTrees(result.Tree, textReader);
            }
            catch (Exception ex)
            {
                this.AppendLog(ex.ToString());
            }
        }