|
| 1 | +// Copyright 2023 Google LLC. |
| 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 | +// https://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 | +// This should replace the Line class in the database. For now, it is local to |
| 16 | +// the parser generator. |
| 17 | + |
| 18 | +use filepath |
| 19 | + |
| 20 | +class Location(self, filepath: Filepath, pos:u32, len: u32, line: u32) { |
| 21 | + self.pos = pos |
| 22 | + self.len = len |
| 23 | + self.line = line |
| 24 | + filepath.appendLocation(self) |
| 25 | + |
| 26 | + // Write the line to stdout, with a filename and line number prefix. |
| 27 | + func dump(self) { |
| 28 | + filepath = self.filepath |
| 29 | + assert !isnull(filepath) |
| 30 | + path = filepath!.getRelativePath() |
| 31 | + if path.length() > 0 { |
| 32 | + println "File %s, line %u:" % (path, self.line) |
| 33 | + } |
| 34 | + if filepath!.text.length() > 0 { |
| 35 | + self.printLineAndCarots() |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + // Like dump(), but prints msg. |
| 40 | + func error(self, msg) { |
| 41 | + filepath = self.filepath |
| 42 | + assert !isnull(filepath) |
| 43 | + path = filepath!.getRelativePath() |
| 44 | + if path.length() > 0 { |
| 45 | + println "File %s, line %u: %s" % (path, self.line, msg) |
| 46 | + } |
| 47 | + if filepath!.text.length() > 0 { |
| 48 | + self.printLineAndCarots() |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + func printLineAndCarots(self) { |
| 53 | + start = self.findLineStart() |
| 54 | + end = self.findLineEnd() |
| 55 | + println self.filepath!.text[start:end] |
| 56 | + for i in range(self.pos - start) { |
| 57 | + print " " |
| 58 | + } |
| 59 | + for i in range(self.len) { |
| 60 | + print "^" |
| 61 | + } |
| 62 | + println |
| 63 | + } |
| 64 | + |
| 65 | + func findLineStart(self) -> u32 { |
| 66 | + pos = self.pos |
| 67 | + while pos != 0 { |
| 68 | + pos -= 1 |
| 69 | + c = self.filepath!.text[pos] |
| 70 | + if c == '\n' { |
| 71 | + return pos + 1 |
| 72 | + } |
| 73 | + } |
| 74 | + return 0u32 |
| 75 | + } |
| 76 | + |
| 77 | + func findLineEnd(self) -> u32 { |
| 78 | + pos = self.pos |
| 79 | + while pos < <pos>self.filepath!.text.length() { |
| 80 | + if self.filepath!.text[pos] == '\n' { |
| 81 | + return pos |
| 82 | + } |
| 83 | + pos += 1 |
| 84 | + } |
| 85 | + return <pos>self.filepath!.text.length() |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +relation DoublyLinked Filepath Location |
| 90 | + |
| 91 | +unittest { |
| 92 | + rootFilepath = Filepath.new("testdata", null(Filepath), false) |
| 93 | + filepath = Filepath.new("testdata/hello", rootFilepath, false) |
| 94 | + location1 = Location(filepath, 0u32, 5u32, 1u32) |
| 95 | + location2 = Location(filepath, 7u32, 6u32, 1u32) |
| 96 | + filepath.readFile() |
| 97 | + location1.dump() |
| 98 | + location2.dump() |
| 99 | +} |
0 commit comments