File: parser.go

package info (click to toggle)
bombadillo 2.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 412 kB
  • sloc: makefile: 59
file content (141 lines) | stat: -rw-r--r-- 2,551 bytes parent folder | download
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
package cmdparse

import (
	"fmt"
	"io"
)

//------------------------------------------------\\
// + + +             T Y P E S               + + + \\
//--------------------------------------------------\\

type Parser struct {
	s      *scanner
	buffer struct {
		token Token
		size  int
	}
}

type Command struct {
	Action string
	Target string
	Value  []string
	Type   Comtype
}

type Comtype int

const (
	GOURL Comtype = iota
	GOLINK
	SIMPLE
	DOLINK
	DOLINKAS
	DOAS
	DO
)

//------------------------------------------------\\
// + + +           R E C E I V E R S         + + + \\
//--------------------------------------------------\\

func (p *Parser) scan() (current Token) {
	if p.buffer.size != 0 {
		p.buffer.size = 0
		return p.buffer.token
	}

	current = p.s.scan()
	for {
		if current.kind != Whitespace {
			break
		}
		current = p.s.scan()
	}

	p.buffer.token = current
	return
}

func (p *Parser) unscan() { p.buffer.size = 1 }

func (p *Parser) parseNonAction() (*Command, error) {
	p.unscan()
	t := p.scan()
	cm := &Command{}

	if t.kind == Value {
		cm.Target = t.val
		cm.Type = GOLINK
	} else if t.kind == Word {
		cm.Target = t.val
		cm.Type = GOURL
	} else {
		return nil, fmt.Errorf("Found %q, expected action, url, or link number", t.val)
	}

	if u := p.scan(); u.kind != End {
		return nil, fmt.Errorf("Found %q, expected EOF", u.val)
	}
	return cm, nil
}

func (p *Parser) parseAction() (*Command, error) {
	p.unscan()
	t := p.scan()
	cm := &Command{}
	cm.Action = t.val
	t = p.scan()
	switch t.kind {
	case End:
		cm.Type = SIMPLE
		return cm, nil
	case Value:
		cm.Target = t.val
		cm.Type = DOLINK
	case Word, Action:
		cm.Value = append(cm.Value, t.val)
		cm.Type = DO
	case Whitespace:
		return nil, fmt.Errorf("Found %q (%d), expected value", t.val, t.kind)
	}
	t = p.scan()
	if t.kind == End {
		return cm, nil
	} else {
		if cm.Type == DOLINK {
			cm.Type = DOLINKAS
		} else {
			cm.Type = DOAS
		}
		cm.Value = append(cm.Value, t.val)

		for {
			token := p.scan()
			if token.kind == End {
				break
			} else if token.kind == Whitespace {
				continue
			}
			cm.Value = append(cm.Value, token.val)
		}
	}
	return cm, nil
}

func (p *Parser) Parse() (*Command, error) {
	if t := p.scan(); t.kind != Action {
		return p.parseNonAction()
	} else {
		return p.parseAction()
	}
}

//------------------------------------------------\\
// + + +          F U N C T I O N S          + + + \\
//--------------------------------------------------\\

func NewParser(r io.Reader) *Parser {
	return &Parser{s: NewScanner(r)}
}