示例#1
0
文件: parser.go 项目: mk2/yon
func parseChainWordBody(p *parser, expectType kit.WordType) (kit.ChainWord, kit.WordType) {

	w := word.NewChainWord()
	actualType := expectType

PARSE_WORD_CHAIN_LOOP:
	for {
		switch t := p.peek(); {

		case w.Size() > 0 && t.GetType() == token.TDblColon:
			// TDblColon indicates the word is a dict word
			actualType = word.TDictWord
			p.next()

		case t.GetType() == token.TNumber:
			w.Push(word.NewNumberWord(t.GetVal()))
			p.next()

		case t.GetType() == token.TString:
			w.Push(word.NewStringWord(t.GetVal()))
			p.next()

		case t.GetType() == token.TIdentifier:
			ident := t.GetVal()
			if v := p.memo.Vocab().Read(ident); v != nil {
				w.Push(v)
			} else {
				w.Push(word.NewNameWord(ident))
			}
			p.next()

		case t.GetType() == token.TLeftBrace:
			if aw, dw := parseArrayDictBody(p); aw != nil {
				w.Push(aw)
			} else if dw != nil {
				w.Push(dw)
			}

		case t.GetType() == token.TLeftSquareBracket:
			w.Push(parseAnonFuncBody(p))

		case expectType == word.TArrayWord && t.GetType() == token.TRightBrace:
			p.next()
			break PARSE_WORD_CHAIN_LOOP

		case expectType == word.TFuncWord && t.GetType() == token.TRightSquareBracket:
			p.next()
			break PARSE_WORD_CHAIN_LOOP

		case t.GetType() == token.TEOF:
			break PARSE_WORD_CHAIN_LOOP

		case t.GetType() == token.TSpace:
			p.next()
		}
	}

	return w, actualType
}
示例#2
0
文件: parser.go 项目: mk2/yon
func parse(p *parser) stateFn {

	p.leftDelim = nil
	p.rightDelim = nil

	switch t := p.peek(); t.GetType() {

	case token.TIdentifier:
		return parseIdentifier(p)

	case token.TLeftBrace:
		p.leftDelim = t
		return parseArrayDict(p)

	case token.TNumber:
		w := word.NewNumberWord(t.GetVal())
		p.emit(w)
		p.next()

	case token.TString:
		w := word.NewStringWord(t.GetVal())
		p.emit(w)
		p.next()

	case token.TLeftSquareBracket:
		p.leftDelim = t
		return parseAnonFunc(p)

	case token.TSpace:
		p.next()

	case token.TEOF:
		p.emit(word.NewNilWord())
		return nil

	default:
		p.next()

	}

	return parse
}