| 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 firstscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/first`/ |
| 16 | |
| 17 | import ( |
| 18 | "fmtscip-go gomod github.com/golang/go/src go1.24 fmt/" |
| 19 | "stringsscip-go gomod github.com/golang/go/src go1.24 strings/" |
| 20 | |
| 21 | "github.com/goccmack/gocc/internal/astscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/" |
| 22 | "github.com/goccmack/gocc/internal/parser/symbolsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/symbols`/" |
| 23 | ) |
| 24 | |
| 25 | // FirstSets represents a map of id->production. |
| 26 | type FirstSetsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/first`/FirstSets# struct { |
| 27 | firstSetsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/first`/FirstSets#firstSets. map[string]SymbolSetscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/first`/SymbolSet# |
| 28 | symbolsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/first`/FirstSets#symbols. *symbolsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/symbols`/.Symbolsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/symbols`/Symbols# |
| 29 | } |
| 30 | |
| 31 | const EMPTYscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/first`/EMPTY. = "empty" |
| 32 | |
| 33 | // GetFirstSets returns the FirstSets of the Grammar. |
| 34 | func GetFirstSetsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/first`/GetFirstSets().(g *astscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/.Grammarscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/Grammar#, symbols *symbolsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/symbols`/.Symbolsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/symbols`/Symbols#) *FirstSetsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/first`/FirstSets# { |
| 35 | firstSets := &FirstSetsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/first`/FirstSets#{ |
| 36 | firstSetsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/first`/FirstSets#firstSets.: make(map[string]SymbolSetscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/first`/SymbolSet#), |
| 37 | symbolsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/first`/FirstSets#symbols.: symbols, |
| 38 | } |
| 39 | |
| 40 | if g.SyntaxPartscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/Grammar#SyntaxPart. == nil { |
| 41 | return firstSets |
| 42 | } |
| 43 | |
| 44 | for again := true; again; { |
| 45 | again = false |
| 46 | for _, prod := range g.SyntaxPartscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/Grammar#SyntaxPart..ProdListscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/SyntaxPart#ProdList. { |
| 47 | switch { |
| 48 | case len(prod.Bodyscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/SyntaxProd#Body..Symbolsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/SyntaxBody#Symbols.) == 0: |
| 49 | if firstSets.AddTokenscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/first`/FirstSets#AddToken().(prod.Idscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/SyntaxProd#Id., EMPTYscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/first`/EMPTY.) { |
| 50 | again = true |
| 51 | } |
| 52 | case symbols.IsTerminalscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/symbols`/Symbols#IsTerminal().(prod.Bodyscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/SyntaxProd#Body..Symbolsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/SyntaxBody#Symbols.[0].SymbolStringscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/SyntaxSymbol#SymbolString.()): |
| 53 | if firstSets.AddTokenscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/first`/FirstSets#AddToken().(prod.Idscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/SyntaxProd#Id., prod.Bodyscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/SyntaxProd#Body..Symbolsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/SyntaxBody#Symbols.[0].SymbolStringscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/SyntaxSymbol#SymbolString.()) { |
| 54 | again = true |
| 55 | } |
| 56 | default: |
| 57 | first := FirstSscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/first`/FirstS().(firstSets, stringListscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/first`/stringList().(prod.Bodyscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/SyntaxProd#Body..Symbolsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/SyntaxBody#Symbols.)) |
| 58 | if !first.Equalscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/first`/SymbolSet#Equal().(firstSets.GetSetscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/first`/FirstSets#GetSet().(prod.Idscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/SyntaxProd#Id.)) { |
| 59 | if firstSets.AddSetscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/first`/FirstSets#AddSet().(prod.Idscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/SyntaxProd#Id., first) { |
| 60 | again = true |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | return firstSets |
| 68 | } |
| 69 | |
| 70 | func stringListscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/first`/stringList().(symbols []astscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/.SyntaxSymbolscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/SyntaxSymbol#) []string { |
| 71 | sl := make([]string, len(symbols)) |
| 72 | for i, sym := range symbols { |
| 73 | sl[i] = sym.SymbolStringscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/SyntaxSymbol#SymbolString.() |
| 74 | } |
| 75 | return sl |
| 76 | } |
| 77 | |
| 78 | func (this *FirstSetsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/first`/FirstSets#) AddSetscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/first`/FirstSets#AddSet().(prodName string, terminals SymbolSetscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/first`/SymbolSet#) (symbolsAdded bool) { |
| 79 | for symbol := range terminals { |
| 80 | if this.AddTokenscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/first`/FirstSets#AddToken().(prodName, symbol) { |
| 81 | symbolsAdded = true |
| 82 | } |
| 83 | } |
| 84 | return |
| 85 | } |
| 86 | |
| 87 | func (this *FirstSetsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/first`/FirstSets#) AddTokenscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/first`/FirstSets#AddToken().(prodName string, terminal string) (symbolAdded bool) { |
| 88 | set, ok := this.firstSetsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/first`/FirstSets#firstSets.[prodName] |
| 89 | if !ok { |
| 90 | set = make(SymbolSetscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/first`/SymbolSet#) |
| 91 | this.firstSetsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/first`/FirstSets#firstSets.[prodName] = set |
| 92 | } |
| 93 | if _, contain := set[terminal]; !contain { |
| 94 | set[terminal] = true |
| 95 | symbolAdded = true |
| 96 | } |
| 97 | return |
| 98 | } |
| 99 | |
| 100 | // GetSet will return the SymbolSet corresponding to `prodName` or nil. |
| 101 | func (this *FirstSetsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/first`/FirstSets#) GetSetscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/first`/FirstSets#GetSet().(prodName string) SymbolSetscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/first`/SymbolSet# { |
| 102 | if set, ok := this.firstSetsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/first`/FirstSets#firstSets.[prodName]; ok { |
| 103 | return set |
| 104 | } |
| 105 | return nil |
| 106 | } |
| 107 | |
| 108 | // String will return the string representing the FirstSets. |
| 109 | func (this *FirstSetsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/first`/FirstSets#) Stringscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/first`/FirstSets#String().() string { |
| 110 | buf := new(stringsscip-go gomod github.com/golang/go/src go1.24 strings/.Builderscip-go gomod github.com/golang/go/src go1.24 strings/Builder#) |
| 111 | for _, nt := range this.symbolsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/first`/FirstSets#symbols..NTListscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/symbols`/Symbols#NTList().() { |
| 112 | set := this.firstSetsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/first`/FirstSets#firstSets.[nt] |
| 113 | fmtscip-go gomod github.com/golang/go/src go1.24 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.24 fmt/Fprintf().(buf, "%s: %s\n", nt, set) |
| 114 | } |
| 115 | return buf.Stringscip-go gomod github.com/golang/go/src go1.24 strings/Builder#String().() |
| 116 | } |
| 117 | |
| 118 | // First returns the first `SymbolSet` given the ast.SyntaxSymbol. |
| 119 | func Firstscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/first`/First().(fs *FirstSetsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/first`/FirstSets#, sym string) SymbolSetscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/first`/SymbolSet# { |
| 120 | if fs.symbolsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/first`/FirstSets#symbols..IsTerminalscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/symbols`/Symbols#IsTerminal().(sym) { |
| 121 | return SymbolSetscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/first`/SymbolSet#{sym: true} |
| 122 | } |
| 123 | return fs.GetSetscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/first`/FirstSets#GetSet().(sym) |
| 124 | } |
| 125 | |
| 126 | /* |
| 127 | Returns First of the string, xyz, e.g.: for the item, |
| 128 | |
| 129 | X : w • xyz |
| 130 | |
| 131 | Let x, y, z be strings from the union of T and NT. |
| 132 | First(xy...z) = |
| 133 | First(x) if First(x) does not contain ϵ |
| 134 | First(x) + First(y) if First(x) contains ϵ but First(y) does not contain ϵ |
| 135 | ... |
| 136 | First(x) + First(y) + ... + First(z) |
| 137 | */ |
| 138 | func FirstSscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/first`/FirstS().(firstSets *FirstSetsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/first`/FirstSets#, symbols []string) (first SymbolSetscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/first`/SymbolSet#) { |
| 139 | first = make(SymbolSetscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/first`/SymbolSet#) |
| 140 | if len(symbols) == 0 { |
| 141 | return |
| 142 | } |
| 143 | fst := Firstscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/first`/First().(firstSets, symbols[0]) |
| 144 | first.AddSetscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/first`/SymbolSet#AddSet().(fst) |
| 145 | _, containEmpty := fst["empty"] |
| 146 | for i := 1; i < len(symbols) && containEmpty; i++ { |
| 147 | fst = Firstscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/first`/First().(firstSets, symbols[i]) |
| 148 | first.AddSetscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/parser/first`/SymbolSet#AddSet().(fst) |
| 149 | _, containEmpty = fst["empty"] |
| 150 | } |
| 151 | if !containEmpty { |
| 152 | delete(first, "empty") |
| 153 | } |
| 154 | return |
| 155 | } |