vim-vimlparser icon indicating copy to clipboard operation
vim-vimlparser copied to clipboard

Add 'printer' feature: Restore source code string from AST

Open tyru opened this issue 8 years ago • 9 comments
trafficstars

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.

tyru avatar Nov 19 '17 12:11 tyru

[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 ?

haya14busa avatar Nov 21 '17 12:11 haya14busa

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 avatar Nov 21 '17 14:11 tyru

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

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

tyru avatar Nov 22 '17 19:11 tyru

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

tyru avatar Nov 22 '17 20:11 tyru

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 avatar Nov 22 '17 20:11 tyru

What function name should I rename to...?

How about Dumper.dump() ?

tyru avatar Nov 22 '17 20:11 tyru

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?

lesguillemets avatar Nov 23 '17 04:11 lesguillemets

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

tyru avatar Nov 23 '17 05:11 tyru

@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.

tyru avatar Nov 23 '17 09:11 tyru