-
Notifications
You must be signed in to change notification settings - Fork 37
/
macro.go
93 lines (84 loc) · 1.95 KB
/
macro.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
package parser
import (
"fmt"
"github.com/alecthomas/participle/lexer"
"github.com/sleepinggenius2/gosmi/types"
)
type MacroBody struct {
Pos lexer.Position
TypeNotation string
ValueNotation string
Tokens map[string]string
}
func (m *MacroBody) Parse(lex *lexer.PeekingLexer) error {
token, err := lex.Next()
if err != nil {
return fmt.Errorf("Get 'BEGIN' token: %w", err)
}
if token.Value != "BEGIN" {
return fmt.Errorf("Expected 'BEGIN', Got '%s'", token.Value)
}
m.Pos = token.Pos
var tokenName, tokenValue string
m.Tokens = make(map[string]string)
symbols := smiLexer.Symbols()
for {
token, err = lex.Next()
if err != nil {
return fmt.Errorf("Next token: %w", err)
}
if token.Value == "END" {
break
}
peek, _ := lex.Peek(0)
if ((token.Value == "TYPE" || token.Value == "VALUE") && peek.Value == "NOTATION") || peek.Type == symbols["Assign"] {
if peek.Value == "NOTATION" {
tokenName += " NOTATION"
// Peek should guarantee there is a next token
_, _ = lex.Next()
continue
}
if tokenName != "" {
switch tokenName {
case "TYPE NOTATION":
m.TypeNotation = tokenValue
case "VALUE NOTATION":
m.ValueNotation = tokenValue
default:
m.Tokens[tokenName] = tokenValue
}
}
tokenName = token.Value
tokenValue = ""
if peek.Type == symbols["Assign"] {
// Peek should guarantee there is a next token
_, _ = lex.Next()
}
continue
}
if len(tokenValue) > 0 {
tokenValue += " "
}
if token.Type == symbols["Text"] {
tokenValue += `"` + token.Value + `"`
} else {
tokenValue += token.Value
}
}
switch tokenName {
case "":
break
case "TYPE NOTATION":
m.TypeNotation = tokenValue
case "VALUE NOTATION":
m.ValueNotation = tokenValue
default:
m.Tokens[tokenName] = tokenValue
}
return nil
}
type Macro struct {
Pos lexer.Position
Name types.SmiIdentifier `parser:"@Ident \"MACRO\" Assign"`
Body MacroBody `parser:"@@"`
}