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

Method Parse

parser/parser.go:92–126  ·  view source on GitHub ↗

Parse parses the expression represented by source and returns the result.

(source common.Source)

Source from the content-addressed store, hash-verified

90
91// Parse parses the expression represented by source and returns the result.
92func (p *Parser) Parse(source common.Source) (*ast.AST, *common.Errors) {
93 errs := common.NewErrors(source)
94 accu := AccumulatorName
95 if p.enableHiddenAccumulatorName {
96 accu = HiddenAccumulatorName
97 }
98 fac := ast.NewExprFactoryWithAccumulator(accu)
99 impl := parser{
100 errors: &parseErrors{errs},
101 exprFactory: fac,
102 helper: newParserHelper(source, fac),
103 macros: p.macros,
104 maxRecursionDepth: p.maxRecursionDepth,
105 errorReportingLimit: p.errorReportingLimit,
106 errorRecoveryLimit: p.errorRecoveryLimit,
107 errorRecoveryLookaheadTokenLimit: p.errorRecoveryTokenLookaheadLimit,
108 populateMacroCalls: p.populateMacroCalls,
109 enableOptionalSyntax: p.enableOptionalSyntax,
110 enableVariadicOperatorASTs: p.enableVariadicOperatorASTs,
111 enableIdentEscapeSyntax: p.enableIdentEscapeSyntax,
112 }
113 buf, ok := source.(runes.Buffer)
114 if !ok {
115 buf = runes.NewBuffer(source.Content())
116 }
117 var out ast.Expr
118 if buf.Len() > p.expressionSizeCodePointLimit {
119 out = impl.reportError(common.NoLocation,
120 "expression code point size exceeds limit: size: %d, limit %d",
121 buf.Len(), p.expressionSizeCodePointLimit)
122 } else {
123 out = impl.parse(buf, source.Description())
124 }
125 return ast.NewAST(out, impl.helper.getSourceInfo()), errs
126}
127
128// reservedIds are not legal to use as variables. We exclude them post-parse, as they *are* valid
129// field names for protos, and it would complicate the grammar to distinguish the cases.

Callers 5

BenchmarkParseFunction · 0.95
BenchmarkParseParallelFunction · 0.95
TestExprHelperCopyFunction · 0.95
TestUnparseFunction · 0.95

Calls 11

reportErrorMethod · 0.95
parseMethod · 0.95
NewErrorsFunction · 0.92
NewBufferFunction · 0.92
NewASTFunction · 0.92
newParserHelperFunction · 0.85
getSourceInfoMethod · 0.80
ContentMethod · 0.65
LenMethod · 0.65
DescriptionMethod · 0.65

Tested by 5

BenchmarkParseFunction · 0.76
BenchmarkParseParallelFunction · 0.76
TestExprHelperCopyFunction · 0.76
TestUnparseFunction · 0.76