Documentation
¶
Overview ¶
Package parser implements a parser for JavaScript.
import (
"github.com/peoplegroupservices/axonasp/v2/jscript/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.
- AxonASP Server
- Copyright (C) 2026 G3pix Ltda. All rights reserved. *
- Developed by Lucas Guimarães - G3pix Ltda
- Contact: https://g3pix.com.br
- Project URL: https://g3pix.com.br/axonasp *
- This Source Code Form is subject to the terms of the Mozilla Public
- License, v. 2.0. If a copy of the MPL was not distributed with this
- file, You can obtain one at https://mozilla.org/MPL/2.0/.
Index ¶
- Constants
- func IsIdentifier(s string) bool
- func ParseFile(fileSet *file.FileSet, filename string, src any, mode Mode, options ...Option) (*ast.Program, error)
- func ParseFunction(parameterList, body string, options ...Option) (*ast.FunctionLiteral, error)
- func ReadSource(filename string, src any) ([]byte, error)
- func TransformRegExp(pattern string, dotAll, unicode bool) (transformed string, err error)
- func WithDisableSourceMaps(opts *options)
- type Error
- type ErrorList
- func (self *ErrorList) Add(position file.Position, msg string)
- func (self ErrorList) Err() error
- func (self ErrorList) Error() string
- func (self ErrorList) Len() int
- func (self ErrorList) Less(i, j int) bool
- func (self *ErrorList) Reset()
- func (self ErrorList) Sort()
- func (self ErrorList) Swap(i, j int)
- type Mode
- type Option
- type RegexpErrorIncompatible
- type RegexpSyntaxError
- type UnicodePropertyExpander
Constants ¶
const ( WhitespaceChars = " \f\n\r\t\v\u00a0\u1680\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u2028\u2029\u202f\u205f\u3000\ufeff" Re2Dot = "[^\r\n\u2028\u2029]" )
Variables ¶
This section is empty.
Functions ¶
func IsIdentifier ¶
func ParseFile ¶
func ParseFile(fileSet *file.FileSet, filename string, src any, mode Mode, options ...Option) (*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 ¶
func ParseFunction(parameterList, body string, options ...Option) (*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 TransformRegExp ¶
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.
func WithDisableSourceMaps ¶
func WithDisableSourceMaps(opts *options)
WithDisableSourceMaps is an option to disable source maps support. May save a bit of time when source maps are not in use.
Types ¶
type Error ¶
An Error represents a parsing error. It includes the position where the error occurred and a message/description.
type ErrorList ¶
type ErrorList []*Error
ErrorList is a list of *Errors.
type Mode ¶
type Mode uint
A Mode value is a set of flags (or 0). They control optional parser functionality.
type Option ¶
type Option func(*options)
Option represents one of the options for the parser to use in the Parse methods. Currently supported are: WithDisableSourceMaps and WithSourceMapLoader.
func WithSourceMapLoader ¶
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.
type RegexpErrorIncompatible ¶
type RegexpErrorIncompatible struct {
// contains filtered or unexported fields
}
type RegexpSyntaxError ¶
type RegexpSyntaxError struct {
// contains filtered or unexported fields
}
type UnicodePropertyExpander ¶
type UnicodePropertyExpander struct {
// contains filtered or unexported fields
}
UnicodePropertyExpander expands Unicode property escapes like \p{Letter} to character classes.
func NewUnicodePropertyExpander ¶
func NewUnicodePropertyExpander() *UnicodePropertyExpander
NewUnicodePropertyExpander creates a new expander with built-in Unicode properties.
func (*UnicodePropertyExpander) ExpandProperty ¶
func (e *UnicodePropertyExpander) ExpandProperty(propertyName string, negate bool) (string, error)
ExpandProperty expands a Unicode property name to a character class.