github.com/sandwich-go/boost@v1.3.29/misc/annotation/parser.go (about)

     1  package annotation
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/sandwich-go/boost/xstrings"
     6  	"strings"
     7  	"text/scanner"
     8  )
     9  
    10  func parser(line string, lowerKey bool) (Annotation, error) {
    11  	var (
    12  		s           scanner.Scanner
    13  		token       rune
    14  		currentStep = initialStep
    15  		attrName    string
    16  		ann         = annotation{
    17  			line:       line,
    18  			attributes: make(map[string]string),
    19  		}
    20  	)
    21  	s.Init(strings.NewReader(strings.TrimLeft(strings.TrimSpace(line), "/")))
    22  
    23  	for token != scanner.EOF && currentStep < doneStep {
    24  		token = s.Scan()
    25  		switch token {
    26  		case '@':
    27  			currentStep = annotationNameStep
    28  		case '(':
    29  			currentStep = attributeNameStep
    30  		case '=':
    31  			currentStep = attributeValueStep
    32  		case ',':
    33  			currentStep = attributeNameStep
    34  		case ')':
    35  			currentStep = doneStep
    36  		case scanner.Ident:
    37  			switch currentStep {
    38  			case annotationNameStep:
    39  				if n := s.TokenText(); len(n) > 0 {
    40  					ann.name = n
    41  				}
    42  			case attributeNameStep:
    43  				if n := s.TokenText(); len(n) > 0 {
    44  					attrName = n
    45  				}
    46  			}
    47  		default:
    48  			switch currentStep {
    49  			case attributeValueStep:
    50  				var key = attrName
    51  				if lowerKey {
    52  					key = strings.ToLower(key)
    53  				}
    54  				ann.attributes[key] =
    55  					xstrings.Trim(strings.Trim(strings.Trim(strings.Trim(s.TokenText(), "\""), "'"), "`"))
    56  			}
    57  		}
    58  	}
    59  	if currentStep != doneStep {
    60  		return nil, fmt.Errorf("invalid completion-status %v name:%s for annotation:%s", currentStep, attrName, line)
    61  	}
    62  	return ann, nil
    63  }