diff --git a/bootstrap/pkg/ast/location.go b/bootstrap/pkg/ast/location.go index 7b3921c..e3800e5 100644 --- a/bootstrap/pkg/ast/location.go +++ b/bootstrap/pkg/ast/location.go @@ -18,10 +18,21 @@ along with this program. If not, see . 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 { - start int - end int - source Source + start int + end int + // 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 } @@ -35,6 +46,9 @@ func (l *Location) GetEnd() int { 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 { if l.IsKnownLocaiton() { return &l.source @@ -42,6 +56,8 @@ func (l *Location) GetSource() *Source { 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) { if x+l.start < len(*l.source.Buffer) { 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) { if l.start-x >= 0 { 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) { if x+l.end < len(*l.source.Buffer) { 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) { if l.end-x >= 0 { l.end -= x @@ -80,10 +102,8 @@ func (l *Location) IsKnownLocaiton() bool { return l.knownLocation } -type ILocatable interface { - GetLocation() *Location -} - +// MakeLocation returns a pointer to a `Location` in the given source `input` +// specified by the `start` and `end` boundaries func MakeLocation(input *Source, start int, end int) *Location { return &Location{ source: *input,