-
-
Notifications
You must be signed in to change notification settings - Fork 274
/
textparser.go
35 lines (28 loc) · 864 Bytes
/
textparser.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package parser
import (
"errors"
"io"
"github.com/a-h/lexical/parse"
)
func newTextParser() textParser {
return textParser{}
}
type textParser struct {
}
var tagTemplOrNewLine = parse.Any(parse.Rune('<'), parse.Rune('{'), parse.Rune('}'), parse.Rune('\n'))
func (p textParser) Parse(pi parse.Input) parse.Result {
from := NewPositionFromInput(pi)
// Read until a tag or templ expression opens.
dtr := parse.StringUntil(tagTemplOrNewLine)(pi)
if dtr.Error != nil {
if errors.Is(dtr.Error, io.EOF) {
return parse.Failure("textParser", newParseError("textParser: unterminated text, expected tag open, templ expression open, or newline", from, NewPositionFromInput(pi)))
}
return dtr
}
s, ok := dtr.Item.(string)
if !ok || len(s) == 0 {
return parse.Failure("textParser", nil)
}
return parse.Success("textParser", Text{Value: s}, nil)
}