github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/lang/ref/ref.go (about)

     1  package ref
     2  
     3  import (
     4  	"sync"
     5  	"time"
     6  )
     7  
     8  // File is used to group the source cursor
     9  type File struct {
    10  	Source *Source
    11  	Line   int
    12  	Column int
    13  }
    14  
    15  // Source is a cache of the source file
    16  type Source struct {
    17  	Filename string
    18  	Module   string
    19  	DateTime time.Time
    20  	source   []byte
    21  }
    22  
    23  // Equal checks two FileRef sources are the same
    24  func (s Source) Equal(source *Source) bool {
    25  	return s.Module == source.Module &&
    26  		s.Filename == source.Filename &&
    27  		s.DateTime.Equal(source.DateTime)
    28  }
    29  
    30  type history struct {
    31  	hist  []*Source
    32  	mutex sync.Mutex
    33  }
    34  
    35  // AddSource creates a new ref.Source object and appends it to the source history
    36  func (h *history) AddSource(filename, module string, source []byte) *Source {
    37  	src := &Source{
    38  		Filename: filename,
    39  		Module:   module,
    40  		DateTime: time.Now(),
    41  		source:   source,
    42  	}
    43  
    44  	h.mutex.Lock()
    45  	h.hist = append(h.hist, src)
    46  	h.mutex.Unlock()
    47  
    48  	return src
    49  }
    50  
    51  type dumpVals struct {
    52  	Filename string
    53  	Module   string
    54  	DateTime time.Time
    55  	Source   string
    56  }
    57  
    58  func (h *history) Dump() []dumpVals {
    59  	dump := make([]dumpVals, len(h.hist))
    60  
    61  	for i, src := range h.hist {
    62  		dump[i] = dumpVals{
    63  			Filename: src.Filename,
    64  			Module:   src.Module,
    65  			DateTime: src.DateTime,
    66  			Source:   string(src.source),
    67  		}
    68  	}
    69  
    70  	return dump
    71  }
    72  
    73  // History is an array of all the murex source code loaded
    74  var History = new(history)
    75  
    76  func NewModule(module string) *File {
    77  	return &File{
    78  		Source: &Source{
    79  			Module:   module,
    80  			DateTime: time.Now(),
    81  		},
    82  	}
    83  }