-
Notifications
You must be signed in to change notification settings - Fork 0
/
syntaxhead.go
105 lines (84 loc) · 1.82 KB
/
syntaxhead.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package treerack
import (
"bufio"
"errors"
"fmt"
"io"
)
type CommitType int
const (
None CommitType = 0
Alias CommitType = 1 << iota
Whitespace
NoWhitespace
Keyword
NoKeyword
FailPass
Root
userDefined
)
type formatFlags int
const (
formatNone formatFlags = 0
formatPretty formatFlags = 1 << iota
formatIncludeComments
)
// ParseError is returned when the input text doesn't match
// the used syntax during parsing.
type ParseError struct {
// Input is the name of the input file or <input> if not
// available.
Input string
// Offset is the index of the right-most failing
// token in the input text.
Offset int
// Line tells the line index of the right-most failing
// token in the input text.
//
// It is zero-based, and for error reporting, it is
// recommended to increment it by one.
Line int
// Column tells the column index of the right-most failing
// token in the input text.
Column int
// Definition tells the right-most unmatched parser definition.
Definition string
}
type parser interface {
nodeName() string
nodeID() int
commitType() CommitType
parse(*context)
}
type builder interface {
nodeName() string
nodeID() int
build(*context) ([]*Node, bool)
}
var ErrInvalidUnicodeCharacter = errors.New("invalid unicode character")
func (pe *ParseError) Error() string {
return fmt.Sprintf(
"%s:%d:%d:parse failed, parsing: %s",
pe.Input,
pe.Line+1,
pe.Column+1,
pe.Definition,
)
}
func parseInput(r io.Reader, p parser, b builder, kw []parser) (*Node, error) {
c := newContext(bufio.NewReader(r), kw)
p.parse(c)
if c.readErr != nil {
return nil, c.readErr
}
if err := c.finalizeParse(p); err != nil {
if perr, ok := err.(*ParseError); ok {
perr.Input = "<input>"
}
return nil, err
}
c.offset = 0
c.results.resetPending()
n, _ := b.build(c)
return n[0], nil
}