本文整理汇总了Golang中text/scanner.Scanner.Peek方法的典型用法代码示例。如果您正苦于以下问题:Golang Scanner.Peek方法的具体用法?Golang Scanner.Peek怎么用?Golang Scanner.Peek使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类text/scanner.Scanner
的用法示例。
在下文中一共展示了Scanner.Peek方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: 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
}
示例2: copyUntilDelim
func copyUntilDelim(in *scanner.Scanner, out *bufio.Writer) error {
var err error
for err == nil && !isEOF(in) && in.Peek() != lDelim && in.Peek() != rDelim {
err = copyNext(in, out)
}
return err
}
示例3: 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
}
示例4: ProcessConfig
// ProcessConfig replaces references of environment varialbes for the given data
// Support variable syntax: $varname, ${varname}
func ProcessConfig(data []byte, e *env.Env, escapeChar rune) ([]byte, error) {
var result []byte
var sc scanner.Scanner
sc.Init(bytes.NewReader(data))
DONE:
for {
switch ch := sc.Peek(); ch {
default:
result = append(result, byte(sc.Next()))
case scanner.EOF:
break DONE
case escapeChar:
curr, next := sc.Next(), sc.Peek()
if next != '$' {
result = append(result, byte(curr))
}
if next != scanner.EOF {
result = append(result, byte(sc.Next()))
}
case '$':
name, err := parseVariable(&sc)
if err != nil {
pos := sc.Pos()
return result, fmt.Errorf(`parseError:%d:%d: %v %q`, pos.Line, pos.Offset, err, name)
}
result = append(result, e.Get(string(name))...)
}
}
return result, nil
}
示例5: scanArray
func scanArray(s *scanner.Scanner, out chan Part, stop villa.Stop) (toStop bool) {
if scanRune(s, out, stop, TP_ARRAY_START, '[') {
return true
}
skipWhitespaces(s)
if s.Peek() != ']' {
for {
if scanValue(s, out, stop) {
return true
}
skipWhitespaces(s)
if s.Peek() != ',' {
break
}
if scanRune(s, out, stop, TP_COMMA, ',') {
return true
}
skipWhitespaces(s)
}
}
return scanRune(s, out, stop, TP_ARRAY_END, ']')
}
示例6: split
func split(expr string) (keys []string, err error) {
var msgs []string
var s scanner.Scanner
s.Init(strings.NewReader(expr))
s.Mode = scanner.ScanIdents | scanner.ScanInts | scanner.ScanStrings
s.Error = func(s *scanner.Scanner, msg string) { msgs = append(msgs, fmt.Sprintf("%s %s", s.Pos(), msg)) }
key := ""
keys = []string{}
for err == nil {
t := s.Peek()
// fmt.Printf(">>> %s: %s %s\n", s.Pos(), scanner.TokenString(t), s.TokenText())
switch t {
case '[':
key, err = scanBracketedKey(&s)
case '.':
s.Scan()
continue
case scanner.EOF:
goto end
default:
key, err = scanKey(&s)
}
if len(msgs) > 0 {
err = errors.New(strings.Join(msgs, "\n"))
}
if err == nil {
keys = append(keys, key)
}
}
end:
return
}
示例7: scanTo3
func scanTo3(s *scanner.Scanner, target0, target1, target2 rune) bool {
for {
// match 0, 1
if !scanTo2(s, target0, target1) {
// EOF
return false
}
switch s.Peek() {
case scanner.EOF:
s.Next()
return false
case target2:
// matched 2, found
s.Next()
return true
case target1:
if target0 == target1 {
loop:
for {
switch s.Next() {
case scanner.EOF:
return false
case target2:
return true
case target0:
// keep scanning
default:
// not found, go to out loop
break loop
}
}
}
}
}
}
示例8: 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())
}
}
示例9: 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
}
示例10: 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())}
}
}
}
示例11: 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
}
}
}
}
示例12: Number
// collect the next characters in s that are numbers
// assumes at least TokenText is a number
func Number(s *scanner.Scanner) string {
var buffer bytes.Buffer
for s.Scan() != scanner.EOF {
buffer.WriteString(s.TokenText())
if _, ok := strconv.Atoi(string(s.Peek())); ok != nil {
return buffer.String()
}
}
// reached the end of file, but that's ok potentially?
return buffer.String()
}
示例13: readVar
func readVar(in *scanner.Scanner) (s string, err error) {
var buf bytes.Buffer
for isVarRune(in.Peek()) {
buf.WriteRune(in.Next())
}
s = buf.String()
if in.Peek() != rDelim {
err = parseErr(in, "Unexpected character '%c'", in.Next())
} else if len(s) == 0 {
err = parseErr(in, "Empty variable")
}
return
}
示例14: parseXML
func parseXML(sc *scanner.Scanner) (*Token, bool) {
var entity = new(bytes.Buffer)
token := new(Token)
// Skip the '<'
sc.Scan()
switch sc.Peek() {
case '/':
token.Type = XMLEndToken
sc.Next()
case '!':
log.Tracef("parseXML skipping comment")
next := sc.Next()
for next != '>' {
next = sc.Next()
}
return nil, false
default:
token.Type = XMLStartToken
}
log.Tracef("parseXML creating %s element", token.Type)
for {
tok := sc.Scan()
log.Tracef("parseXML found %s. Token is %v. Entity is: '%s'",
sc.TokenText(),
tok,
entity.String())
switch {
case tok == '>':
token.Text = entity.String()
return token, true
case unicode.IsSpace(tok):
return nil, false
default:
log.Tracef("parseXML appending %s to string",
sc.TokenText())
entity.WriteString(sc.TokenText())
}
}
}
示例15: scanKeyword
func scanKeyword(s *scanner.Scanner, out chan Part, stop villa.Stop) (toStop bool) {
start := s.Pos()
switch s.Peek() {
case scanner.EOF:
s.Next()
return output(out, stop, TP_EOF_UNEXPECTED, start, s.Pos())
case 't':
return scanWord(s, out, stop, []rune("true"))
case 'f':
return scanWord(s, out, stop, []rune("false"))
case 'n':
return scanWord(s, out, stop, []rune("null"))
}
s.Next()
return output(out, stop, TP_ERROR, start, s.Pos())
}