This repository has been archived by the owner on Mar 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
parse.go
212 lines (187 loc) · 5.89 KB
/
parse.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package parser
import (
"fmt"
"golang.org/x/text/unicode/norm"
"github.com/demizer/go-rst/pkg/log"
doc "github.com/demizer/go-rst/pkg/document"
tok "github.com/demizer/go-rst/pkg/token"
)
const (
// Default indent width
indentWidth = 4
)
// Parser contains the parser Parser. The Nodes field contains the parsed nodes of the input input data.
type Parser struct {
Name string // The name of the current parser input
Nodes *doc.NodeList // The root node list
Messages *doc.NodeList // Messages generated by the parser
nodeTarget *doc.NodeTarget // Used to append nodes to a target NodeList
text string // The input text
lex *tok.Lexer // The place where tokens come from
indents *indentQueue // Indent level tracking
bqLevel *doc.BlockQuoteNode // FIXME: will be replaced with blockquoteLevels
sectionLevels *sectionLevels // Encountered section levels
sections []*doc.SectionNode // Pointers to encountered sections
sectionSubState *sectionParseSubState // Parsing substate for sections
openList doc.Node // Open Bullet List, Enum List, or Definition List
tokenBuffer // Buffered tokens from the scanner to allow going forward and back in the stream
logConf log.Config
log.Logger
}
// New returns a fresh parser Parser.
func NewParser(name, text string, logConf log.Config) (*Parser, error) {
var ntext string
if !norm.NFC.IsNormalString(text) {
ntext = norm.NFC.String(text)
} else {
ntext = text
}
conf := logConf
conf.Name = "parser"
l, err := tok.Lex(name, []byte(ntext), conf)
if err != nil {
return nil, fmt.Errorf("error initializing lexer: %s", err)
}
ml := make(doc.NodeList, 0)
nl := make(doc.NodeList, 0)
p := &Parser{
Name: name,
Messages: &ml,
Nodes: &nl,
text: ntext,
lex: l,
logConf: conf,
sectionLevels: newSectionLevels(conf),
sectionSubState: new(sectionParseSubState),
indents: new(indentQueue),
nodeTarget: doc.NewNodeTarget(&nl, conf),
Logger: log.NewLogger(conf),
tokenBuffer: newTokenBuffer(l, conf),
}
p.Msgr("Parser.Nodes pointer", "nodeListPointer", fmt.Sprintf("%p", nl))
return p, err
}
// Parse starts parsing the document.
func (p *Parser) Parse() {
for {
var n doc.Node
token := p.next(1)
p.printToken("Parser got token", token)
// if token.Line == 7 && token.Type == tok.Text && token.Text == "-----" {
// p.DumpExit(p.buf[p.index-2 : p.index+3])
// }
if token == nil || token.Type == tok.EOF {
break
}
switch token.Type {
case tok.Text:
p.paragraph(token)
case tok.InlineEmphasisOpen:
p.inlineEmphasis(token, true)
case tok.InlineStrongOpen:
p.inlineStrong(token, true)
case tok.InlineLiteralOpen:
p.inlineLiteral(token, true)
case tok.InlineInterpretedTextOpen:
p.inlineInterpretedText(token)
case tok.InlineInterpretedTextRoleOpen:
p.inlineInterpretedTextRole(token)
case tok.Transition:
// FIXME: Workaround until transitions are supported
p.nodeTarget.Append(doc.NewTransition(token))
case tok.CommentMark:
p.comment(token)
case tok.SectionAdornment:
p.section(token)
// p.DumpExit(p.buf)
case tok.EnumListArabic:
n = p.enumList(token)
// FIXME: This is only until enumerated list are properly implemented.
if n == nil {
continue
}
p.nodeTarget.Append(n)
case tok.Space:
//
// FIXME: Blockquote parsing is NOT fully implemented.
//
if p.peekBack(1).Type == tok.BlankLine && p.bqLevel == nil {
// Ignore if next item is a blockquote from the lexer
if pn := p.peek(1); pn != nil && pn.Type == tok.BlockQuote {
p.Msg("Next item is blockquote; not creating empty blockquote")
continue
}
p.Msg("Creating empty blockquote!")
p.emptyblockquote(token)
} else if p.peekBack(1).Type == tok.BlankLine {
p.nodeTarget.SetParent(p.bqLevel)
}
case tok.BlankLine, tok.Title, tok.Escape:
// itemTitle is consumed when evaluating SectionAdornment
continue
case tok.BlockQuote:
p.blockquote(token)
case tok.DefinitionTerm:
p.definitionTerm(token)
case tok.Bullet:
p.bulletList(token)
default:
p.Msg(fmt.Sprintf("Token type: %q is not yet supported in the parser", token.Type.String()))
}
}
}
func (p *Parser) subParseBodyElements(token *tok.Item) doc.Node {
p.Msgr("Have token", "tokenType", token.Type, "tokenText", fmt.Sprintf("%q", token.Text))
var n doc.Node
switch token.Type {
case tok.Text:
n = p.paragraph(token)
case tok.InlineEmphasisOpen:
p.inlineEmphasis(token, false)
case tok.InlineStrongOpen:
p.inlineStrong(token, false)
case tok.InlineLiteralOpen:
p.inlineLiteral(token, false)
case tok.InlineInterpretedTextOpen:
p.inlineInterpretedText(token)
case tok.InlineInterpretedTextRoleOpen:
p.inlineInterpretedTextRole(token)
case tok.CommentMark:
p.comment(token)
case tok.EnumListArabic:
p.enumList(token)
case tok.Space:
case tok.BlankLine, tok.Escape:
case tok.BlockQuote:
p.blockquote(token)
default:
p.Msg(fmt.Sprintf("Token type: %q is not yet supported in the parser", token.Type.String()))
}
return n
}
func (p *Parser) subParseInlineMarkup(token *tok.Item) {
p.Msgr("Have token", "tokenType", token.Type, "tokenText", fmt.Sprintf("%q", token.Text))
switch token.Type {
case tok.InlineEmphasisOpen:
p.inlineEmphasis(token, false)
case tok.InlineStrongOpen:
p.inlineStrong(token, false)
case tok.InlineLiteralOpen:
p.inlineLiteral(token, false)
case tok.InlineInterpretedTextOpen:
p.inlineInterpretedText(token)
case tok.InlineInterpretedTextRoleOpen:
p.inlineInterpretedTextRole(token)
default:
p.Msg(fmt.Sprintf("Token type: %q is not inline markup", token.Type.String()))
}
}
func (p *Parser) printToken(msg string, i *tok.Item) {
log.WithCallDepth(p.Logger, p.Logger.CallDepth+1).Msgr(msg,
"index", p.index,
"type", i.Type,
"line", i.Line,
"startPosition", i.StartPosition,
"text", i.Text,
)
}