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 'printer' feature: Restore source code string from AST #70

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

tyru
Copy link
Member

@tyru tyru commented Nov 19, 2017

This is big change.
so I don't think I want this feature to get merged soon.

Disclaimer

This feature is not intended to restore full original source code.
This feature provides a way to get Vim script source code from AST which does same behavior as original source code.

What this feature provides?

Restore source code string from AST.
Here is the example.

let s:vimlparser = vimlparser#import()
let src = readfile('test/test1.vim')
let reader = s:vimlparser.StringReader.new(src)
let parser = s:vimlparser.VimLParser.new(0)
let printer = s:vimlparser.Printer.new()
echo join(printer.print(parser.parse(reader)), "\n")

Concern

  • [Todo] Currently 1 + 2 * 3 becomes (1 + (2 * 3))
  • [Question] Added test/*.vimok files which is same content as printer.print(...), is it complicated?

Reference

This is similar feature to printer of https://github.com/haya14busa/go-vimlparser , but as I said first, this PR aims to generate Vim script which does only same behavior, not literally generate same source code as original one.

@haya14busa
Copy link
Member

[Todo] Currently 1 + 2 * 3 becomes (1 + (2 * 3))

In go-vimlparser, I added ParenExpr which represents parenthesis expression, so we can handle the above cases.
https://godoc.org/github.com/haya14busa/go-vimlparser/ast#ParenExpr

I have a plan to port it back to vim-vimlparser, but it's breaking changes, so I'm wondering how to do this. maybe flag ?

@tyru
Copy link
Member Author

tyru commented Nov 21, 2017

ParenExpr sounds good!

maybe flag ?

👍 for flag argument of VimLParser.parse().
maybe it would be better that the argument is Dictionary
because it can support more flags to control parser behavior.

@tyru
Copy link
Member Author

tyru commented Nov 22, 2017

[Todo] Currently 1 + 2 * 3 becomes (1 + (2 * 3))

Implemented operator precedence, so echo 1 + 2 * 3 becomes echo 1 + 2 * 3.

@tyru
Copy link
Member Author

tyru commented Nov 22, 2017

How should I write printer tests of JS/Python?
Add --print option to js/vimlparser.js and py/vimlparser.py?

@tyru
Copy link
Member Author

tyru commented Nov 22, 2017

https://travis-ci.org/vim-jp/vim-vimlparser/jobs/305985385
and python tests were failed because cannot define print() function in python...
What function name should I rename to...?

@tyru
Copy link
Member Author

tyru commented Nov 22, 2017

What function name should I rename to...?

How about Dumper.dump() ?

@lesguillemets
Copy link

I haven't checked the whole code, but I think SyntaxError: invalid syntax when defining print can be avoided by from __future__ import print_function. It shouldn't be necessary when using python 3, but somehow the test seems to be run with 2?

@tyru
Copy link
Member Author

tyru commented Nov 23, 2017

@lesguillemets oh thanks! I'll give it a try.

Without the above statement, cannot define print() method in Printer class
@tyru
Copy link
Member Author

tyru commented Nov 23, 2017

@lesguillemets thanks, it's all green now :)

Now I'm wondering this feature should be in vim-vimlparser itself.
I think this should be in separated repository because vim-vimlparser has already too much features (and code of lines).
Adding unnecessary features in vim-vimlparser may complicate vim-vimlparser code maintenance.

I'll wait a few days to hear ideas, but maybe I'll create a new repository for printer.
And also add uglifier and pretiffier features.

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.

3 participants