You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
223 lines
4.4 KiB
Go
223 lines
4.4 KiB
Go
package parser
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"gitea.paas.celticinfo.fr/oabrivard/gojson/lexer"
|
|
"gitea.paas.celticinfo.fr/oabrivard/gojson/token"
|
|
)
|
|
|
|
type Parser struct {
|
|
lexer *lexer.Lexer
|
|
|
|
curToken token.Token
|
|
peekToken token.Token
|
|
|
|
errors []string
|
|
}
|
|
|
|
func NewParser(l *lexer.Lexer) *Parser {
|
|
p := &Parser{lexer: l}
|
|
// Initialize curToken and peekToken
|
|
p.nextToken()
|
|
p.nextToken()
|
|
return p
|
|
}
|
|
|
|
func (p *Parser) nextToken() {
|
|
p.curToken = p.peekToken
|
|
p.peekToken = p.lexer.NextToken()
|
|
}
|
|
|
|
// Methods to parse JSON structure
|
|
|
|
type JsonObject map[string]interface{}
|
|
type JsonArray []interface{}
|
|
|
|
func (p *Parser) Parse() JsonObject {
|
|
return p.parseObject()
|
|
}
|
|
|
|
func (p *Parser) parseObject() JsonObject {
|
|
object := make(JsonObject)
|
|
|
|
// Expect the current token to be TokenBeginObject
|
|
if !p.curTokenIs(token.BEGIN_OBJECT) {
|
|
p.addError("expected '{'")
|
|
return nil
|
|
}
|
|
|
|
// Move to the next token
|
|
p.nextToken()
|
|
|
|
// Loop until we find an end object token
|
|
for !p.curTokenIs(token.END_OBJECT) && !p.curTokenIs(token.EOF) {
|
|
key := p.parseObjectKey()
|
|
if key == "" {
|
|
return nil
|
|
}
|
|
|
|
// Expect a name separator (:)
|
|
if !p.expectPeek(token.NAME_SEPARATOR) {
|
|
return nil
|
|
}
|
|
|
|
// Move to the value token
|
|
p.nextToken()
|
|
|
|
// Parse the value
|
|
value, err := p.parseValue()
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
|
|
object[key] = value
|
|
|
|
// Move past the value, potentially to a comma or the end object
|
|
p.nextToken()
|
|
|
|
// If we have a comma, the object continues
|
|
if p.curTokenIs(token.VALUE_SEPARATOR) {
|
|
if p.peekToken.Type == token.END_OBJECT { // no comma just before the end of the object
|
|
p.addError("No ',' before '}'")
|
|
return nil
|
|
}
|
|
|
|
p.nextToken()
|
|
}
|
|
}
|
|
|
|
// Expect the end object token
|
|
if !p.curTokenIs(token.END_OBJECT) {
|
|
p.addError("expected '}'")
|
|
return nil
|
|
}
|
|
|
|
return object
|
|
}
|
|
|
|
func (p *Parser) parseArray() JsonArray {
|
|
array := JsonArray{}
|
|
|
|
// Expect the current token to be TokenBeginArray
|
|
if !p.curTokenIs(token.BEGIN_ARRAY) {
|
|
p.addError("expected '['")
|
|
return nil
|
|
}
|
|
|
|
// Move to the next token
|
|
p.nextToken()
|
|
|
|
// Loop until we find an end array token
|
|
for !p.curTokenIs(token.END_ARRAY) {
|
|
// Parse the value
|
|
value, err := p.parseValue()
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
|
|
array = append(array, value)
|
|
|
|
// Move past the value
|
|
p.nextToken()
|
|
|
|
// If we have a value separator (comma), continue to the next value
|
|
if p.curTokenIs(token.VALUE_SEPARATOR) {
|
|
p.nextToken()
|
|
}
|
|
}
|
|
|
|
// Expect the end array token
|
|
if !p.curTokenIs(token.END_ARRAY) {
|
|
return nil
|
|
}
|
|
|
|
return array
|
|
}
|
|
|
|
func (p *Parser) addError(msg string) {
|
|
p.errors = append(p.errors, msg)
|
|
}
|
|
|
|
func (p *Parser) parseObjectKey() string {
|
|
if p.curToken.Type != token.STRING {
|
|
p.addError("expected string for key")
|
|
return ""
|
|
}
|
|
return p.curToken.Value
|
|
}
|
|
|
|
func (p *Parser) parseValue() (interface{}, error) {
|
|
switch p.curToken.Type {
|
|
case token.STRING:
|
|
return p.curToken.Value, nil
|
|
case token.NUMBER:
|
|
return p.parseNumber(), nil
|
|
case token.TRUE, token.FALSE:
|
|
return p.parseBoolean(), nil
|
|
case token.NULL:
|
|
return nil, nil
|
|
case token.BEGIN_OBJECT:
|
|
return p.parseObject(), nil
|
|
case token.BEGIN_ARRAY:
|
|
return p.parseArray(), nil
|
|
// ... other cases
|
|
default:
|
|
p.addError("unexpected token")
|
|
return nil, errors.New("unexpected token")
|
|
}
|
|
}
|
|
|
|
func (p *Parser) parseNumber() interface{} {
|
|
// Assuming the number is in a string format in the token
|
|
numStr := p.curToken.Value
|
|
|
|
// Check if the number is an integer or a float
|
|
if strings.Contains(numStr, ".") || strings.ContainsAny(numStr, "eE") {
|
|
// Parse as float
|
|
val, err := strconv.ParseFloat(numStr, 64)
|
|
if err != nil {
|
|
p.addError(fmt.Sprintf("could not parse %q as float", numStr))
|
|
return nil
|
|
}
|
|
return val
|
|
}
|
|
|
|
// Parse as integer
|
|
val, err := strconv.ParseInt(numStr, 10, 64)
|
|
if err != nil {
|
|
p.addError(fmt.Sprintf("could not parse %q as integer", numStr))
|
|
return nil
|
|
}
|
|
return val
|
|
}
|
|
|
|
func (p *Parser) parseBoolean() bool {
|
|
return p.curToken.Type == token.TRUE
|
|
}
|
|
|
|
func (p *Parser) expectPeek(t token.TokenType) bool {
|
|
if p.peekToken.Type == t {
|
|
p.nextToken()
|
|
return true
|
|
} else {
|
|
p.addError(fmt.Sprintf("expected next token to be %v, got %v instead", t, p.peekToken.Type))
|
|
return false
|
|
}
|
|
}
|
|
|
|
func (p *Parser) curTokenIs(t token.TokenType) bool {
|
|
return p.curToken.Type == t
|
|
}
|
|
|
|
/*
|
|
func (p *Parser) parseArray() *JsonArray {
|
|
// Implementation for parsing an array
|
|
}
|
|
*/
|
|
|
|
// ... other parse methods for different types
|