示例#1
0
文件: parser.go 项目: yonglehou/hpipe
func (P *Parser) Error(err error, scanner Scanner) (recovered bool, errorAttrib *parseError.Error) {
	errorAttrib = &parseError.Error{
		Err:            err,
		ErrorToken:     P.nextToken,
		ErrorSymbols:   P.popNonRecoveryStates(),
		ExpectedTokens: make([]string, 0, 8),
	}
	for t, action := range actionTab[P.stack.top()].actions {
		if action != nil {
			errorAttrib.ExpectedTokens = append(errorAttrib.ExpectedTokens, token.TokMap.Id(token.Type(t)))
		}
	}

	if action := actionTab[P.stack.top()].actions[token.TokMap.Type("error")]; action != nil {
		P.stack.push(int(action.(shift)), errorAttrib) // action can only be shift
	} else {
		return
	}

	if action := actionTab[P.stack.top()].actions[P.nextToken.Type]; action != nil {
		recovered = true
	}
	for !recovered && P.nextToken.Type != token.EOF {
		P.nextToken = scanner.Scan()
		if action := actionTab[P.stack.top()].actions[P.nextToken.Type]; action != nil {
			recovered = true
		}
	}

	return
}
示例#2
0
文件: parser.go 项目: yonglehou/hpipe
func (P *Parser) newError(err error) error {
	w := new(bytes.Buffer)
	fmt.Fprintf(w, "Error in S%d: %s, %s", P.stack.top(), token.TokMap.TokenString(P.nextToken), P.nextToken.Pos.String())
	if err != nil {
		w.WriteString(err.Error())
	} else {
		w.WriteString(", expected one of: ")
		actRow := actionTab[P.stack.top()]
		for i, t := range actRow.actions {
			if t != nil {
				fmt.Fprintf(w, "%s ", token.TokMap.Id(token.Type(i)))
			}
		}
	}
	return errors.New(w.String())
}