Example #1
0
func (P *ParserUTab) newError(err error) error {
	errmsg := "Error: " + P.TokString(P.nextToken) + " @ " + P.pos.String()
	if err != nil {
		errmsg += " " + err.Error()
	} else {
		errmsg += ", expected one of: "
		actRow := P.actTab[P.stack.Top()]
		i := 0
		for t, act := range actRow {
			if act != nil {
				errmsg += P.tokenMap.TokenString(token.Type(t))
				if i < len(actRow)-1 {
					errmsg += " "
				}
				i++
			}
		}
	}
	return errors.New(errmsg)
}
Example #2
0
func (P *ParserUTab) Error(err error, scanner Scanner) (recovered bool, errorAttrib *errs.Error) {
	errorAttrib = &errs.Error{
		Err:            err,
		ErrorToken:     P.nextToken,
		ErrorPos:       P.pos,
		ErrorSymbols:   P.popNonRecoveryStates(),
		ExpectedTokens: make([]string, 0, 8),
	}
	for t, act := range P.actTab[P.stack.Top()] {
		if act != nil {
			errorAttrib.ExpectedTokens = append(errorAttrib.ExpectedTokens, P.tokenMap.TokenString(token.Type(t)))
		}
	}

	action := P.actTab[P.stack.Top()][P.tokenMap.Type("error")]
	if action == nil {
		return
	}
	P.stack.Push(State(action.(Shift)), errorAttrib) // action can only be shift

	for P.actTab[P.stack.Top()][P.nextToken.Type] == nil && P.nextToken.Type != token.EOF {
		P.nextToken, P.pos = scanner.Scan()
	}

	return
}