| 1 | // Copyright 2018 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 | // 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 common |
| 16 | |
| 17 | // Location interface to represent a location within Source. |
| 18 | type Location interface { |
| 19 | Line() int // 1-based line number within source. |
| 20 | Column() int // 0-based column number within source. |
| 21 | } |
| 22 | |
| 23 | // SourceLocation helper type to manually construct a location. |
| 24 | type SourceLocation struct { |
| 25 | line int |
| 26 | column int |
| 27 | } |
| 28 | |
| 29 | var ( |
| 30 | // Location implements the SourceLocation interface. |
| 31 | _ Location = &SourceLocation{} |
| 32 | // NoLocation is a particular illegal location. |
| 33 | NoLocation = &SourceLocation{-1, -1} |
| 34 | ) |
| 35 | |
| 36 | // NewLocation creates a new location. |
| 37 | func NewLocation(line, column int) Location { |
| 38 | return &SourceLocation{ |
| 39 | line: line, |
| 40 | column: column} |
| 41 | } |
| 42 | |
| 43 | // Line returns the 1-based line of the location. |
| 44 | func (l *SourceLocation) Line() int { |
| 45 | return l.line |
| 46 | } |
| 47 | |
| 48 | // Column returns the 0-based column number of the location. |
| 49 | func (l *SourceLocation) Column() int { |
| 50 | return l.column |
| 51 | } |