package parser

Import Path
	github.com/dop251/goja/parser (on go.dev)

Dependency Relation
	imports 19 packages, and imported by one package

Involved Source Files error.go expression.go lexer.go Package parser implements a parser for JavaScript. 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) # Warning The parser and AST interfaces are still works-in-progress (particularly where node types are concerned) and may change in the future. regexp.go scope.go statement.go
Package-Level Type Names (total 6)
/* sort by: | */
An Error represents a parsing error. It includes the position where the error occurred and a message/description. Message string Position file.Position ( Error) Error() string Error : error
ErrorList is a list of *Errors. Add adds an Error with given position and message to an ErrorList. Err returns an error equivalent to this ErrorList. If the list is empty, Err returns nil. Error implements the Error interface. ( ErrorList) Len() int ( ErrorList) Less(i, j int) bool Reset resets an ErrorList to no errors. ( ErrorList) Sort() ( ErrorList) Swap(i, j int) ErrorList : error ErrorList : sort.Interface
A Mode value is a set of flags (or 0). They control optional parser functionality. func ParseFile(fileSet *file.FileSet, filename string, src interface{}, mode Mode, options ...Option) (*ast.Program, error) const IgnoreRegExpErrors
Option represents one of the options for the parser to use in the Parse methods. Currently supported are: WithDisableSourceMaps and WithSourceMapLoader. func WithSourceMapLoader(loader func(path string) ([]byte, error)) Option func ParseFile(fileSet *file.FileSet, filename string, src interface{}, mode Mode, options ...Option) (*ast.Program, error) func ParseFunction(parameterList, body string, options ...Option) (*ast.FunctionLiteral, error) func github.com/dop251/goja.Parse(name, src string, options ...Option) (prg *js_ast.Program, err error) func github.com/dop251/goja.(*Runtime).SetParserOptions(opts ...Option)
( RegexpErrorIncompatible) Error() string RegexpErrorIncompatible : error
( RegexpSyntaxError) Error() string RegexpSyntaxError : error
Package-Level Functions (total 7)
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)
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)
TransformRegExp transforms a JavaScript pattern into a Go "regexp" pattern. re2 (Go) cannot do backtracking, so the presence of a lookahead (?=) (?!) or backreference (\1, \2, ...) will cause an error. re2 (Go) has a different definition for \s: [\t\n\f\r ]. The JavaScript definition, on the other hand, also includes \v, Unicode "Separator, Space", etc. If the pattern is valid, but incompatible (contains a lookahead or backreference), then this function returns an empty string an error of type RegexpErrorIncompatible. If the pattern is invalid (not valid even in JavaScript), then this function returns an empty string and a generic error.
WithDisableSourceMaps is an option to disable source maps support. May save a bit of time when source maps are not in use.
WithSourceMapLoader is an option to set a custom source map loader. The loader will be given a path or a URL from the sourceMappingURL. If sourceMappingURL is not absolute it is resolved relatively to the name of the file being parsed. Any error returned by the loader will fail the parsing. Note that setting this to nil does not disable source map support, there is a default loader which reads from the filesystem. Use WithDisableSourceMaps to disable source map support.
Package-Level Constants (total 3)
const IgnoreRegExpErrors Mode = 1 // Ignore RegExp compatibility errors (allow backtracking)
const Re2Dot = "[^\r\n\u2028\u2029]"
const WhitespaceChars = " \f\n\r\t\v\u00a0\u1680\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2...