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

Function Parse

repl/commands.go:181–221  ·  view source on GitHub ↗

Parse parses a repl command line into a command object. This provides the normalized command name plus any parsed parameters (e.g. variable names in let statements). An error is returned if the statement isn't well formed. See the parser pacakage for details on the antlr grammar.

(line string)

Source from the content-addressed store, hash-verified

179// An error is returned if the statement isn't well formed. See the parser
180// pacakage for details on the antlr grammar.
181func Parse(line string) (Cmder, error) {
182 line = strings.TrimSpace(line)
183 listener := &commandParseListener{}
184 is := antlr.NewInputStream(line)
185 lexer := parser.NewCommandsLexer(is)
186 lexer.RemoveErrorListeners()
187 lexer.AddErrorListener(listener)
188 p := parser.NewCommandsParser(antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel))
189 p.RemoveErrorListeners()
190 p.AddErrorListener(listener)
191
192 antlr.ParseTreeWalkerDefault.Walk(listener, p.StartCommand())
193
194 if len(listener.errs) > 0 {
195 errFmt := make([]string, len(listener.errs))
196 for i, err := range listener.errs {
197 errFmt[i] = err.Error()
198 }
199
200 if listener.usage != "" {
201 errFmt = append(errFmt, "", "Usage:", listener.usage)
202 }
203 return nil, fmt.Errorf("invalid command: %v", strings.Join(errFmt, "\n"))
204 }
205 if listener.cmd.Cmd() == "help" {
206 return nil, errors.New(strings.Join([]string{
207 compileUsage,
208 parseUsage,
209 declareUsage,
210 deleteUsage,
211 letUsage,
212 statusUsage,
213 configUsage,
214 optionUsage,
215 loadDescriptorsUsage,
216 helpUsage,
217 exitUsage,
218 }, "\n\n"))
219 }
220 return listener.cmd, nil
221}
222
223// ANTLR interface implementations
224

Callers 4

mainFunction · 0.92
TestParseFunction · 0.70
TestParseErrorsFunction · 0.70
parseYAMLConfigMethod · 0.70

Calls 6

StartCommandMethod · 0.95
NewCommandsLexerFunction · 0.92
NewCommandsParserFunction · 0.92
NewMethod · 0.80
CmdMethod · 0.65
ErrorMethod · 0.45

Tested by 2

TestParseFunction · 0.56
TestParseErrorsFunction · 0.56