parser1.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. func (parser *Parser) parseArray(depth int) (Sexp, error) {
  2. arr := make([]Sexp, 0, SliceDefaultCap)
  3. var tok Token
  4. var err error
  5. for {
  6. getTok:
  7. for {
  8. tok, err = parser.lexer.peekNextToken()
  9. if err != nil {
  10. return SexpEnd, err
  11. }
  12. if tok.typ == TokenComma {
  13. // pop off the ,
  14. _, _ = parser.lexer.getNextToken()
  15. continue getTok
  16. }
  17. if tok.typ != TokenEnd {
  18. break getTok
  19. } else {
  20. // we ask for more, and then loop
  21. err = parser.getMoreInput(nil, ErrMoreInputNeeded)
  22. switch err {
  23. case ParserHaltRequested:
  24. return SexpNull, err
  25. case ResetRequested:
  26. return SexpEnd, err
  27. }
  28. }
  29. }
  30. if tok.typ == TokenRSquare {
  31. // pop off the ]
  32. _, _ = parser.lexer.getNextToken()
  33. break
  34. }
  35. expr, err := parser.parseExpression(depth + 1)
  36. if err != nil {
  37. return SexpNull, err
  38. }
  39. arr = append(arr, expr)
  40. }
  41. return &SexpArray{Val: arr, Env: parser.env}, nil
  42. }
  43. func (parser *Parser) parseList(depth int) (sx Sexp, err error) {
  44. var tok Token
  45. tokFilled:
  46. for {
  47. // if lexer runs out of tokens it will
  48. // return EndTk = Token{typ: TokenEnd}.
  49. //
  50. tok, err = parser.lexer.peekNextToken()
  51. if err != nil {
  52. return SexpNull, err
  53. }
  54. if tok.typ != TokenEnd {
  55. break tokFilled
  56. }
  57. // instead of returning UnexpectedEnd, we:
  58. err = parser.getMoreInput(nil, ErrMoreInputNeeded)
  59. switch err {
  60. case ParserHaltRequested:
  61. return SexpNull, err
  62. case ResetRequested:
  63. return SexpEnd, err
  64. }
  65. // have to still fill tok, so
  66. // loop to the top to peekNextToken
  67. }
  68. if tok.typ == TokenRParen {
  69. _, _ = parser.lexer.getNextToken()
  70. return SexpNull, nil
  71. }
  72. var start = &SexpPair{}
  73. expr, err := parser.parseExpression(depth + 1)
  74. if err != nil {
  75. return SexpNull, err
  76. }
  77. start.Head = expr
  78. tok, err = parser.lexer.peekNextToken()
  79. if err != nil {
  80. return SexpNull, err
  81. }
  82. // backslash '\' replaces dot '.' in zygomys
  83. if tok.typ == TokenBackslash {
  84. // eat up the backslash
  85. _, _ = parser.lexer.getNextToken()
  86. expr, err = parser.parseExpression(depth + 1)
  87. if err != nil {
  88. return SexpNull, err
  89. }
  90. // eat up the end paren
  91. tok, err = parser.lexer.getNextToken()
  92. if err != nil {
  93. return SexpNull, err
  94. }
  95. // make sure it was actually an end paren
  96. if tok.typ != TokenRParen {
  97. return SexpNull, errors.New("extra value in dotted pair")
  98. }
  99. start.Tail = expr
  100. return start, nil
  101. }
  102. expr, err = parser.parseList(depth + 1)
  103. if err != nil {
  104. return start, err
  105. }
  106. start.Tail = expr
  107. return start, nil
  108. }
  109. func (parser *Parser) ParseBlockComment(start *Token) (sx Sexp, err error) {
  110. defer func() {
  111. if sx != nil {
  112. //Q("returning from ParseBlockComment with sx ='%v', err='%v'",
  113. // sx.SexpString(), err)
  114. }
  115. }()
  116. lexer := parser.lexer
  117. var tok Token
  118. var block = &SexpComment{Block: true, Comment: start.str}
  119. for {
  120. tokFilled:
  121. for {
  122. tok, err = lexer.PeekNextToken()
  123. if err != nil {
  124. return SexpNull, err
  125. }
  126. if tok.typ != TokenEnd {
  127. break tokFilled
  128. }
  129. err = parser.GetMoreInput(nil, ErrMoreInputNeeded)
  130. switch err {
  131. case ParserHaltRequested:
  132. return SexpNull, err
  133. case ResetRequested:
  134. return SexpEnd, err
  135. }
  136. // have to still fill tok, so
  137. // loop to the top to PeekNextToken
  138. }
  139. // consume it
  140. //cons, err := lexer.GetNextToken()
  141. _, err := lexer.GetNextToken()
  142. if err != nil {
  143. return nil, err
  144. }
  145. //Q("parse block comment is consuming '%v'", cons)
  146. switch tok.typ {
  147. case TokenEndBlockComment:
  148. block.Comment += tok.str
  149. return block, nil
  150. case TokenComment:
  151. block.Comment += tok.str
  152. default:
  153. panic("internal error: inside a block comment, we should only see TokenComment and TokenEndBlockComment tokens")
  154. }
  155. }
  156. //return block, nil
  157. }
  158. func (parser *Parser) ParseInfix(depth int) (Sexp, error) {
  159. lexer := parser.lexer
  160. arr := make([]Sexp, 0, SliceDefaultCap)
  161. var err error
  162. var tok Token
  163. for {
  164. getTok:
  165. for {
  166. tok, err = lexer.PeekNextToken()
  167. if err != nil {
  168. return SexpEnd, err
  169. }
  170. if tok.typ != TokenEnd {
  171. break getTok
  172. } else {
  173. //instead of return SexpEnd, UnexpectedEnd
  174. // we ask for more, and then loop
  175. err = parser.GetMoreInput(nil, ErrMoreInputNeeded)
  176. switch err {
  177. case ParserHaltRequested:
  178. return SexpNull, err
  179. case ResetRequested:
  180. return SexpEnd, err
  181. }
  182. }
  183. }
  184. if tok.typ == TokenRCurly {
  185. // pop off the }
  186. _, _ = lexer.GetNextToken()
  187. break
  188. }
  189. Q("debug: ParseInfix(depth=%v) calling ParseExpression", depth)
  190. expr, err := parser.ParseExpression(depth + 1)
  191. if err != nil {
  192. return SexpNull, err
  193. }
  194. Q("debug2: ParseInfix(depth=%v) appending expr = '%v'", depth, expr.SexpString(nil))
  195. arr = append(arr, expr)
  196. }
  197. var list SexpPair
  198. list.Head = parser.env.MakeSymbol("infix")
  199. list.Tail = SexpNull
  200. if len(arr) > 0 {
  201. list.Tail = Cons(&SexpArray{Val: arr, Infix: true, Env: parser.env}, SexpNull)
  202. }
  203. return &list, nil
  204. //return &SexpArray{Val: arr, Infix: true, Env: env}, nil
  205. }