With Erick's help I have now made the first commit of the Javascript parser to the repository.
I'd like everyone to take a look at the AST representation so that we can make adjustments earlier rather than later. The AST constructors are defined in file parser.js (search for the comment "// Constructors."). The file js.js has an AST pretty-printer which walks an AST recursively to show its content. It also gives location information for each node. If you install the emacs package "tachyon.el" (follow the instructions at the top of the file) then you can scroll through the pretty-printed AST to automatically highlight the corresponding source code (in a subwindow) simply by placing the cursor on the line with the location information.
Here is a sample execution of the parser on the file:
function f(x) { if (x < 2) return 1; else return f(x-1) + f(x-2); }
% cd parser % ./js tests/test1.js Program ("tests/test1.js"@1.1-7.2:) |-statements= | FunctionDeclaration ("tests/test1.js"@1.1-7.2:) | |-id= f | |-params= x | |-body= | | IfStatement ("tests/test1.js"@3.5-6.32:) | | |-expr= | | | OpExpr ("tests/test1.js"@3.9-3.14:) | | | |-op= "x < y" | | | |-exprs= | | | | Var ("tests/test1.js"@3.9-3.10:) | | | | |-id= x | | | | Literal ("tests/test1.js"@3.13-3.14:) | | | | |-value= 2 | | |-statements= | | | ReturnStatement ("tests/test1.js"@4.9-4.18:) | | | |-expr= | | | | Literal ("tests/test1.js"@4.16-4.17:) | | | | |-value= 1 | | | ReturnStatement ("tests/test1.js"@6.9-6.32:) | | | |-expr= | | | | OpExpr ("tests/test1.js"@6.16-6.31:) | | | | |-op= "x + y" | | | | |-exprs= | | | | | CallExpr ("tests/test1.js"@6.16-6.22:) | | | | | |-fn= | | | | | | Var ("tests/test1.js"@6.16-6.17:) | | | | | | |-id= f | | | | | |-args= | | | | | | OpExpr ("tests/test1.js"@6.18-6.21:) | | | | | | |-op= "x - y" | | | | | | |-exprs= | | | | | | | Var ("tests/test1.js"@6.18-6.19:) | | | | | | | |-id= x | | | | | | | Literal ("tests/test1.js"@6.20-6.21:) | | | | | | | |-value= 1 | | | | | CallExpr ("tests/test1.js"@6.25-6.31:) | | | | | |-fn= | | | | | | Var ("tests/test1.js"@6.25-6.26:) | | | | | | |-id= f | | | | | |-args= | | | | | | OpExpr ("tests/test1.js"@6.27-6.30:) | | | | | | |-op= "x - y" | | | | | | |-exprs= | | | | | | | Var ("tests/test1.js"@6.27-6.28:) | | | | | | | |-id= x | | | | | | | Literal ("tests/test1.js"@6.29-6.30:) | | | | | | | |-value= 2
Note that the parse tree construction is not fully implemented. Some more difficult things to parse (for loops, switch statements, with statement, object literals, regular expressions, consts, etc) are not yet done. Automatic semicolon insertion is also on the todo list. On the bright side, all the .js files of the parser (over 7,000 LOC) can be parsed by the parser. Also, the parser processes about 50,000 LOC per second, which should be OK for now (I haven't tried to optimize anything yet).
Marc
Afficher les réponses par date