Exemplo n.º 1
0
        public Grammar Read(IGrammarSource source, ILogging logging)
        {
            var cilSource = source as CilGrammarSource;
            if (cilSource == null)
            {
                return null;
            }

            this.logging = logging;

            CilGrammar definition = null;

            logging.WithTimeLogging(
                cilSource.LanguageName,
                cilSource.Origin,
                () =>
                {
                    definition = new CilGrammar(cilSource, logging);
                },
                "parsing language definition");

            if (definition == null || !definition.IsValid)
            {
                return null;
            }

            var grammar = BuildGrammar(definition);

            grammar.Options = (IronText.Reflection.RuntimeOptions)Attributes.First<LanguageAttribute>(cilSource.DefinitionType).Flags;
            return grammar;
        }
Exemplo n.º 2
0
        public Grammar Read(IGrammarSource source, ILogging logging)
        {
            var cilSource = source as CilGrammarSource;

            if (cilSource == null)
            {
                return(null);
            }

            this.logging = logging;

            CilGrammar definition = null;

            logging.WithTimeLogging(
                cilSource.LanguageName,
                cilSource.Origin,
                () =>
            {
                definition = new CilGrammar(cilSource, logging);
            },
                "parsing language definition");

            if (definition == null || !definition.IsValid)
            {
                return(null);
            }

            var grammar = BuildGrammar(definition);

            grammar.Options = (IronText.Reflection.RuntimeOptions)Attributes.First <LanguageAttribute>(cilSource.DefinitionType).Flags;
            return(grammar);
        }
        private bool Build(ILogging logging, out LanguageData result)
        {
            this.logging = logging;

            result = new LanguageData();

            var readerType = Type.GetType(source.ReaderTypeName);

            if (readerType == null)
            {
                logging.Write(
                    new LogEntry
                {
                    Severity = Severity.Error,
                    Message  = string.Format(
                        "Unable to find grammar reader '{0}' for language '{1}'",
                        source.ReaderTypeName,
                        source.LanguageName),
                    Origin = source.Origin
                });
                return(false);
            }

            var reader  = (IGrammarReader)Activator.CreateInstance(readerType);
            var grammar = reader.Read(source, logging);

            if (grammar == null)
            {
                return(false);
            }

            grammar.Joint.Add(source);

//            var inliner = new ProductionInliner(grammar);
//            grammar = inliner.Inline();

            if (!bootstrap && !CompileScannerTdfas(grammar))
            {
                result = null;
                return(false);
            }

            // Build parsing tables
            ILrDfa parserDfa = null;

            var grammarAnalysis = new GrammarAnalysis(grammar);

            logging.WithTimeLogging(
                source.LanguageName,
                source.Origin,
                () =>
            {
                parserDfa = new Lalr1Dfa(grammarAnalysis, LrTableOptimizations.Default);
            },
                "building LALR1 DFA");

            if (parserDfa == null)
            {
                result = null;
                return(false);
            }

            var lrTable = new ConfigurableLrTable(parserDfa, grammar.Options);

            if (!lrTable.ComplyWithConfiguration)
            {
                grammar.Reports.Add(new ConflictMessageBuilder(logging));
            }

            var localParseContexts = CollectLocalContexts(grammar, parserDfa);

            // Prepare language data for the language assembly generation
            result.IsDeterministic           = !lrTable.RequiresGlr;
            result.Grammar                   = grammar;
            result.TokenComplexity           = grammarAnalysis.GetTokenComplexity();
            result.StateToToken              = parserDfa.GetStateToSymbolTable();
            result.ParserActionTable         = lrTable.GetParserActionTable();
            result.ParserConflictActionTable = lrTable.GetConflictActionTable();

            result.LocalParseContexts = localParseContexts.ToArray();

            if (!bootstrap)
            {
                IReportData reportData = new ReportData(source, result, lrTable.Conflicts, parserDfa.States);
                foreach (var report in grammar.Reports)
                {
                    report.Build(reportData);
                }
            }

            return(true);
        }