| 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 astscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/ |
| 16 | |
| 17 | import ( |
| 18 | "fmtscip-go gomod github.com/golang/go/src go1.24 fmt/" |
| 19 | "sortscip-go gomod github.com/golang/go/src go1.24 sort/" |
| 20 | "stringsscip-go gomod github.com/golang/go/src go1.24 strings/" |
| 21 | ) |
| 22 | |
| 23 | // All maps are indexed by production id |
| 24 | type LexPartscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexPart# struct { |
| 25 | Headerscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexPart#Header. *FileHeaderscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/FileHeader# |
| 26 | *LexImportsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexPart#LexImports. |
| 27 | TokDefsListscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexPart#TokDefsList. []*LexTokDefscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexTokDef# |
| 28 | TokDefsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexPart#TokDefs. map[string]*LexTokDefscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexTokDef# |
| 29 | stringLitToksscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexPart#stringLitToks. map[string]*LexTokDefscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexTokDef# |
| 30 | RegDefsListscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexPart#RegDefsList. []*LexRegDefscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexRegDef# |
| 31 | RegDefsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexPart#RegDefs. map[string]*LexRegDefscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexRegDef# |
| 32 | IgnoredTokDefsListscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexPart#IgnoredTokDefsList. []*LexIgnoredTokDefscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexIgnoredTokDef# |
| 33 | IgnoredTokDefsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexPart#IgnoredTokDefs. map[string]*LexIgnoredTokDefscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexIgnoredTokDef# |
| 34 | ProdListscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexPart#ProdList. *LexProductionsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexProductions# |
| 35 | ProdMapscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexPart#ProdMap. *LexProdMapscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexProdMap# |
| 36 | } |
| 37 | |
| 38 | func NewLexPartscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/NewLexPart().(header, imports, prodList interface{}) (*LexPartscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexPart#, error) { |
| 39 | lexPart := &LexPartscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexPart#{ |
| 40 | TokDefsListscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexPart#TokDefsList.: make([]*LexTokDefscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexTokDef#, 0, 16), |
| 41 | TokDefsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexPart#TokDefs.: make(map[string]*LexTokDefscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexTokDef#, 16), |
| 42 | stringLitToksscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexPart#stringLitToks.: make(map[string]*LexTokDefscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexTokDef#, 16), |
| 43 | RegDefsListscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexPart#RegDefsList.: make([]*LexRegDefscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexRegDef#, 0, 16), |
| 44 | RegDefsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexPart#RegDefs.: make(map[string]*LexRegDefscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexRegDef#, 16), |
| 45 | IgnoredTokDefsListscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexPart#IgnoredTokDefsList.: make([]*LexIgnoredTokDefscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexIgnoredTokDef#, 0, 16), |
| 46 | IgnoredTokDefsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexPart#IgnoredTokDefs.: make(map[string]*LexIgnoredTokDefscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexIgnoredTokDef#, 16), |
| 47 | } |
| 48 | if header != nil { |
| 49 | lexPart.Headerscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexPart#Header. = header.(*FileHeaderscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/FileHeader#) |
| 50 | } else { |
| 51 | lexPart.Headerscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexPart#Header. = new(FileHeaderscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/FileHeader#) |
| 52 | } |
| 53 | if imports != nil { |
| 54 | lexPart.LexImportsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexPart#LexImports. = imports.(*LexImportsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexImports#) |
| 55 | } else { |
| 56 | lexPart.LexImportsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexPart#LexImports. = newLexImportsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/newLexImports().() |
| 57 | } |
| 58 | if prodList != nil { |
| 59 | lexPart.ProdListscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexPart#ProdList. = prodList.(*LexProductionsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexProductions#) |
| 60 | lexPart.ProdMapscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexPart#ProdMap. = NewLexProdMapscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/NewLexProdMap().(prodList.(*LexProductionsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexProductions#)) |
| 61 | for _, p := range prodList.(*LexProductionsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexProductions#).Productionsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexProductions#Productions. { |
| 62 | pid := p.Idscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexProduction#Id.() |
| 63 | |
| 64 | switch p1 := p.(type) { |
| 65 | case *LexTokDefscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexTokDef#: |
| 66 | //TODO: decide whether to handle in separate semantic check |
| 67 | if _, exist := lexPart.TokDefsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexPart#TokDefs.[pid]; exist { |
| 68 | return nil, fmtscip-go gomod github.com/golang/go/src go1.24 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24 fmt/Errorf().("duplicate token def: %s", pid) |
| 69 | } |
| 70 | lexPart.TokDefsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexPart#TokDefs.[pid] = p1 |
| 71 | lexPart.TokDefsListscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexPart#TokDefsList. = append(lexPart.TokDefsListscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexPart#TokDefsList., p1) |
| 72 | case *LexRegDefscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexRegDef#: |
| 73 | //TODO: decide whether to handle in separate semantic check |
| 74 | if _, exist := lexPart.RegDefsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexPart#RegDefs.[pid]; exist { |
| 75 | return nil, fmtscip-go gomod github.com/golang/go/src go1.24 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24 fmt/Errorf().("duplicate token def: %s", pid) |
| 76 | } |
| 77 | lexPart.RegDefsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexPart#RegDefs.[pid] = p1 |
| 78 | lexPart.RegDefsListscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexPart#RegDefsList. = append(lexPart.RegDefsListscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexPart#RegDefsList., p1) |
| 79 | case *LexIgnoredTokDefscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexIgnoredTokDef#: |
| 80 | if _, exist := lexPart.IgnoredTokDefsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexPart#IgnoredTokDefs.[pid]; exist { |
| 81 | return nil, fmtscip-go gomod github.com/golang/go/src go1.24 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24 fmt/Errorf().("duplicate ignored token def: %s", pid) |
| 82 | } |
| 83 | lexPart.IgnoredTokDefsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexPart#IgnoredTokDefs.[pid] = p1 |
| 84 | lexPart.IgnoredTokDefsListscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexPart#IgnoredTokDefsList. = append(lexPart.IgnoredTokDefsListscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexPart#IgnoredTokDefsList., p1) |
| 85 | } |
| 86 | } |
| 87 | } else { |
| 88 | lexPart.ProdListscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexPart#ProdList. = newLexProductionsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/newLexProductions().() |
| 89 | lexPart.ProdMapscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexPart#ProdMap. = newLexProdMapscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/newLexProdMap().() |
| 90 | } |
| 91 | return lexPart, nil |
| 92 | } |
| 93 | |
| 94 | func (this *LexPartscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexPart#) StringLitTokDefscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexPart#StringLitTokDef().(id string) *LexTokDefscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexTokDef# { |
| 95 | tokDef := this.stringLitToksscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexPart#stringLitToks.[id] |
| 96 | return tokDef |
| 97 | } |
| 98 | |
| 99 | func (this *LexPartscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexPart#) Productionscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexPart#Production().(id string) LexProductionscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexProduction# { |
| 100 | idx, exist := this.ProdMapscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexPart#ProdMap..idMapscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexProdMap#idMap.[id] |
| 101 | if !exist { |
| 102 | panic(fmtscip-go gomod github.com/golang/go/src go1.24 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.24 fmt/Sprintf().("Unknown production: %s", id)) |
| 103 | } |
| 104 | return this.ProdListscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexPart#ProdList..Productionsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexProductions#Productions.[idx] |
| 105 | } |
| 106 | |
| 107 | func (this *LexPartscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexPart#) ProdIndexscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexPart#ProdIndex().(id string) LexProdIndexscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexProdIndex# { |
| 108 | idx, exist := this.ProdMapscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexPart#ProdMap..idMapscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexProdMap#idMap.[id] |
| 109 | if !exist { |
| 110 | panic(fmtscip-go gomod github.com/golang/go/src go1.24 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.24 fmt/Sprintf().("Unknown production %s", id)) |
| 111 | } |
| 112 | return idx |
| 113 | } |
| 114 | |
| 115 | func (this *LexPartscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexPart#) TokenIdsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexPart#TokenIds().() []string { |
| 116 | tids := make([]string, 0, len(this.TokDefsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexPart#TokDefs.)) |
| 117 | for tid := range this.TokDefsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexPart#TokDefs. { |
| 118 | tids = append(tids, tid) |
| 119 | } |
| 120 | sortscip-go gomod github.com/golang/go/src go1.24 sort/.Stringsscip-go gomod github.com/golang/go/src go1.24 sort/Strings().(tids) |
| 121 | return tids |
| 122 | } |
| 123 | |
| 124 | func (this *LexPartscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexPart#) UpdateStringLitTokensscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexPart#UpdateStringLitTokens().(tokens []string) { |
| 125 | for _, strLit := range tokens { |
| 126 | tokDef := NewLexStringLitTokDefscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/NewLexStringLitTokDef().(strLit) |
| 127 | this.ProdMapscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexPart#ProdMap..Addscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexProdMap#Add().(tokDef) |
| 128 | this.TokDefsListscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexPart#TokDefsList. = append(this.TokDefsListscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexPart#TokDefsList., tokDef) |
| 129 | this.TokDefsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexPart#TokDefs.[strLit] = tokDef |
| 130 | this.stringLitToksscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexPart#stringLitToks.[strLit] = tokDef |
| 131 | this.ProdListscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexPart#ProdList..Productionsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexProductions#Productions. = append(this.ProdListscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexPart#ProdList..Productionsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexProductions#Productions., tokDef) |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | func (this *LexPartscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexPart#) Stringscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexPart#String().() string { |
| 136 | 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#) |
| 137 | fmtscip-go gomod github.com/golang/go/src go1.24 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.24 fmt/Fprintf().(buf, "LexPart.ProdMap:\n") |
| 138 | if this.ProdListscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexPart#ProdList. != nil { |
| 139 | for i, p := range this.ProdListscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexPart#ProdList..Productionsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexProductions#Productions. { |
| 140 | fmtscip-go gomod github.com/golang/go/src go1.24 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.24 fmt/Fprintf().(buf, "\t%d: %s\n", i, p) |
| 141 | } |
| 142 | } |
| 143 | fmtscip-go gomod github.com/golang/go/src go1.24 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.24 fmt/Fprintf().(buf, "LexPart.TokDefs:\n") |
| 144 | for t := range this.TokDefsscip-go gomod github.com/goccmack/gocc . `github.com/goccmack/gocc/internal/ast`/LexPart#TokDefs. { |
| 145 | fmtscip-go gomod github.com/golang/go/src go1.24 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.24 fmt/Fprintf().(buf, "\t%s\n", t) |
| 146 | } |
| 147 | return buf.Stringscip-go gomod github.com/golang/go/src go1.24 strings/Builder#String().() |
| 148 | } |