source

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2025 License: MIT Imports: 11 Imported by: 4

Documentation

Index

Constants

View Source
const BOM = 0xFEFF

BOM byte order mark

Variables

View Source
var (
	ErrIllegalMinimalLineNumber = errors.New("illegal line number (line numbering starts at 1)")
	ErrIllegalLineNumber        = errors.New("illegal line number")
	ErrIllegalFileOffset        = errors.New("illegal file offset")
	ErrIllegalPosition          = errors.New("illegal position")
)

Functions

func MustFileLine

func MustFileLine(f *File, pos Pos) int

func MustFileOffset

func MustFileOffset(f *File, pos Pos) int

Types

type Data

type Data struct {
	// contains filtered or unexported fields
}

Data represents a File data

func NewData

func NewData(data []byte) *Data

func (*Data) Bytes

func (d *Data) Bytes() []byte

Bytes return the data bytes

func (*Data) LineData

func (d *Data) LineData(line int) (r []byte, _ error)

LineData return line data, of error if isn't valid line.

func (*Data) LineOffset

func (d *Data) LineOffset(line int) (offset int, _ error)

LineOffset returns the offset of the first character in the line or error if line number isn't valid.

func (*Data) LineSliceDataUpDown

func (d *Data) LineSliceDataUpDown(line, upCount, downCount int) (up, down []*LineData, s []byte)

LineSliceDataUpDown returns data of line and slices of up and down lines

func (*Data) LinesData

func (d *Data) LinesData(lineStart, count int) (s []*LineData)

LinesData return slice data of lines

func (*Data) NumLines

func (d *Data) NumLines() int

NumLines return a number of lines

func (*Data) Pack

func (d *Data) Pack(line, column int) (offset int, err error)

Pack returns the offset of position by line and column.

func (*Data) Slice

func (d *Data) Slice(startLine, numLines int) (_ []byte, err error)

func (*Data) SplitLines

func (d *Data) SplitLines() (r [][]byte)

SplitLines return a splited data into lines

func (*Data) ToString

func (d *Data) ToString() string

ToString return the data as string without copy

func (*Data) TraceLines

func (d *Data) TraceLines(s io.Writer, line, column, up, down int)

func (*Data) Unpack

func (d *Data) Unpack(offset int) (line, column int)

Unpack converts offset to line and column pair.

type File

type File struct {

	// File name as provided to AddFile
	Name string
	// SourcePos value range for this file is [base...base+size]
	Base int
	// File size as provided to AddFile
	Size int
	// Lines contains the offset of the first character for each line
	// (the first entry is always 0)
	Lines []int
	// Index is a index of `set`
	Index int
	// Data is data
	Data *Data
	// contains filtered or unexported fields
}

File represents a source file.

func (*File) AddLine

func (f *File) AddLine(offset int)

AddLine adds a new line by first data offset (after LF char).

func (*File) DataPosition

func (f *File) DataPosition(p Pos) (pos FilePos, err error)

DataPosition translates the file set position into the file data position.

func (*File) FileSetPos

func (f *File) FileSetPos(offset int) (Pos, error)

FileSetPos returns the position in the file set.

func (*File) LineCount

func (f *File) LineCount() int

LineCount returns the current number of lines.

func (*File) Offset

func (f *File) Offset(p Pos) (int, error)

Offset translates the file set position into the file offset.

func (*File) Position

func (f *File) Position(p Pos) (pos FilePos, err error)

Position translates the file set position into the file position.

func (*File) SafePosition

func (f *File) SafePosition(p Pos) (pos FilePos)

SafePosition return a position without validations

func (*File) Set

func (f *File) Set() *FileSet

Set returns FileSet.

func (*File) Slice

func (f *File) Slice(slicedSet *FileSet, name string, startLine, numLines int) (_ *SliceFile, err error)

func (*File) Unpack

func (f *File) Unpack(offset int) (filename string, line, column int)

Unpack casts offset to filename, line and column.

type FilePos

type FilePos struct {
	File   *File // filename, if any
	Offset int   // offset, starting at 0
	Line   int   // line number, starting at 1
	Column int   // column number, starting at 1 (byte count)
}

FilePos represents a position information in the file.

func MustFilePosition

func MustFilePosition(f *File, pos Pos) FilePos

func MustFilePositionFromOffset

func MustFilePositionFromOffset(f *File, offset int) FilePos

func (FilePos) Dump

func (p FilePos) Dump(w io.Writer, up, down int)

func (FilePos) FileName

func (p FilePos) FileName() string

func (FilePos) IsValid

func (p FilePos) IsValid() bool

IsValid returns true if the position is valid.

func (FilePos) Pos

func (p FilePos) Pos() Pos

func (FilePos) PositionString

func (p FilePos) PositionString() (s string)

func (FilePos) SourceLineDataRange

func (p FilePos) SourceLineDataRange(up, down int) (line *LineData, upLines, downLines []*LineData)

func (FilePos) String

func (p FilePos) String() string

String returns a string in one of several forms:

[empty]             no valid pos
file:line:column    valid position with file name
file:line           valid position with file name but no column (column == 0)
line:column         valid position without file name
line                valid position without file name and no column (column == 0)
file                invalid position with file name
-                   invalid position without file name

func (FilePos) TraceLines

func (p FilePos) TraceLines(w io.Writer, up, down int)

type FilePosStackTrace

type FilePosStackTrace []FilePos

FilePosStackTrace is the stack of source file positions.

func (FilePosStackTrace) Format

func (st FilePosStackTrace) Format(s fmt.State, verb rune)

Format formats the FilePosStackTrace to the fmt.Formatter interface.

type FileReaderOption

type FileReaderOption func(r *Reader)

func FileReaderWithData

func FileReaderWithData(data any) FileReaderOption

func FileReaderWithSkipWhitespaceFunc

func FileReaderWithSkipWhitespaceFunc(f func(fr *Reader)) FileReaderOption

type FileSet

type FileSet struct {
	Base     int     // base offset for the next file
	Files    []*File // list of files in the order added to the set
	LastFile *File   // cache of last file looked up
}

FileSet represents a set of source files.

func NewFileSet

func NewFileSet() *FileSet

NewFileSet creates a new file set.

func (*FileSet) AddFile

func (s *FileSet) AddFile(filename string, base, size int) *File

AddFile adds a new file in the file set.

func (*FileSet) AddFileData

func (s *FileSet) AddFileData(filename string, base int, data []byte) *File

AddFileData adds a new file in the file set with data.

func (*FileSet) AppendFileData

func (s *FileSet) AppendFileData(filename string, data []byte) *File

AppendFileData appends a new file in the file set with data.

func (*FileSet) File

func (s *FileSet) File(p Pos) (f *File)

File returns the file that contains the position p. If no such file is found (for instance for p == NoPos), the result is nil.

func (*FileSet) Position

func (s *FileSet) Position(p Pos) (pos FilePos)

Position converts a SourcePos p in the fileset into a FilePos value.

type LineData

type LineData struct {
	Line int
	Data []byte
}

LineData represents a Data of line

func (*LineData) CreateSpace

func (d *LineData) CreateSpace(prefix []rune, column int) []byte

type NextHandlers

type NextHandlers struct {
	LineEndHandlers     []func()
	PostLineEndHandlers []func()
	EOFHandlers         []func(r *Reader)
}

func (*NextHandlers) CallLineEndHandlers

func (h *NextHandlers) CallLineEndHandlers()

func (*NextHandlers) CallPostLineEndHandlers

func (h *NextHandlers) CallPostLineEndHandlers()

func (*NextHandlers) EOFHandler

func (h *NextHandlers) EOFHandler(f func(r *Reader))

func (*NextHandlers) LineEndHandler

func (h *NextHandlers) LineEndHandler(f func())

func (*NextHandlers) PostLineEndHandler

func (h *NextHandlers) PostLineEndHandler(f func())

type NumberType

type NumberType uint8
const (
	Int NumberType = iota
	Uint
	Float
	Decimal
)

type Pos

type Pos int

Pos represents a position in the file set.

const NoPos Pos = 0

NoPos represents an invalid position.

func MustFileLineStartPos

func MustFileLineStartPos(f *File, line int) Pos

func MustFileSetPos

func MustFileSetPos(f *File, offset int) Pos

func (Pos) IsValid

func (p Pos) IsValid() bool

IsValid returns true if the position is valid.

type PosStackTrace

type PosStackTrace []Pos

PosStackTrace is the stack of source positions

func (PosStackTrace) Format

func (t PosStackTrace) Format(fs *FileSet, s fmt.State, verb rune)

Format formats the FilePosStackTrace to the fmt.Formatter

type Reader

type Reader struct {
	Data       any
	File       *File
	Ch         rune // current character
	Offset     int  // character offset
	ReadOffset int  // reading offset (position after current character)

	NewLineEscaped     bool
	Src                []byte
	NewLineEscape      func() bool
	SkipWhitespaceFunc func(r *Reader)

	NextHandlers
	// contains filtered or unexported fields
}

func NewFileReader

func NewFileReader(file *File, option ...FileReaderOption) (fr *Reader)

func (*Reader) CallEOFHandlers

func (s *Reader) CallEOFHandlers()

func (*Reader) Error

func (s *Reader) Error(offset int, msg string)

func (*Reader) ErrorCount

func (s *Reader) ErrorCount() int

ErrorCount returns the number of errors.

func (*Reader) ErrorHandler

func (s *Reader) ErrorHandler(h ...ScannerErrorHandler)

func (*Reader) Expect

func (s *Reader) Expect(ch rune, msg string) bool

func (*Reader) ExpectError

func (s *Reader) ExpectError(msg string)

func (*Reader) FindLineEnd

func (s *Reader) FindLineEnd() bool

func (*Reader) HasError

func (s *Reader) HasError() bool

func (*Reader) IndexOfInlineNoSpace

func (s *Reader) IndexOfInlineNoSpace() int

func (*Reader) IsSpace

func (s *Reader) IsSpace() bool

func (*Reader) Next

func (s *Reader) Next()

func (*Reader) NextC

func (s *Reader) NextC(count int)

func (*Reader) NextNoSpace

func (s *Reader) NextNoSpace()

func (*Reader) NextPosOf

func (s *Reader) NextPosOf(b byte) (end int)

func (*Reader) NextTo

func (s *Reader) NextTo(v string)

func (*Reader) Peek

func (s *Reader) Peek() byte

func (*Reader) PeekAtEndLine

func (s *Reader) PeekAtEndLine() (start, end int)

func (*Reader) PeekEq

func (s *Reader) PeekEq(str string) bool

func (*Reader) PeekIdentEq

func (s *Reader) PeekIdentEq(to string, skip int) bool

func (*Reader) PeekInlineNoSpace

func (s *Reader) PeekInlineNoSpace() byte

func (*Reader) PeekN

func (s *Reader) PeekN(n int) []byte

func (*Reader) PeekNoSingleSpaceEq

func (s *Reader) PeekNoSingleSpaceEq(to string, skip int) (length int)

func (*Reader) PeekNoSpace

func (s *Reader) PeekNoSpace() byte

func (*Reader) PeekNoSpaceEq

func (s *Reader) PeekNoSpaceEq(to string, skip int) bool

func (*Reader) PeekNoSpaceN

func (s *Reader) PeekNoSpaceN(n int) []byte

func (*Reader) Read

func (s *Reader) Read(b []byte) (n int, err error)

func (*Reader) ReadAt

func (s *Reader) ReadAt(b rune) []byte

func (*Reader) ReadAtMany

func (s *Reader) ReadAtMany(quote []byte) []byte

func (*Reader) ReadCount

func (s *Reader) ReadCount(q int) []byte

func (Reader) ReadEscape

func (s Reader) ReadEscape(quote rune) bool

func (*Reader) ReadWhen

func (s *Reader) ReadWhen(b rune) []byte

func (*Reader) ScanMantissa

func (s *Reader) ScanMantissa(base int)

func (*Reader) ScanNumber

func (s *Reader) ScanNumber(seenDecimalPoint bool) (tok NumberType, lit string)

func (*Reader) ScanRawString

func (s *Reader) ScanRawString() (string, bool)

func (*Reader) ScanRune

func (s *Reader) ScanRune() string

func (*Reader) ScanString

func (s *Reader) ScanString() string

func (*Reader) ScanStringDelimiter

func (s *Reader) ScanStringDelimiter(delimiter rune) string

func (*Reader) Skip

func (s *Reader) Skip(str string)

func (*Reader) SkipWhitespace

func (s *Reader) SkipWhitespace()

func (*Reader) Source

func (s *Reader) Source() []byte

func (*Reader) SourceFile

func (s *Reader) SourceFile() *File

func (*Reader) Start

func (s *Reader) Start()

type ScannerErrorHandler

type ScannerErrorHandler func(pos FilePos, msg string)

ScannerErrorHandler is an error handler for the scanner.

type SliceFile

type SliceFile struct {
	*File
	// contains filtered or unexported fields
}

SliceFile represets a slice source File

func (*SliceFile) CastPos

func (f *SliceFile) CastPos(srcPos FilePos) (pos FilePos, err error)

CastPos cast Pos from source File to sliced File Pos

type StartEndDelimiter

type StartEndDelimiter struct {
	Start []rune
	End   []rune
}

func (*StartEndDelimiter) Ends

func (m *StartEndDelimiter) Ends(b []byte) bool

func (*StartEndDelimiter) EndsR

func (m *StartEndDelimiter) EndsR(r rune, b []byte) bool

func (*StartEndDelimiter) IsZero

func (m *StartEndDelimiter) IsZero() bool

func (*StartEndDelimiter) Starts

func (m *StartEndDelimiter) Starts(r rune, b []byte) bool

func (*StartEndDelimiter) String

func (m *StartEndDelimiter) String() string

func (*StartEndDelimiter) Strings

func (m *StartEndDelimiter) Strings() (start, end string)

func (*StartEndDelimiter) WrapString

func (m *StartEndDelimiter) WrapString(s string) string

Jump to

Keyboard shortcuts

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