Documentation
¶
Overview ¶
Package scanner provides some common scanner stuff.
Index ¶
- type ErrList
- type ErrWithPosition
- type RecScanner
- func (s *RecScanner) AddErr(pos token.Position, msg string, args ...interface{})
- func (s *RecScanner) AddLine(offset int)
- func (s *RecScanner) AddLineColumnInfo(offset int, filename string, line, column int)
- func (s *RecScanner) Ch(n int) rune
- func (s *RecScanner) Err() error
- func (s *RecScanner) Len() int
- func (s *RecScanner) Position(offset int) (r token.Position)
- func (s *RecScanner) Scan() Token
- func (s *RecScanner) Token(n int) Token
- type ScanSep
- type ScanSrc
- type Scanner
- func (s *Scanner) AddErr(pos token.Position, msg string, args ...interface{})
- func (s *Scanner) AddLine(offset int)
- func (s *Scanner) AddLineColumnInfo(offset int, filename string, line, column int)
- func (s *Scanner) Ch(n int) rune
- func (s *Scanner) Err() error
- func (s *Scanner) Len() int
- func (s *Scanner) Position(offset int) (r token.Position)
- func (s *Scanner) Scan() Token
- func (s *Scanner) Token(n int) Token
- type Token
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.
type ErrWithPosition ¶
ErrWithPosition augments an error with optional position information.
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 ¶
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 ¶
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) AddLine ¶
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 ¶
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
Ch returns the Ch field of 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 ¶
IsValid reports whether t is a valid token. Zero values of Token report false.
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. |