source

package
v0.0.0-...-9e081f2 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2024 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package source defines source file an source queue for parsers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NormalizeNls

func NormalizeNls(content *[]byte)

NormalizeNls replaces all occurrences of "\r" and "\r\n" with "\n".

Types

type Pos

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

Pos combines captured source, position, line, and column number corresponding to that position. Zero value means no source and position information available.

func NewPos

func NewPos(s *Source, pos int) Pos

NewPos returns Pos structure. Returns zero value if s is nil.

func (Pos) Col

func (p Pos) Col() int

Col returns captured 1-based column number or 0.

func (Pos) Line

func (p Pos) Line() int

Line returns captured 1-based line number or 0.

func (Pos) Pos

func (p Pos) Pos() int

Pos returns captured position in source or 0.

func (Pos) Source

func (p Pos) Source() *Source

Source returns captured source or nil.

func (Pos) SourceName

func (p Pos) SourceName() string

SourceName returns captured source name or empty string.

type Queue

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

Queue represents a queue of source files to be processed.

func NewQueue

func NewQueue() *Queue

NewQueue creates empty queue.

func (*Queue) Append

func (q *Queue) Append(s *Source) *Queue

Append adds new source to the end of the queue. Does nothing if s is nil. Does not add empty source if the queue is not empty.

func (*Queue) ContentPos

func (q *Queue) ContentPos() ([]byte, int)

ContentPos returns content of current source and current position, or (nil, 0) if the queue is empty.

func (*Queue) Eof

func (q *Queue) Eof() bool

Eof returns true if the queue is empty or current source position is beyond the end of current source.

func (*Queue) IsEmpty

func (q *Queue) IsEmpty() bool

IsEmpty returns true if the queue is empty (contains no sources) and false otherwise.

func (*Queue) LineCol

func (q *Queue) LineCol(pos int) (line, col int)

LineCol returns line and column number for current source position. Returns (0, 0) if the queue is empty.

func (*Queue) NextSource

func (q *Queue) NextSource() bool

NextSource discards current source from the queue. The next source (if there is one) becomes the current one and its saved current position is restored. Returns true if the queue is not empty.

func (*Queue) Pos

func (q *Queue) Pos() int

Pos returns current position in current source or 0 if the queue is empty.

func (*Queue) Prepend

func (q *Queue) Prepend(s *Source) *Queue

Prepend adds new source to the beginning of the queue. Current position for current source (if there is one) is saved, added source becomes the current one. Does nothing if s is nil. Does not add empty source if the queue is not empty.

func (*Queue) Rewind

func (q *Queue) Rewind(size int)

Rewind decreases current source position by given amount of bytes. New position will be ≥ 0. Does nothing if size is ≤ 0 or the queue is empty.

func (*Queue) Seek

func (q *Queue) Seek(pos int)

Seek changes current source position to given value. New position is adjusted so that 0 ≤ position ≤ content length. Does nothing if the queue is empty.

func (*Queue) Skip

func (q *Queue) Skip(size int)

Skip increases current source position by given amount of bytes. New position will not exceed current source length. Does nothing if size is ≤ 0 or the queue is empty.

func (*Queue) Source

func (q *Queue) Source() *Source

Source returns current (i.e. first) source in the queue or nil if the queue is empty.

func (*Queue) SourceName

func (q *Queue) SourceName() string

SourceName returns the name of current source or empty string if the queue is empty.

func (*Queue) SourcePos

func (q *Queue) SourcePos() Pos

SourcePos returns current source and current position in it. Returns zero value if the queue is empty.

type Source

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

Source represents single source file.

func New

func New(name string, content []byte) *Source

New creates new source. Name may be any string identifying the source, does not have to be unique, may be empty. Content should be a valid UTF-8 encoded text, lines should be separated by "\n" rune. Content should not be modified.

func (*Source) Content

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

Content returns source content.

func (*Source) Len

func (s *Source) Len() int

Len returns source content length in bytes.

func (*Source) LineCol

func (s *Source) LineCol(pos int) (line, col int)

LineCol returns line and column number (both 1-based) of rune starting at given position in the source content. Negative position is treated as 0, position equal to or higher than length of content is treated as position right after EoF.

func (*Source) Name

func (s *Source) Name() string

Name returns source name.

func (*Source) Pos

func (s *Source) Pos(line, col int) int

Pos returns position in source content corresponding to given line and column. Returns 0 for lines or columns < 1. Returns content length for line exceeding total number of lines. Returns position after the last rune in line for column exceeding number of runes in line.

Jump to

Keyboard shortcuts

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