unilex/example/json/main.go

138 lines
2.4 KiB
Go
Raw Normal View History

2021-03-07 23:44:10 +03:00
// +build ignore
package main
import (
"fmt"
"github.com/neonxp/unilex"
)
func main() {
testJson := `
{
"key1": "value1",
"key2": {
"key3" : "value 3"
},
2021-03-10 00:47:58 +03:00
"key4": 123.321,
"key5": [
1,
2,
[
3,
4,
5,
{
"key6": "value6"
}
]
]
2021-03-07 23:44:10 +03:00
}`
l := unilex.New(testJson)
go l.Run(initJson)
for ll := range l.Output {
fmt.Println(ll)
}
}
const (
2021-03-10 00:47:58 +03:00
lObjectStart unilex.LexType = iota
lObjectEnd
lObjectKey
lObjectValue
lArrayStart
lArrayEnd
lString
lNumber
2021-03-07 23:44:10 +03:00
)
func initJson(l *unilex.Lexer) unilex.StateFunc {
ignoreWhiteSpace(l)
switch {
case l.Accept("{"):
l.Emit(lObjectStart)
2021-03-10 00:47:58 +03:00
return stateInObject
2021-03-07 23:44:10 +03:00
case l.Peek() == unilex.EOF:
return nil
}
2021-03-10 00:47:58 +03:00
return l.Errorf("Unknown token: %s", string(l.Peek()))
2021-03-07 23:44:10 +03:00
}
2021-03-10 00:47:58 +03:00
func stateInObject(l *unilex.Lexer) unilex.StateFunc {
// we in object, so we expect field keys and values
ignoreWhiteSpace(l)
if l.Accept("}") {
l.Emit(lObjectEnd)
// If meet close object return to previous state (including initial)
return l.PopState()
}
ignoreWhiteSpace(l)
l.Accept(",")
ignoreWhiteSpace(l)
if !unilex.ScanQuotedString(l, '"') {
return l.Errorf("Unknown token: %s", string(l.Peek()))
}
l.Emit(lObjectKey)
ignoreWhiteSpace(l)
if !l.Accept(":") {
return l.Errorf("Expected ':'")
}
ignoreWhiteSpace(l)
l.Emit(lObjectValue)
switch {
case unilex.ScanQuotedString(l, '"'):
l.Emit(lString)
ignoreWhiteSpace(l)
l.Accept(",")
l.Ignore()
2021-03-07 23:44:10 +03:00
ignoreWhiteSpace(l)
2021-03-10 00:47:58 +03:00
return stateInObject
case unilex.ScanNumber(l):
l.Emit(lNumber)
2021-03-07 23:44:10 +03:00
ignoreWhiteSpace(l)
2021-03-10 00:47:58 +03:00
l.Accept(",")
l.Ignore()
2021-03-07 23:44:10 +03:00
ignoreWhiteSpace(l)
2021-03-10 00:47:58 +03:00
return stateInObject
case l.Accept("{"):
l.Emit(lObjectStart)
l.PushState(stateInObject)
return stateInObject
case l.Accept("["):
l.Emit(lArrayStart)
l.PushState(stateInObject)
return stateInArray
}
return l.Errorf("Unknown token: %s", string(l.Peek()))
}
func stateInArray(l *unilex.Lexer) unilex.StateFunc {
ignoreWhiteSpace(l)
l.Accept(",")
ignoreWhiteSpace(l)
switch {
case unilex.ScanQuotedString(l, '"'):
l.Emit(lString)
case unilex.ScanNumber(l):
l.Emit(lNumber)
case l.Accept("{"):
l.Emit(lObjectStart)
l.PushState(stateInArray)
return stateInObject
case l.Accept("["):
l.Emit(lArrayStart)
l.PushState(stateInArray)
return stateInArray
case l.Accept("]"):
l.Emit(lArrayEnd)
return l.PopState()
2021-03-07 23:44:10 +03:00
}
2021-03-10 00:47:58 +03:00
return stateInArray
2021-03-07 23:44:10 +03:00
}
func ignoreWhiteSpace(l *unilex.Lexer) {
l.AcceptWhile(" \n\t") //ignore whitespaces
l.Ignore()
}