Documentation
¶
Index ¶
Examples ¶
Constants ¶
View Source
const EOF = -1
Variables ¶
View Source
var ErrYbase = errors.New("Ybase")
Functions ¶
func NilDebugFunc ¶
Types ¶
type Bytes ¶ added in v0.3.0
type Bytes []byte
func (Bytes) Adjacency ¶ added in v0.5.0
Adjacency retrieves runes before and after a specified line and column.
func (Bytes) Context ¶ added in v0.4.0
Context retrieves lines before and after a specified line number.
It returns the surrounding context, including the given number of lines before and after the specified line.
func (Bytes) LineColumn ¶ added in v0.3.0
LineColumn calculates line number and column from offset.
type Context ¶ added in v0.4.0
type Context struct { Target *ContextLine Lines []*ContextLine }
type ContextLine ¶ added in v0.4.0
type Lexer ¶
type Lexer interface { Scanner // DoLex runs the lexical analysis. // Returns EOF if EOF or an error occurs. DoLex(callback func(Token)) int }
Lexer is an utility to implement yyLexer.
Recommendation: - Set level to yyDebug (YYDEBUG in yacc). - Set yyErrorVerbose to true (YYERROR_VERBOSE in yacc)
Implements yyLexer by Error(string) and Lex(*yySymType) int, e.g.
type ActualLexer struct { Lexer } func (a *ActualLexer) Lex(lval *yySymType) int { return a.DoLex(func(tok Token) { lval.token = tok // declares in %union }) }
Example ¶
package main import ( "bytes" "fmt" "unicode" "github.com/berquerant/ybase" ) func main() { input := "1 + 12 - (34-56)" s := ybase.NewLexer(ybase.NewScanner(ybase.NewReader(bytes.NewBufferString(input), nil), func(r ybase.Reader) int { r.DiscardWhile(unicode.IsSpace) top := r.Peek() switch { case unicode.IsDigit(top): r.NextWhile(unicode.IsDigit) return 901 default: switch top { case '+': _ = r.Next() return 911 case '-': _ = r.Next() return 912 case '(': _ = r.Next() return 921 case ')': _ = r.Next() return 922 } } return ybase.EOF })) for s.DoLex(func(tok ybase.Token) { fmt.Printf("%d %s\n", tok.Type(), tok.Value()) }) != ybase.EOF { } if err := s.Err(); err != nil { panic(err) } }
Output: 901 1 911 + 901 12 912 - 921 ( 901 34 912 - 901 56 922 )
type Reader ¶
type Reader interface { // ResetBuffer clears the buffer. ResetBuffer() // Buffer returns the read runes. Buffer() string // Next gets the next rune and advances the pos. Next() rune // Peek gets the next rune but keeps the pos. Peek() rune // Discard ignores the next rune. Discard() rune // Err returns an error during the reading. Err() error // Debugf outputs debug logs. Debugf(msg string, v ...any) // Errorf outputs logs and set an error. Errorf(err error, msg string, v ...any) // DiscardWhile calls Discard() while pred(Peek()). DiscardWhile(pred func(rune) bool) // NextWhile calls Next() while pred(Peek()). NextWhile(pred func(rune) bool) // Pos returns the current position. Pos() Pos }
Reader represents a reader object for lex.
Click to show internal directories.
Click to hide internal directories.