https://github.com/berquerant/ybase
Utilities to implement a lexer for goyacc.
https://github.com/berquerant/ybase
go goyacc
Last synced: 4 months ago
JSON representation
Utilities to implement a lexer for goyacc.
- Host: GitHub
- URL: https://github.com/berquerant/ybase
- Owner: berquerant
- License: mit
- Created: 2022-09-19T07:51:19.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2025-09-12T19:08:02.000Z (9 months ago)
- Last Synced: 2025-10-20T23:53:44.155Z (8 months ago)
- Topics: go, goyacc
- Language: Go
- Homepage:
- Size: 40 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://goreportcard.com/report/github.com/berquerant/ybase)
[](https://pkg.go.dev/github.com/berquerant/ybase)
# ybase
Utilities to implement a lexer for goyacc.
## Example
``` go
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 )
}
```