memos/plugin/gomark/parser/line_break.go

34 lines
656 B
Go
Raw Normal View History

2023-12-12 23:38:43 +08:00
package parser
import (
2023-12-13 23:50:05 +08:00
"errors"
2023-12-12 23:38:43 +08:00
"github.com/usememos/memos/plugin/gomark/ast"
"github.com/usememos/memos/plugin/gomark/parser/tokenizer"
)
type LineBreakParser struct{}
func NewLineBreakParser() *LineBreakParser {
2023-12-13 21:00:13 +08:00
return &LineBreakParser{}
2023-12-12 23:38:43 +08:00
}
func (*LineBreakParser) Match(tokens []*tokenizer.Token) (int, bool) {
if len(tokens) == 0 {
return 0, false
}
if tokens[0].Type != tokenizer.Newline {
return 0, false
}
return 1, true
}
2023-12-13 23:50:05 +08:00
func (p *LineBreakParser) Parse(tokens []*tokenizer.Token) (ast.Node, error) {
2023-12-12 23:38:43 +08:00
size, ok := p.Match(tokens)
if size == 0 || !ok {
2023-12-13 23:50:05 +08:00
return nil, errors.New("not matched")
2023-12-12 23:38:43 +08:00
}
2023-12-13 23:50:05 +08:00
return &ast.LineBreak{}, nil
2023-12-12 23:38:43 +08:00
}