Module ringo/parser
This module provides an interface to the Rhino parser.
Parser (options)
Create a new Parser object. The constructor must be called with the new
keyword.
It takes an options
argument which may contain the following properties:
-
languageVersion
(number) the JavaScript language version to use. Defaults to180
. -
parseComments
(boolean) whether to attach jsdoc-style comments to parsed nodes. Defaults tofalse
.
Parameters
Object | options | the parser options |
Parser.prototype. parse (script, [encoding])
Parse a script resource and return its AST tree.
Parameters
Resource|String | script | a string or org.ringojs.repository.Resource object representing the script. |
string | [encoding] | optional encoding to use, defaults to UTF-8 |
Returns
AstNode | the root node of the AST tree, an instance of org.mozilla.javascript.ast.AstRoot |
Parser.prototype. visit (script, visitorFunction, [encoding])
Parse a script resource and apply the visitor function to its AST tree. The function takes one argument which is a org.mozilla.javascript.ast.AstNode. The function must return true to visit child nodes of the current node.
Parameters
Resource|String | script | a string or org.ringojs.repository.Resource object representing the script. |
Function | visitorFunction | the visitor function |
string | [encoding] | optional encoding to use, defaults to UTF-8 |
Token
The org.mozilla.javascript.Token class. This can be used to easily check find out the types of AST nodes:
node.type == Token.NAME
getName (node)
Utility function to get the name value of a node, or the empty
string if it is not a NAME
node.
Parameters
AstNode | node | an AST node |
Returns
String | the name value of the node |
getTypeName (node)
Get the type name of the token as string such as "CALL" or "NAME".
Parameters
AstNode | node | a AST node |
Returns
String | the name of the AST node's type |
isName (node)
Utility function to test whether a node is a NAME
node
(a node of type org.mozilla.javascript.ast.Name)
Parameters
Object | node | an AST node |
Returns
Boolean | true if node is a name node |