Example #1
0
 private void TriggerWarning(Antlr.Runtime.RecognitionException e)
 {
     if (this.Warning != null)
     {
         Warning(this, new ParseErrorEventArgs(e));
     }
 }
Example #2
0
 private void TriggerMessage(Antlr.Runtime.RecognitionException e)
 {
     if (this.Message != null)
     {
         Message(this, new ParseErrorEventArgs(e));
     }
 }
Example #3
0
        protected override void DebugRecognitionException(Antlr.Runtime.RecognitionException ex)
        {
            Console.WriteLine("L:DebugRecognitionException");
            Console.WriteLine(ex);

            base.DebugRecognitionException(ex);
        }
 public override object Create(int tokenType, Antlr.Runtime.IToken fromToken, string text)
 {
     fromToken = CreateToken(fromToken);
     fromToken.Type = tokenType;
     fromToken.Text = text;
     return new LSLAst(fromToken);
 }
Example #5
0
    static bool AreProductionsSame(Grammar authority, Grammar copy)
    {
        Func <Grammar, Dictionary <string, Production> > ToDictionary;

        ToDictionary = g =>
        {
            var d = new Dictionary <string, Production>();
            foreach (var pp in g.Productions)
            {
                if (pp.ProductionName != null)
                {
                    d[pp.ProductionName] = pp;
                }
            }
            return(d);
        };
        var dauthority = ToDictionary(authority);
        var dcopy      = ToDictionary(copy);
        var ok         = true;

        foreach (var p in dauthority.Keys)
        {
            if (!dcopy.ContainsKey(p))
            {
                continue;
            }
            string pauthority = Antlr.ToString(dauthority[p]), pcopy = Antlr.ToString(dcopy[p]);
            if (pauthority == pcopy)
            {
                continue;
            }
            ok = false;
            Console.WriteLine($"MISMATCH for '{p}'\r\nAUTHORITY:\r\n{pauthority}\r\nCOPY:\r\n{pcopy}\r\n");
        }

        foreach (var p in dauthority.Keys)
        {
            if (p == "start")
            {
                continue;
            }
            if (!dcopy.ContainsKey(p))
            {
                Console.WriteLine($"Copy doesn't contain '{p}'"); ok = false;
            }
        }
        foreach (var p in dcopy.Keys)
        {
            if (p == "start")
            {
                continue;
            }
            if (!dauthority.ContainsKey(p))
            {
                Console.WriteLine($"Authority doesn't contain '{p}'"); ok = false;
            }
        }

        return(ok);
    }
Example #6
0
        protected override void DebugRecognitionException(Antlr.Runtime.RecognitionException ex)
        {
            if (find.debug)
               Console.WriteLine(ex);

            base.DebugRecognitionException(ex);
        }
Example #7
0
 private void TriggerError(Antlr.Runtime.RecognitionException e)
 {
     if (this.Error != null)
     {
         Error(this, new ParseErrorEventArgs(e));
     }
 }
Example #8
0
 protected override object RecoverFromMismatchedToken(Antlr.Runtime.IIntStream input, int ttype, Antlr.Runtime.BitSet follow)
 {
     if (find.debug)
     {
         Console.WriteLine("RecoverFromMismatchedToken");
     }
     return base.RecoverFromMismatchedToken(input, ttype, follow);
 }
Example #9
0
 public override object RecoverFromMismatchedSet(Antlr.Runtime.IIntStream input, Antlr.Runtime.RecognitionException e, Antlr.Runtime.BitSet follow)
 {
     if (find.debug)
     {
         Console.WriteLine("RecoverFromMismatchedSet");
         Console.WriteLine(e);
     }
     return base.RecoverFromMismatchedSet(input, e, follow);
 }
Example #10
0
 public override void ReportError(Antlr.Runtime.RecognitionException e)
 {
     if (find.debug)
     {
         Console.WriteLine("ReportError");
         Console.WriteLine(e);
     }
     base.ReportError(e);
 }
Example #11
0
 public override void Recover(Antlr.Runtime.IIntStream input, Antlr.Runtime.RecognitionException re)
 {
     if (find.debug)
     {
         Console.WriteLine("Recover");
         Console.WriteLine(re);
     }
     base.Recover(input, re);
 }
        public override void DisplayRecognitionError(string[] tokenNames, Antlr.Runtime.RecognitionException e)
        {
            var sb = new StringBuilder();
            sb.AppendLine("There was a problem with the json input during syntactical analysis and it cannot be parsed. Here is some more info:");
            sb.AppendLine("----");

            sb.AppendLine(e.Message);
            sb.AppendLine("region: " + e.Input.ToString());
            sb.AppendLine("line: " + e.Line);
            sb.AppendLine("col: " + e.CharPositionInLine);

            throw new JsonException(sb.ToString(), e);
        }
        public ApplicationOAuthProvider(string publicClientId, Antlr.Runtime.Misc.Func<UserManager<IdentityUser>> userManagerFactory)
        {
            if (publicClientId == null)
            {
                throw new ArgumentNullException("publicClientId");
            }

            if (userManagerFactory == null)
            {
                throw new ArgumentNullException("userManagerFactory");
            }

            _publicClientId = publicClientId;
            _userManagerFactory = userManagerFactory;
        }
Example #14
0
        public virtual void GrammarError(ErrorType etype,
                                 string fileName,
                                 Antlr.Runtime.IToken token,
                                 params object[] args)
        {
            ANTLRMessage msg = new GrammarSemanticsMessage(etype, fileName, token, args);
            Emit(etype, msg);

        }
Example #15
0
 public virtual void SyntaxError(ErrorType etype,
                                string fileName,
                                Antlr.Runtime.IToken token,
                                Antlr.Runtime.RecognitionException antlrException,
                                params object[] args)
 {
     ANTLRMessage msg = new GrammarSyntaxMessage(etype, fileName, token, antlrException, args);
     Emit(etype, msg);
 }
 public override object Create(Antlr.Runtime.IToken payload)
 {
     return new SQLTree(payload);
 }
Example #17
0
 public TokenStreamRewriteEngine( Antlr.Runtime.ITokenSource source )
 {
     _source = source;
 }
 public SaveStruct(Antlr.Runtime.ICharStream input)
 {
     this.input = input;
     this.marker = input.Mark();
 }
Example #19
0
        IEnumerable <OpenXmlCompositeElement> Paragraph2Paragraphs(MarkdownParagraph md)
        {
            if (md.IsHeading)
            {
                var mdh   = md as MarkdownParagraph.Heading;
                var level = mdh.Item1;
                var spans = mdh.Item2;
                var sr    = sections[new SectionRef(mdh, filename).Url];
                var props = new ParagraphProperties(new ParagraphStyleId()
                {
                    Val = $"Heading{level}"
                });
                var p = new Paragraph {
                    ParagraphProperties = props
                };
                maxBookmarkId.Value += 1;
                p.AppendChild(new BookmarkStart {
                    Name = sr.BookmarkName, Id = maxBookmarkId.Value.ToString()
                });
                p.Append(Spans2Elements(spans));
                p.AppendChild(new BookmarkEnd {
                    Id = maxBookmarkId.Value.ToString()
                });
                yield return(p);

                Console.WriteLine(new string(' ', level * 4 - 4) + sr.Number + " " + sr.Title);
                yield break;
            }

            else if (md.IsParagraph)
            {
                var mdp   = md as MarkdownParagraph.Paragraph;
                var spans = mdp.Item;
                yield return(new Paragraph(Spans2Elements(spans)));

                yield break;
            }

            else if (md.IsListBlock)
            {
                var mdl  = md as MarkdownParagraph.ListBlock;
                var flat = FlattenList(mdl);

                // Let's figure out what kind of list it is - ordered or unordered? nested?
                var format0 = new[] { "1", "1", "1", "1" };
                foreach (var item in flat)
                {
                    format0[item.Level] = (item.IsBulletOrdered ? "1" : "o");
                }
                var format = string.Join("", format0);

                var numberingPart = wdoc.MainDocumentPart.NumberingDefinitionsPart ?? wdoc.MainDocumentPart.AddNewPart <NumberingDefinitionsPart>("NumberingDefinitionsPart001");
                if (numberingPart.Numbering == null)
                {
                    numberingPart.Numbering = new Numbering();
                }

                Func <int, bool, Level> createLevel;
                createLevel = (level, isOrdered) =>
                {
                    var numformat = NumberFormatValues.Bullet;
                    var levelText = new[] { "·", "o", "·", "o" }[level];
                    if (isOrdered && level == 0)
                    {
                        numformat = NumberFormatValues.Decimal; levelText = "%1.";
                    }
                    if (isOrdered && level == 1)
                    {
                        numformat = NumberFormatValues.LowerLetter; levelText = "%2.";
                    }
                    if (isOrdered && level == 2)
                    {
                        numformat = NumberFormatValues.LowerRoman; levelText = "%3.";
                    }
                    if (isOrdered && level == 3)
                    {
                        numformat = NumberFormatValues.LowerRoman; levelText = "%4.";
                    }
                    var r = new Level {
                        LevelIndex = level
                    };
                    r.Append(new StartNumberingValue {
                        Val = 1
                    });
                    r.Append(new NumberingFormat {
                        Val = numformat
                    });
                    r.Append(new LevelText {
                        Val = levelText
                    });
                    r.Append(new ParagraphProperties(new Indentation {
                        Left = (540 + 360 * level).ToString(), Hanging = "360"
                    }));
                    if (levelText == "·")
                    {
                        r.Append(new NumberingSymbolRunProperties(new RunFonts {
                            Hint = FontTypeHintValues.Default, Ascii = "Symbol", HighAnsi = "Symbol", EastAsia = "Times new Roman", ComplexScript = "Times new Roman"
                        }));
                    }
                    if (levelText == "o")
                    {
                        r.Append(new NumberingSymbolRunProperties(new RunFonts {
                            Hint = FontTypeHintValues.Default, Ascii = "Courier New", HighAnsi = "Courier New", ComplexScript = "Courier New"
                        }));
                    }
                    return(r);
                };
                var level0 = createLevel(0, format[0] == '1');
                var level1 = createLevel(1, format[1] == '1');
                var level2 = createLevel(2, format[2] == '1');
                var level3 = createLevel(3, format[3] == '1');

                var abstracts = numberingPart.Numbering.OfType <AbstractNum>().Select(an => an.AbstractNumberId.Value).ToList();
                var aid       = (abstracts.Count == 0 ? 1 : abstracts.Max() + 1);
                var aabstract = new AbstractNum(new MultiLevelType()
                {
                    Val = MultiLevelValues.Multilevel
                }, level0, level1, level2, level3)
                {
                    AbstractNumberId = aid
                };
                numberingPart.Numbering.InsertAt(aabstract, 0);

                var instances   = numberingPart.Numbering.OfType <NumberingInstance>().Select(ni => ni.NumberID.Value);
                var nid         = (instances.Count() == 0 ? 1 : instances.Max() + 1);
                var numInstance = new NumberingInstance(new AbstractNumId {
                    Val = aid
                })
                {
                    NumberID = nid
                };
                numberingPart.Numbering.AppendChild(numInstance);

                // We'll also figure out the indentation(for the benefit of those paragraphs that should be
                // indendent with the list but aren't numbered). I'm not sure what the indent comes from.
                // in the docx, each AbstractNum that I created has an indent for each of its levels,
                // defaulted at 900, 1260, 1620, ... but I can't see where in the above code that's created?
                Func <int, string> calcIndent = level => (540 + level * 360).ToString();

                foreach (var item in flat)
                {
                    var content = item.Paragraph;
                    if (content.IsParagraph || content.IsSpan)
                    {
                        var spans = (content.IsParagraph ? (content as MarkdownParagraph.Paragraph).Item : (content as MarkdownParagraph.Span).Item);
                        if (item.HasBullet)
                        {
                            yield return new Paragraph(Spans2Elements(spans))
                                   {
                                       ParagraphProperties = new ParagraphProperties(new NumberingProperties(new ParagraphStyleId {
                                    Val = "ListParagraph"
                                }, new NumberingLevelReference {
                                    Val = item.Level
                                }, new NumberingId {
                                    Val = nid
                                }))
                                   }
                        }
                        ;
                        else
                        {
                            yield return new Paragraph(Spans2Elements(spans))
                                   {
                                       ParagraphProperties = new ParagraphProperties(new Indentation {
                                    Left = calcIndent(item.Level)
                                })
                                   }
                        };
                    }
                    else if (content.IsQuotedBlock || content.IsCodeBlock)
                    {
                        foreach (var p in Paragraph2Paragraphs(content))
                        {
                            var props = p.GetFirstChild <ParagraphProperties>();

                            if (props == null)
                            {
                                props = new ParagraphProperties(); p.InsertAt(props, 0);
                            }
                            var indent = props?.GetFirstChild <Indentation>();
                            if (indent == null)
                            {
                                indent = new Indentation(); props.Append(indent);
                            }
                            indent.Left = calcIndent(item.Level);
                            yield return(p);
                        }
                    }
                    else if (content.IsTableBlock)
                    {
                        foreach (var p in Paragraph2Paragraphs(content))
                        {
                            var table = p as Table;
                            if (table == null)
                            {
                                yield return(p); continue;
                            }
                            var tprops  = table.GetFirstChild <TableProperties>();
                            var tindent = tprops?.GetFirstChild <TableIndentation>();
                            if (tindent == null)
                            {
                                throw new Exception("Ooops! Table is missing indentation");
                            }
                            tindent.Width = int.Parse(calcIndent(item.Level));
                            yield return(table);
                        }
                    }
                    else
                    {
                        throw new Exception("Unexpected item in list");
                    }
                }
            }

            else if (md.IsCodeBlock)
            {
                var mdc  = md as MarkdownParagraph.CodeBlock;
                var code = mdc.Item1;
                var lang = mdc.Item2;
                code = BugWorkaroundDecode(code);
                var runs        = new List <Run>();
                var onFirstLine = true;
                IEnumerable <ColorizedLine> lines;
                if (lang == "csharp" || lang == "c#" || lang == "cs")
                {
                    lines = Colorize.CSharp(code);
                }
                else if (lang == "vb" || lang == "vbnet" || lang == "vb.net")
                {
                    lines = Colorize.VB(code);
                }
                else if (lang == "" || lang == "xml")
                {
                    lines = Colorize.PlainText(code);
                }
                else if (lang == "antlr")
                {
                    lines = Antlr.ColorizeAntlr(code);
                }
                else
                {
                    throw new NotSupportedException($"unrecognized language {lang}");
                }
                foreach (var line in lines)
                {
                    if (onFirstLine)
                    {
                        onFirstLine = false;
                    }
                    else
                    {
                        runs.Add(new Run(new Break()));
                    }
                    foreach (var word in line.Words)
                    {
                        var run   = new Run();
                        var props = new RunProperties();
                        if (word.Red != 0 || word.Green != 0 || word.Blue != 0)
                        {
                            props.Append(new Color {
                                Val = $"{word.Red:X2}{word.Green:X2}{word.Blue:X2}"
                            });
                        }
                        if (word.IsItalic)
                        {
                            props.Append(new Italic());
                        }
                        if (props.HasChildren)
                        {
                            run.Append(props);
                        }
                        run.Append(new Text(word.Text)
                        {
                            Space = SpaceProcessingModeValues.Preserve
                        });
                        runs.Add(run);
                    }
                }
                var style = new ParagraphStyleId {
                    Val = (lang == "antlr" ? "Grammar" : "Code")
                };
                yield return(new Paragraph(runs)
                {
                    ParagraphProperties = new ParagraphProperties(style)
                });
            }

            else if (md.IsQuotedBlock)
            {
                var mdq     = md as MarkdownParagraph.QuotedBlock;
                var quoteds = mdq.Item;
                var kind    = "";
                foreach (var quoted0 in quoteds)
                {
                    var quoted = quoted0;
                    if (quoted.IsParagraph)
                    {
                        var p     = quoted as MarkdownParagraph.Paragraph;
                        var spans = p.Item;
                        if (spans.Any() && spans.First().IsStrong)
                        {
                            var strong = (spans.First() as MarkdownSpan.Strong).Item;
                            if (strong.Any() && strong.First().IsLiteral)
                            {
                                var literal = mdunescape(strong.First() as MarkdownSpan.Literal);
                                if (literal == "Note")
                                {
                                    kind = "AlertText";
                                }
                                else if (literal == "Annotation")
                                {
                                    kind = "Annotation";
                                    yield return(new Paragraph(Span2Elements(spans.Head))
                                    {
                                        ParagraphProperties = new ParagraphProperties(new ParagraphStyleId {
                                            Val = kind
                                        })
                                    });

                                    if (spans.Tail.Any() && spans.Tail.First().IsLiteral)
                                    {
                                        quoted = MarkdownParagraph.NewParagraph(new Microsoft.FSharp.Collections.FSharpList <MarkdownSpan>(MarkdownSpan.NewLiteral(mdunescape(spans.Tail.First() as MarkdownSpan.Literal).TrimStart()), spans.Tail.Tail));
                                    }
                                    else
                                    {
                                        quoted = MarkdownParagraph.NewParagraph(spans.Tail);
                                    }
                                }
                            }
                        }
                        //
                        foreach (var qp in Paragraph2Paragraphs(quoted))
                        {
                            var qpp = qp as Paragraph;
                            if (qpp != null)
                            {
                                var props = new ParagraphProperties(new ParagraphStyleId()
                                {
                                    Val = kind
                                }); qpp.ParagraphProperties = props;
                            }
                            yield return(qp);
                        }
                    }

                    else if (quoted.IsCodeBlock)
                    {
                        var mdc              = quoted as MarkdownParagraph.CodeBlock;
                        var code             = mdc.Item1;
                        var lang             = mdc.Item2;
                        var ignoredAfterLang = mdc.Item3;
                        var lines            = code.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None).ToList();
                        if (string.IsNullOrWhiteSpace(lines.Last()))
                        {
                            lines.RemoveAt(lines.Count - 1);
                        }
                        var run = new Run()
                        {
                            RunProperties = new RunProperties(new RunStyle {
                                Val = "CodeEmbedded"
                            })
                        };
                        foreach (var line in lines)
                        {
                            if (run.ChildElements.Count() > 1)
                            {
                                run.Append(new Break());
                            }
                            run.Append(new Text("    " + line)
                            {
                                Space = SpaceProcessingModeValues.Preserve
                            });
                        }
                        yield return(new Paragraph(run)
                        {
                            ParagraphProperties = new ParagraphProperties(new ParagraphStyleId {
                                Val = kind
                            })
                        });
                    }

                    else if (quoted.IsListBlock)
                    {
                        if (!(quoted as MarkdownParagraph.ListBlock).Item1.IsOrdered)
                        {
                            throw new NotImplementedException("unordered list inside annotation");
                        }
                        var count = 1;
                        foreach (var qp in Paragraph2Paragraphs(quoted))
                        {
                            var qpp = qp as Paragraph;
                            if (qpp == null)
                            {
                                yield return(qp); continue;
                            }
                            qp.InsertAt(new Run(new Text($"{count}. ")
                            {
                                Space = SpaceProcessingModeValues.Preserve
                            }), 0);
                            count += 1;
                            var props = new ParagraphProperties(new ParagraphStyleId()
                            {
                                Val = kind
                            });
                            qpp.ParagraphProperties = props;
                            yield return(qp);
                        }
                    }
                }
            }

            else if (md.IsTableBlock)
            {
                var mdt    = md as MarkdownParagraph.TableBlock;
                var header = mdt.Item1.Option();
                var align  = mdt.Item2;
                var rows   = mdt.Item3;
                var table  = new Table();
                var tstyle = new TableStyle {
                    Val = "TableGrid"
                };
                var tindent = new TableIndentation {
                    Width = 360, Type = TableWidthUnitValues.Dxa
                };
                var tborders = new TableBorders();
                tborders.TopBorder = new TopBorder {
                    Val = BorderValues.Single
                };
                tborders.BottomBorder = new BottomBorder {
                    Val = BorderValues.Single
                };
                tborders.LeftBorder = new LeftBorder {
                    Val = BorderValues.Single
                };
                tborders.RightBorder = new RightBorder {
                    Val = BorderValues.Single
                };
                tborders.InsideHorizontalBorder = new InsideHorizontalBorder {
                    Val = BorderValues.Single
                };
                tborders.InsideVerticalBorder = new InsideVerticalBorder {
                    Val = BorderValues.Single
                };
                var tcellmar = new TableCellMarginDefault();
                tcellmar.Append();
                table.Append(new TableProperties(tstyle, tindent, tborders));
                var ncols = align.Length;
                for (int irow = -1; irow < rows.Length; irow++)
                {
                    if (irow == -1 && header == null)
                    {
                        continue;
                    }
                    var mdrow = (irow == -1 ? header : rows[irow]);
                    var row   = new TableRow();
                    for (int icol = 0; icol < Math.Min(ncols, mdrow.Length); icol++)
                    {
                        var mdcell = mdrow[icol];
                        var cell   = new TableCell();
                        var pars   = Paragraphs2Paragraphs(mdcell).ToList();
                        for (int ip = 0; ip < pars.Count; ip++)
                        {
                            var p = pars[ip] as Paragraph;
                            if (p == null)
                            {
                                cell.Append(pars[ip]); continue;
                            }
                            var props = new ParagraphProperties(new ParagraphStyleId {
                                Val = "TableCellNormal"
                            });
                            if (align[icol].IsAlignCenter)
                            {
                                props.Append(new Justification {
                                    Val = JustificationValues.Center
                                });
                            }
                            if (align[icol].IsAlignRight)
                            {
                                props.Append(new Justification {
                                    Val = JustificationValues.Right
                                });
                            }
                            p.InsertAt(props, 0);
                            cell.Append(pars[ip]);
                        }
                        if (pars.Count == 0)
                        {
                            cell.Append(new Paragraph(new ParagraphProperties(new SpacingBetweenLines {
                                After = "0"
                            }), new Run(new Text(""))));
                        }
                        row.Append(cell);
                    }
                    table.Append(row);
                }
                yield return(new Paragraph(new Run(new Text("")))
                {
                    ParagraphProperties = new ParagraphProperties(new ParagraphStyleId {
                        Val = "TableLineBefore"
                    })
                });

                yield return(table);

                yield return(new Paragraph(new Run(new Text("")))
                {
                    ParagraphProperties = new ParagraphProperties(new ParagraphStyleId {
                        Val = "TableLineAfter"
                    })
                });
            }
            else
            {
                yield return(new Paragraph(new Run(new Text($"[{md.GetType().Name}]"))));
            }
        }
Example #20
0
 protected virtual void CheckForInvalidArguments( string[] args, Antlr.Runtime.BitSet cmdLineArgValid )
 {
     // check for invalid command line args
     for ( int a = 0; a < args.Length; a++ )
     {
         if ( !cmdLineArgValid.Member( a ) )
         {
             Console.Error.WriteLine( "invalid command-line argument: " + args[a] + "; ignored" );
         }
     }
 }
Example #21
0
 public override SetOfValue CreateSetOfValue(SetOfValue o, Antlr.Runtime.Tree.ITree antlr)
 {
     return new SCCSetOfValue(o, antlr);
 }
Example #22
0
    static int Main(string[] args)
    {
        var ifn = (args.Length >= 2 ? args[1] : "readme.md");

        if (!File.Exists(ifn) || !File.Exists("template.docx") || Directory.GetFiles(".", "*.g4").Length > 1)
        {
            Console.Error.WriteLine("md2docx <filename>.md -- converts it to '<filename>.docx', based on 'template.docx'");
            Console.Error.WriteLine();
            Console.Error.WriteLine("If no file is specified:");
            Console.Error.WriteLine("    it looks for readme.md instead");
            Console.Error.WriteLine("If input file has a list with links of the form `* [Link](subfile.md)`:");
            Console.Error.WriteLine("   it converts the listed subfiles instead of <filename>.md");
            Console.Error.WriteLine("If the current directory contains one <grammar>.g4 file:");
            Console.Error.WriteLine("   it verifies all ```antlr blocks correspond, and also generates <grammar>.html");
            Console.Error.WriteLine("If 'template.docx' contains a Table of Contents:");
            Console.Error.WriteLine("   it replaces it with one based on the markdown (but page numbers aren't supported)");
            return(1);
        }

        // Read input file. If it contains a load of linked filenames, then read them instead.
        var readme = FSharp.Markdown.Markdown.Parse(File.ReadAllText(ifn));
        var files  = (from list in readme.Paragraphs.OfType <FSharp.Markdown.MarkdownParagraph.ListBlock>()
                      let items = list.Item2
                                  from par in items
                                  from spanpar in par.OfType <FSharp.Markdown.MarkdownParagraph.Span>()
                                  let spans = spanpar.Item
                                              from link in spans.OfType <FSharp.Markdown.MarkdownSpan.DirectLink>()
                                              let url = link.Item2.Item1
                                                        where url.EndsWith(".md", StringComparison.InvariantCultureIgnoreCase)
                                                        select url).ToList().Distinct();

        if (files.Count() == 0)
        {
            files = new[] { ifn }
        }
        ;
        var md = MarkdownSpec.ReadFiles(files);


        // Now md.Gramar contains the grammar as extracted out of the *.md files, and moreover has
        // correct references to within the spec. We'll check that it has the same productions as
        // in the corresponding ANTLR file
        var antlrfn = Directory.GetFiles(".", "*.g4").FirstOrDefault();

        if (antlrfn != null)
        {
            var htmlfn  = Path.ChangeExtension(antlrfn, ".html");
            var grammar = Antlr.ReadFile(antlrfn);
            if (!AreProductionsSame(grammar, md.Grammar))
            {
                throw new Exception("Grammar mismatch");
            }
            foreach (var p in grammar.Productions)
            {
                p.Link     = md.Grammar.Productions.FirstOrDefault(mdp => mdp?.ProductionName == p.ProductionName)?.Link;
                p.LinkName = md.Grammar.Productions.FirstOrDefault(mdp => mdp?.ProductionName == p.ProductionName)?.LinkName;
            }

            File.WriteAllText(htmlfn, grammar.ToHtml(), Encoding.UTF8);
            Process.Start(htmlfn);
        }

        // Generate the Specification.docx file
        var fn = PickUniqueFilename(Path.ChangeExtension(ifn, ".docx"));

        md.WriteFile("template.docx", fn);
        Process.Start(fn);
        return(0);
    }
Example #23
0
        static int Main(string[] args)
        {
            // mdspec2docx *.md csharp.g4 template.docx -o spec.docx
            var    ifiles    = new List <string>();
            var    ofiles    = new List <string>();
            string argserror = "";

            for (int i = 0; i < args.Length; i++)
            {
                var arg = args[i];
                if (arg.StartsWith("-"))
                {
                    if (arg == "-o" && i < args.Length - 1)
                    {
                        i++; ofiles.Add(args[i]);
                    }
                    else
                    {
                        argserror += $"Unrecognized '{arg}'\n";
                    }
                }
                else if (!arg.Contains("*") && !arg.Contains("?"))
                {
                    if (!File.Exists(arg))
                    {
                        Console.Error.WriteLine($"Not found - {arg}"); return(1);
                    }
                    ifiles.Add(arg);
                }
                else
                {
                    // Windows command-shell doesn't do globbing, so we have to do it ourselves
                    string dir = Path.GetDirectoryName(arg), filename = Path.GetFileName(arg);
                    if (dir.Contains("*") || dir.Contains("?"))
                    {
                        Console.Error.WriteLine("Can't match wildcard directory names");
                        return(1);
                    }
                    if (dir == "")
                    {
                        dir = Directory.GetCurrentDirectory();
                    }

                    if (!Directory.Exists(dir))
                    {
                        Console.Error.WriteLine($"Not found - \"{dir}\""); return(1);
                    }
                    var fns2 = Directory.GetFiles(dir, filename);
                    if (fns2.Length == 0)
                    {
                        Console.Error.WriteLine($"Not found - \"{arg}\""); return(1);
                    }
                    ifiles.AddRange(fns2);
                }
            }

            var    imdfiles = new List <string>();
            string ireadmefile = null, iantlrfile = null, idocxfile = null, odocfile = null;

            foreach (var ifile in ifiles)
            {
                var name = Path.GetFileName(ifile);
                var ext  = Path.GetExtension(ifile).ToLower();
                if (ext == ".g4")
                {
                    if (iantlrfile != null)
                    {
                        argserror += "Multiple input .g4 files\n";
                    }
                    iantlrfile = ifile;
                }
                else if (ext == ".docx")
                {
                    if (idocxfile != null)
                    {
                        argserror += "Multiple input .docx files\n";
                    }
                    idocxfile = ifile;
                }
                else if (ext != ".md")
                {
                    argserror += $"Not .g4 or .docx or .md '{ifile}'\n"; continue;
                }
                else if (String.Equals(name, "readme.md", StringComparison.InvariantCultureIgnoreCase))
                {
                    if (ireadmefile != null)
                    {
                        argserror += "Multiple readme.md files\n";
                    }
                    ireadmefile = ifile;
                }
                else
                {
                    imdfiles.Add(ifile);
                }
            }
            foreach (var ofile in ofiles)
            {
                var ext = Path.GetExtension(ofile).ToLower();
                if (ext == ".docx")
                {
                    if (odocfile != null)
                    {
                        argserror += "Multiple output docx files\n";
                    }
                    odocfile = ofile;
                }
                else
                {
                    argserror += $"Unknown output file extension: {ext}\n";
                }
            }

            if (odocfile == null)
            {
                argserror += "No output .docx file specified\n";
            }

            if (ireadmefile == null && ifiles.Count == 0)
            {
                argserror += "No .md files supplied\n";
            }

            if (idocxfile == null)
            {
                argserror += "No template.docx supplied\n";
            }

            if (argserror != "")
            {
                Console.Error.WriteLine(argserror);
                Console.Error.WriteLine("mdspec2docx *.md grammar.g4 template.docx -o spec.docx");
                Console.Error.WriteLine();
                Console.Error.WriteLine("Turns the markdown files into a word document based on the template.");
                Console.Error.WriteLine("If readme.md and other files are given, then readme is used solely to");
                Console.Error.WriteLine("   sort the docx based on its list of `* [Link](subfile.md)`.");
                Console.Error.WriteLine("If a .g4 is given, it verifies 1:1 correspondence with ```antlr blocks.");
                Console.Error.WriteLine("The -td temp directory will enable incremental (faster) runs in future.");
                return(1);
            }

            Console.WriteLine("mdspec2docx");
            Console.WriteLine("Reading markdown files");

            // Read input file. If it contains a load of linked filenames, then read them instead.
            List <string> ifiles_in_order = new List <string>();
            List <Tuple <int, string, string, SourceLocation> > urls = null;

            if (ireadmefile == null)
            {
                ifiles_in_order.AddRange(ifiles);
            }
            else if (ireadmefile != null && ifiles.Count == 0)
            {
                ifiles_in_order.Add(ireadmefile);
            }
            else
            {
                var readme = FSharp.Markdown.Markdown.Parse(File.ReadAllText(ireadmefile));
                urls = new List <Tuple <int, string, string, SourceLocation> >();
                // is there a nicer way to get the URLs of all depth-1 and depth-2 URLs in this list? ...
                foreach (var list in readme.Paragraphs.OfType <FSharp.Markdown.MarkdownParagraph.ListBlock>())
                {
                    var pp = new List <Tuple <int, FSharp.Markdown.MarkdownParagraph> >();
                    foreach (var pars in list.items)
                    {
                        foreach (var par in pars)
                        {
                            pp.Add(Tuple.Create(1, par));
                            var sublist = par as FSharp.Markdown.MarkdownParagraph.ListBlock;
                            if (sublist != null)
                            {
                                pp.AddRange(from subpars in sublist.items
                                            from subpar in subpars
                                            select Tuple.Create(2, subpar));
                            }
                        }
                    }
                    foreach (var tpp in pp)
                    {
                        var level   = tpp.Item1;
                        var spanpar = tpp.Item2 as FSharp.Markdown.MarkdownParagraph.Span;
                        if (spanpar == null)
                        {
                            continue;
                        }

                        var links = spanpar.body.OfType <FSharp.Markdown.MarkdownSpan.DirectLink>();
                        urls.AddRange(from link in links
                                      let title                     = string.Join("", link.body.OfType <FSharp.Markdown.MarkdownSpan.Literal>().Select(l => l.text))
                                                            let url = link.link
                                                                      where url.ToLower().EndsWith(".md") || url.ToLower().Contains(".md#")
                                                                      let loc = new SourceLocation(ireadmefile, null, list, link)
                                                                                select Tuple.Create(level, title, url, loc));
                    }
                }
                var filelinks = (from turl in urls
                                 let url = turl.Item3
                                           let i = url.IndexOf('#')
                                                   let url2 = (i == -1 ? url : url.Substring(0, i))
                                                              select url2).ToList().Distinct();
                foreach (var link in filelinks)
                {
                    var ifile = ifiles.FirstOrDefault(f => Path.GetFileName(f) == link);
                    if (ifile == null)
                    {
                        Console.Error.WriteLine($"readme.md link '{link}' wasn't one of the files passed on the command line"); return(1);
                    }
                    ifiles_in_order.Add(ifile);
                }
            }
            var md = MarkdownSpec.ReadFiles(ifiles_in_order, urls);


            // Now md.Gramar contains the grammar as extracted out of the *.md files, and moreover has
            // correct references to within the spec. We'll check that it has the same productions as
            // in the corresponding ANTLR file
            if (iantlrfile != null)
            {
                Console.WriteLine($"Reading {Path.GetFileName(iantlrfile)}");
                var grammar = Antlr.ReadFile(iantlrfile);
                foreach (var diff in CompareGrammars(grammar, md.Grammar))
                {
                    if (diff.authority == null)
                    {
                        Report("MD21", "error", $"markdown has superfluous production '{diff.productionName}'", "mdspec2docx");
                    }
                    else if (diff.copy == null)
                    {
                        Report("MD22", "error", $"markdown lacks production '{diff.productionName}'", "mdspec2docx");
                    }
                    else
                    {
                        Report("MD23", "error", $"production '{diff.productionName}' differs between markdown and antlr.g4", "mdspec2docx");
                        Report("MD23b", "error", "antlr.g4 says " + diff.authority.Replace("\r", "\\r").Replace("\n", "\\n"), "mdspec2docx");
                        Report("MD23c", "error", "markdown says " + diff.copy.Replace("\r", "\\r").Replace("\n", "\\n"), "mdspec2docx");
                    }
                }
                foreach (var p in grammar.Productions)
                {
                    p.Link     = md.Grammar.Productions.FirstOrDefault(mdp => mdp?.Name == p.Name)?.Link;
                    p.LinkName = md.Grammar.Productions.FirstOrDefault(mdp => mdp?.Name == p.Name)?.LinkName;
                }
            }

            // Generate the Specification.docx file
            if (odocfile != null)
            {
                var odocfile2 = odocfile;
                if (odocfile2 != odocfile)
                {
                    Report("MD26", "error", $"File '{odocfile}' was in use", "mdspec2docx");
                }

                Console.WriteLine($"Writing '{Path.GetFileName(odocfile2)}'");
                try
                {
                    MarkdownSpecConverter.ConvertToWord(md, idocxfile, odocfile2);
                }
                catch (Exception ex)
                {
                    Report("MD27", "error", ex.Message, "mdspec2docx");
                    return(1);
                }
                if (odocfile2 != odocfile)
                {
                    return(1);
                }
            }
            return(0);
        }
Example #24
0
 public override void ReportError(Antlr.Runtime.RecognitionException e)
 {
     throw new ParserException(e.Message, e);
 }
        private static string ToOriginalString(Antlr.Runtime.CommonTokenStream tokenStream, int start, int end)
        {
            StringBuilder buf = new StringBuilder();
            for (int i = start; i >= 0 && i <= end && i < tokenStream.Count; i++)
            {
                string s = tokenStream.Get(i).Text;
                if (tokenStream.Get(i).Type == ANTLRParser.BLOCK)
                    s = "(";

                buf.Append(s);
            }

            return buf.ToString();
        }
Example #26
0
        IEnumerable <OpenXmlCompositeElement> Paragraph2Paragraphs(MarkdownParagraph md)
        {
            reporter.CurrentParagraph = md;
            if (md.IsHeading)
            {
                var mdh   = md as MarkdownParagraph.Heading;
                var level = mdh.size;
                var spans = mdh.body;
                var sr    = sections[new SectionRef(mdh, filename).Url];
                reporter.CurrentSection = sr;
                var properties = new List <OpenXmlElement>
                {
                    new ParagraphStyleId {
                        Val = $"Heading{level}"
                    }
                };
                if (sr.Number is null)
                {
                    properties.Add(new NumberingProperties(new NumberingLevelReference {
                        Val = 0
                    }, new NumberingId {
                        Val = 0
                    }));
                }
                var props = new ParagraphProperties(properties);
                var p     = new Paragraph {
                    ParagraphProperties = props
                };
                context.MaxBookmarkId.Value += 1;
                p.AppendChild(new BookmarkStart {
                    Name = sr.BookmarkName, Id = context.MaxBookmarkId.Value.ToString()
                });
                p.Append(Span2Elements(MarkdownSpan.NewLiteral(sr.TitleWithoutNumber, FSharpOption <MarkdownRange> .None)));
                p.AppendChild(new BookmarkEnd {
                    Id = context.MaxBookmarkId.Value.ToString()
                });
                yield return(p);

                var    i = sr.Url.IndexOf("#");
                string currentSection = $"{sr.Url.Substring(0, i)} {new string('#', level)} {sr.Title} [{sr.Number}]";
                reporter.Log(currentSection);
                yield break;
            }

            else if (md.IsParagraph)
            {
                var mdp   = md as MarkdownParagraph.Paragraph;
                var spans = mdp.body;
                yield return(new Paragraph(Spans2Elements(spans)));

                yield break;
            }

            else if (md.IsQuotedBlock)
            {
                var mdq = md as MarkdownParagraph.QuotedBlock;
                // TODO: Actually make this a block quote.
                // See https://github.com/ECMA-TC49-TG2/conversion-to-markdown/issues/123
                foreach (var paragraph in mdq.paragraphs.SelectMany(Paragraph2Paragraphs))
                {
                    yield return(paragraph);
                }
                yield break;
            }

            else if (md.IsListBlock)
            {
                var mdl  = md as MarkdownParagraph.ListBlock;
                var flat = FlattenList(mdl);

                // Let's figure out what kind of list it is - ordered or unordered? nested?
                var format0 = new[] { "1", "1", "1", "1" };
                foreach (var item in flat)
                {
                    format0[item.Level] = (item.IsBulletOrdered ? "1" : "o");
                }

                var format = string.Join("", format0);

                var numberingPart = wordDocument.MainDocumentPart.NumberingDefinitionsPart ?? wordDocument.MainDocumentPart.AddNewPart <NumberingDefinitionsPart>("NumberingDefinitionsPart001");
                if (numberingPart.Numbering == null)
                {
                    numberingPart.Numbering = new Numbering();
                }

                Func <int, bool, Level> createLevel;
                createLevel = (level, isOrdered) =>
                {
                    var numformat = NumberFormatValues.Bullet;
                    var levelText = new[] { "·", "o", "·", "o" }[level];
                    if (isOrdered && level == 0)
                    {
                        numformat = NumberFormatValues.Decimal; levelText = "%1.";
                    }
                    if (isOrdered && level == 1)
                    {
                        numformat = NumberFormatValues.LowerLetter; levelText = "%2.";
                    }
                    if (isOrdered && level == 2)
                    {
                        numformat = NumberFormatValues.LowerRoman; levelText = "%3.";
                    }
                    if (isOrdered && level == 3)
                    {
                        numformat = NumberFormatValues.LowerRoman; levelText = "%4.";
                    }
                    var r = new Level {
                        LevelIndex = level
                    };
                    r.Append(new StartNumberingValue {
                        Val = 1
                    });
                    r.Append(new NumberingFormat {
                        Val = numformat
                    });
                    r.Append(new LevelText {
                        Val = levelText
                    });
                    r.Append(new ParagraphProperties(new Indentation {
                        Left = (540 + 360 * level).ToString(), Hanging = "360"
                    }));
                    if (levelText == "·")
                    {
                        r.Append(new NumberingSymbolRunProperties(new RunFonts {
                            Hint = FontTypeHintValues.Default, Ascii = "Symbol", HighAnsi = "Symbol", EastAsia = "Times new Roman", ComplexScript = "Times new Roman"
                        }));
                    }

                    if (levelText == "o")
                    {
                        r.Append(new NumberingSymbolRunProperties(new RunFonts {
                            Hint = FontTypeHintValues.Default, Ascii = "Courier New", HighAnsi = "Courier New", ComplexScript = "Courier New"
                        }));
                    }

                    return(r);
                };
                var level0 = createLevel(0, format[0] == '1');
                var level1 = createLevel(1, format[1] == '1');
                var level2 = createLevel(2, format[2] == '1');
                var level3 = createLevel(3, format[3] == '1');

                var abstracts = numberingPart.Numbering.OfType <AbstractNum>().Select(an => an.AbstractNumberId.Value).ToList();
                var aid       = (abstracts.Count == 0 ? 1 : abstracts.Max() + 1);
                var aabstract = new AbstractNum(new MultiLevelType()
                {
                    Val = MultiLevelValues.Multilevel
                }, level0, level1, level2, level3)
                {
                    AbstractNumberId = aid
                };
                numberingPart.Numbering.InsertAt(aabstract, 0);

                var instances   = numberingPart.Numbering.OfType <NumberingInstance>().Select(ni => ni.NumberID.Value);
                var nid         = (instances.Count() == 0 ? 1 : instances.Max() + 1);
                var numInstance = new NumberingInstance(new AbstractNumId {
                    Val = aid
                })
                {
                    NumberID = nid
                };
                numberingPart.Numbering.AppendChild(numInstance);

                // We'll also figure out the indentation(for the benefit of those paragraphs that should be
                // indendent with the list but aren't numbered). I'm not sure what the indent comes from.
                // in the docx, each AbstractNum that I created has an indent for each of its levels,
                // defaulted at 900, 1260, 1620, ... but I can't see where in the above code that's created?
                Func <int, string> calcIndent = level => (540 + level * 360).ToString();

                foreach (var item in flat)
                {
                    var content = item.Paragraph;
                    if (content.IsParagraph || content.IsSpan)
                    {
                        var spans = (content.IsParagraph ? (content as MarkdownParagraph.Paragraph).body : (content as MarkdownParagraph.Span).body);
                        if (item.HasBullet)
                        {
                            yield return(new Paragraph(Spans2Elements(spans))
                            {
                                ParagraphProperties = new ParagraphProperties(new NumberingProperties(new ParagraphStyleId {
                                    Val = "ListParagraph"
                                }, new NumberingLevelReference {
                                    Val = item.Level
                                }, new NumberingId {
                                    Val = nid
                                }))
                            });
                        }
                        else
                        {
                            yield return(new Paragraph(Spans2Elements(spans))
                            {
                                ParagraphProperties = new ParagraphProperties(new Indentation {
                                    Left = calcIndent(item.Level)
                                })
                            });
                        }
                    }
                    else if (content.IsQuotedBlock || content.IsCodeBlock)
                    {
                        foreach (var p in Paragraph2Paragraphs(content))
                        {
                            var props = p.GetFirstChild <ParagraphProperties>();
                            if (props == null)
                            {
                                props = new ParagraphProperties();
                                p.InsertAt(props, 0);
                            }
                            var indent = props?.GetFirstChild <Indentation>();
                            if (indent == null)
                            {
                                indent = new Indentation();
                                props.Append(indent);
                            }
                            indent.Left = calcIndent(item.Level);
                            yield return(p);
                        }
                    }
                    else if (content.IsTableBlock)
                    {
                        foreach (var p in Paragraph2Paragraphs(content))
                        {
                            var table = p as Table;
                            if (table == null)
                            {
                                yield return(p);

                                continue;
                            }
                            var tprops  = table.GetFirstChild <TableProperties>();
                            var tindent = tprops?.GetFirstChild <TableIndentation>();
                            if (tindent == null)
                            {
                                throw new Exception("Ooops! Table is missing indentation");
                            }

                            tindent.Width = int.Parse(calcIndent(item.Level));
                            yield return(table);
                        }
                    }
                    else
                    {
                        reporter.Error("MD08", $"Unexpected item in list '{content.GetType().Name}'");
                    }
                }
            }

            else if (md.IsCodeBlock)
            {
                var mdc  = md as MarkdownParagraph.CodeBlock;
                var code = mdc.code;
                var lang = mdc.language;
                code = BugWorkaroundDecode(code);
                var runs        = new List <Run>();
                var onFirstLine = true;
                IEnumerable <ColorizedLine> lines;
                switch (lang)
                {
                case "csharp":
                case "c#":
                case "cs":
                    lines = Colorize.CSharp(code);
                    break;

                case "vb":
                case "vbnet":
                case "vb.net":
                    lines = Colorize.VB(code);
                    break;

                case "":
                case "console":
                case "xml":
                    lines = Colorize.PlainText(code);
                    break;

                case "ANTLR":
                case "antlr":
                    lines = Antlr.ColorizeAntlr(code);
                    break;

                default:
                    reporter.Error("MD09", $"unrecognized language {lang}");
                    lines = Colorize.PlainText(code);
                    break;
                }

                foreach (var line in lines)
                {
                    if (onFirstLine)
                    {
                        onFirstLine = false;
                    }
                    else
                    {
                        runs.Add(new Run(new Break()));
                    }

                    foreach (var word in line.Words)
                    {
                        var run   = new Run();
                        var props = new RunProperties();
                        if (word.Red != 0 || word.Green != 0 || word.Blue != 0)
                        {
                            props.Append(new Color {
                                Val = $"{word.Red:X2}{word.Green:X2}{word.Blue:X2}"
                            });
                        }

                        if (word.IsItalic)
                        {
                            props.Append(new Italic());
                        }

                        if (props.HasChildren)
                        {
                            run.Append(props);
                        }

                        run.Append(new Text(word.Text)
                        {
                            Space = SpaceProcessingModeValues.Preserve
                        });
                        runs.Add(run);
                    }
                }
                if (lang == "antlr")
                {
                    var p = new Paragraph()
                    {
                        ParagraphProperties = new ParagraphProperties(new ParagraphStyleId {
                            Val = "Grammar"
                        })
                    };
                    var prodref = productions.Single(prod => prod.Code == code);
                    context.MaxBookmarkId.Value += 1;
                    p.AppendChild(new BookmarkStart {
                        Name = prodref.BookmarkName, Id = context.MaxBookmarkId.Value.ToString()
                    });
                    p.Append(runs);
                    p.AppendChild(new BookmarkEnd {
                        Id = context.MaxBookmarkId.Value.ToString()
                    });
                    yield return(p);
                }
                else
                {
                    var p = new Paragraph()
                    {
                        ParagraphProperties = new ParagraphProperties(new ParagraphStyleId {
                            Val = "Code"
                        })
                    };
                    p.Append(runs);
                    yield return(p);
                }
            }

            else if (md.IsTableBlock)
            {
                var mdt    = md as MarkdownParagraph.TableBlock;
                var header = mdt.headers.Option();
                var align  = mdt.alignments;
                var rows   = mdt.rows;
                var table  = new Table();
                if (header == null)
                {
                    reporter.Error("MD10", "Github requires all tables to have header rows");
                }

                if (!header.Any(cell => cell.Length > 0))
                {
                    header = null; // even if Github requires an empty header, we can at least cull it from Docx
                }

                var tstyle = new TableStyle {
                    Val = "TableGrid"
                };
                var tindent = new TableIndentation {
                    Width = 360, Type = TableWidthUnitValues.Dxa
                };
                var tborders = new TableBorders();
                tborders.TopBorder = new TopBorder {
                    Val = BorderValues.Single
                };
                tborders.BottomBorder = new BottomBorder {
                    Val = BorderValues.Single
                };
                tborders.LeftBorder = new LeftBorder {
                    Val = BorderValues.Single
                };
                tborders.RightBorder = new RightBorder {
                    Val = BorderValues.Single
                };
                tborders.InsideHorizontalBorder = new InsideHorizontalBorder {
                    Val = BorderValues.Single
                };
                tborders.InsideVerticalBorder = new InsideVerticalBorder {
                    Val = BorderValues.Single
                };
                var tcellmar = new TableCellMarginDefault();
                tcellmar.Append();
                table.Append(new TableProperties(tstyle, tindent, tborders));
                var ncols = align.Length;
                for (int irow = -1; irow < rows.Length; irow++)
                {
                    if (irow == -1 && header == null)
                    {
                        continue;
                    }

                    var mdrow = (irow == -1 ? header : rows[irow]);
                    var row   = new TableRow();
                    for (int icol = 0; icol < Math.Min(ncols, mdrow.Length); icol++)
                    {
                        var mdcell = mdrow[icol];
                        var cell   = new TableCell();
                        var pars   = Paragraphs2Paragraphs(mdcell).ToList();
                        for (int ip = 0; ip < pars.Count; ip++)
                        {
                            var p = pars[ip] as Paragraph;
                            if (p == null)
                            {
                                cell.Append(pars[ip]);
                                continue;
                            }
                            var props = new ParagraphProperties(new ParagraphStyleId {
                                Val = "TableCellNormal"
                            });
                            if (align[icol].IsAlignCenter)
                            {
                                props.Append(new Justification {
                                    Val = JustificationValues.Center
                                });
                            }

                            if (align[icol].IsAlignRight)
                            {
                                props.Append(new Justification {
                                    Val = JustificationValues.Right
                                });
                            }

                            p.InsertAt(props, 0);
                            cell.Append(pars[ip]);
                        }
                        if (pars.Count == 0)
                        {
                            cell.Append(new Paragraph(new ParagraphProperties(new SpacingBetweenLines {
                                After = "0"
                            }), new Run(new Text(""))));
                        }

                        row.Append(cell);
                    }
                    table.Append(row);
                }
                yield return(new Paragraph(new Run(new Text("")))
                {
                    ParagraphProperties = new ParagraphProperties(new ParagraphStyleId {
                        Val = "TableLineBefore"
                    })
                });

                yield return(table);

                yield return(new Paragraph(new Run(new Text("")))
                {
                    ParagraphProperties = new ParagraphProperties(new ParagraphStyleId {
                        Val = "TableLineAfter"
                    })
                });
            }
            else
            {
                reporter.Error("MD11", $"Unrecognized markdown element {md.GetType().Name}");
                yield return(new Paragraph(new Run(new Text($"[{md.GetType().Name}]"))));
            }
        }
Example #27
0
 public GrammarASTAdaptor(Antlr.Runtime.ICharStream input)
 {
     this.input = input;
 }
Example #28
0
 public override UTCTimeValue CreateUTCTimeValueValue(Antlr.Runtime.Tree.ITree antlrNode, Module module, Asn1Type type)
 {
     return new SCCUTCTimeValue(antlrNode, module, type);
 }
Example #29
0
 public override object ErrorNode(Antlr.Runtime.ITokenStream input, IToken start, IToken stop,
                         Antlr.Runtime.RecognitionException e)
 {
     return new GrammarASTErrorNode(input, start, stop, e);
 }
Example #30
0
 public override UTCTimeValue CreateUTCTimeValueValue(UTCTimeValue o, Antlr.Runtime.Tree.ITree antlr)
 {
     return new SCCUTCTimeValue(o, antlr);
 }
        public override object Create(Antlr.Runtime.IToken token)
        {
            if (token == null)
            {
                return new CommonTree();
            }

            switch (token.Type)
            {
                case LinqToQuerystringLexer.TOP:
                    return new TopNode(inputType, token, this);
                case LinqToQuerystringLexer.SKIP:
                    return new SkipNode(inputType, token, this);
                case LinqToQuerystringLexer.ORDERBY:
                    return new OrderByNode(inputType, token, this);
                case LinqToQuerystringLexer.FILTER:
                    return new FilterNode(inputType, token, this);
                case LinqToQuerystringLexer.SELECT:
                    return new SelectNode(inputType, token, this);
                case LinqToQuerystringLexer.INLINECOUNT:
                    return new InlineCountNode(inputType, token, this);
                case LinqToQuerystringLexer.EXPAND:
                    return new ExpandNode(inputType, token, this);
                case LinqToQuerystringLexer.NOT:
                    return new NotNode(inputType, token, this);
                case LinqToQuerystringLexer.AND:
                    return new AndNode(inputType, token, this);
                case LinqToQuerystringLexer.OR:
                    return new OrNode(inputType, token, this);
                case LinqToQuerystringLexer.EQUALS:
                    return new EqualsNode(inputType, token, this);
                case LinqToQuerystringLexer.NOTEQUALS:
                    return new NotEqualsNode(inputType, token, this);
                case LinqToQuerystringLexer.GREATERTHAN:
                    return new GreaterThanNode(inputType, token, this);
                case LinqToQuerystringLexer.GREATERTHANOREQUAL:
                    return new GreaterThanOrEqualNode(inputType, token, this);
                case LinqToQuerystringLexer.LESSTHAN:
                    return new LessThanNode(inputType, token, this);
                case LinqToQuerystringLexer.LESSTHANOREQUAL:
                    return new LessThanOrEqualNode(inputType, token, this);
                case LinqToQuerystringLexer.STARTSWITH:
                    return new StartsWithNode(inputType, token, this);
                case LinqToQuerystringLexer.ENDSWITH:
                    return new EndsWithNode(inputType, token, this);
                case LinqToQuerystringLexer.SUBSTRINGOF:
                    return new SubstringOfNode(inputType, token, this);
                case LinqToQuerystringLexer.TOLOWER:
                    return new ToLowerNode(inputType, token, this);
                case LinqToQuerystringLexer.TOUPPER:
                    return new ToUpperNode(inputType, token, this);
                case LinqToQuerystringLexer.YEAR:
                    return new YearNode(inputType, token, this);
                case LinqToQuerystringLexer.YEARS:
                    return new YearsNode(inputType, token, this);
                case LinqToQuerystringLexer.MONTH:
                    return new MonthNode(inputType, token, this);
                case LinqToQuerystringLexer.DAY:
                    return new DayNode(inputType, token, this);
                case LinqToQuerystringLexer.DAYS:
                    return new DaysNode(inputType, token, this);
                case LinqToQuerystringLexer.HOUR:
                    return new HourNode(inputType, token, this);
                case LinqToQuerystringLexer.HOURS:
                    return new HoursNode(inputType, token, this);
                case LinqToQuerystringLexer.MINUTE:
                    return new MinuteNode(inputType, token, this);
                case LinqToQuerystringLexer.MINUTES:
                    return new MinutesNode(inputType, token, this);
                case LinqToQuerystringLexer.SECOND:
                    return new SecondNode(inputType, token, this);
                case LinqToQuerystringLexer.SECONDS:
                    return new SecondsNode(inputType, token, this);
                case LinqToQuerystringLexer.ANY:
                    return new AnyNode(inputType, token, this);
                case LinqToQuerystringLexer.ALL:
                    return new AllNode(inputType, token, this);
                case LinqToQuerystringLexer.COUNT:
                    return new CountNode(inputType, token, this);
                case LinqToQuerystringLexer.AVERAGE:
                    return new AverageNode(inputType, token, this);
                case LinqToQuerystringLexer.MAX:
                    return new MaxNode(inputType, token, this);
                case LinqToQuerystringLexer.MIN:
                    return new MinNode(inputType, token, this);
                case LinqToQuerystringLexer.SUM:
                    return new SumNode(inputType, token, this);
                case LinqToQuerystringLexer.ALIAS:
                    return new AliasNode(inputType, token, this);
                case LinqToQuerystringLexer.DYNAMICIDENTIFIER:
                    return new DynamicIdentifierNode(inputType, token, this);
                case LinqToQuerystringLexer.IDENTIFIER:
                    if (forceDynamicProperties)
                    {
                        return new DynamicIdentifierNode(inputType, token, this);
                    }
                    return new IdentifierNode(inputType, token, this);
                case LinqToQuerystringLexer.STRING:
                    return new StringNode(inputType, token, this);
                case LinqToQuerystringLexer.BOOL:
                    return new BoolNode(inputType, token, this);
                case LinqToQuerystringLexer.INT:
                    return new IntNode(inputType, token, this);
                case LinqToQuerystringLexer.DATETIME:
                    return new DateTimeNode(inputType, token, this);
                case LinqToQuerystringLexer.DOUBLE:
                    return new DoubleNode(inputType, token, this);
                case LinqToQuerystringLexer.SINGLE:
                    return new SingleNode(inputType, token, this);
                case LinqToQuerystringLexer.DECIMAL:
                    return new DecimalNode(inputType, token, this);
                case LinqToQuerystringLexer.LONG:
                    return new LongNode(inputType, token, this);
                case LinqToQuerystringLexer.BYTE:
                    return new ByteNode(inputType, token, this);
                case LinqToQuerystringLexer.GUID:
                    return new GuidNode(inputType, token, this);
                case LinqToQuerystringLexer.DESC:
                    return new DescNode(inputType, token, this);
                case LinqToQuerystringLexer.ASC:
                    return new AscNode(inputType, token, this);
                case LinqToQuerystringLexer.NULL:
                    return new NullNode(inputType, token, this);
                case LinqToQuerystringLexer.IGNORED:
                    return new IgnoredNode(inputType, token, this);
            }

            return null;
        }
Example #32
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);
                    }
                }
            }
        }
    }
Example #33
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);
                        }
                    }
                }
            }
        }
Example #34
0
 public override OctetStringValue CreateOctetStringValue(OctetStringValue o, Antlr.Runtime.Tree.ITree antlr)
 {
     return new SCCOctetStringValue(o, antlr);
 }
 partial void CreateTreeAdaptor(ref Antlr.Runtime.Tree.ITreeAdaptor adaptor)
 {
     adaptor = new StringTreeAdaptor();
 }
Example #36
0
 public override RealValue CreateRealValue(RealValue o, Antlr.Runtime.Tree.ITree antlr)
 {
     return new SCCRealValue(o, antlr);
 }
Example #37
0
        static IEnumerable <ProductionDifference> CompareGrammars(EbnfGrammar authority, EbnfGrammar copy)
        {
            Func <EbnfGrammar, Dictionary <string, Production> > ToDictionary;

            ToDictionary = g =>
            {
                var d = new Dictionary <string, Production>();
                foreach (var pp in g.Productions)
                {
                    if (pp.Name != null)
                    {
                        d[pp.Name] = pp;
                    }
                }

                return(d);
            };
            var dauthority = ToDictionary(authority);
            var dcopy      = ToDictionary(copy);

            foreach (var p in dauthority.Keys)
            {
                if (!dcopy.ContainsKey(p))
                {
                    continue;
                }

                Production pauthority0 = dauthority[p], pcopy0 = dcopy[p];
                string     pauthority = Antlr.ToString(pauthority0), pcopy = Antlr.ToString(pcopy0);
                if (pauthority == pcopy)
                {
                    continue;
                }

                yield return(new ProductionDifference {
                    productionName = p, authority = pauthority, copy = pcopy
                });
            }

            foreach (var p in dauthority.Keys)
            {
                if (p == "start")
                {
                    continue;
                }

                if (!dcopy.ContainsKey(p))
                {
                    yield return(new ProductionDifference {
                        productionName = p, authority = "<defined>", copy = null
                    });
                }
            }
            foreach (var p in dcopy.Keys)
            {
                if (p == "start")
                {
                    continue;
                }

                if (!dauthority.ContainsKey(p))
                {
                    yield return(new ProductionDifference {
                        productionName = p, authority = null, copy = "<defined>"
                    });
                }
            }
        }
Example #38
0
 public override SequenceOrSetValue CreateSequenceOrSetValue(Antlr.Runtime.Tree.ITree antlrNode, Module module, Asn1Type type)
 {
     return new SCCSequenceOrSetValue(antlrNode, module, type);
 }