Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add :common subpackage without libdparse #54

Merged
merged 1 commit into from
May 20, 2021

Conversation

WebFreak001
Copy link
Member

adds ddoc.unhighlight and ddoc.types modules for more useful usage. Unhighlight can be used to emit embedded D code as-is, instead of parsing it and (potentially) causing issues as well as needing a
dependency on libdparse.

The ddoc.types module now contains the Comment struct that was in ddoc.comments and has a new parseUnexpanded and parse call, which does what parseComment has done before, but with unhighlight, without expanding, which is commonly used for example in D-Scanner to just check if a comment is ditto or contains some section with a given name.

With only minor modifications to D-Scanner just the libddoc:common subpackage can be used there, avoiding keeping libddoc libdparse up-to-date and synced.

Makes the root libdparse version any version up to <1.0.0, because only tokenization is used and updates should not be needed that often. Having the separate common package makes it less likely that things will break for packages not needing libdparse, other packages will manually specify versions that will make ddoc use older versions if necessary.

Comment on lines +39 to +97
static Comment parse(string text, string[string] macros, bool removeUnknown,
string delegate(string) highlightFn)
out(retVal)
{
assert(retVal.sections.length >= 2);
}
do
{
import std.algorithm : find;
import ddoc.macros : expand;

auto sections = splitSections(text);
string[string] sMacros = macros;
auto m = sections.find!(p => p.name == "Macros");
const e = sections.find!(p => p.name == "Escapes");
auto p = sections.find!(p => p.name == "Params");
if (m.length)
{
if (!doMapping(m[0]))
throw new DdocParseException("Unable to parse Key/Value pairs", m[0].content);
foreach (kv; m[0].mapping)
sMacros[kv[0]] = kv[1];
}
if (e.length)
{
assert(0, "Escapes not handled yet");
}
if (p.length)
{
if (!doMapping(p[0]))
throw new DdocParseException("Unable to parse Key/Value pairs", p[0].content);
foreach (ref kv; p[0].mapping)
kv[1] = expand(Lexer(highlightFn(kv[1])), sMacros, removeUnknown);
}

foreach (ref Section sec; sections)
{
if (sec.name != "Macros" && sec.name != "Escapes" && sec.name != "Params")
sec.content = expand(Lexer(highlightFn(sec.content)), sMacros, removeUnknown);
}
return Comment(sections);
}
}

private:
bool doMapping(ref Section s)
{
import ddoc.macros : KeyValuePair, parseKeyValuePair;

auto lex = Lexer(s.content);
KeyValuePair[] pairs;
if (parseKeyValuePair(lex, pairs))
{
foreach (idx, kv; pairs)
s.mapping ~= kv;
return true;
}
return false;
}
Copy link
Member Author

@WebFreak001 WebFreak001 May 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this code is pure copy-paste from ddoc.comments, but changing the legacy parameter and making it more flexible with a callback.

Comment on lines +6 to +7
* Copyright: © 2014 Economic Modeling Specialists, Intl.
* Authors: Brian Schott, Mathias 'Geod24' Lang, Jan Jurzitza
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is probably not accurate for a new file.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

most of the content is copy pasted, so I kept the authors

],
"dependencies": {
"libdparse": ">=0.13.0 <0.18.0"
}
"libdparse": ">=0.13.0 <1.0.0",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

adds ddoc.unhighlight and ddoc.types modules for more useful usage.
Unhighlight can be used to emit embedded D code as-is, instead of
parsing it and (potentially) causing issues as well as needing a
dependency on libdparse.

The ddoc.types module now contains the Comment struct that was in
ddoc.comments and has a new parseUnexpanded call, which puts the split
sections directly into the comment, without expanding, which is commonly
used for example in D-Scanner to just check if a comment is `ditto` or
contains some section with a given name.
@dlang-bot dlang-bot merged commit fbbfb82 into dlang-community:master May 20, 2021
@RazvanN7
Copy link

Thank you for doing this!

@RazvanN7
Copy link

RazvanN7 commented Jun 7, 2021

@WebFreak001 This seems to have broken something in libbdoc. I tried using the newest version of dscanner (that contains the version of libddoc with this PR), but ended up with an error [1]:

std.utf.UTFException@/home/razvan/dmd/dmd/generated/linux/release/64/../../../../../phobos/std/utf.d(1498): Invalid UTF-8 sequence (at index 1)
----------------
/home/razvan/dmd/dmd/generated/linux/release/64/../../../../../phobos/std/utf.d:1588 pure dchar std.utf.decodeImpl!(true, 0, const(char)[]).decodeImpl(const(char)[], ref ulong) [0x55ce6c186a5d]
/home/razvan/dmd/dmd/generated/linux/release/64/../../../../../phobos/std/utf.d:1137 pure @trusted dchar std.utf.decode!(0, immutable(char)[]).decode(ref immutable(char)[], ref ulong) [0x55ce6c186979]
libddoc/common/source/ddoc/lexer.d:217 void ddoc.lexer.Lexer.lexWord() [0x55ce6c278c6b]
libddoc/common/source/ddoc/lexer.d:199 void ddoc.lexer.Lexer.popFront() [0x55ce6c278bbe]
libddoc/common/source/ddoc/sections.d:165 ddoc.sections.Section[] ddoc.sections.splitSections(immutable(char)[]) [0x55ce6c27c934]
libddoc/common/source/ddoc/types.d:50 ddoc.types.Comment ddoc.types.Comment.parse(immutable(char)[], immutable(char)[][immutable(char)[]], bool, immutable(char)[] delegate(immutable(char)[])) [0x55ce6c276c14]
libddoc/common/source/ddoc/types.d:36 ddoc.types.Comment ddoc.types.Comment.parse(immutable(char)[], immutable(char)[][immutable(char)[]], bool, immutable(char)[] function(immutable(char)[])*) [0x55ce6c276bd7]
libddoc/src/ddoc/comments.d:21 ddoc.types.Comment ddoc.comments.parseComment(immutable(char)[], immutable(char)[][immutable(char)[]], bool) [0x55ce6c276434]
src/dscanner/analysis/properly_documented_public_functions.d:295 const(ddoc.types.Comment) dscanner.analysis.properly_documented_public_functions.ProperlyDocumentedPublicFunctions.setLastDdocParams(ulong, ulong, immutable(char)[]) [0x55ce6c362c9c]
src/dscanner/analysis/properly_documented_public_functions.d:167 void dscanner.analysis.properly_documented_public_functions.ProperlyDocumentedPublicFunctions.visit(const(dparse.ast.StructDeclaration)) [0x55ce6c362464]
libdparse/src/dparse/ast.d:1288 const void dparse.ast.Declaration.accept(dparse.ast.ASTVisitor) [0x55ce6c211a70]
src/dscanner/analysis/properly_documented_public_functions.d:146 void dscanner.analysis.properly_documented_public_functions.ProperlyDocumentedPublicFunctions.visit(const(dparse.ast.Declaration)) [0x55ce6c3622f4]
libdparse/src/dparse/ast.d:2289 const void dparse.ast.Module.accept(dparse.ast.ASTVisitor) [0x55ce6c217f4c]
src/dscanner/analysis/properly_documented_public_functions.d:52 void dscanner.analysis.properly_documented_public_functions.ProperlyDocumentedPublicFunctions.visit(const(dparse.ast.Module)) [0x55ce6c361ce7]
src/dscanner/analysis/run.d:599 std.container.rbtree.RedBlackTree!(dscanner.analysis.base.Message, " a.line < b.line || (a.line == b.line && a.column < b.column) ", true).RedBlackTree dscanner.analysis.run.analyze(immutable(char)[], const(dparse.ast.Module), const(dscanner.analysis.config.StaticAnalysisConfig), ref dsymbol.modulecache.ModuleCache, const(std.experimental.lexer.TokenStructure!(ubyte, "import dparse.lexer:TokenTriviaFields; mixin TokenTriviaFields;").TokenStructure)[], bool) [0x55ce6c315c09]
src/dscanner/analysis/run.d:247 bool dscanner.analysis.run.analyze(immutable(char)[][], const(dscanner.analysis.config.StaticAnalysisConfig), immutable(char)[], ref dparse.lexer.StringCache, ref dsymbol.modulecache.ModuleCache, bool) [0x55ce6c3127f3]
src/dscanner/main.d:263 _Dmain [0x55ce6c3946b4]

I think that the breakage comes from here:

libddoc/common/source/ddoc/types.d:50 ddoc.types.Comment ddoc.types.Comment.parse(immutable(char)[], immutable(char)[][immutable(char)[]], bool, immutable(char)[] delegate(immutable(char)[])) [0x55ce6c276c14]
libddoc/common/source/ddoc/types.d:36 ddoc.types.Comment ddoc.types.Comment.parse(immutable(char)[], immutable(char)[][immutable(char)[]], bool, immutable(char)[] function(immutable(char)[])*) [0x55ce6c276bd7]
libddoc/src/ddoc/comments.d:21 ddoc.types.Comment ddoc.comments.parseComment(immutable(char)[], immutable(char)[][immutable(char)[]], bool) [0x55ce6c276434]

Any ideas for a quick fix?

[1] dlang/phobos#8136

@WebFreak001
Copy link
Member Author

hm no logic has changed though, all the old functions should still have the same execution. Do you know which file this happens on?

@RazvanN7
Copy link

RazvanN7 commented Jun 7, 2021

yes, it is etc/c/sqlite3.d from phobos (literally the first file it encounters in etc). I tried running dscanner on std and it successfully analyzes some files before it fails on std/utf.d.

@WebFreak001
Copy link
Member Author

WebFreak001 commented Jun 7, 2021

can't reproduce locally (issue with GC deallocating when memory is limited?)

@RazvanN7
Copy link

RazvanN7 commented Jun 8, 2021

Have you tried running dscanner (commit: 2400d9c9e7763c264412048021d0bd8f977d7e0c) on phobos? (via make -f posix.mak dscanner). This fails on both my machine and the phobos testing pipeline

@WebFreak001
Copy link
Member Author

oh I'll try that later, I tried ./bin/dscanner -S phobos with phobos checked out which works fine.

@RazvanN7
Copy link

I investigated this issue and this PR isn't the culprit. The issue might be with dscanner after all.

@RazvanN7
Copy link

It turns out dscanner starts failing phobos with the UTFException since: dlang-community/D-Scanner@a40492b . It looks like the libdparse upgrade introduced the regression.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants