Exemple #1
0
            internal void CreateRules(SourceJavadocToXmldocGrammar grammar)
            {
                AllBlockTerms.Rule = AuthorDeclaration
                                     | ApiSinceDeclaration
                                     | DeprecatedDeclaration
                                     | DeprecatedSinceDeclaration
                                     | ExceptionDeclaration
                                     | ParamDeclaration
                                     | ReturnDeclaration
                                     | SeeDeclaration
                                     | SerialDataDeclaration
                                     | SerialFieldDeclaration
                                     | SinceDeclaration
                                     | ThrowsDeclaration
                                     | UnknownTagDeclaration
                                     | VersionDeclaration
                ;
                BlockValue.Rule = grammar.HtmlTerms.ParsedCharacterData
                                  | grammar.HtmlTerms.InlineDeclaration
                ;
                BlockValues.MakePlusRule(grammar, BlockValue);

                AuthorDeclaration.Rule = "@author" + BlockValues;
                AuthorDeclaration.AstConfig.NodeCreator = (context, parseNode) => {
                    if (!grammar.ShouldImport(ImportJavadoc.AuthorTag))
                    {
                        return;
                    }
                    // Ignore; not sure how best to convert to Xmldoc
                    FinishParse(context, parseNode);
                };

                ApiSinceDeclaration.Rule = "@apiSince" + BlockValues;
                ApiSinceDeclaration.AstConfig.NodeCreator = (context, parseNode) => {
                    if (!grammar.ShouldImport(ImportJavadoc.SinceTag))
                    {
                        return;
                    }
                    var p = new XElement("para", "Added in API level ", AstNodeToXmlContent(parseNode.ChildNodes [1]), ".");
                    FinishParse(context, parseNode).Remarks.Add(p);
                    parseNode.AstNode = p;
                };

                DeprecatedDeclaration.Rule = "@deprecated" + BlockValues;
                DeprecatedDeclaration.AstConfig.NodeCreator = (context, parseNode) => {
                    if (!grammar.ShouldImport(ImportJavadoc.DeprecatedTag))
                    {
                        return;
                    }
                    var p = new XElement("para", "This member is deprecated. ", AstNodeToXmlContent(parseNode.ChildNodes [1]));
                    FinishParse(context, parseNode).Remarks.Add(p);
                    parseNode.AstNode = p;
                };

                DeprecatedSinceDeclaration.Rule = "@deprecatedSince" + BlockValues;
                DeprecatedSinceDeclaration.AstConfig.NodeCreator = (context, parseNode) => {
                    if (!grammar.ShouldImport(ImportJavadoc.DeprecatedTag))
                    {
                        return;
                    }
                    var p = new XElement("para", "This member was deprecated in API level ", AstNodeToXmlContent(parseNode.ChildNodes [1]), ".");
                    FinishParse(context, parseNode).Remarks.Add(p);
                    parseNode.AstNode = p;
                };

                var nonSpaceTerm = new RegexBasedTerminal("[^ ]", "[^ ]+")
                {
                    AstConfig = new AstNodeConfig {
                        NodeCreator = (context, parseNode) => parseNode.AstNode = parseNode.Token.Value,
                    },
                };

                ExceptionDeclaration.Rule = "@exception" + nonSpaceTerm + BlockValues;
                ExceptionDeclaration.AstConfig.NodeCreator = (context, parseNode) => {
                    if (!grammar.ShouldImport(ImportJavadoc.ExceptionTag))
                    {
                        return;
                    }

                    /* TODO: convert `nonSpaceTerm` into a proper CREF
                     * var e = new XElement ("exception",
                     *              new XAttribute ("cref", string.Join ("", AstNodeToXmlContent (parseNode.ChildNodes [1]))),
                     *              AstNodeToXmlContent (parseNode.ChildNodes [2]));
                     * FinishParse (context, parseNode).Exceptions.Add (e);
                     * parseNode.AstNode   = e;
                     */
                    FinishParse(context, parseNode);
                };

                ParamDeclaration.Rule = "@param" + nonSpaceTerm + BlockValues;
                ParamDeclaration.AstConfig.NodeCreator = (context, parseNode) => {
                    if (!grammar.ShouldImport(ImportJavadoc.ParamTag))
                    {
                        return;
                    }
                    var p = new XElement("param",
                                         new XAttribute("name", string.Join("", AstNodeToXmlContent(parseNode.ChildNodes [1]))),
                                         AstNodeToXmlContent(parseNode.ChildNodes [2]));
                    FinishParse(context, parseNode).Parameters.Add(p);
                    parseNode.AstNode = p;
                };

                ReturnDeclaration.Rule = "@return" + BlockValues;
                ReturnDeclaration.AstConfig.NodeCreator = (context, parseNode) => {
                    if (!grammar.ShouldImport(ImportJavadoc.ReturnTag))
                    {
                        return;
                    }
                    // When encountering multiple @return keys in a line, append subsequent @return key content to the original <returns> element.
                    var jdi = FinishParse(context, parseNode);
                    if (jdi.Returns.Count == 0)
                    {
                        var r = new XElement("returns",
                                             AstNodeToXmlContent(parseNode.ChildNodes [1]));
                        FinishParse(context, parseNode).Returns.Add(r);
                        parseNode.AstNode = r;
                    }
                    else
                    {
                        var r = jdi.Returns.First() as XElement;
                        if (r != null)
                        {
                            r.Add(" ", AstNodeToXmlContent(parseNode.ChildNodes [1]));
                            parseNode.AstNode = r;
                        }
                    }
                };

                SeeDeclaration.Rule = "@see" + BlockValues;
                SeeDeclaration.AstConfig.NodeCreator = (context, parseNode) => {
                    if (!grammar.ShouldImport(ImportJavadoc.SeeTag))
                    {
                        return;
                    }

                    /* TODO: @see supports multiple forms; see: https://docs.oracle.com/javase/7/docs/technotes/tools/windows/javadoc.html#see
                     * // Also need to convert to appropriate CREF value, ignore for now
                     * var e = new XElement ("seealso",
                     *              new XAttribute ("cref", string.Join ("", AstNodeToXmlContent (parseNode.ChildNodes [1]))));
                     * FinishParse (context, parseNode).Extra.Add (e);
                     * parseNode.AstNode   = e;
                     */
                    FinishParse(context, parseNode);
                };

                SinceDeclaration.Rule = "@since" + BlockValues;
                SinceDeclaration.AstConfig.NodeCreator = (context, parseNode) => {
                    if (!grammar.ShouldImport(ImportJavadoc.SinceTag))
                    {
                        return;
                    }
                    var p = new XElement("para", "Added in ", AstNodeToXmlContent(parseNode.ChildNodes [1]), ".");
                    FinishParse(context, parseNode).Remarks.Add(p);
                    parseNode.AstNode = p;
                };

                ThrowsDeclaration.Rule = "@throws" + nonSpaceTerm + BlockValues;
                ThrowsDeclaration.AstConfig.NodeCreator = (context, parseNode) => {
                    if (!grammar.ShouldImport(ImportJavadoc.ExceptionTag))
                    {
                        return;
                    }

                    /* TODO: convert `nonSpaceTerm` into a proper CREF
                     * var e = new XElement ("exception",
                     *              new XAttribute ("cref", string.Join ("", AstNodeToXmlContent (parseNode.ChildNodes [1]))),
                     *              AstNodeToXmlContent (parseNode.ChildNodes [2]));
                     * FinishParse (context, parseNode).Exceptions.Add (e);
                     * parseNode.AstNode   = e;
                     */
                    FinishParse(context, parseNode);
                };

                // Ignore serialization informatino
                SerialDeclaration.Rule = "@serial" + BlockValues;
                SerialDeclaration.AstConfig.NodeCreator = (context, parseNode) => {
                    if (!grammar.ShouldImport(ImportJavadoc.SerialTag))
                    {
                        return;
                    }
                    FinishParse(context, parseNode);
                };

                SerialDataDeclaration.Rule = "@serialData" + BlockValues;
                SerialDataDeclaration.AstConfig.NodeCreator = (context, parseNode) => {
                    if (!grammar.ShouldImport(ImportJavadoc.SerialTag))
                    {
                        return;
                    }
                    FinishParse(context, parseNode);
                };

                SerialFieldDeclaration.Rule = "@serialField" + BlockValues;
                SerialFieldDeclaration.AstConfig.NodeCreator = (context, parseNode) => {
                    if (!grammar.ShouldImport(ImportJavadoc.SerialTag))
                    {
                        return;
                    }
                    FinishParse(context, parseNode);
                };

                var unknownTagTerminal = new RegexBasedTerminal("@[unknown]", @"@\S+")
                {
                    Priority = TerminalPriority.Low,
                };

                unknownTagTerminal.AstConfig.NodeCreator = (context, parseNode) =>
                                                           parseNode.AstNode = parseNode.Token.Value.ToString();


                UnknownTagDeclaration.Rule = unknownTagTerminal + BlockValues;
                UnknownTagDeclaration.AstConfig.NodeCreator = (context, parseNode) => {
                    if (!grammar.ShouldImport(ImportJavadoc.Remarks))
                    {
                        return;
                    }
                    Console.WriteLine($"# Unsupported @block-tag value: {parseNode.ChildNodes [0].AstNode}");
                    FinishParse(context, parseNode);
                };

                // Ignore Version
                VersionDeclaration.Rule = "@version" + BlockValues;
                VersionDeclaration.AstConfig.NodeCreator = (context, parseNode) => {
                    if (!grammar.ShouldImport(ImportJavadoc.VersionTag))
                    {
                        return;
                    }
                    FinishParse(context, parseNode);
                };
            }
            internal void CreateRules(SourceJavadocToXmldocGrammar grammar)
            {
                AllHtmlTerms.Rule = TopLevelInlineDeclaration
                                    | PBlockDeclaration
                                    | PreBlockDeclaration
                ;

                var inlineDeclaration = new NonTerminal("<html inline decl>", ConcatChildNodes)
                {
                    Rule = ParsedCharacterData
                           | FontStyleDeclaration

                           /*
                            | PhraseDeclaration
                            | SpecialDeclaration
                            | FormCtrlDeclaration
                            */
                           | grammar.InlineTagsTerms.AllInlineTerms
                           | UnknownHtmlElementStart
                    ,
                };
                var inlineDeclarations = new NonTerminal("<html inline decl>*", ConcatChildNodes);

                inlineDeclarations.MakePlusRule(grammar, inlineDeclaration);

                InlineDeclaration.Rule = inlineDeclaration;
                InlineDeclarations.MakeStarRule(grammar, InlineDeclaration);

                TopLevelInlineDeclaration.Rule = inlineDeclarations;
                TopLevelInlineDeclaration.AstConfig.NodeCreator = (context, parseNode) => {
                    var remarks    = FinishParse(context, parseNode).Remarks;
                    var addRemarks = grammar.ShouldImport(ImportJavadoc.Remarks) ||
                                     (grammar.ShouldImport(ImportJavadoc.Summary) && remarks.Count == 0);
                    if (!addRemarks)
                    {
                        parseNode.AstNode = "";
                        return;
                    }
                    foreach (var p in GetParagraphs(parseNode.ChildNodes))
                    {
                        remarks.Add(p);
                    }
                    parseNode.AstNode = "";
                };

                var fontstyle_tt = CreateHtmlToCrefElement(grammar, "tt", "c", InlineDeclarations);
                var fontstyle_i  = CreateHtmlToCrefElement(grammar, "i", "i", InlineDeclarations);

                var preText = new PreBlockDeclarationBodyTerminal();

                PreBlockDeclaration.Rule = CreateStartElement("pre", grammar) + preText + CreateEndElement("pre", grammar);
                PreBlockDeclaration.AstConfig.NodeCreator = (context, parseNode) => {
                    if (!grammar.ShouldImport(ImportJavadoc.Remarks))
                    {
                        parseNode.AstNode = "";
                        return;
                    }
                    var c = new XElement("code",
                                         new XAttribute("lang", "text/java"),
                                         parseNode.ChildNodes [1].Token.Value);
                    FinishParse(context, parseNode).Remarks.Add(c);
                    parseNode.AstNode = c;
                };

                FontStyleDeclaration.Rule = fontstyle_tt | fontstyle_i;

                PBlockDeclaration.Rule =
                    CreateStartElement("p", grammar) + InlineDeclarations + CreateEndElement("p", grammar, optional: true)
                ;
                PBlockDeclaration.AstConfig.NodeCreator = (context, parseNode) => {
                    var remarks    = FinishParse(context, parseNode).Remarks;
                    var addRemarks = grammar.ShouldImport(ImportJavadoc.Remarks) ||
                                     (grammar.ShouldImport(ImportJavadoc.Summary) && remarks.Count == 0);
                    if (!addRemarks)
                    {
                        parseNode.AstNode = "";
                        return;
                    }
                    var p = new XElement("para",
                                         parseNode.ChildNodes
                                         .Select(c => AstNodeToXmlContent(c)));
                    FinishParse(context, parseNode).Remarks.Add(p);
                    parseNode.AstNode = p;
                };
            }
            internal void CreateRules(SourceJavadocToXmldocGrammar grammar)
            {
                AllInlineTerms.Rule = CodeDeclaration
                                      | DocRootDeclaration
                                      | InheritDocDeclaration
                                      | LinkDeclaration
                                      | LinkplainDeclaration
                                      | LiteralDeclaration
                                      | ValueDeclaration
                ;

                CodeDeclaration.Rule = grammar.ToTerm("{@code") + InlineValue + "}";
                CodeDeclaration.AstConfig.NodeCreator = (context, parseNode) => {
                    parseNode.AstNode = new XElement("c", parseNode.ChildNodes [1].AstNode.ToString().Trim());
                };

                DocRootDeclaration.Rule = grammar.ToTerm("{@docRoot}");
                DocRootDeclaration.AstConfig.NodeCreator = (context, parseNode) => {
                    parseNode.AstNode = new XText("[TODO: @docRoot]");
                };

                InheritDocDeclaration.Rule = grammar.ToTerm("{@inheritDoc}");
                InheritDocDeclaration.AstConfig.NodeCreator = (context, parseNode) => {
                    parseNode.AstNode = new XText("[TODO: @inheritDoc]");
                };

                LinkDeclaration.Rule = grammar.ToTerm("{@link") + InlineValue + "}";
                LinkDeclaration.AstConfig.NodeCreator = (context, parseNode) => {
                    // TODO: *everything*; {@link target label}, but target can contain spaces!
                    // Also need to convert to appropriate CREF value
                    var target = parseNode.ChildNodes [1].AstNode;
                    var x      = new XElement("c");
                    parseNode.AstNode = new XElement("c", new XElement("see", new XAttribute("cref", target)));
                };

                LinkplainDeclaration.Rule = grammar.ToTerm("{@linkplain") + InlineValue + "}";
                LinkplainDeclaration.AstConfig.NodeCreator = (context, parseNode) => {
                    // TODO: *everything*; {@link target label}, but target can contain spaces!
                    // Also need to convert to appropriate CREF value
                    var target = parseNode.ChildNodes [1].AstNode;
                    parseNode.AstNode = new XElement("see", new XAttribute("cref", target));
                };

                LiteralDeclaration.Rule = grammar.ToTerm("{@literal") + InlineValue + "}";
                LiteralDeclaration.AstConfig.NodeCreator = (context, parseNode) => {
                    var content = parseNode.ChildNodes [1].AstNode.ToString();
                    parseNode.AstNode = new XText(content);
                };

                ValueDeclaration.Rule = grammar.ToTerm("{@value}")
                                        | grammar.ToTerm("{@value") + InlineValue + "}";
                ValueDeclaration.AstConfig.NodeCreator = (context, parseNode) => {
                    if (parseNode.ChildNodes.Count > 1)
                    {
                        var field = parseNode.ChildNodes [1].AstNode.ToString();
                        parseNode.AstNode = new XText($"[TODO: @value for `{field}`]");
                    }
                    else
                    {
                        parseNode.AstNode = new XText("[TODO: @value]");
                    }
                };
            }