Esempio n. 1
0
        private MarkdownSpec(IEnumerable <Tuple <string, MarkdownDocument> > sources)
        {
            Sources = sources;

            // (1) Add sections into the dictionary
            string url = "", title = "";

            // (2) Turn all the antlr code blocks into a grammar
            var sbantlr = new StringBuilder();

            foreach (var src in sources)
            {
                var reporter = new Reporter(src.Item1);
                var filename = Path.GetFileName(src.Item1);
                var md       = src.Item2;

                foreach (var mdp in md.Paragraphs)
                {
                    reporter.CurrentParagraph = mdp;
                    reporter.CurrentSection   = null;
                    if (mdp.IsHeading)
                    {
                        try
                        {
                            var sr = new SectionRef(mdp as MarkdownParagraph.Heading, filename);
                            if (Sections.Any(s => s.Url == sr.Url))
                            {
                                reporter.Error("MD02", $"Duplicate section title {sr.Url}");
                            }
                            else
                            {
                                Sections.Add(sr);
                                url   = sr.Url;
                                title = sr.Title;
                                reporter.CurrentSection = sr;
                            }
                        }
                        catch (Exception ex)
                        {
                            reporter.Error("MD03", ex.Message); // constructor of SectionRef might throw
                        }
                    }
                    else if (mdp.IsCodeBlock)
                    {
                        var    mdc = mdp as MarkdownParagraph.CodeBlock;
                        string code = mdc.code, lang = mdc.language;
                        if (lang != "antlr")
                        {
                            continue;
                        }

                        var g = Antlr.ReadString(code, "");
                        Productions.Add(new ProductionRef(code, g.Productions));
                        foreach (var p in g.Productions)
                        {
                            p.Link = url; p.LinkName = title;
                            if (p.Name != null && Grammar.Productions.Any(dupe => dupe.Name == p.Name))
                            {
                                reporter.Warning("MD04", $"Duplicate grammar for {p.Name}");
                            }
                            Grammar.Productions.Add(p);
                        }
                    }
                }
            }
        }
Esempio n. 2
0
    private void Init()
    {
        // (1) Add sections into the dictionary
        int    h1 = 0, h2 = 0, h3 = 0, h4 = 0;
        string url = "", title = "";

        // (2) Turn all the antlr code blocks into a grammar
        var sbantlr = new StringBuilder();

        foreach (var src in Sources())
        {
            var filename = Path.GetFileName(src.Item1);
            var md       = Markdown.Parse(src.Item2);

            foreach (var mdp in md.Paragraphs)
            {
                if (mdp.IsHeading)
                {
                    var sr = new SectionRef(mdp as MarkdownParagraph.Heading, filename);
                    if (sr.Level == 1)
                    {
                        h1 += 1; h2 = 0; h3 = 0; h4 = 0; sr.Number = $"{h1}";
                    }
                    if (sr.Level == 2)
                    {
                        h2 += 1; h3 = 0; h4 = 0; sr.Number = $"{h1}.{h2}";
                    }
                    if (sr.Level == 3)
                    {
                        h3 += 1; h4 = 0; sr.Number = $"{h1}.{h2}.{h3}";
                    }
                    if (sr.Level == 4)
                    {
                        h4 += 1; sr.Number = $"{h1}.{h2}.{h3}.{h4}";
                    }
                    if (sr.Level > 4)
                    {
                        throw new NotSupportedException("Only support heading depths up to ####");
                    }
                    if (Sections.Any(s => s.Url == sr.Url))
                    {
                        throw new Exception($"Duplicate section title {sr.Url}");
                    }
                    Sections.Add(sr);
                    url   = sr.Url;
                    title = sr.Title;
                }
                else if (mdp.IsCodeBlock)
                {
                    var    mdc = mdp as MarkdownParagraph.CodeBlock;
                    string code = mdc.Item1, lang = mdc.Item2;
                    if (lang != "antlr")
                    {
                        continue;
                    }
                    var g = Antlr.ReadString(code, "");
                    foreach (var p in g.Productions)
                    {
                        p.Link = url; p.LinkName = title;
                        if (p.ProductionName != null && Grammar.Productions.Any(dupe => dupe.ProductionName == p.ProductionName))
                        {
                            Console.WriteLine($"Duplicate grammar for {p.ProductionName}");
                        }
                        Grammar.Productions.Add(p);
                    }
                }
            }
        }
    }