Debian Codegraph @

internal/lexer/items/disjunctrangeset.go

raw symbols SCIP
1 //Copyright 2013 Vastech SA (PTY) LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package items
16
17 import (
18 "fmt"
19 "strings"
20
21 "github.com/goccmack/gocc/internal/ast"
22 )
23
24 // set is kept sorted
25 type DisjunctRangeSet struct {
26 set []CharRange
27 MatchAny bool
28 }
29
30 func NewDisjunctRangeSet() *DisjunctRangeSet {
31 return &DisjunctRangeSet{
32 set: make([]CharRange, 0, 16),
33 MatchAny: false,
34 }
35 }
36
37 func (this *DisjunctRangeSet) AddLexTNode(sym ast.LexTNode) {
38 switch s := sym.(type) {
39 case *ast.LexCharRange:
40 this.AddRange(s.From.Val, s.To.Val)
41 case *ast.LexCharLit:
42 this.AddRange(s.Val, s.Val)
43 case *ast.LexDot:
44 this.MatchAny = true
45 case *ast.LexRegDefId:
46 // ignore
47 default:
48 panic(fmt.Sprintf("Unexpected type of symbol: %T", sym))
49 }
50 }
51
52 func (this *DisjunctRangeSet) addRange(rng ...CharRange) {
53 for _, r := range rng {
54 this.AddRange(r.From, r.To)
55 }
56 }
57
58 /*
59 |===========|
60
61 |-------| |===========| 1
62 |-----------+---|-------+ 2
63 |-----------+-----------| 3
64 |-----------+-----------+---| 4
65
66 |---|-------+ 5
67 |===========| 6
68 |-----------+---| 7
69 +===========+ |----| 8
70 +---|----|--+ 9
71 +---|-------| 10
72 +---|-------+---| 11
73 */
74 func (this *DisjunctRangeSet) AddRange(from, to rune) {
75 for i := 0; i < len(this.set) && from <= to; {
76 rng := this.set[i]
77 switch {
78 case from < rng.From:
79 switch {
80 case to < rng.From: // (1)
81 this.insertRange(i, from, to)
82 i += 2
83 case to < rng.To: // but >= set[i].From (2)
84 this.set[i] = CharRange{from, rng.From - 1}
85 this.insertRange(i+1, rng.From, to)
86 this.insertRange(i+2, to+1, rng.To)
87 i += 3
88 case to == rng.To: //(3)
89 this.insertRange(i, from, rng.From-1)
90 i += 2
91 case to > rng.To: // (4)
92 this.insertRange(i, from, rng.From-1)
93 i += 2
94 }
95 from = rng.To + 1
96 case from == rng.From:
97 switch {
98 case to < rng.To: // (5)
99 this.set[i].To = to
100 this.insertRange(i+1, to+1, rng.To)
101 i += 2
102 case to > rng.To: // (7)
103 i++ // do nothing
104 case to == rng.To: // (6)
105 i++ // do nothing
106 }
107 from = rng.To + 1
108 case from > rng.To: // (8)
109 i++ // do nothing
110 default: // from > rng.From && from <= rng.To:
111 switch {
112 case to < rng.To: // 9
113 this.set[i].To = from - 1
114 this.insertRange(i+1, from, to)
115 this.insertRange(i+2, to+1, rng.To)
116 i += 3
117 case to == rng.To: // (10)
118 this.set[i].To = from - 1
119 this.insertRange(i+1, from, to)
120 i += 2
121 case to > rng.To: // (11)
122 this.set[i].To = from - 1
123 this.insertRange(i+1, from, rng.To)
124 i += 2
125 }
126 from = rng.To + 1
127 }
128 }
129 if from <= to {
130 this.set = append(this.set, CharRange{from, to})
131 }
132 }
133
134 func (this *DisjunctRangeSet) Range(i int) CharRange {
135 return this.set[i]
136 }
137
138 func (this *DisjunctRangeSet) insertRange(at int, from, to rune) {
139 mvSegSize := len(this.set) - at
140 this.set = append(this.set, CharRange{from, to})
141 if mvSegSize > 0 {
142 copy(this.set[at+1:], this.set[at:at+mvSegSize])
143 this.set[at].From, this.set[at].To = from, to
144 }
145 }
146
147 func (this *DisjunctRangeSet) List() []CharRange {
148 return this.set
149 }
150
151 func (this *DisjunctRangeSet) Size() int {
152 return len(this.set)
153 }
154
155 func (this *DisjunctRangeSet) String() string {
156 w := new(strings.Builder)
157 fmt.Fprintf(w, "{")
158 for i, rng := range this.set {
159 if i > 0 {
160 fmt.Fprintf(w, ", %s", rng.String())
161 } else {
162 fmt.Fprintf(w, "%s", rng.String())
163 }
164 }
165 fmt.Fprintf(w, "}")
166 return w.String()
167 }