Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/berquerant/ybase
Utilities to implement a lexer for goyacc.
https://github.com/berquerant/ybase
go goyacc
Last synced: about 1 month 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 2 years ago)
- Default Branch: main
- Last Pushed: 2024-11-28T12:06:25.000Z (about 2 months ago)
- Last Synced: 2024-11-28T13:20:04.105Z (about 2 months ago)
- Topics: go, goyacc
- Language: Go
- Homepage:
- Size: 42 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ybase
Utilities to implement a lexer for goyacc.
## Example
``` go
package mainimport (
"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 )
}
```