-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.go
142 lines (130 loc) · 3.28 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
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
// Copyright (c) Christian Surlykke
//
// This file is part of the WindowArranger project.
// It is distributed under the GPL v2 license.
// Please refer to the GPL2 file for a copy of the license.
//
package main
import (
"fmt"
"strings"
"unicode"
"unicode/utf8"
)
type Workspace struct {
Output string
Node *Node
}
type Node struct {
Criteria string
Layout string
Children []*Node
}
func Parse(bytes []byte) []Workspace {
var (
currentChar rune = 0
isInString bool = false
isInComment bool = false
currentLine int = 1
currentColumn int = 1
)
var failIf = func(condition bool, msg string) {
if condition {
panic(fmt.Sprintf("%d,%d: %s", currentLine, currentColumn, msg))
}
}
var isCurrentCharWhitespace = func() bool {
return unicode.IsSpace(currentChar) || isInComment
}
var gotoNextChar = func() {
r, w := utf8.DecodeRune(bytes)
failIf(r == utf8.RuneError && w > 0, "Not valid utf-8")
if isInComment {
isInComment = r != '\n' && r != utf8.RuneError
} else if isInString {
failIf(r == utf8.RuneError, "String not terminated")
if r == '\'' {
isInString = false
}
} else {
if r == '\'' {
isInString = true
} else if r == '#' {
isInComment = true
}
}
if currentChar == '\n' {
currentLine, currentColumn = currentLine+1, 1
} else {
currentColumn = currentColumn + 1
}
currentChar = r
bytes = bytes[w:]
}
var gotoNextNonWsChar = func() {
for {
gotoNextChar()
if !isCurrentCharWhitespace() {
break
}
}
}
// The read functions expect, when called, currentChar to be the first char of what they read
// and they ensure, on return, currentChar to be first non-whitespace char after what they read.
var readString = func() string {
var builder = strings.Builder{}
for gotoNextChar(); isInString; gotoNextChar() {
builder.WriteRune(currentChar)
}
gotoNextNonWsChar()
return builder.String()
}
var readLayout func() *Node
readLayout = func() *Node {
var layoutMap = map[string]string{
"H": "splith",
"V": "splitv",
"T": "tabbed",
"S": "stacking",
}
var node = &Node{
Layout: layoutMap[string(currentChar)],
}
failIf("" == node.Layout, "Layout type (H,V,T or S) expected")
gotoNextNonWsChar()
failIf(currentChar != '[', "'[' expected")
gotoNextNonWsChar()
for currentChar != ']' {
if currentChar == '\'' {
node.Children = append(node.Children, &Node{Criteria: readString()})
} else {
node.Children = append(node.Children, readLayout())
}
}
gotoNextNonWsChar()
return node
}
var readOutput = func() Workspace {
failIf(!unicode.IsLetter(currentChar), "Output identifier expected")
var outputBuilder = strings.Builder{}
outputBuilder.WriteRune(currentChar)
for gotoNextChar(); unicode.IsLetter(currentChar) || unicode.IsDigit(currentChar) || '-' == currentChar; gotoNextChar() {
outputBuilder.WriteRune(currentChar)
}
if isCurrentCharWhitespace() {
gotoNextNonWsChar()
}
failIf(':' != currentChar, "':' expected")
gotoNextNonWsChar()
return Workspace{Output: outputBuilder.String(), Node: readLayout()}
}
var workspaces []Workspace = nil
gotoNextNonWsChar()
for currentChar != utf8.RuneError {
workspaces = append(workspaces, readOutput())
}
if len(workspaces) == 0 {
panic("Empty configuration")
}
return workspaces
}