Javascript 词法分析器/分词器(Python 中?)
有谁知道 Javascript 词法分析器或分词器(最好是 Python 中的?)
基本上,给定一个任意 Javascript 文件,我想获取标记。
例如
foo = 1
变成类似:
- 变量名 : "foo"
- 空白
- 运算符 : 等于
- 空白
- 整数 : 1
Does anyone know of a Javascript lexical analyzer or tokenizer (preferably in Python?)
Basically, given an arbitrary Javascript file, I want to grab the tokens.
e.g.
foo = 1
becomes something like:
- variable name : "foo"
- whitespace
- operator : equals
- whitespace
- integer : 1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
http://code.google.com/p/pynarcissus/ 有一个。
我还制作了一个,但它不支持自动分号插入,因此对于您无法控制的 javascript 来说它几乎没有用(因为几乎所有现实生活中的 javascript 程序都缺少至少一个分号):) 这是我的:
http://bitbucket.org/santagada/jaspyon/src/tip/jaspyon/
语法位于 jsgrammar.txt 中,它由 PyPy 解析库进行解析(您必须从 pypy 源中下载并提取),并构建一个解析树,我在 astbuilder.py 上行走
但是如果您没有许可问题我会选择pynarcissus。这是查看代码的直接链接(从 narcissus 移植):
http://code.google.com/p/pynarcissus/source/browse/trunk/jsparser.py
http://code.google.com/p/pynarcissus/ has one.
Also I made one but it doesn't support automatic semicolon insertion so it is pretty useless for javascript that you have no control over (as almost all real life javascript programs lack at least one semicolon) :) Here is mine:
http://bitbucket.org/santagada/jaspyon/src/tip/jaspyon/
the grammar is in jsgrammar.txt, it is parsed by the PyPy parsing lib (which you will have to download and extract from the pypy source) and it build a parse tree which I walk on astbuilder.py
But if you don't have licensing problems I would go with pynarcissus. heres a direct link to look at the code (ported from narcissus):
http://code.google.com/p/pynarcissus/source/browse/trunk/jsparser.py