当前位置: 首页>>代码示例>>Golang>>正文


Golang Scanner.Next方法代码示例

本文整理汇总了Golang中text/scanner.Scanner.Next方法的典型用法代码示例。如果您正苦于以下问题:Golang Scanner.Next方法的具体用法?Golang Scanner.Next怎么用?Golang Scanner.Next使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在text/scanner.Scanner的用法示例。


在下文中一共展示了Scanner.Next方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: ParseIdMatcher

// Parse an id matcher
// e.g. `#my-picture`
func ParseIdMatcher(selector *CSSSelector, s scanner.Scanner) error {
	var id bytes.Buffer
	defer func() {
		regexpStr := `^` + regexp.QuoteMeta(id.String()) + `$`
		selector.Attrs["id"] = regexp.MustCompile(regexpStr)
	}()
	for {
		c := s.Next()
		switch c {
		case scanner.EOF:
			return nil
		case '.':
			return ParseClassMatcher(selector, s)
		case '#':
			return ParseIdMatcher(selector, s)
		case '[':
			return ParseAttrMatcher(selector, s)
		case ':':
			return ParsePseudo(selector, s)
		default:
			if _, err := id.WriteRune(c); err != nil {
				return err
			}
		}
	}
}
开发者ID:bbeardsley,项目名称:pup,代码行数:28,代码来源:selector.go

示例2: addString

func (w *wordsStruct) addString(str string) {
	var scan scanner.Scanner
	scan.Init(strings.NewReader(str))
	for scan.Peek() != scanner.EOF {
		w.addChar(scan.Next())
	}
}
开发者ID:Mic92,项目名称:docker,代码行数:7,代码来源:shell_parser.go

示例3: ParseTagMatcher

// Parse the initial tag
// e.g. `div`
func ParseTagMatcher(selector *CSSSelector, s scanner.Scanner) error {
	tag := bytes.NewBuffer([]byte{})
	defer func() {
		selector.Tag = tag.String()
	}()
	for {
		c := s.Next()
		switch c {
		case scanner.EOF:
			return nil
		case '.':
			return ParseClassMatcher(selector, s)
		case '#':
			return ParseIdMatcher(selector, s)
		case '[':
			return ParseAttrMatcher(selector, s)
		case ':':
			return ParsePseudo(selector, s)
		default:
			if _, err := tag.WriteRune(c); err != nil {
				return err
			}
		}
	}
}
开发者ID:bbeardsley,项目名称:pup,代码行数:27,代码来源:selector.go

示例4: expandDirective

func (d *Dict) expandDirective(in *scanner.Scanner, out *bufio.Writer) error {
	var err error

	c := in.Next()
	switch c {
	case lDelim:
		switch in.Peek() {
		case lDelim:
			return copyNext(in, out)
		case condDelim:
			in.Next()
			err = d.expandCond(in, out)
		default:
			if err = d.expandVar(in, out); err == nil {
				err = match(in, rDelim)
			}
		}
	case rDelim:
		if err = writeString(out, "}"); err == nil {
			err = match(in, rDelim)
		}
	case scanner.EOF:
		err = parseErr(in, "Expected '%c' or '%c', got EOF", lDelim, rDelim)
	default:
		err = parseErr(in, "Expected '%c' or '%c', got '%c'", lDelim, rDelim, c)
	}

	return err
}
开发者ID:eZanmoto,项目名称:Bake,代码行数:29,代码来源:template.go

示例5: ParsePseudo

// Parse the selector after ':'
func ParsePseudo(selector *CSSSelector, s scanner.Scanner) error {
	if selector.Pseudo != nil {
		return fmt.Errorf("Combined multiple pseudo classes")
	}
	var b bytes.Buffer
	for s.Peek() != scanner.EOF {
		if _, err := b.WriteRune(s.Next()); err != nil {
			return err
		}
	}
	cmd := b.String()
	var err error
	switch {
	case cmd == "empty":
		selector.Pseudo = func(n *html.Node) bool {
			return n.FirstChild == nil
		}
	case cmd == "first-child":
		selector.Pseudo = firstChildPseudo
	case cmd == "last-child":
		selector.Pseudo = lastChildPseudo
	case cmd == "only-child":
		selector.Pseudo = func(n *html.Node) bool {
			return firstChildPseudo(n) && lastChildPseudo(n)
		}
	case cmd == "first-of-type":
		selector.Pseudo = firstOfTypePseudo
	case cmd == "last-of-type":
		selector.Pseudo = lastOfTypePseudo
	case cmd == "only-of-type":
		selector.Pseudo = func(n *html.Node) bool {
			return firstOfTypePseudo(n) && lastOfTypePseudo(n)
		}
	case strings.HasPrefix(cmd, "contains("):
		selector.Pseudo, err = parseContainsPseudo(cmd[len("contains("):])
		if err != nil {
			return err
		}
	case strings.HasPrefix(cmd, "nth-child("),
		strings.HasPrefix(cmd, "nth-last-child("),
		strings.HasPrefix(cmd, "nth-last-of-type("),
		strings.HasPrefix(cmd, "nth-of-type("):
		if selector.Pseudo, err = parseNthPseudo(cmd); err != nil {
			return err
		}
	case strings.HasPrefix(cmd, "not("):
		if selector.Pseudo, err = parseNotPseudo(cmd[len("not("):]); err != nil {
			return err
		}
	case strings.HasPrefix(cmd, "parent-of("):
		if selector.Pseudo, err = parseParentOfPseudo(cmd[len("parent-of("):]); err != nil {
			return err
		}
	default:
		return fmt.Errorf("%s not a valid pseudo class", cmd)
	}
	return nil
}
开发者ID:bbeardsley,项目名称:pup,代码行数:59,代码来源:selector.go

示例6: parseText

func parseText(s *scanner.Scanner, depth int) ([]ast, error) {
	var slice []ast
	for {
		switch s.Scan() {
		case '+', '-', '/', '%', '*', '=', '<', '>', '!':
			slice = append(slice, parseIdent(s.TokenText()))
		case scanner.Ident:
			ident := s.TokenText()
			// Periods are allowed in package names.
			for s.Peek() == '.' {
				s.Next()
				ident += "."
				if s.Scan() != scanner.Ident {
					return nil, stitchError{pos: s.Pos(),
						err: fmt.Errorf("bad ident name: %s",
							ident)}
				}
				ident += s.TokenText()
			}
			slice = append(slice, parseIdent(ident))
		case scanner.Float:
			x, _ := strconv.ParseFloat(s.TokenText(), 64)
			slice = append(slice, astFloat(x))
		case scanner.Int:
			x, _ := strconv.Atoi(s.TokenText())
			slice = append(slice, astInt(x))
		case scanner.String:
			str := strings.Trim(s.TokenText(), "\"")
			slice = append(slice, astString(str))
		case '(':
			// We need to save our position before recursing because the
			// scanner will have moved on by the time the recursive call
			// returns.
			pos := s.Pos()
			sexp, err := parseText(s, depth+1)
			if err != nil {
				return nil, err
			}

			slice = append(slice, astSexp{sexp: sexp, pos: pos})

		case ')':
			if depth == 0 {
				return nil, stitchError{s.Pos(), errUnbalancedParens}
			}
			return slice, nil
		case scanner.EOF:
			if depth != 0 {
				return nil, stitchError{s.Pos(), errUnbalancedParens}
			}
			return slice, nil

		default:
			return nil, stitchError{s.Pos(), fmt.Errorf("bad element: %s",
				s.TokenText())}
		}
	}
}
开发者ID:yuenmeiwan,项目名称:quilt,代码行数:58,代码来源:parse.go

示例7: match

// Consume the next rune in `n` and return an error if it's not `r`.
func match(in *scanner.Scanner, r rune) error {
	var err error
	if c := in.Next(); c == scanner.EOF {
		err = parseErr(in, "Expected '%c', got EOF", r)
	} else if c != r {
		err = parseErr(in, "Expected '%c', got '%c'", r, c)
	}
	return err
}
开发者ID:eZanmoto,项目名称:Bake,代码行数:10,代码来源:template.go

示例8: Skip

// Skip N tokens, if possible. Returns true if it worked out.
func Skip(tokenizer *scanner.Scanner, n int) bool {
	for counter := 0; counter < n; counter++ {
		toktype := tokenizer.Next()
		if toktype == scanner.EOF {
			return false
		}
	}
	return true
}
开发者ID:xyproto,项目名称:archlog,代码行数:10,代码来源:archlog.go

示例9: scanRune

func scanRune(s *scanner.Scanner, out chan Part, stop villa.Stop, tp int, exp rune) (toStop bool) {
	start := s.Pos()
	if r := s.Next(); r == scanner.EOF {
		return output(out, stop, TP_EOF_UNEXPECTED, start, s.Pos())
	} else if r != exp {
		return output(out, stop, TP_ERROR, start, s.Pos())
	}
	return output(out, stop, tp, start, s.Pos())
}
开发者ID:postfix,项目名称:sgrep,代码行数:9,代码来源:json.go

示例10: scanTo1

func scanTo1(s *scanner.Scanner, target rune) bool {
	for {
		switch s.Next() {
		case scanner.EOF:
			return false
		case target:
			return true
		}
	}
}
开发者ID:postfix,项目名称:sgrep,代码行数:10,代码来源:xml.go

示例11: scanWord

func scanWord(s *scanner.Scanner, out chan Part, stop villa.Stop, word []rune) (toStop bool) {
	start := s.Pos()
	for i := 0; i < len(word); i++ {
		if r := s.Next(); r == scanner.EOF {
			return output(out, stop, TP_EOF_UNEXPECTED, start, s.Pos())
		} else if r != word[i] {
			return output(out, stop, TP_ERROR, start, s.Pos())
		}
	}
	return output(out, stop, TP_KEYWORD, start, s.Pos())
}
开发者ID:postfix,项目名称:sgrep,代码行数:11,代码来源:json.go

示例12: expandCond

func (d *Dict) expandCond(in *scanner.Scanner, writer *bufio.Writer) error {
	expand, err := d.evalBool(in)
	if err != nil {
		return err
	}

	var out *bufio.Writer
	expanded := false
	for err == nil && !isEOF(in) {
		if !expanded && expand {
			out = writer
			expanded = true
		}

		if err = copyUntilDelim(in, out); err != nil {
			break
		}

		if in.Peek() == lDelim {
			in.Next()

			switch in.Peek() {
			case lDelim:
				err = copyNext(in, out)
			case condDelim:
				in.Next()
				if in.Peek() == rDelim {
					in.Next()
					return nil
				} else {
					err = d.expandCond(in, out)
				}
			case condElsif:
				in.Next()
				out = nil
				if in.Peek() == rDelim {
					in.Next()
					expand = true
				} else {
					expand, err = d.evalBool(in)
				}
			default:
				err = d.expandVar(in, out)
				if out == nil || err == nil {
					err = match(in, rDelim)
				}
			}
		} else {
			err = d.expandDirective(in, out)
		}
	}

	return err
}
开发者ID:eZanmoto,项目名称:Bake,代码行数:54,代码来源:template.go

示例13: copyNext

func copyNext(in *scanner.Scanner, out *bufio.Writer) error {
	c := in.Next()
	if out != nil {
		if n, err := out.WriteRune(c); err == nil && n < 1 {
			return fmt.Errorf("Couldn't write: %c", c)
		} else if err != nil {
			return err
		}
	}
	return nil
}
开发者ID:eZanmoto,项目名称:Bake,代码行数:11,代码来源:template.go

示例14: scanTo2

func scanTo2(s *scanner.Scanner, target0, target1 rune) bool {
	for {
		switch s.Next() {
		case scanner.EOF:
			return false
		case target0:
			if s.Peek() == target1 {
				s.Next() // skip targe1
				return true
			}
		}
	}
}
开发者ID:postfix,项目名称:sgrep,代码行数:13,代码来源:xml.go

示例15: parseVariable

func parseVariable(sc *scanner.Scanner) (result []byte, err error) {
	delims := []byte{byte(sc.Next())}

	if ch := sc.Peek(); ch == '{' {
		delims = append(delims, byte(sc.Next()))
	}

	name, err := parseName(sc)

	if err == nil && len(delims) > 1 && '}' != byte(sc.Peek()) {
		err = errInvalidSyntax
	}

	if err != nil {
		name = append(delims, name...)
		if len(delims) > 1 {
			name = append(name, byte(sc.Next()))
		}

		return name, err
	}

	if len(delims) > 1 {
		sc.Next()
	}
	return name, err
}
开发者ID:qrtz,项目名称:livedev,代码行数:27,代码来源:config.go


注:本文中的text/scanner.Scanner.Next方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。