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

     1  package lang
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/lmorg/murex/lang/ref"
     8  )
     9  
    10  func ErrPrivateNotFound(module string) error {
    11  	return fmt.Errorf("no private functions exist for module `%s`", module)
    12  }
    13  
    14  type privateFunctions struct {
    15  	module map[string]*MurexFuncs
    16  }
    17  
    18  func NewMurexPrivs() *privateFunctions {
    19  	pf := new(privateFunctions)
    20  	pf.module = make(map[string]*MurexFuncs)
    21  	return pf
    22  }
    23  
    24  func (pf *privateFunctions) Define(name string, parameters []MxFunctionParams, block []rune, fileRef *ref.File) {
    25  	if pf.module[fileRef.Source.Module] == nil {
    26  		pf.module[fileRef.Source.Module] = NewMurexFuncs()
    27  	}
    28  
    29  	pf.module[fileRef.Source.Module].Define(name, parameters, block, fileRef)
    30  }
    31  
    32  func (pf *privateFunctions) get(name string, fileRef *ref.File) *murexFuncDetails {
    33  	if pf.module[fileRef.Source.Module] == nil {
    34  		return nil
    35  	}
    36  
    37  	return pf.module[fileRef.Source.Module].get(name)
    38  }
    39  
    40  func (pf *privateFunctions) GetString(name string, module string) *murexFuncDetails {
    41  	if pf.module[module] == nil {
    42  		return nil
    43  	}
    44  
    45  	return pf.module[module].get(name)
    46  }
    47  
    48  func (pf *privateFunctions) Exists(name string, fileRef *ref.File) bool {
    49  	if pf.module[fileRef.Source.Module] == nil {
    50  		return false
    51  	}
    52  
    53  	return pf.module[fileRef.Source.Module].Exists(name)
    54  }
    55  
    56  func (pf *privateFunctions) ExistsString(name string, module string) bool {
    57  	if pf.module[module] == nil {
    58  		return false
    59  	}
    60  
    61  	return pf.module[module].Exists(name)
    62  }
    63  
    64  func (pf *privateFunctions) BlockString(name string, module string) ([]rune, error) {
    65  	if pf.module[module] == nil {
    66  		return nil, ErrPrivateNotFound(module)
    67  	}
    68  
    69  	return pf.module[module].Block(name)
    70  }
    71  
    72  func (pf *privateFunctions) Summary(name string, fileRef *ref.File) (string, error) {
    73  	if pf.module[fileRef.Source.Module] == nil {
    74  		return "", ErrPrivateNotFound(fileRef.Source.Module)
    75  	}
    76  
    77  	return pf.module[fileRef.Source.Module].Summary(name)
    78  }
    79  
    80  func (pf *privateFunctions) Undefine(name string, fileRef *ref.File) error {
    81  	if pf.module[fileRef.Source.Module] == nil {
    82  		return ErrPrivateNotFound(fileRef.Source.Module)
    83  	}
    84  
    85  	return pf.module[fileRef.Source.Module].Undefine(name)
    86  }
    87  
    88  func (pf *privateFunctions) Dump() interface{} {
    89  	dump := make(map[string]map[string]interface{})
    90  	for name, module := range pf.module {
    91  		path := strings.SplitN(name, "/", 2)
    92  		if len(path) != 2 {
    93  			return fmt.Sprintf("error: module path doesn't follow standard 'package/module' format: '%s'", name)
    94  		}
    95  
    96  		if len(dump[path[0]]) == 0 {
    97  			dump[path[0]] = make(map[string]interface{})
    98  		}
    99  
   100  		dump[path[0]][path[1]] = module.Dump()
   101  	}
   102  
   103  	return dump
   104  }