vim-vimlparser
vim-vimlparser copied to clipboard
Add 'printer' feature: Restore source code string from AST
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 * 3becomes(1 + (2 * 3))~~ - [Question] Added
test/*.vimokfiles which is same content asprinter.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.
[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 ?
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.
[Todo] Currently 1 + 2 * 3 becomes (1 + (2 * 3))
Implemented operator precedence, so echo 1 + 2 * 3 becomes echo 1 + 2 * 3.
How should I write printer tests of JS/Python?
Add --print option to js/vimlparser.js and py/vimlparser.py?
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...?
What function name should I rename to...?
How about Dumper.dump() ?
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 oh thanks! I'll give it a try.
@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.