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

Method Parse

parser/parser.go:98–133  ·  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

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

Callers 6

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 6

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