| 19 | type ConstantParser struct{} |
| 20 | |
| 21 | func (cp *ConstantParser) parse(filename string) (constants []phpConstant, err error) { |
| 22 | file, err := os.Open(filename) |
| 23 | if err != nil { |
| 24 | return nil, err |
| 25 | } |
| 26 | |
| 27 | defer func() { |
| 28 | err = errors.Join(err, file.Close()) |
| 29 | }() |
| 30 | |
| 31 | scanner := bufio.NewScanner(file) |
| 32 | |
| 33 | lineNumber := 0 |
| 34 | expectConstDecl := false |
| 35 | expectClassConstDecl := false |
| 36 | currentClassName := "" |
| 37 | currentConstantValue := 0 |
| 38 | inConstBlock := false |
| 39 | exportAllInBlock := false |
| 40 | lastConstValue := "" |
| 41 | lastConstWasIota := false |
| 42 | |
| 43 | for scanner.Scan() { |
| 44 | lineNumber++ |
| 45 | line := strings.TrimSpace(scanner.Text()) |
| 46 | |
| 47 | if constRegex.MatchString(line) { |
| 48 | expectConstDecl = true |
| 49 | expectClassConstDecl = false |
| 50 | currentClassName = "" |
| 51 | |
| 52 | continue |
| 53 | } |
| 54 | |
| 55 | if matches := classConstRegex.FindStringSubmatch(line); len(matches) == 2 { |
| 56 | expectClassConstDecl = true |
| 57 | expectConstDecl = false |
| 58 | currentClassName = matches[1] |
| 59 | |
| 60 | continue |
| 61 | } |
| 62 | |
| 63 | if strings.HasPrefix(line, "const (") { |
| 64 | inConstBlock = true |
| 65 | currentConstantValue = 0 |
| 66 | if expectConstDecl || expectClassConstDecl { |
| 67 | exportAllInBlock = true |
| 68 | } |
| 69 | continue |
| 70 | } |
| 71 | |
| 72 | if inConstBlock && line == ")" { |
| 73 | inConstBlock = false |
| 74 | exportAllInBlock = false |
| 75 | expectConstDecl = false |
| 76 | expectClassConstDecl = false |
| 77 | currentClassName = "" |
| 78 | lastConstValue = "" |