SlimIt - JavaScript minifier
Project description
SlimIt is a Python library that parses JavaScript and returns AST.
At version 0.2 it includes a JavaScript parser, lexer, pretty printer and a tree visitor.
Iterate over, modify a JavaScript AST and pretty print it
>>> from slimit.parser import Parser >>> from slimit.visitors import nodevisitor >>> from slimit import ast >>> >>> parser = Parser() >>> tree = parser.parse('for(var i=0; i<10; i++) {var x=5+i;}') >>> for node in nodevisitor.visit(tree): ... if isinstance(node, ast.Identifier) and node.value == 'i': ... node.value = 'hello' ... >>> print tree.to_ecma() # print awesome javascript :) for (var hello = 0; hello < 10; hello++) { var x = 5 + hello; } >>>
N.B. First time you invoke parse method it will generate the lextab.py and yacctab.py LALR tables in current directory and you may see some warnings - that’s OK. Previously generated tables are cached and reused if possible. For more details visit PLY’s official site.
Using lexer in your project
>>> from slimit.lexer import Lexer >>> lexer = Lexer() >>> lexer.input('a = 1;') >>> for token in lexer: ... print token ... LexToken(ID,'a',1,0) LexToken(EQ,'=',1,2) LexToken(NUMBER,'1',1,4) LexToken(SEMI,';',1,5)
You can get one token at a time using token method:
>>> lexer.input('a = 1;') >>> while True: ... token = lexer.token() ... if not token: ... break ... print token ... LexToken(ID,'a',1,0) LexToken(EQ,'=',1,2) LexToken(NUMBER,'1',1,4) LexToken(SEMI,';',1,5)
LexToken instance has different attributes:
>>> lexer.input('a = 1;') >>> token = lexer.token() >>> token.type, token.value, token.lineno, token.lexpos ('ID', 'a', 1, 0)
Installation
Using pip:
$ sudo pip install slimit
Using easy_install:
$ sudo easy_install slimit
Roadmap
Add JavaScript minification
Change History
0.2 (2011-05-07)
Added a JavaScript parser
Added pretty printer
Added node visitor
0.1 (2011-05-02)
Initial public version. It contains only a JavaScript lexer
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file slimit-0.2.tar.gz
.
File metadata
- Download URL: slimit-0.2.tar.gz
- Upload date:
- Size: 24.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f74a6085c5b1fe4cc6dd04f7c8d6b9833cbe6177ae347aa22c3464d876134a68 |
|
MD5 | 2b1103da211377f9a41c9183e46c39f5 |
|
BLAKE2b-256 | cb621a1604d83032459a50041155e780cd52f762ac003eec7dd5797adcca0e67 |