August 2019
Beginner to intermediate
798 pages
17h 2m
English
A parser reads the output of a scanner (tokens) in order to generate a structure from those tokens. Parsers use a grammar that describes a programming language to make sure that the given tokens compose a valid program. That structure is represented as a tree, which is the AST.
The use of the go/parser package that processes the output of go/token is illustrated in goParser.go, which is going to be presented in four parts.
The first part of goParser.go is as follows:
package main
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"os"
"strings"
)
type visitor int
The second part of goParser.go contains the following Go code:
func (v visitor) Visit(n ast.Node) ast.Visitor { if n == nil { return nil } fmt.Printf("%s%T\n", ...Read now
Unlock full access