func (p *Parser) Parse() (ast.Document, error) {
	var document ast.Document
	p.token = p.lexer.Scan()
	var definitions *ast.Definitions
	typeExtensions := make(map[string]struct{})
	for {
		definition, err := p.parseDefinition(document)
		if err != nil {
			return ast.Document{}, err
		}
		definitions = definitions.Add(definition)
		switch definition.Kind {
		case ast.DefinitionKindExecutable:
			switch definition.ExecutableDefinition.Kind {
			case ast.ExecutableDefinitionKindOperation:
				document.OperationDefinitions++
			case ast.ExecutableDefinitionKindFragment:
				document.FragmentDefinitions++
			}
		case ast.DefinitionKindTypeSystem:
			switch definition.TypeSystemDefinition.Kind {
			case ast.TypeSystemDefinitionKindDirective:
				document.DirectiveDefinitions++
			case ast.TypeSystemDefinitionKindSchema:
				document.SchemaDefinitions++
			case ast.TypeSystemDefinitionKindType:
				document.TypeDefinitions++
			}
		case ast.DefinitionKindTypeSystemExtension:
			switch definition.TypeSystemExtension.Kind {
			case ast.TypeSystemExtensionKindSchema:
				document.SchemaExtensions++
			case ast.TypeSystemExtensionKindType:
				typeExtensions[definition.TypeSystemExtension.TypeExtension.Name] = struct{}{}
			}
		}
		if p.peek0(TokenKindIllegal) {
			return ast.Document{}, p.unexpected(p.token)
		}
		if p.peek0(TokenKindEOF) {
			break
		}
	}
	document.TypeExtensions = int32(len(typeExtensions))
	document.Definitions = definitions.Reverse()
	return document, nil
}
Parse loops over the lexically analysed tokens produced by the lexer from the raw bytes of input
and parses them into an AST of the GraphQL Document which it returns.
package language
import (
	"bytes"
	"errors"
	"strconv"
	"strings"
	"github.com/bucketd/go-graphqlparser/ast"
)
type Parser struct {
	lexer *Lexer
	token Token
}
func NewParser(input []byte) *Parser {
	return &Parser{
		lexer: NewLexer(input),
	}
}