MCPcopy Create free account
hub / github.com/docopt/docopt.go / parseAtom

Function parseAtom

docopt.go:319–353  ·  view source on GitHub ↗
(tokens *tokenList, options *patternList)

Source from the content-addressed store, hash-verified

317}
318
319func parseAtom(tokens *tokenList, options *patternList) (patternList, error) {
320 // atom ::= '(' expr ')' | '[' expr ']' | 'options' | long | shorts | argument | command ;
321 tok := tokens.current()
322 result := patternList{}
323 if tokens.current().match(false, "(", "[") {
324 tokens.move()
325 var matching string
326 pl, err := parseExpr(tokens, options)
327 if err != nil {
328 return nil, err
329 }
330 if tok.eq("(") {
331 matching = ")"
332 result = patternList{newRequired(pl...)}
333 } else if tok.eq("[") {
334 matching = "]"
335 result = patternList{newOptional(pl...)}
336 }
337 moved := tokens.move()
338 if !moved.eq(matching) {
339 return nil, tokens.errorFunc("unmatched '%s', expected: '%s' got: '%s'", tok, matching, moved)
340 }
341 return result, nil
342 } else if tok.eq("options") {
343 tokens.move()
344 return patternList{newOptionsShortcut()}, nil
345 } else if tok.hasPrefix("--") && !tok.eq("--") {
346 return parseLong(tokens, options)
347 } else if tok.hasPrefix("-") && !tok.eq("-") && !tok.eq("--") {
348 return parseShorts(tokens, options)
349 } else if tok.hasPrefix("<") && tok.hasSuffix(">") || tok.isUpper() {
350 return patternList{newArgument(tokens.move().String(), nil)}, nil
351 }
352 return patternList{newCommand(tokens.move().String(), false)}, nil
353}
354
355func parseLong(tokens *tokenList, options *patternList) (patternList, error) {
356 // long ::= '--' chars [ ( ' ' | '=' ) chars ] ;

Callers 1

parseSeqFunction · 0.85

Calls 15

parseExprFunction · 0.85
newRequiredFunction · 0.85
newOptionalFunction · 0.85
newOptionsShortcutFunction · 0.85
parseLongFunction · 0.85
parseShortsFunction · 0.85
newArgumentFunction · 0.85
newCommandFunction · 0.85
currentMethod · 0.80
moveMethod · 0.80
hasPrefixMethod · 0.80
hasSuffixMethod · 0.80

Tested by

no test coverage detected