Exemplo n.º 1
0
func complVariable(n parse.Node, ed *Editor) (int, int, []*candidate) {
	begin, end := n.Begin(), n.End()

	primary, ok := n.(*parse.Primary)
	if !ok || primary.Type != parse.Variable {
		return -1, -1, nil
	}

	splice, ns, head := eval.ParseVariable(primary.Value)

	// Collect matching variables.
	var varnames []string
	iterateVariables(ed.evaler, ns, func(varname string) {
		if strings.HasPrefix(varname, head) {
			varnames = append(varnames, varname)
		}
	})
	sort.Strings(varnames)

	cands := make([]*candidate, len(varnames))
	// Build candidates.
	for i, varname := range varnames {
		cands[i] = &candidate{text: "$" + eval.MakeVariableName(splice, ns, varname)}
	}
	return begin, end, cands
}
Exemplo n.º 2
0
func (s *Stylist) style(n parse.Node) {
	if fn, ok := n.(*parse.Form); ok {
		for _, an := range fn.Assignments {
			if an.Dst != nil && an.Dst.Head != nil {
				v := an.Dst.Head
				s.add(styleForType[Variable].String(), v.Begin(), v.End())
			}
		}
		if fn.Head != nil {
			s.formHead(fn.Head)
		}
	}
	if cn, ok := n.(*parse.Control); ok {
		switch cn.Kind {
		case parse.ForControl:
			if cn.Iterator != nil {
				v := cn.Iterator.Head
				s.add(styleForType[Variable].String(), v.Begin(), v.End())
			}
		case parse.TryControl:
			if cn.ExceptVar != nil {
				v := cn.ExceptVar.Head
				s.add(styleForType[Variable].String(), v.Begin(), v.End())
			}
		}
	}
	for _, child := range n.Children() {
		s.style(child)
	}
}
Exemplo n.º 3
0
func complNewForm(n parse.Node, ed *Editor) []*candidate {
	if _, ok := n.(*parse.Chunk); ok {
		return complFormHeadInner("", ed)
	}
	if _, ok := n.Parent().(*parse.Chunk); ok {
		return complFormHeadInner("", ed)
	}
	return nil
}
Exemplo n.º 4
0
func produceTokens(n parse.Node, tokenCh chan<- Token) {
	if len(n.Children()) == 0 {
		tokenType := ParserError
		moreStyle := ""
		switch n := n.(type) {
		case *parse.Primary:
			switch n.Type {
			case parse.Bareword:
				tokenType = Bareword
			case parse.SingleQuoted:
				tokenType = SingleQuoted
			case parse.DoubleQuoted:
				tokenType = DoubleQuoted
			case parse.Variable:
				tokenType = Variable
			case parse.Wildcard:
				tokenType = Wildcard
			case parse.Tilde:
				tokenType = Tilde
			}
		case *parse.Sep:
			tokenType = Sep
			moreStyle = styleForSep[n.SourceText()]
		}
		tokenCh <- Token{tokenType, n.SourceText(), n, moreStyle}
	}
	for _, child := range n.Children() {
		produceTokens(child, tokenCh)
	}
}
Exemplo n.º 5
0
func findArgContext(n parse.Node) (int, int, string, parse.PrimaryType, *parse.Form) {
	if sep, ok := n.(*parse.Sep); ok {
		if form, ok := sep.Parent().(*parse.Form); ok {
			return n.End(), n.End(), "", parse.Bareword, form
		}
	}
	if primary, ok := n.(*parse.Primary); ok {
		if compound, head := primaryInSimpleCompound(primary); compound != nil {
			if form, ok := compound.Parent().(*parse.Form); ok {
				if form.Head != compound {
					return compound.Begin(), compound.End(), head, primary.Type, form
				}
			}
		}
	}
	return -1, -1, "", 0, nil
}
Exemplo n.º 6
0
func findFormHeadContext(n parse.Node) (int, int, string, parse.PrimaryType) {
	_, isChunk := n.(*parse.Chunk)
	_, isPipeline := n.(*parse.Pipeline)
	if isChunk || isPipeline {
		return n.Begin(), n.End(), "", parse.Bareword
	}

	if primary, ok := n.(*parse.Primary); ok {
		if compound, head := primaryInSimpleCompound(primary); compound != nil {
			if form, ok := compound.Parent().(*parse.Form); ok {
				if form.Head == compound {
					return compound.Begin(), compound.End(), head, primary.Type
				}
			}
		}
	}
	return -1, -1, "", 0
}
Exemplo n.º 7
0
func complVariable(n parse.Node, ed *Editor) (int, int, []*candidate) {
	begin, end := n.Begin(), n.End()

	primary, ok := n.(*parse.Primary)
	if !ok || primary.Type != parse.Variable {
		return -1, -1, nil
	}

	// XXX Repeats eval.ParseVariable.
	explode, qname := eval.ParseVariableSplice(primary.Value)
	nsPart, nameHead := eval.ParseVariableQName(qname)
	ns := nsPart
	if len(ns) > 0 {
		ns = ns[:len(ns)-1]
	}

	// Collect matching variables.
	var varnames []string
	iterateVariables(ed.evaler, ns, func(varname string) {
		if strings.HasPrefix(varname, nameHead) {
			varnames = append(varnames, nsPart+varname)
		}
	})
	// Collect namespace prefixes.
	// TODO Support non-module namespaces.
	for ns := range ed.evaler.Modules {
		if hasProperPrefix(ns+":", qname) {
			varnames = append(varnames, ns+":")
		}
	}
	sort.Strings(varnames)

	cands := make([]*candidate, len(varnames))
	// Build candidates.
	for i, varname := range varnames {
		cands[i] = &candidate{text: "$" + explode + varname}
	}
	return begin, end, cands
}
Exemplo n.º 8
0
Arquivo: token.go Projeto: zhsj/elvish
func produceTokens(n parse.Node, tokenCh chan<- Token) {
	if n.Begin() == n.End() {
		// Ignore empty node. This happens e.g. with an empty source code, where
		// the parsed node is an empty Chunk.
		return
	}
	if len(n.Children()) == 0 {
		tokenType := ParserError
		moreStyle := ""
		switch n := n.(type) {
		case *parse.Primary:
			switch n.Type {
			case parse.Bareword:
				tokenType = Bareword
			case parse.SingleQuoted:
				tokenType = SingleQuoted
			case parse.DoubleQuoted:
				tokenType = DoubleQuoted
			case parse.Variable:
				tokenType = Variable
			case parse.Wildcard:
				tokenType = Wildcard
			case parse.Tilde:
				tokenType = Tilde
			}
		case *parse.Sep:
			tokenType = Sep
			septext := n.SourceText()
			if strings.HasPrefix(septext, "#") {
				moreStyle = styleForSep["#"]
			} else {
				moreStyle = styleForSep[septext]
			}
		default:
			Logger.Printf("bad leaf type %T", n)
		}
		tokenCh <- Token{tokenType, n.SourceText(), n, moreStyle}
	}
	for _, child := range n.Children() {
		produceTokens(child, tokenCh)
	}
}
Exemplo n.º 9
0
func (cp *compiler) compiling(n parse.Node) {
	cp.begin, cp.end = n.Begin(), n.End()
}