-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconditional.go
More file actions
79 lines (64 loc) · 1.46 KB
/
conditional.go
File metadata and controls
79 lines (64 loc) · 1.46 KB
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
// dcc - dependency-driven C/C++ compiler front end
//
// Copyright © A.Newman 2015.
//
// This source code is released under version 2 of the GNU Public License.
// See the file LICENSE for details.
//
package main
import "errors"
type ScanState int
const (
TrueConditionState ScanState = iota
FalseConditionState
InElseState
)
var ErrNoCondition = errors.New("not within a conditional section")
type Conditional struct {
states []ScanState
}
func (c *Conditional) IsActive() bool {
return len(c.states) > 0
}
func (c *Conditional) PushState(state ScanState) {
c.states = append(c.states, state)
}
func (c *Conditional) PopState() error {
if len(c.states) == 0 {
return ErrNoCondition
}
c.states = c.states[:len(c.states)-1]
return nil
}
func (c *Conditional) SetState(state ScanState) {
c.states[len(c.states)-1] = state
}
func (c *Conditional) CurrentState() ScanState {
return c.states[len(c.states)-1]
}
func (c *Conditional) IsSkippingLines() bool {
if !c.IsActive() {
return false
}
if c.CurrentState() == TrueConditionState {
return false
}
return true
}
func (c *Conditional) IsNested() bool {
return len(c.states) > 1
}
func (c *Conditional) SkipLine(line string) bool {
if c.CurrentState() == TrueConditionState {
return false
}
return false
}
func (c *Conditional) ToggleState() {
switch c.CurrentState() {
case TrueConditionState:
c.SetState(FalseConditionState)
case FalseConditionState:
c.SetState(TrueConditionState)
}
}