scanner

package module
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2026 License: BSD-3-Clause Imports: 8 Imported by: 8

README

scanner

import path "modernc.org/scanner"

Package scanner provides some common scanner stuff.

Documentation

Overview

Package scanner provides some common scanner stuff.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ErrList

type ErrList []ErrWithPosition

ErrList is a list of errors.

func (*ErrList) AddErr

func (e *ErrList) AddErr(pos token.Position, msg string, args ...interface{})

AddErr adds an error message associated with an optional position.

func (ErrList) Err

func (e ErrList) Err() (r error)

Err returns e if len(e) != 0 or nil.

func (ErrList) Error

func (e ErrList) Error() string

Error implements error.

type ErrWithPosition

type ErrWithPosition struct {
	Pos token.Position
	Err error
}

ErrWithPosition augments an error with optional position information.

func (ErrWithPosition) Error

func (e ErrWithPosition) Error() string

Error implements error.

type RecScanner added in v1.3.0

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

RecScanner represents the data structures and methods common to some/many lexical scanners, specialized for using scan functions produced by the modernc.org/rec compiler.

func NewRecScanner added in v1.3.0

func NewRecScanner(name string, buf []byte, scan func(s []byte) (id, length int), whiteSpace int) *RecScanner

NewRecScanner returns a newly created RecScanner. The 'name' argument is used to report positions. 'buf' becomes owned by the scanner and should not be mutated by the caller afterwards.

The 'scan' function that is compatible with functions that the modernc.org/rec compiler produces. 'whiteSpace' is the id the 'scan' function returns for white space. The production for white space does not have to handle sequences of white space. RecScanner handles sequences of white space automatically. You can still write your regular expression for white_space like for example

`(// |\t|\n|\r)*`

or

`( |\t|\n|\r)*`

But you can also write it simply as

` |\t|\n|\r`

with the same effect. This helps avoiding the problems described at egg issue 1.

Calling AddLine is done automatically by the RecScanner.Scan method.

func (*RecScanner) AddErr added in v1.3.0

func (s *RecScanner) AddErr(pos token.Position, msg string, args ...interface{})

AddErr registers an error.

func (*RecScanner) AddLine added in v1.3.0

func (s *RecScanner) AddLine(offset int)

AddLine adds the line offset for a new line. The line offset must be larger than the offset for the previous line and smaller than the scanner buffer size; otherwise the line offset is ignored.

func (*RecScanner) AddLineColumnInfo added in v1.3.0

func (s *RecScanner) AddLineColumnInfo(offset int, filename string, line, column int)

AddLineColumnInfo adds alternative file, line, and column number information for a given scanner buffer offset. The offset must be larger than the offset for the previously added alternative line info and smaller than the scanner buffer size; otherwise the information is ignored.

AddLineColumnInfo is typically used to register alternative position information for line directives such as //line filename:line:column.

func (*RecScanner) Ch added in v1.3.0

func (s *RecScanner) Ch(n int) rune

Ch returns the Ch field of the n-th token in 's'. Ch panics if n is out of range [0..Len()-1].

func (*RecScanner) Err added in v1.3.0

func (s *RecScanner) Err() error

Err reports any errors from reported by AddErr()

func (*RecScanner) Len added in v1.3.0

func (s *RecScanner) Len() int

Len reports the number of tokens in 's'.

func (*RecScanner) Position added in v1.3.0

func (s *RecScanner) Position(offset int) (r token.Position)

Position returns the position determined by offset.

func (*RecScanner) Scan added in v1.3.0

func (s *RecScanner) Scan() Token

Scan returns the next token.

func (*RecScanner) Token added in v1.3.0

func (s *RecScanner) Token(n int) Token

Token returns the n-th token in 's'. Ch panics if n is out of range [0..Len()-1].

type ScanSep

type ScanSep func() int

ScanSep is a function used by Scanner to determine the length in bytes of any separator preceding a token or EOF.

If the buf contains "if foo()" and the current offset is 2, calling

ScanSep()

may return

1

to indicate the separator is " ".

Scanner increments its own kept buffer offset by the returned length, the ScanSep function is assumed to do the same.

type ScanSrc

type ScanSrc func() (int, rune)

ScanSrc is a function used by Scanner to determine the length in bytes of a token.

If the buf contains "if foo()" and the current offset is 3, calling

ScanSrc()

may return

(3, IDENT)

to indicate the token is "foo" and its semantic value is IDENT.

Once ScanSrc returns zero for the token length an EOF is assumed.

Scanner increments its own kept buffer offset by the returned length, the ScanSrc function is assumed to do the same.

type Scanner

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

Scanner represents the data structures and methods common to some/many lexical scanners.

func NewScanner

func NewScanner(name string, buf []byte, scanSep ScanSep, scanSrc ScanSrc) *Scanner

NewScanner returns a newly created Scanner. The 'name' argument is used to report positions. 'buf' becomes owned by the scanner and should not be mutated by the caller afterwards.

func (*Scanner) AddErr

func (s *Scanner) AddErr(pos token.Position, msg string, args ...interface{})

AddErr registers an error.

func (*Scanner) AddLine

func (s *Scanner) AddLine(offset int)

AddLine adds the line offset for a new line. The line offset must be larger than the offset for the previous line and smaller than the scanner buffer size; otherwise the line offset is ignored.

func (*Scanner) AddLineColumnInfo

func (s *Scanner) AddLineColumnInfo(offset int, filename string, line, column int)

AddLineColumnInfo adds alternative file, line, and column number information for a given scanner buffer offset. The offset must be larger than the offset for the previously added alternative line info and smaller than the scanner buffer size; otherwise the information is ignored.

AddLineColumnInfo is typically used to register alternative position information for line directives such as //line filename:line:column.

func (*Scanner) Ch added in v1.2.0

func (s *Scanner) Ch(n int) rune

Ch returns the Ch field of the n-th token in 's'. Ch panics if n is out of range [0..Len()-1].

func (*Scanner) Err

func (s *Scanner) Err() error

Err reports any errors from reported by AddErr()

func (*Scanner) Len added in v1.2.0

func (s *Scanner) Len() int

Len reports the number of tokens in 's'.

func (*Scanner) Position

func (s *Scanner) Position(offset int) (r token.Position)

Position returns the position determined by offset.

func (*Scanner) Scan

func (s *Scanner) Scan() Token

Scan returns the next token.

func (*Scanner) Token added in v1.2.0

func (s *Scanner) Token(n int) Token

Token returns the n-th token in 's'. Ch panics if n is out of range [0..Len()-1].

type Token

type Token struct {
	// Ch represents the semantic value of the token as determined by the Scan
	// function.
	Ch rune
	// contains filtered or unexported fields
}

Token represents a lexeme, its position and semantic value.

func (Token) IsValid

func (t Token) IsValid() bool

IsValid reports whether t is a valid token. Zero values of Token report false.

func (Token) Next

func (t Token) Next() (r Token)

Next returns the token following t or a zero value if no such token exists.

func (Token) Position

func (t Token) Position() (r token.Position)

Position reports the position of t.

func (Token) Prev

func (t Token) Prev() (r Token)

Prev returns the token preceding t or a zero value if no such token exists.

func (Token) Sep

func (t Token) Sep() string

Sep returns the separator preceding t.

func (Token) SetSep

func (t Token) SetSep(s string)

SetSep sets t's separator.

func (Token) SetSrc

func (t Token) SetSrc(s string)

SetSrc sets t's source form.

func (Token) Src

func (t Token) Src() string

Src returns t's source form, without its preceding separator.

func (Token) String

func (t Token) String() string

String implements fmt.Stringer.

Directories

Path Synopsis
Package scanner implements a scanner for N-Quads[0] source text.
Package scanner implements a scanner for N-Quads[0] source text.
Package scanner implements a scanner for yacc[0] source text with actions written in Go.
Package scanner implements a scanner for yacc[0] source text with actions written in Go.

Jump to

Keyboard shortcuts

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