Skip to content

Commit

Permalink
refactor parser
Browse files Browse the repository at this point in the history
  • Loading branch information
goccy committed Nov 26, 2024
1 parent 6a4fdd3 commit e3c4103
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 58 deletions.
35 changes: 25 additions & 10 deletions parser/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,26 +115,41 @@ func (c *context) next() bool {
return c.tokenRef.idx < c.tokenRef.size
}

func (c *context) insertNullToken(tk *Token) *Token {
nullToken := c.createNullToken(tk)
c.insertToken(nullToken)
c.goNext()

return nullToken
}

func (c *context) createNullToken(base *Token) *Token {
pos := *(base.RawToken().Position)
pos.Column++
return &Token{Token: token.New("null", "null", &pos)}
}

func (c *context) insertToken(tk *Token) {
idx := c.tokenRef.idx
if c.tokenRef.size < idx {
ref := c.tokenRef
idx := ref.idx
if ref.size < idx {
return
}
if c.tokenRef.size == idx {
curToken := c.tokenRef.tokens[c.tokenRef.size-1]
if ref.size == idx {
curToken := ref.tokens[ref.size-1]
tk.RawToken().Next = curToken.RawToken()
curToken.RawToken().Prev = tk.RawToken()

c.tokenRef.tokens = append(c.tokenRef.tokens, tk)
c.tokenRef.size = len(c.tokenRef.tokens)
ref.tokens = append(ref.tokens, tk)
ref.size = len(ref.tokens)
return
}

curToken := c.tokenRef.tokens[idx]
curToken := ref.tokens[idx]
tk.RawToken().Next = curToken.RawToken()
curToken.RawToken().Prev = tk.RawToken()

c.tokenRef.tokens = append(c.tokenRef.tokens[:idx+1], c.tokenRef.tokens[idx:]...)
c.tokenRef.tokens[idx] = tk
c.tokenRef.size = len(c.tokenRef.tokens)
ref.tokens = append(ref.tokens[:idx+1], ref.tokens[idx:]...)
ref.tokens[idx] = tk
ref.size = len(ref.tokens)
}
52 changes: 7 additions & 45 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,17 +325,14 @@ func (p *parser) parseFlowMap(ctx *context) (*ast.MappingNode, error) {
}
colonTk := mapKeyTk.Group.Last()
if p.isFlowMapDelim(ctx.nextToken()) {
nullToken := p.createNullToken(colonTk)
ctx.insertToken(nullToken)
value, err := newNullNode(ctx, nullToken)
value, err := newNullNode(ctx, ctx.insertNullToken(colonTk))
if err != nil {
return nil, err
}
mapValue, err := newMappingValueNode(ctx, colonTk, key, value)
if err != nil {
return nil, err
}
ctx.goNext()
node.Values = append(node.Values, mapValue)
ctx.goNext()
} else {
Expand All @@ -358,13 +355,10 @@ func (p *parser) parseFlowMap(ctx *context) (*ast.MappingNode, error) {
if err != nil {
return nil, err
}
nullToken := p.createNullToken(mapKeyTk)
ctx.insertToken(nullToken)
value, err := newNullNode(ctx, nullToken)
value, err := newNullNode(ctx, ctx.insertNullToken(mapKeyTk))
if err != nil {
return nil, err
}
ctx.goNext()
mapValue, err := newMappingValueNode(ctx, mapKeyTk, key, value)
if err != nil {
return nil, err
Expand Down Expand Up @@ -619,17 +613,7 @@ func (p *parser) mapKeyText(n ast.Node) string {
func (p *parser) parseMapValue(ctx *context, key ast.MapKeyNode, colonTk *Token) (ast.Node, error) {
tk := ctx.currentToken()
if tk == nil {
nullToken := p.createNullToken(colonTk)
ctx.insertToken(nullToken)
nullNode, err := newNullNode(ctx, nullToken)
if err != nil {
return nil, err
}
ctx.goNext()
return nullNode, nil
} else if tk.Type() == token.CollectEntryType {
// implicit null value.
return newNullNode(ctx, tk)
return newNullNode(ctx, ctx.insertNullToken(colonTk))
}

if ctx.isComment() {
Expand All @@ -641,31 +625,15 @@ func (p *parser) parseMapValue(ctx *context, key ast.MapKeyNode, colonTk *Token)
// ----
// key: <value does not defined>
// next

nullToken := p.createNullToken(colonTk)
ctx.insertToken(nullToken)
nullNode, err := newNullNode(ctx, nullToken)
if err != nil {
return nil, err
}
ctx.goNext()
return nullNode, nil
return newNullNode(ctx, ctx.insertNullToken(colonTk))
}

if tk.Column() < key.GetToken().Position.Column {
// in this case,
// ----
// key: <value does not defined>
// next
nullToken := p.createNullToken(colonTk)
ctx.insertToken(nullToken)
nullNode, err := newNullNode(ctx, nullToken)
if err != nil {
return nil, err
}

ctx.goNext()
return nullNode, nil
return newNullNode(ctx, ctx.insertNullToken(colonTk))
}

value, err := p.parseToken(ctx, ctx.currentToken())
Expand Down Expand Up @@ -776,7 +744,7 @@ func (p *parser) parseTag(ctx *context) (*ast.TagNode, error) {

func (p *parser) parseTagValue(ctx *context, tagRawTk *token.Token, tk *Token) (ast.Node, error) {
if tk == nil {
return newNullNode(ctx, p.createNullToken(&Token{Token: tagRawTk}))
return newNullNode(ctx, ctx.createNullToken(&Token{Token: tagRawTk}))
}
switch token.ReservedTagKeyword(tagRawTk.Value) {
case token.MappingTag, token.OrderedMapTag:
Expand Down Expand Up @@ -863,7 +831,7 @@ func (p *parser) parseSequence(ctx *context) (*ast.SequenceNode, error) {

valueTk := ctx.currentToken()
if valueTk == nil {
node, err := newNullNode(ctx, p.createNullToken(seqTk))
node, err := newNullNode(ctx, ctx.createNullToken(seqTk))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -945,9 +913,3 @@ func (p *parser) parseFootComment(ctx *context, col int) *ast.CommentGroupNode {
}
return ast.CommentGroup(tks)
}

func (p *parser) createNullToken(base *Token) *Token {
pos := *(base.RawToken().Position)
pos.Column++
return &Token{Token: token.New("null", "null", &pos)}
}
3 changes: 0 additions & 3 deletions parser/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ type TokenGroupType int

const (
TokenGroupNone TokenGroupType = iota
TokenGroupComment
TokenGroupDirective
TokenGroupDocument
TokenGroupDocumentBody
Expand All @@ -30,8 +29,6 @@ func (t TokenGroupType) String() string {
switch t {
case TokenGroupNone:
return "none"
case TokenGroupComment:
return "comment"
case TokenGroupDirective:
return "directive"
case TokenGroupDocument:
Expand Down

0 comments on commit e3c4103

Please sign in to comment.