こちらに新しい記事があります。
jsparsenodetree.c part2
これは下記のurlの内容の設定例です。
探したところ、javascriptを分割する場合には
http://siliconforks.com/doc/parsing-javascript-with-spidermonkey/
http://ido.nu/kuma/2009/10/22/spidermonkey-parsenode-to-json/
http://www.mozilla-japan.org/js/spidermonkey/
が良いように見受けられます。
各々のサイトから example.c jsparsenodetree.c js-1.8.0-rc1.tar.gz をダウンロードします。
$ tar xvzf js-1.8.0-rc1.tar.gz
$ cd js/src/
$ cp -i /tmp/jsparsenodetree.c .
const char * TOKENS[] を修正します。
$ gcc -o jsparsenodetree -Wall -Wno-format -g -DXP_UNIX -DSVR4 -DSYSV -D_BSD_SOURCE -DPOSIX_SOURCE -DDARWIN -DX86_LINUX -DDEBUG -DDEBUG_kuma -DEDITLINE -I./Linux_All_DBG.OBJ -DHAVE_VA_COPY -DVA_COPY=va_copy -L./Linux_All_DBG.OBJ -ljs -Wl,-rpath,'$ORIGIN/Linux_All_DBG.OBJ' jsparsenodetree.c
動作しました。
$ cat /tmp/good.js
print('hello world');
$ ./jsparsenodetree /tmp/good.js
{
"node": "LIST",
"token": "LC",
"list": [
{
"node": "UNARY",
"token": "SEMI",
"kid": {
"node": "LIST",
"token": "LP",
"list": [
{ "node":"NAME", "token":"NAME", "identifier":"print"},
{ "node":"NULLARY", "token":"STRING", "opcode":"string", "text": "hello world"}
]
}
}
]
}
php の token_get_all($buf); に似た感じになります。
$ ./jsparsenodetree /tmp/good.js | php -r '$buf = file_get_contents("php://stdin"); var_export(json_decode($buf));'
stdClass::__set_state(array(
'node' => 'LIST',
'token' => 'LC',
'list' =>
array (
0 =>
stdClass::__set_state(array(
'node' => 'UNARY',
'token' => 'SEMI',
'kid' =>
stdClass::__set_state(array(
'node' => 'LIST',
'token' => 'LP',
'list' =>
array (
0 =>
stdClass::__set_state(array(
'node' => 'NAME',
'token' => 'NAME',
'identifier' => 'print',
)),
1 =>
stdClass::__set_state(array(
'node' => 'NULLARY',
'token' => 'STRING',
'opcode' => 'string',
'text' => 'hello world',
)),
),
)),
)),
),
))
また
$ pecl list-all|grep -i spidermonkey
pecl/spidermonkey 0.1.4 JavaScript engine for PHP
が、似たような分割ができるのでは?と思いましたが試していないです。
また
const char * TOKENS[] を修正します。
の詳細は jsscan.h の typedef enum JSTokenType {...} を example.c を参考に修正します。
$ diff /tmp/jsparsenodetree.c jsparsenodetree.c
1d0
<
16,18c15,24
< const char * TOKENS[] = {
< #include "tokens.def"
< NULL
---
> const char * TOKENS[87] = {
> /*"ERROR",*/ "EOF", "EOL", "SEMI", "COMMA", "ASSIGN", "HOOK", "COLON", "OR", "AND",
> "BITOR", "BITXOR", "BITAND", "EQOP", "RELOP", "SHOP", "PLUS", "MINUS", "STAR", "DIVOP",
> "UNARYOP", "INC", "DEC", "DOT", "LB", "RB", "LC", "RC", "LP", "RP",
> "NAME", "NUMBER", "STRING", "REGEXP", "PRIMARY", "FUNCTION", "EXPORT", "IMPORT", "IF", "ELSE",
> "SWITCH", "CASE", "DEFAULT", "WHILE", "DO", "FOR", "BREAK", "CONTINUE", "IN", "VAR",
> "WITH", "RETURN", "NEW", "DELETE", "DEFSHARP", "USESHARP", "TRY", "CATCH", "FINALLY", "THROW",
> "INSTANCEOF", "DEBUGGER", "XMLSTAGO", "XMLETAGO", "XMLPTAGC", "XMLTAGC", "XMLNAME", "XMLATTR", "XMLSPACE", "XMLTEXT",
> "XMLCOMMENT", "XMLCDATA", "XMLPI", "AT", "DBLCOLON", "ANYNAME", "DBLDOT", "FILTER", "XMLELEM", "XMLLIST",
> "YIELD", "ARRAYCOMP", "ARRAYPUSH", "LEXICALSCOPE", "LET", "BODY", "RESERVED", "LIMIT",
また、下記は実行ファイルの移動のサンプルです。
$ gcc -o jsparsenodetree -Wall -Wno-format -g -DXP_UNIX -DSVR4 -DSYSV -D_BSD_SOURCE -DPOSIX_SOURCE -DDARWIN -DX86_LINUX -DDEBUG -DDEBUG_kuma -DEDITLINE -I./Linux_All_DBG.OBJ -DHAVE_VA_COPY -DVA_COPY=va_copy -L./Linux_All_DBG.OBJ -ljs -Wl,-rpath,'$ORIGIN/../lib64' jsparsenodetree.c
$ mkdir /usr/local/stow/jsparsenodetree-2009-10-22/
$ mkdir /usr/local/stow/jsparsenodetree-2009-10-22/bin
$ mkdir /usr/local/stow/jsparsenodetree-2009-10-22/lib64
$ cp -i jsparsenodetree /usr/local/stow/jsparsenodetree-2009-10-22/bin/
$ cp -i Linux_All_DBG.OBJ/libjs.so /usr/local/stow/jsparsenodetree-2009-10-22/lib64/
$ cd /usr/local/stow/
$ stow jsparsenodetree-2009-10-22/
$ find jsparsenodetree-2009-10-22/ ! -type d
jsparsenodetree-2009-10-22/bin/jsparsenodetree
jsparsenodetree-2009-10-22/lib64/libjs.so
$ find /usr/local/ -type l -exec ls -al {} \; | grep jsparsenodetree-2009-10-22
lrwxrwxrwx 1 root root 54 2010-03-24 07:07 /usr/local/bin/jsparsenodetree -> ../stow/jsparsenodetree-2009-10-22/bin/jsparsenodetree
lrwxrwxrwx 1 root root 49 2010-03-24 07:07 /usr/local/lib64/libjs.so -> ../stow/jsparsenodetree-2009-10-22/lib64/libjs.so
$ find jsparsenodetree-2009-10-22/ -type f -exec strip {} \;
jsparsenodetree.c part2
jsparsenodetree.c part3