github.com/atrn/dcc@v0.0.0-20220806184050-4470d2553272/conditional.go (about) 1 // dcc - dependency-driven C/C++ compiler front end 2 // 3 // Copyright © A.Newman 2015. 4 // 5 // This source code is released under version 2 of the GNU Public License. 6 // See the file LICENSE for details. 7 // 8 9 package main 10 11 import "errors" 12 13 type ScanState int 14 15 const ( 16 TrueConditionState ScanState = iota 17 FalseConditionState 18 InElseState 19 ) 20 21 var ErrNoCondition = errors.New("not within a conditional section") 22 23 type Conditional struct { 24 states []ScanState 25 } 26 27 func (c *Conditional) IsActive() bool { 28 return len(c.states) > 0 29 } 30 31 func (c *Conditional) PushState(state ScanState) { 32 c.states = append(c.states, state) 33 } 34 35 func (c *Conditional) PopState() error { 36 if len(c.states) == 0 { 37 return ErrNoCondition 38 } 39 c.states = c.states[:len(c.states)-1] 40 return nil 41 } 42 43 func (c *Conditional) SetState(state ScanState) { 44 c.states[len(c.states)-1] = state 45 } 46 47 func (c *Conditional) CurrentState() ScanState { 48 return c.states[len(c.states)-1] 49 } 50 51 func (c *Conditional) IsSkippingLines() bool { 52 if !c.IsActive() { 53 return false 54 } 55 if c.CurrentState() == TrueConditionState { 56 return false 57 } 58 return true 59 } 60 61 func (c *Conditional) IsNested() bool { 62 return len(c.states) > 1 63 } 64 65 func (c *Conditional) SkipLine(line string) bool { 66 if c.CurrentState() == TrueConditionState { 67 return false 68 } 69 return false 70 } 71 72 func (c *Conditional) ToggleState() { 73 switch c.CurrentState() { 74 case TrueConditionState: 75 c.SetState(FalseConditionState) 76 case FalseConditionState: 77 c.SetState(TrueConditionState) 78 } 79 }