Skip to content

Saving documentation in the .beam file #211

Closed

Description

@rvirding

We are now working towards saving the module, function and macro documentation strings in the .beam file. As a first step along the way the define-function and define-macro core forms not have an extra element for the documentation strings. The macro expander now saves the documentation string from the defun and defmacro in the forms when they are expanded. The doc strings are then passed through the compiler where they are ignored except in the lint pass where they are checked that they are either list character strings or binaries.

It is @yurrriq who is doing the actual work around this.

I think the based place to get these strings is after the split pass and before the expand pass in the compiler because between these two the macro definitions are still in the code forms. After the expand pass all macros have been expanded and their definitions are no longer saved. Then they have to be saved of course which is where it starts getting interesting. :-)

You can actually comment local function and macro definitions:

(flet ((sune (a b)
         "The sune function."
         (* a b)))
  ...)

However, these comments are lost, mainly because I don't know what to do with them.

Activity

  1. yurrriq commented on Mar 27, 2016

    @yurrriq
    Contributor

    The commit has since been rebased away, but there's some discussion here.

  2. rvirding commented on Mar 27, 2016

    @rvirding
    MemberAuthor

    Yes, I made a mistake and managed to delete the initial commit :-( That is why I made an issue of it so it won't be in any commit and that won't happen again. I don't trust myself.

  3. yurrriq commented on Mar 27, 2016

    @yurrriq
    Contributor

    I think it was me aggressively rebasing 😄

  4. yurrriq commented on Mar 27, 2016

    @yurrriq
    Contributor

    Ok, I'm getting close to resolving all the conflicts. I'm going to rework the doc parsing s.t. it only happens in a do_docs pass when you mentioned above. Will likely pull out the helper functions into a new module, lfe_doc.

  5. rvirding commented on Mar 27, 2016

    @rvirding
    MemberAuthor

    Would you like me to wait with Duncan's doc PR? It shouldn't affect you but who knows.

  6. yurrriq commented on Mar 27, 2016

    @yurrriq
    Contributor

    It looks safe to me.

  7. rvirding commented on Mar 28, 2016

    @rvirding
    MemberAuthor

    I have been thinking about the compiler. One problem with doing it after split and before the expand passes is just the expansion. The reason is that the split pass only expands the forms at the top-level so it can detect define-module and partition the forms into modules. Everything before the first module is copied into every module. The expand pass, however, expands everything all the way down but it removes the macro definitions.

    So what could happen is that in a function defined by a match-lambda there might be macros in the arguments which will only be expanded in the expand pass. This will mean that your argument descriptions may be off. This does not happen if the functions is a lambda.

    I don't know if this is a problem but if it is we can fix it later.

  8. yurrriq commented on Mar 31, 2016

    @yurrriq
    Contributor

    After my latest efforts (and some more rebasing 😈), the output of test/docs.lfe looks like:

    #(lfe_docs_v1
      (#(doc macro false MODULE 0 (()) #"")
       #(doc macro true add 2 ((x y)) #"Add `x` and `y`.")
       #(doc macro true varargs 255 (args) #"This is a varags macro.")
       #(doc macro false forty-two? 1
         ((x (when (=:= x 42))) (x (when (< x 42))) (x (when (> x 42))))
         #"Return `'true` iff `x` is 42.")
       #(doc macro false tricky 255
         ((x) (x y) (x y . z))
         #"This is a tricky varargs macro.")
       #(doc function true subtract 2 ((x y)) #"Subtract `y` from `x`.")
       #(doc function true matchfun 3
         ((x y z (when (< x y) (< y z)))
          (x y z (when (> x y) (> y z)))
          (x y z (when (=:= x y) (=:= y z)))
          (_ _ _))
         #"This is a function with pattern clauses.")
       #(doc function true LFE-EXPAND-EXPORTED-MACRO 3
         ((|\|- MACRO NAME -\|| |\|- CALL ARGS -\|| $ENV))
         #"")))

    Note the patterns and exported booleans. For convenience, here's the #doc{} definition, which now lives in src/lfe_doc.hrl:

    -record(doc, {type     = error(missing_type)  :: function | macro,
                  exported = false                :: boolean(),
                  name     = error(missing_name)  :: atom(),
                  arity    = error(missing_arity) :: non_neg_integer(),
                  patterns = [[]]                 :: [pattern()],
                  doc      = <<"">>               :: binary()
                 }).
  9. yurrriq commented on Mar 31, 2016

    @yurrriq
    Contributor

    I'm pretty satisfied at this point, but the question is: do we want to do anything analogous to Elixir's moduledoc, callback_docs and type_docs?

    My thoughts: type_docs probably doesn't make much sense for us (for now), nor does callback_docs. It could be quite nice, however, to put a module's docstring in moduledoc. I'm not sure what Elixir stores there, but it's probably worth looking into that too.

    OTOH...

    > (iolist_to_binary (proplists:get_value 'doc (example:module_info 'attributes) []))
    #"This is an example module."
  10. yurrriq commented on Mar 31, 2016

    @yurrriq
    Contributor

    One more thing, do we want to prune LFE-EXPAND-EXPORTED-MACRO/3 and MODULE/0, or leave them there for accuracy?

  11. rvirding commented on Mar 31, 2016

    @rvirding
    MemberAuthor

    Nice. Some answers to your questions:

    • Yes I would have a module-doc similar to elixir's and I would (will) remove that attribute from the module attributes when generating the .beam file. The reason being that I want all the documentation in the separate LDoc chunk as it can become very large and you can easily remove it when you want to. For example with beam_lib:strip_release/1.
    • I would prune LFE-EXPAND-EXPORTED-MACRO/3 and MODULE/0 and some other predefined stuff as well. In one sense they don't really exist.
  12. yurrriq commented on Mar 31, 2016

    @yurrriq
    Contributor

    I've pushed some hacks to those ends.

    Current output of lfe test/docs.lfe:

    #(lfe_docs_v1
      (#(doc macro true add 2 ((x y)) #"Add `x` and `y`.")
       #(doc macro true varargs 255 (args) #"This is a varags macro.")
       #(doc macro false forty-two? 1
         ((x (when (=:= x 42))) (x (when (< x 42))) (x (when (> x 42))))
         #"Return `'true` iff `x` is 42.")
       #(doc macro false tricky 255
         ((x) (x y) (x y . z))
         #"This is a tricky varargs macro.")
       #(doc function true subtract 2 ((x y)) #"Subtract `y` from `x`.")
       #(doc function true matchfun 3
         ((x y z (when (< x y) (< y z)))
          (x y z (when (> x y) (> y z)))
          (x y z (when (=:= x y) (=:= y z)))
          (_ _ _))
         #"This is a function with pattern clauses."))
      #"This is an example module.")
  13. yurrriq commented on Mar 31, 2016

    @yurrriq
    Contributor

    Note that doc is removed from attributes now:

    > (set `#(ok [#(ok example ,beam)]) (lfe_comp:file "test/example.lfe" '[binary]))
    #(ok
      (#(ok example
         #B(70 79 82 49 0 0 8 116 66 69 65 77 76 68 111 99 0 0 3 149 131 104 3 100 0 11 ...))))
    > (beam_lib:chunks beam '[attributes])
    #(ok
      #(example
        (#(attributes
           (#(export-macro (add varargs))
            #(vsn (317309562976526214531974017463898375168)))))))
  14. yurrriq commented on Mar 31, 2016

    @yurrriq
    Contributor

    @rvirding, shall I remove export-macro too?

  15. 16 remaining items

  16. yurrriq commented on Apr 1, 2016

    @yurrriq
    Contributor

    I can try to take a look at the PDF issues tonight too.

  17. rvirding commented on Apr 1, 2016

    @rvirding
    MemberAuthor

    No, I will fix the PDF stuff, almost got it working. Then I will clean up
    develop and merge it into master as 1.0.2, then you can rebase your stuff
    and I will pull it.

    Robert

    On 2 April 2016 at 01:32, Eric Bailey [email protected] wrote:

    I can try to take a look at the PDF issues tonight too.

    —
    You are receiving this because you were mentioned.
    Reply to this email directly or view it on GitHub
    #211 (comment)

  18. rvirding commented on Apr 1, 2016

    @rvirding
    MemberAuthor

    Do you know if you can comment a line in MD?

    On 2 April 2016 at 01:35, Robert Virding [email protected] wrote:

    No, I will fix the PDF stuff, almost got it working. Then I will clean up
    develop and merge it into master as 1.0.2, then you can rebase your stuff
    and I will pull it.

    Robert

    On 2 April 2016 at 01:32, Eric Bailey [email protected] wrote:

    I can try to take a look at the PDF issues tonight too.

    —
    You are receiving this because you were mentioned.
    Reply to this email directly or view it on GitHub
    #211 (comment)

  19. yurrriq commented on Apr 1, 2016

    @yurrriq
    Contributor

    You do it HTML style: <!-- this is a comment -->

  20. rvirding commented on Apr 2, 2016

    @rvirding
    MemberAuthor

    Yes, that works, thank you.

    The problem is the latin1 characters and two unicode codepoints in the
    source. And you get different problems depending on what you are generating:

    • The pdf can't handle 2 latin1 chars and no unicode codepoints at all, but
      everything goes fine. Maybe there is a switch which can fix this.
    • The man page doesn't seem to be able to handle anything above 128. Well
      it can handle it but the output is unintelligible.
    • The textfile works ok except for a few latin1 chars.

    Do you know of any options which may help me then let me know. My plan is
    to rewrite those sections and just mention the char ranges, not explicitly
    show them.

    Robert

    On 2 April 2016 at 01:38, Eric Bailey [email protected] wrote:

    You do it HTML style:

    —
    You are receiving this because you were mentioned.
    Reply to this email directly or view it on GitHub
    #211 (comment)

  21. yurrriq commented on Apr 3, 2016

    @yurrriq
    Contributor

    I'm not sure about those issues, but I've pushed a crucial fix for the to_* steps, as well as some general cleanup and tooling improvements. I won't link to commits, since they'll likely get rebased soon, but you can look at the log of my branch.

  22. yurrriq commented on Apr 3, 2016

    @yurrriq
    Contributor

    XeLaTeX might be of use here. /cc @jgm

  23. rvirding commented on Apr 3, 2016

    @rvirding
    MemberAuthor

    What I have done now is to quite simply remove all the unicode and upper
    latin-1 characters. I just have to shift some bits to the left and I am
    done.

    Robert

    On 3 April 2016 at 21:40, Eric Bailey [email protected] wrote:

    XeLaTeX might be of use here. /cc @jgm https://github.com/jgm

    —
    You are receiving this because you were mentioned.
    Reply to this email directly or view it on GitHub
    #211 (comment)

  24. yurrriq commented on Apr 4, 2016

    @yurrriq
    Contributor

    Based on #216, I've started writing doc/1 and similar. You can find a snapshot here or here.

  25. yurrriq commented on Apr 4, 2016

    @yurrriq
    Contributor

    The process has exposed a bug/feature of lfe_doc that may need correcting:

    (defmodule example
      (export (drop-prefix 2)))
    
    (defun drop-prefix
      "Drop a matching prefix of `xs` from `ys`."
      ([`(,x . ,xs) `(,y . ,ys)] (when (=:= x y))
       (drop-prefix xs ys))
      ([_xs ys]
       ys))
    > (let ((`#(ok ,docs) (exports 'example))) (lists:keyfind 'drop-prefix 4 docs))
    #(doc function true drop-prefix 2
      ((`(,x comma xs) `(,y comma ys) (when (=:= x y))) (xs ys))
      #"Drop a matching prefix of `xs` from `ys`."
      4)

    Edit: Check out the workaround I adopted from Lodox, format-patterns/1.

    The question remains: Do we want to handle this in lfe_doc or let/force consumers to handle it?

  26. rvirding commented on Apr 4, 2016

    @rvirding
    MemberAuthor

    Do you mean not expanding the backspace macro?

    We could solve that by moving the comment pass until after the macro
    expansion. Currently that would mean the macros are gone but that would be
    easily fixed by leaving them and ignoring them later.

    Robert

    On 4 April 2016 at 08:46, Eric Bailey [email protected] wrote:

    The process has exposed a bug/feature of lfe_doc that may need correcting:

    (defmodule example
    (export (drop-prefix 2)))

    (defun drop-prefix
    "Drop a matching prefix of xs from ys."
    ([(,x . ,xs)(,y . ,ys)](when %28=:= x y%29)
    (drop-prefix xs ys))
    ([_xs ys]
    ys))

    (let ((#(ok ,docs) (exports 'example))) (lists:keyfind 'drop-prefix 4 docs)) #(doc function true drop-prefix 2 (((,x comma xs) (,y comma ys) (when (=:= x y))) (xs ys)) #"Drop a matching prefix ofxsfromys`." 4)

    —
    You are receiving this because you were mentioned.
    Reply to this email directly or view it on GitHub
    #211 (comment)

  27. yurrriq commented on Apr 4, 2016

    @yurrriq
    Contributor

    I mean when you use quasiquoting in function clauses, the show up as (,x comma xs) in patterns.

  28. yurrriq commented on Apr 10, 2016

    @yurrriq
    Contributor

    I think we should leave the do_docs pass where it is and add a function that sanitizes patterns.

  29. yurrriq commented on Apr 10, 2016

    @yurrriq
    Contributor

    The question is: is the LDoc chunk going to be used for more than just display purposes, i.e. do we need to ensure (length pattern) remains accurate? Or can we use a hack to make sure it "looks right" and tell users to use the arity field if they need to, rather than (length pattern)?

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions