This repository has been archived by the owner on Aug 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
parser.go
59 lines (55 loc) · 1.58 KB
/
parser.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
// Package parser turns the source text into a sequence of blocks.
package parser
import (
"bytes"
"github.com/bouncepaw/mycomarkup/blocks"
"github.com/bouncepaw/mycomarkup/mycocontext"
"sync"
)
// Parse parses the Mycomarkup document in the given context. All parsed blocks are written to out.
func Parse(ctx mycocontext.Context, out chan blocks.Block) {
// Using a channel seems like a good idea. The downside is that using this function is harder. But does it matter in this case? Not really. Channel supremacy all the way down.
var (
token blocks.Block
done bool
)
defer close(out)
for !done {
select {
case <-ctx.Done():
return
default:
token, done = nextToken(ctx)
if token != nil {
out <- token
}
}
}
}
// parseSubdocumentForEachBlock replaces the buffer in the given context and parses the document contained in the buffer. The function is called on every block.
func parseSubdocumentForEachBlock(ctx mycocontext.Context, buf *bytes.Buffer, f func(block blocks.Block)) {
var (
wg sync.WaitGroup
blocksCh = make(chan blocks.Block)
)
wg.Add(1)
go func() {
Parse(mycocontext.WithBuffer(ctx, buf), blocksCh)
wg.Done()
}()
for block := range blocksCh {
f(block)
}
wg.Wait()
}
// MakeHeading parses the heading on the given line and returns it. Set its level by yourself though.
func MakeHeading(line, hyphaName string, level uint) blocks.Heading {
// TODO: figure out the level here.
// TODO: move to the parser module.
h := blocks.Heading{
Level: level,
Contents: MakeFormatted(line[level+1:], hyphaName),
Src: line,
}
return h
}