getmore1.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // getMoreInput is called by the Parser routines mid-parse, if
  2. // need be, to obtain the next line/rune of input.
  3. //
  4. // getMoreInput() is used by Parser.ParseList(), Parser.ParseArray(),
  5. // Parser.ParseBlockComment(), and Parser.ParseInfix().
  6. //
  7. // getMoreInput() is also used by Parser.infiniteParsingLoop() which
  8. // is the main driver behind parsing.
  9. //
  10. // This function should *return* when it has more input
  11. // for the parser/lexer, which will call it when they get wedged.
  12. //
  13. // Listeners on p.ParsedOutput should know the Convention: sending
  14. // a length 0 []ParserReply on p.ParsedOutput channel means: we need more
  15. // input! They should send some in on p.AddInput channel; or request
  16. // a reset and simultaneously give us new input with p.ReqReset channel.
  17. func (p *Parser) getMoreInput(deliverThese []Sexp, errorToReport error) error {
  18. if len(deliverThese) == 0 && errorToReport == nil {
  19. p.FlagSendNeedInput = true
  20. } else {
  21. p.sendMe = append(p.sendMe,ParserReply{Expr: deliverThese,Err: errorToReport})
  22. }
  23. for {
  24. select {
  25. case <-p.reqStop:
  26. return ParserHaltRequested
  27. case input := <-p.AddInput:
  28. p.lexer.AddNextStream(input)
  29. p.FlagSendNeedInput = false
  30. return nil
  31. case input := <-p.ReqReset:
  32. p.lexer.Reset()
  33. p.lexer.AddNextStream(input)
  34. p.FlagSendNeedInput = false
  35. return ResetRequested
  36. case p.HaveStuffToSend() <- p.sendMe:
  37. // that was a conditional send, because
  38. // HaveStuffToSend() will return us a
  39. // nil channel if there's nothing ready.
  40. p.sendMe = make([]ParserReply, 0, 1)
  41. p.FlagSendNeedInput = false
  42. }
  43. }
  44. }