Пример #1
0
func (p *Parser) parseTree(tree *ast.ListCell) (ast.Cell, error) {
	for {
		switch t := <-p.tokens; t.Type {
		// tree is done
		case token.RParen:
			return tree, nil
		// EOF found before tree is done, error
		case token.EOF:
			return ast.NewEOF(), fmt.Errorf(
				"syntax error: hanging parenthese, s-expression %v does not close", tree)
		default:
			val, err := p.parseAST(t)
			if err != nil {
				return val, err
			}
			tree.Add(val)
		}
	}
}
Пример #2
0
func (p *Parser) parseAST(t token.Item) (ast.Cell, error) {
	var val ast.Cell
	var err error
	switch t.Type {
	// use following tokens to recursivly build the tree
	case token.LParen:
		val, err = p.parseTree(ast.NewList())
	// this "tree" is a single sym
	case token.Sym:
		val, err = ast.NewSym(t.Value)
	// this "tree" is a single constant
	case token.Const:
		val, err = p.parseConst(t)
	// the next token is quoted
	case token.Quote:
		val, err = p.parseQuote(t)
	// close paren found without open paren, error
	case token.RParen:
		val, err = nil, errors.New("syntax error: close parenthese ')' without s-expression")
	case token.EOF:
		val, err = ast.NewEOF(), nil
	}
	return val, err
}