MCPcopy Create free account
hub / github.com/cel-expr/cel-go / parseSelectorChainTail

Method parseSelectorChainTail

parser/pratt_parser.go:579–684  ·  view source on GitHub ↗

parseSelectorChainTail parses the field selections, member calls, indexes and struct creations following lhs, which is lhsDepth deep, and leaves the depth of the resulting expression in lastParsedDepth.

(lhs ast.Expr, canBeStructName bool, lhsDepth int)

Source from the content-addressed store, hash-verified

577// creations following lhs, which is lhsDepth deep, and leaves the depth of the resulting
578// expression in lastParsedDepth.
579func (p *prattParserWorker) parseSelectorChainTail(lhs ast.Expr, canBeStructName bool, lhsDepth int) ast.Expr {
580 depth := lhsDepth
581loop:
582 for !p.recursionLimitExceeded {
583 switch p.peekTok.kind {
584 case tokDot:
585 dotTok := p.nextToken()
586 optional := false
587 if p.peekTok.kind == tokQuestion {
588 p.nextToken()
589 optional = true
590 if !p.enableOptionalSyntax {
591 p.reportError(dotTok, "unsupported syntax '.?'")
592 }
593 }
594 fieldTok := p.nextToken()
595 if fieldTok.kind != tokIdent && fieldTok.kind != tokReservedWord {
596 if fieldTok.kind != tokError {
597 p.reportSyntaxError(fieldTok, "expected identifier after '.'")
598 }
599 p.synchronizeOnDelimiter()
600 break loop
601 }
602 isMemberCall := p.peekTok.kind == tokLeftParen
603 var isQuoted bool
604 field := p.normalizeIdent(fieldTok, p.enableCallEscapeSyntax || !isMemberCall, &isQuoted)
605 depth++
606 if optional {
607 fieldID := p.nextID(fieldTok)
608 opID := p.nextID(dotTok)
609 lhs = p.helper.newGlobalCall(opID, operators.OptSelect, lhs, p.helper.newLiteralString(fieldID, field))
610 canBeStructName = false
611 } else if isMemberCall {
612 lparen := p.nextToken()
613 callID := p.nextID(lparen)
614 args := p.parseArguments(tokRightParen)
615 lhs = p.receiverCallOrMacro(callID, field, lhs, args...)
616 // parseArguments leaves lastParsedDepth at the deepest argument.
617 // Arguments hang one level below the call node, so "a.f(b.c.d.e)" is 4
618 // deep. The max preserves the selectors already walked when the
619 // arguments are shallower, as in "a.b.c.f(1)".
620 depth = max(depth, p.lastParsedDepth+1)
621 canBeStructName = false
622 } else {
623 dotID := p.nextID(dotTok)
624 lhs = p.helper.newSelect(dotID, lhs, field)
625 canBeStructName = canBeStructName && !isQuoted
626 }
627 // A plain qualified name such as "a.b.Msg" may still turn out to be the
628 // type name of a struct creation, which adds no depth, so its field
629 // selections are only checked once the chain is known to be something else.
630 if !canBeStructName {
631 p.checkRecursion(depth)
632 }
633 case tokLeftBracket:
634 bracketTok := p.nextToken()
635 opID := p.nextID(bracketTok)
636 optional := false

Callers 3

parseMemberMethod · 0.95
parseUnaryOpsMethod · 0.95
parsePrimaryMethod · 0.95

Calls 15

nextTokenMethod · 0.95
reportErrorMethod · 0.95
reportSyntaxErrorMethod · 0.95
normalizeIdentMethod · 0.95
nextIDMethod · 0.95
parseArgumentsMethod · 0.95
receiverCallOrMacroMethod · 0.95
checkRecursionMethod · 0.95
parseExprMethod · 0.95
expectMethod · 0.95
extractStructNameMethod · 0.95

Tested by

no test coverage detected