Document the location.go file

This commit is contained in:
Sameer Rahmani 2021-01-12 19:01:41 +00:00
parent 2f6f79fe35
commit e26bc00f09
1 changed files with 27 additions and 7 deletions

View File

@ -18,10 +18,21 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
package ast package ast
// ILocatable describes something that can be located in a source
type ILocatable interface {
GetLocation() *Location
}
// Location is used to point to a specific location in the source
// code (before parse). It can point to a single point or a range.
type Location struct { type Location struct {
start int start int
end int end int
source Source // Where this location is pointing too ?
source Source
// Is it a known location or not ? For example builtins doesn't
// have a knowen location
knownLocation bool knownLocation bool
} }
@ -35,6 +46,9 @@ func (l *Location) GetEnd() int {
return l.end return l.end
} }
// GetSource returns the source of the current location or the "builtin"
// source to indicate that the source of this location is not known in
// the context of source code
func (l *Location) GetSource() *Source { func (l *Location) GetSource() *Source {
if l.IsKnownLocaiton() { if l.IsKnownLocaiton() {
return &l.source return &l.source
@ -42,6 +56,8 @@ func (l *Location) GetSource() *Source {
return GetBuiltinSource() return GetBuiltinSource()
} }
// IncStart increases the start pointer of the location by `x` with respect
// to the boundaries of the source
func (l *Location) IncStart(x int) { func (l *Location) IncStart(x int) {
if x+l.start < len(*l.source.Buffer) { if x+l.start < len(*l.source.Buffer) {
l.start += x l.start += x
@ -50,6 +66,8 @@ func (l *Location) IncStart(x int) {
} }
} }
// DecStart decreases the start pointer of the location by `x` with respect
// to the boundaries of the source
func (l *Location) DecStart(x int) { func (l *Location) DecStart(x int) {
if l.start-x >= 0 { if l.start-x >= 0 {
l.start -= x l.start -= x
@ -59,6 +77,8 @@ func (l *Location) DecStart(x int) {
} }
// IncEnd increases the end pointer of the location by `x` with respect
// to the boundaries of the source
func (l *Location) IncEnd(x int) { func (l *Location) IncEnd(x int) {
if x+l.end < len(*l.source.Buffer) { if x+l.end < len(*l.source.Buffer) {
l.end += x l.end += x
@ -68,6 +88,8 @@ func (l *Location) IncEnd(x int) {
} }
// DecEnd decreases the end pointer of the location by `x` with respect
// to the boundaries of the source
func (l *Location) DecEnd(x int) { func (l *Location) DecEnd(x int) {
if l.end-x >= 0 { if l.end-x >= 0 {
l.end -= x l.end -= x
@ -80,10 +102,8 @@ func (l *Location) IsKnownLocaiton() bool {
return l.knownLocation return l.knownLocation
} }
type ILocatable interface { // MakeLocation returns a pointer to a `Location` in the given source `input`
GetLocation() *Location // specified by the `start` and `end` boundaries
}
func MakeLocation(input *Source, start int, end int) *Location { func MakeLocation(input *Source, start int, end int) *Location {
return &Location{ return &Location{
source: *input, source: *input,