go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/common/flag/nestedflagset/lexer.go (about)

     1  // Copyright 2016 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package nestedflagset
    16  
    17  import (
    18  	"bytes"
    19  	"unicode/utf8"
    20  )
    21  
    22  // Context is the lexer's current state.
    23  type lexerContext struct {
    24  	value string
    25  	index int
    26  	delim rune
    27  }
    28  
    29  // nextToken parses and returns the next token in the string.
    30  func (l *lexerContext) nextToken() token {
    31  	buf := new(bytes.Buffer)
    32  
    33  	index := 0
    34  	escaped := false
    35  	quoted := false
    36  
    37  MainLoop:
    38  	for _, c := range l.value[l.index:] {
    39  		index += utf8.RuneLen(c)
    40  
    41  		if escaped {
    42  			escaped = false
    43  			buf.WriteRune(c)
    44  			continue
    45  		}
    46  
    47  		switch c {
    48  		case '\\':
    49  			escaped = true
    50  
    51  		case '"':
    52  			quoted = !quoted
    53  
    54  		case l.delim:
    55  			if quoted {
    56  				buf.WriteRune(c)
    57  			} else {
    58  				break MainLoop
    59  			}
    60  
    61  		default:
    62  			buf.WriteRune(c)
    63  		}
    64  	}
    65  
    66  	l.index += index
    67  	return token(buf.Bytes())
    68  }
    69  
    70  // Lexer creates a new lexer lexerContext.
    71  func lexer(value string, delim rune) *lexerContext {
    72  	return &lexerContext{
    73  		value: value,
    74  		index: 0,
    75  		delim: delim,
    76  	}
    77  }
    78  
    79  // finished returns whether the context is finished parsing.
    80  func (l *lexerContext) finished() bool {
    81  	return l.index == len(l.value)
    82  }
    83  
    84  // split splits the Lexer's string into a slice of Tokens.
    85  func (l *lexerContext) split() []token {
    86  	result := make([]token, 0, 16)
    87  	for !l.finished() {
    88  		result = append(result, l.nextToken())
    89  	}
    90  	return result
    91  }