Example #1
0
// 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 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 mode parameter
// determines how comments, illegal characters, and semicolons are handled.
//
// Note that Init may call err if there is an error in the first character
// of the file.
//
func (S *Scanner) Init(file *token.File, src []byte, err ErrorHandler, mode uint) {
	// Explicitly initialize all fields since a scanner may be reused.
	if file.Size() != len(src) {
		panic("file size does not match src len")
	}
	S.file = file
	S.dir, _ = filepath.Split(file.Name())
	S.src = src
	S.err = err
	S.mode = mode

	S.ch = ' '
	S.offset = 0
	S.rdOffset = 0
	S.lineOffset = 0
	S.runeColumn = 0
	S.insertSemi = false
	S.ErrorCount = 0

	S.prevState.Offset = -1 // declare previous scanning state invalid. Can't call prev() at first or repeatedly.

	file.SetLinesForContent(src) // calculate line-end positions.
	S.Next()
}
/*
Stores the file name and source line position info into the ast.File in a form that can
be persisted with the ast.File when it is GOB-serialized into a .rlc file.
*/
func (f *File) StoreSourceFilePositionInfo(file *token.File) {
	f.FileName = file.Name()
	f.FileSize = file.Size()
	f.FileLines = file.Lines()
}