https://github.com/macrat/simplexer
A simple lexical analyzer for Go
https://github.com/macrat/simplexer
golang-library lexical-analyzer
Last synced: 3 months ago
JSON representation
A simple lexical analyzer for Go
- Host: GitHub
- URL: https://github.com/macrat/simplexer
- Owner: macrat
- License: mit
- Created: 2018-01-03T15:49:24.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2018-01-10T13:17:29.000Z (over 7 years ago)
- Last Synced: 2023-07-27T22:20:04.906Z (about 2 years ago)
- Topics: golang-library, lexical-analyzer
- Language: Go
- Size: 33.2 KB
- Stars: 5
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
simplexer
=========[](https://travis-ci.org/macrat/simplexer)
[](https://codeclimate.com/github/macrat/simplexer/test_coverage)
[](https://codeclimate.com/github/macrat/simplexer/maintainability)
[](https://godoc.org/github.com/macrat/simplexer)A simple lexical analyzser for Go.
## example
### simplest usage
``` go
package mainimport (
"fmt"
"strings""github.com/macrat/simplexer"
)func Example() {
input := "hello_world = \"hello world\"\nnumber = 1"
lexer := simplexer.NewLexer(strings.NewReader(input))fmt.Println(input)
fmt.Println("==========")for {
token, err := lexer.Scan()
if err != nil {
panic(err.Error())
}
if token == nil {
fmt.Println("==========")
return
}fmt.Printf("line %2d, column %2d: %s: %s\n",
token.Position.Line,
token.Position.Column,
token.Type,
token.Literal)
}
}
```It is output as follow.
``` text
hello_world = "hello world"
number = 1
==========
line 0, column 0: IDENT: hello_world
line 0, column 12: OTHER: =
line 0, column 14: STRING: "hello world"
line 1, column 0: IDENT: number
line 1, column 7: OTHER: =
line 1, column 9: NUMBER: 1
==========
```### more examples
Please see [godoc](https://godoc.org/github.com/macrat/simplexer).