scanner

package
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 14, 2024 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

A scanner for Go source text. Takes a []byte as source which can then be tokenized through repeated calls to the Scan function. For a sample use of a scanner, see the implementation of Tokenize.

Index

Constants

View Source
const (
	ScanComments      = 1 << iota // return comments as COMMENT tokens
	AllowIllegalChars             // do not report an error for illegal chars
	InsertSemis                   // automatically insert semicolons
)

The mode parameter to the Init function is a set of flags (or 0). They control scanner behavior.

Variables

This section is empty.

Functions

This section is empty.

Types

type Error

type Error struct {
	Pos token.Position
	Msg string
}

Within ErrorVector, an error is represented by an Error node. The position Pos, if valid, points to the beginning of the offending token, and the error condition is described by Msg.

func (*Error) String

func (e *Error) String() string

type ErrorHandler

type ErrorHandler interface {
	Error(pos token.Position, msg string)
}

An implementation of an ErrorHandler may be provided to the Scanner. If a syntax error is encountered and a handler was installed, Error is called with a position and an error message. The position points to the beginning of the offending token.

type Scanner

type Scanner struct {

	// public state - ok to modify
	ErrorCount int // number of errors encountered
	// contains filtered or unexported fields
}

A Scanner holds the scanner's internal state while processing a given text. It can be allocated as part of another data structure but must be initialized via Init before use. For a sample use, see the implementation of Tokenize.

func (*Scanner) Init

func (S *Scanner) Init(src []byte, tokenMap *token.TokenMap)

Init prepares the scanner S to tokenize the text src. Calls to Scan will use the error handler err if they encounter a syntax error and err is not nil. Also, for each error encountered, the Scanner field ErrorCount is incremented by one. The filename parameter is used as filename in the token.Position returned by Scan for each token. The mode parameter determines how comments and illegal characters are handled.

func (*Scanner) Scan

func (S *Scanner) Scan() (*token.Token, token.Position)

Scan scans the next token and returns the token position pos, the token tok, and the literal text lit corresponding to the token. The source end is indicated by token.EOF.

For more tolerant parsing, Scan will return a valid token if possible even if a syntax error was encountered. Thus, even if the resulting token sequence contains no illegal tokens, a client may not assume that no error occurred. Instead it must check the scanner's ErrorCount or the number of calls of the error handler, if there was one installed.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL