scanner

package
v0.0.0-...-0e20d73 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2016 License: BSD-3-Clause Imports: 7 Imported by: 0

Documentation

Overview

Package scanner implements a scanner for zolang.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func PrintError

func PrintError(w io.Writer, err error)

PrintError is a utility function that prints a list of errors to w, one error per line, if the err parameter is an ErrorList. Otherwise it prints the err string.

Types

type Error

type Error struct {
	Pos token.Position
	Msg string
}

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

func (Error) Error

func (e Error) Error() string

Error implements the error interface.

type ErrorHandler

type ErrorHandler func(pos token.Position, msg string)

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

type ErrorList

type ErrorList []*Error

ErrorList is a list of *Errors. The zero value for an ErrorList is an empty ErrorList ready to use.

func (*ErrorList) Add

func (p *ErrorList) Add(pos token.Position, msg string)

Add adds an Error with given position and error message to an ErrorList.

func (ErrorList) Err

func (l ErrorList) Err() error

Err returns an error equivalent to this error list. If the list is empty, Error returns nil.

func (ErrorList) Error

func (l ErrorList) Error() string

An ErrorList implements the error interface.

func (ErrorList) Len

func (l ErrorList) Len() int

ErrorList implements the sort interface.

func (ErrorList) Less

func (l ErrorList) Less(i, j int) bool

func (*ErrorList) RemoveMultiples

func (p *ErrorList) RemoveMultiples()

RemoveMultiples sorts an ErrorList and removes all but the first error per line.

func (*ErrorList) Reset

func (p *ErrorList) Reset()

Reset resets an ErrorList to no errors.

func (ErrorList) Sort

func (l ErrorList) Sort()

Sort sorts an ErrorList.

func (ErrorList) Swap

func (l ErrorList) Swap(i, j int)

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.

func (*Scanner) Init

func (s *Scanner) Init(file *token.File, src []byte, err ErrorHandler)

Init prepares the scanner s to tokenize the text src by setting the scanner at the beginning of src. The scanner uses the file set file for position information and it adds line information for each line. It is ok to re-use the same file when re-scanning the same file as line information which is already present is ignored. Init causes a panic if the file size does not match the src size.

Calls to Scan will invoke 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.

Note that Init may call err if there is an error in the fisrt character of the file.

func (*Scanner) Scan

func (s *Scanner) Scan() (pos token.Pos, tok token.Token, lit string)

Scan scans the next token and returns the token position, the token, and its literal string if applicable. The source end is indicated by token.EOF.

If the returned token is literal (token.IDENT, token.BOOL, token.INT, token.FLOAT, token.STRING) or token.COMMENT, the literal string has the corresponding value.

If the returned token is token.ILLEGAL, the literal string is the offending character.

In all other cases, Scan returns an empty literal string.

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 token, 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.

Scan adds line information to the file added to the file set with Init. Token positions are relative to that file and thus relative to the file set.

Jump to

Keyboard shortcuts

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