| 683 | }; |
| 684 | } |
| 685 | parse(text, file, debug, mode) { |
| 686 | this._tokenizer = new ast._Tokenizer(text, file); |
| 687 | this._debug = debug; |
| 688 | const position = this._position(); |
| 689 | let body = []; |
| 690 | while (!this._tokenizer.match('eof')) { |
| 691 | const statement = this._parseStatement(); |
| 692 | if (statement) { |
| 693 | body.push(statement); |
| 694 | continue; |
| 695 | } |
| 696 | if (this._tokenizer.accept('\n') || this._tokenizer.accept(';') || this._tokenizer.peek().type === 'eof') { |
| 697 | continue; |
| 698 | } |
| 699 | if (this._tokenizer.accept('indent') && this._tokenizer.peek().type === 'eof') { |
| 700 | continue; |
| 701 | } |
| 702 | throw new python.Error(`Unsupported statement ${this._location()}`); |
| 703 | } |
| 704 | if (mode === 'eval') { |
| 705 | if (body.length !== 1 || body[0] instanceof ast.Expr === false) { |
| 706 | throw new python.Error('Expected expression.'); |
| 707 | } |
| 708 | body = body[0].value; |
| 709 | } |
| 710 | const module = new ast.Module(body); |
| 711 | this._mark(module, position); |
| 712 | return module; |
| 713 | } |
| 714 | _parseSuite() { |
| 715 | const body = []; |
| 716 | let statement = null; |