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

Accessing comments through AST #41

Closed
otm opened this issue Jul 16, 2015 · 9 comments
Closed

Accessing comments through AST #41

otm opened this issue Jul 16, 2015 · 9 comments
Labels

Comments

@otm
Copy link

otm commented Jul 16, 2015

I would like to annotate Lua code through comments, much like Go to generate documentation. However, I don't see the comments when I call parse.Dump.

How should I access the comments through the AST?

-- relase <var> [optionalVar]
function target.relase(var, optionalVar)

end
@yuin
Copy link
Owner

yuin commented Jul 16, 2015

As comments are read and thrown away, you can not access comments through ASTs.
Instead, you can implement a library like lua-annotate.

@otm
Copy link
Author

otm commented Jul 16, 2015

There is no way to change the implementation of the tokenizer so the comments are not thrown away and can be analyzed in the AST?

The reason is that it makes the desired syntax to complex.

@yuin
Copy link
Owner

yuin commented Jul 17, 2015

ASTs in the GopherLua are currently just an intermediate-representing format for translating source codes to byte codes.
GopherLua's ASTs do not have interfaces like visitors, therefore, it is probably hard to analyze ASTs even if comments were not thrown away by the GopherLua parser.
I’m now busy with my work and I can't get much time to make these major alteration.

It might be good idea for you to fork the GopherLua and make it what you want.

@otm
Copy link
Author

otm commented Jul 17, 2015

I will take a look at it. I might write a tiny purpose built tokenizer for this, as I'm only interested in functions and comments associated with them.

However, if you have any pointers on how to implement this is gopher-lua I'm interested. Also, would you be interested in a pull request regarding this?

@otm otm closed this as completed Jul 17, 2015
@yuin
Copy link
Owner

yuin commented Jul 17, 2015

However, if you have any pointers on how to implement this is gopher-lua I'm interested.

If I were you, I would not use ASTs.

1. LFunction objects have a FunctionProto object. This allows you to know where the function was defined.

lineDefined := fn.Proto.LineDefined

2. Read the .lua source file and check whether a line:lineDefined - 1 starts with -- .
3. If line:lineDefined -1 starts with --, parse this line as a directive.

Also, would you be interested in a pull request regarding this?

If you implement AST analyze functions and it is not causes significant performance degradation, I would like to consider merging.

@otm
Copy link
Author

otm commented Jul 17, 2015

Thanks for the help! That is actally a really neat idea. I think that will work well.

Again, thanks for a really nice project.

@otm
Copy link
Author

otm commented Jul 18, 2015

Your idea worked perfectly. I wrote a small tokenizer for Lua comments which emitted comments and line number and then used fn.Proto.LineDefined to check if there was a comment associated with the function.

@yuin
Copy link
Owner

yuin commented Jul 18, 2015

Good to hear 👍

@yuin yuin added the question label Nov 8, 2016
@flzyup
Copy link

flzyup commented Mar 13, 2019

I have a question, how do i get the the LFunction instance to read line number when i exe lua file or lua string. As we register global function as the following:

L.SetGlobal("double", L.NewFunction(Double)) /* Original lua_setglobal uses stack... */

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

No branches or pull requests

3 participants