當前位置: 首頁>>代碼示例>>Golang>>正文


Golang Lexer.Location方法代碼示例

本文整理匯總了Golang中github.com/hialin/hialin/parse/internal/engine/lexer.Lexer.Location方法的典型用法代碼示例。如果您正苦於以下問題:Golang Lexer.Location方法的具體用法?Golang Lexer.Location怎麽用?Golang Lexer.Location使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在github.com/hialin/hialin/parse/internal/engine/lexer.Lexer的用法示例。


在下文中一共展示了Lexer.Location方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: errorRecovery

func (p *Parser) errorRecovery(lex lexer.Lexer, tok lexer.Token, stateStack []ItemSetID, dataStack []interface{}) (lexer.Token, []ItemSetID, []interface{}, error) {
	parseErr := ParseError{
		Invalid:  []lexer.Token{tok},
		Location: lex.Location(),
	}
	topState := stateStack[len(stateStack)-1]
	for expectedTok := range p.parsetable[topState] {
		parseErr.Expected = append(parseErr.Expected, p.Syms.Get(expectedTok))
	}

	// rewind the state stack searching for an error rule
	action, found := p.parsetable[topState][syms.ERROR]
	for !(found && action.actiontype == Shift) {
		stateStack = stateStack[:len(stateStack)-1]
		if len(stateStack) == 0 {
			break
		}
		topState = stateStack[len(stateStack)-1]
		action, found = p.parsetable[topState][syms.ERROR]
	}

	parseErr.Valid = append(parseErr.Valid, dataStack[len(stateStack):]...)
	dataStack = dataStack[:len(stateStack)]
	if len(stateStack) == 0 {
		err := UnexpectedTerminalParseError("parse error; could not find a suitable error rule")
		dataStack = append(dataStack, parseErr)
		if p.debug {
			fmt.Println("\nParse error, no error recovery possible", p.fmtStacks(stateStack, dataStack))
		}
		return tok, stateStack, dataStack, err
	}
	stateStack = append(stateStack, action.state)

	// now search for next action by discarding unfitting tokens
	actionMap := p.parsetable[action.state]
	action, found = actionMap[tok.ID]
	parseErr.Invalid = nil
	for !found {
		parseErr.Invalid = append(parseErr.Invalid, tok)
		if tok.ID == syms.EOF {
			err := UnexpectedTerminalParseError("parse error; could not find a suitable error rule before EOF")
			dataStack = append(dataStack, parseErr)
			if p.debug {
				fmt.Println("\nParse error, EOF while recovering",
					"\nRecovery state expected one of:", p.symNames(actionMap),
					p.fmtStacks(stateStack, dataStack))
			}
			return tok, stateStack, dataStack, err
		}
		var err error
		tok, err = p.nextToken(lex)
		if err != nil {
			dataStack = append(dataStack, parseErr)
			return tok, stateStack, dataStack, err
		}
		action, found = actionMap[tok.ID]
	}
	dataStack = append(dataStack, parseErr)
	if p.debug {
		fmt.Println("\nParse error, stack unwinded", p.fmtStacks(stateStack, dataStack))
	}
	return tok, stateStack, dataStack, nil
}
開發者ID:hialin,項目名稱:hialin,代碼行數:63,代碼來源:parser.go


注:本文中的github.com/hialin/hialin/parse/internal/engine/lexer.Lexer.Location方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。