Ejemplo n.º 1
0
func checkList(list *ast.ListCell, expect []string) []string {
	errors := make([]string, 0)
	cell := list.First()
	// for each expected value
	for i, e := range expect {
		// error if value expected but list end
		if cell == nil {
			unreadStr := fmt.Sprint(expect[i:])
			errors = append(errors, "list terminated before cells "+unreadStr)
			break
		}
		// error if wrong cell contents
		cellStr := fmt.Sprint(cell)
		if cellStr != e {
			errors = append(errors,
				"expected cell value \""+e+"\", got \""+cellStr+"\"")
		}
		cell = cell.Next()
	}
	// error if all expected done but list not done
	if cell != nil {
		lenDiff := list.Value() - len(expect)
		errors = append(errors,
			"output too long by "+strconv.Itoa(lenDiff)+" cells")
	}
	return errors
}
Ejemplo n.º 2
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)
		}
	}
}