-- import "github.com/dop251/goja/parser"
Package parser implements a parser for JavaScript. Borrowed from https://github.com/robertkrimen/otto/tree/master/parser
import (
"github.com/dop251/goja/parser"
)
Parse and return an AST
filename := "" // A filename is optional
src := `
// Sample xyzzy example
(function(){
if (3.14159 > 0) {
console.log("Hello, World.");
return;
}
var xyzzy = NaN;
console.log("Nothing happens.");
return xyzzy;
})();
`
// Parse some JavaScript, yielding a *ast.Program and/or an ErrorList
program, err := parser.ParseFile(nil, filename, src, 0)
The parser and AST interfaces are still works-in-progress (particularly where node types are concerned) and may change in the future.
func ParseFile(fileSet *file.FileSet, filename string, src interface{}, mode Mode) (*ast.Program, error)ParseFile parses the source code of a single JavaScript/ECMAScript source file and returns the corresponding ast.Program node.
If fileSet == nil, ParseFile parses source without a FileSet. If fileSet != nil, ParseFile first adds filename and src to fileSet.
The filename argument is optional and is used for labelling errors, etc.
src may be a string, a byte slice, a bytes.Buffer, or an io.Reader, but it MUST always be in UTF-8.
// Parse some JavaScript, yielding a *ast.Program and/or an ErrorList
program, err := parser.ParseFile(nil, "", `if (abc > 1) {}`, 0)
func ParseFunction(parameterList, body string) (*ast.FunctionLiteral, error)ParseFunction parses a given parameter list and body as a function and returns the corresponding ast.FunctionLiteral node.
The parameter list, if any, should be a comma-separated list of identifiers.
func ReadSource(filename string, src interface{}) ([]byte, error)type Error struct {
Position file.Position
Message string
}An Error represents a parsing error. It includes the position where the error occurred and a message/description.
func (self Error) Error() stringtype ErrorList []*ErrorErrorList is a list of *Errors.
func (self *ErrorList) Add(position file.Position, msg string)Add adds an Error with given position and message to an ErrorList.
func (self ErrorList) Err() errorErr returns an error equivalent to this ErrorList. If the list is empty, Err returns nil.
func (self ErrorList) Error() stringError implements the Error interface.
func (self ErrorList) Len() intfunc (self ErrorList) Less(i, j int) boolfunc (self *ErrorList) Reset()Reset resets an ErrorList to no errors.
func (self ErrorList) Sort()func (self ErrorList) Swap(i, j int)type Mode uintA Mode value is a set of flags (or 0). They control optional parser functionality.