Add support for NAME keyword

This commit is contained in:
Niklas Fasching 2019-10-27 15:04:55 +01:00
parent 2afd11581c
commit 20970ec872
8 changed files with 63 additions and 3 deletions

View file

@ -14,6 +14,11 @@ type Keyword struct {
Value string
}
type NodeWithName struct {
Name string
Node Node
}
type NodeWithMeta struct {
Node Node
Meta Metadata
@ -51,10 +56,12 @@ func (d *Document) parseComment(i int, stop stopFn) (int, Node) {
func (d *Document) parseKeyword(i int, stop stopFn) (int, Node) {
k := parseKeyword(d.tokens[i])
switch k.Key {
case "NAME":
return d.parseNodeWithName(k, i, stop)
case "SETUPFILE":
return d.loadSetupFile(k)
case "INCLUDE":
return d.newInclude(k)
return d.parseInclude(k)
case "CAPTION", "ATTR_HTML":
consumed, node := d.parseAffiliated(i, stop)
if consumed != 0 {
@ -71,6 +78,18 @@ func (d *Document) parseKeyword(i int, stop stopFn) (int, Node) {
}
}
func (d *Document) parseNodeWithName(k Keyword, i int, stop stopFn) (int, Node) {
if stop(d, i+1) {
return 0, nil
}
consumed, node := d.parseOne(i+1, stop)
if consumed == 0 || node == nil {
return 0, nil
}
d.NamedNodes[k.Value] = node
return consumed + 1, NodeWithName{k.Value, node}
}
func (d *Document) parseAffiliated(i int, stop stopFn) (int, Node) {
start, meta := i, Metadata{}
for ; !stop(d, i) && d.tokens[i].kind == "keyword"; i++ {
@ -115,7 +134,7 @@ func parseKeyword(t token) Keyword {
return Keyword{strings.ToUpper(k), strings.TrimSpace(v)}
}
func (d *Document) newInclude(k Keyword) (int, Node) {
func (d *Document) parseInclude(k Keyword) (int, Node) {
resolve := func() Node {
d.Log.Printf("Bad include %#v", k)
return k
@ -161,4 +180,5 @@ func (d *Document) loadSetupFile(k Keyword) (int, Node) {
func (n Comment) String() string { return orgWriter.nodesAsString(n) }
func (n Keyword) String() string { return orgWriter.nodesAsString(n) }
func (n NodeWithMeta) String() string { return orgWriter.nodesAsString(n) }
func (n NodeWithName) String() string { return orgWriter.nodesAsString(n) }
func (n Include) String() string { return orgWriter.nodesAsString(n) }