| 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 config |
| 16 | |
| 17 | import ( |
| 18 | "errors" |
| 19 | "flag" |
| 20 | "fmt" |
| 21 | "go/build" |
| 22 | "os" |
| 23 | "path" |
| 24 | "path/filepath" |
| 25 | "strings" |
| 26 | |
| 27 | "golang.org/x/mod/modfile" |
| 28 | ) |
| 29 | |
| 30 | type Config interface { |
| 31 | Help() bool |
| 32 | Verbose() bool |
| 33 | Zip() bool |
| 34 | AllowUnreachable() bool |
| 35 | AutoResolveLRConf() bool |
| 36 | SourceFile() string |
| 37 | OutDir() string |
| 38 | |
| 39 | NoLexer() bool |
| 40 | DebugLexer() bool |
| 41 | DebugParser() bool |
| 42 | |
| 43 | ErrorsDir() string |
| 44 | ParserDir() string |
| 45 | ScannerDir() string |
| 46 | TokenDir() string |
| 47 | |
| 48 | ProjectName() string |
| 49 | Package() string |
| 50 | |
| 51 | PrintParams() |
| 52 | } |
| 53 | |
| 54 | type ConfigRecord struct { |
| 55 | workingDir string |
| 56 | |
| 57 | allowUnreachable *bool |
| 58 | autoResolveLRConf *bool |
| 59 | debugLexer *bool |
| 60 | debugParser *bool |
| 61 | help *bool |
| 62 | noLexer *bool |
| 63 | outDir string |
| 64 | pkg string |
| 65 | srcFile string |
| 66 | verbose *bool |
| 67 | zip *bool |
| 68 | } |
| 69 | |
| 70 | func New() (Config, error) { |
| 71 | wd, err := os.Getwd() |
| 72 | if err != nil { |
| 73 | return nil, err |
| 74 | } |
| 75 | |
| 76 | cfg := &ConfigRecord{ |
| 77 | workingDir: wd, |
| 78 | } |
| 79 | |
| 80 | if err := cfg.getFlags(); err != nil { |
| 81 | cfg.PrintParams() |
| 82 | return nil, err |
| 83 | } |
| 84 | |
| 85 | return cfg, nil |
| 86 | } |
| 87 | |
| 88 | func (this *ConfigRecord) Help() bool { |
| 89 | return *this.help |
| 90 | } |
| 91 | |
| 92 | func (this *ConfigRecord) Verbose() bool { |
| 93 | return *this.verbose |
| 94 | } |
| 95 | |
| 96 | func (this *ConfigRecord) Zip() bool { |
| 97 | return *this.zip |
| 98 | } |
| 99 | |
| 100 | func (this *ConfigRecord) AllowUnreachable() bool { |
| 101 | return *this.allowUnreachable |
| 102 | } |
| 103 | |
| 104 | func (this *ConfigRecord) AutoResolveLRConf() bool { |
| 105 | return *this.autoResolveLRConf |
| 106 | } |
| 107 | |
| 108 | func (this *ConfigRecord) NoLexer() bool { |
| 109 | return *this.noLexer |
| 110 | } |
| 111 | |
| 112 | func (this *ConfigRecord) DebugLexer() bool { |
| 113 | return *this.debugLexer |
| 114 | } |
| 115 | |
| 116 | func (this *ConfigRecord) DebugParser() bool { |
| 117 | return *this.debugParser |
| 118 | } |
| 119 | |
| 120 | func (this *ConfigRecord) SourceFile() string { |
| 121 | return this.srcFile |
| 122 | } |
| 123 | |
| 124 | func (this *ConfigRecord) OutDir() string { |
| 125 | return this.outDir |
| 126 | } |
| 127 | |
| 128 | func (this *ConfigRecord) ErrorsDir() string { |
| 129 | return path.Join(this.outDir, "errors") |
| 130 | } |
| 131 | |
| 132 | func (this *ConfigRecord) ParserDir() string { |
| 133 | return path.Join(this.outDir, "parser") |
| 134 | } |
| 135 | |
| 136 | func (this *ConfigRecord) ScannerDir() string { |
| 137 | return path.Join(this.outDir, "scanner") |
| 138 | } |
| 139 | |
| 140 | func (this *ConfigRecord) TokenDir() string { |
| 141 | return path.Join(this.outDir, "token") |
| 142 | } |
| 143 | |
| 144 | func (this *ConfigRecord) Package() string { |
| 145 | return this.pkg |
| 146 | } |
| 147 | |
| 148 | func (this *ConfigRecord) ProjectName() string { |
| 149 | _, file := path.Split(this.pkg) |
| 150 | return file |
| 151 | } |
| 152 | |
| 153 | func (this *ConfigRecord) PrintParams() { |
| 154 | fmt.Printf("-a = %v\n", *this.autoResolveLRConf) |
| 155 | fmt.Printf("-debug_lexer = %v\n", *this.debugLexer) |
| 156 | fmt.Printf("-debug_parser = %v\n", *this.debugParser) |
| 157 | fmt.Printf("-h = %v\n", *this.help) |
| 158 | fmt.Printf("-no_lexer = %v\n", *this.noLexer) |
| 159 | fmt.Printf("-o = %v\n", this.outDir) |
| 160 | fmt.Printf("-p = %v\n", this.pkg) |
| 161 | fmt.Printf("-u = %v\n", *this.allowUnreachable) |
| 162 | fmt.Printf("-v = %v\n", *this.verbose) |
| 163 | fmt.Printf("-zip = %v\n", *this.zip) |
| 164 | } |
| 165 | |
| 166 | /*** Utility routines ***/ |
| 167 | |
| 168 | func (this *ConfigRecord) getFlags() error { |
| 169 | pkgDefault, _ := defaultPackage(this.workingDir) |
| 170 | |
| 171 | this.autoResolveLRConf = flag.Bool("a", false, "automatically resolve LR(1) conflicts") |
| 172 | this.debugLexer = flag.Bool("debug_lexer", false, "enable debug logging in lexer") |
| 173 | this.debugParser = flag.Bool("debug_parser", false, "enable debug logging in parser") |
| 174 | this.help = flag.Bool("h", false, "help") |
| 175 | this.noLexer = flag.Bool("no_lexer", false, "do not generate a lexer") |
| 176 | flag.StringVar(&this.outDir, "o", this.workingDir, "output dir.") |
| 177 | flag.StringVar(&this.pkg, "p", pkgDefault, "package") |
| 178 | this.allowUnreachable = flag.Bool("u", false, "allow unreachable productions") |
| 179 | this.verbose = flag.Bool("v", false, "verbose") |
| 180 | this.zip = flag.Bool("zip", false, "zip the actiontable and gototable (experimental)") |
| 181 | flag.Parse() |
| 182 | |
| 183 | if *this.noLexer && *this.debugLexer { |
| 184 | return errors.New("no_lexer and debug_lexer cannot both be set") |
| 185 | } |
| 186 | |
| 187 | this.outDir = getOutDir(this.outDir, this.workingDir) |
| 188 | if this.outDir != this.workingDir { |
| 189 | pkg, err := defaultPackage(this.outDir) |
| 190 | if err != nil { |
| 191 | return fmt.Errorf("getting package: %s", err) |
| 192 | } |
| 193 | this.pkg = pkg |
| 194 | } |
| 195 | |
| 196 | if len(flag.Args()) != 1 && !*this.help { |
| 197 | return errors.New("too few arguments") |
| 198 | } |
| 199 | |
| 200 | this.srcFile = flag.Arg(0) |
| 201 | |
| 202 | return nil |
| 203 | } |
| 204 | |
| 205 | func getOutDir(outDirSpec, wd string) string { |
| 206 | if strings.HasPrefix(outDirSpec, wd) { |
| 207 | return outDirSpec |
| 208 | } |
| 209 | return path.Join(wd, outDirSpec) |
| 210 | } |
| 211 | |
| 212 | func defaultPackage(wd string) (string, error) { |
| 213 | pkg, parent, err := currentModule() |
| 214 | if err == nil { |
| 215 | pkg = pkg + strings.TrimPrefix(wd, parent) |
| 216 | return pkg, nil |
| 217 | } |
| 218 | |
| 219 | dirs := build.Default.SrcDirs() |
| 220 | for _, srcDir := range dirs { |
| 221 | if strings.HasPrefix(wd, srcDir) { |
| 222 | pkg = strings.TrimPrefix(wd, srcDir) |
| 223 | pkg = sanitizePackage(pkg) |
| 224 | return pkg, nil |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | return "", errors.New("can not determine default package name") |
| 229 | } |
| 230 | |
| 231 | func sanitizePackage(pkg string) string { |
| 232 | pkg = filepath.ToSlash(pkg) |
| 233 | pkg = strings.TrimPrefix(pkg, "/") |
| 234 | return pkg |
| 235 | } |
| 236 | |
| 237 | var errNoModules = errors.New("not using modules") |
| 238 | |
| 239 | // currentModule looks in all parent dirs for a go.mod file containing |
| 240 | // a module name. |
| 241 | func currentModule() (string, string, error) { |
| 242 | parent, err := os.Getwd() |
| 243 | if err != nil { |
| 244 | // A nonexistent working directory can't be in a module. |
| 245 | return "", "", fmt.Errorf("getting working directory: %s", err) |
| 246 | } |
| 247 | |
| 248 | var info os.FileInfo |
| 249 | for { |
| 250 | info, err = os.Stat(filepath.Join(parent, "go.mod")) |
| 251 | if err == nil && !info.IsDir() { |
| 252 | break |
| 253 | } |
| 254 | d := filepath.Dir(parent) |
| 255 | if len(d) >= len(parent) { |
| 256 | return "", "", errNoModules // reached top of file system, no go.mod |
| 257 | } |
| 258 | parent = d |
| 259 | } |
| 260 | |
| 261 | full := path.Join(parent, info.Name()) |
| 262 | data, err := os.ReadFile(full) |
| 263 | if err != nil { |
| 264 | return "", "", fmt.Errorf("reading file '%s': %s", full, err) |
| 265 | } |
| 266 | |
| 267 | f, err := modfile.Parse(info.Name(), data, nil) |
| 268 | if err != nil { |
| 269 | return "", "", fmt.Errorf("parsing file '%s': %s", full, err) |
| 270 | } |
| 271 | |
| 272 | m := f.Module.Mod.Path |
| 273 | return m, parent, nil |
| 274 | } |