|
1 year ago | |
---|---|---|
.. | ||
README.md | 1 year ago | |
ast.go | 1 year ago | |
lex.go | 1 year ago | |
parse.go | 1 year ago | |
table.go | 1 year ago | |
tokentype.go | 1 year ago | |
util.go | 1 year ago | |
walk.go | 1 year ago |
This package is a JS lexer (ECMAScript 2020) written in Go. It follows the specification at ECMAScript 2020 Language Specification. The lexer takes an io.Reader and converts it into tokens until the EOF.
Run the following command
go get -u github.com/tdewolff/parse/v2/js
or add the following import and run project with go get
import "github.com/tdewolff/parse/v2/js"
The following initializes a new Lexer with io.Reader r
:
l := js.NewLexer(parse.NewInput(r))
To tokenize until EOF an error, use:
for {
tt, text := l.Next()
switch tt {
case js.ErrorToken:
// error or EOF set in l.Err()
return
// ...
}
}
The ECMAScript specification for PunctuatorToken
(of which the /
and /=
symbols) and RegExpToken
depend on a parser state to differentiate between the two. The lexer will always parse the first token as /
or /=
operator, upon which the parser can rescan that token to scan a regular expression using RegExp()
.
package main
import (
"os"
"github.com/tdewolff/parse/v2/js"
)
// Tokenize JS from stdin.
func main() {
l := js.NewLexer(parse.NewInput(os.Stdin))
for {
tt, text := l.Next()
switch tt {
case js.ErrorToken:
if l.Err() != io.EOF {
fmt.Println("Error on line", l.Line(), ":", l.Err())
}
return
case js.IdentifierToken:
fmt.Println("Identifier", string(text))
case js.NumericToken:
fmt.Println("Numeric", string(text))
// ...
}
}
}
The following parses a file and returns an abstract syntax tree (AST).
ast, err := js.NewParser(parse.NewInputString("if (state == 5) { console.log('In state five'); }"))
See ast.go for all available data structures that can represent the abstact syntax tree.
Released under the MIT license.