本文整理汇总了Golang中text/scanner.TokenString函数的典型用法代码示例。如果您正苦于以下问题:Golang TokenString函数的具体用法?Golang TokenString怎么用?Golang TokenString使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了TokenString函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Token
func (d *Decoder) Token() (Token, error) {
t := d.scan.Scan()
if d.err != nil {
return nil, d.err
}
if d.depth == 0 && t != '(' && t != scanner.EOF {
return nil, fmt.Errorf("expecting '(', got %s", scanner.TokenString(t))
}
switch t {
case scanner.EOF:
return nil, io.EOF
case scanner.Ident:
return Symbol(d.scan.TokenText()), nil
case scanner.String:
text := d.scan.TokenText()
// Assume all strings are quoted.
return String(text[1 : len(text)-1]), nil
case scanner.Int:
n, err := strconv.ParseInt(d.scan.TokenText(), 10, 64)
if err != nil {
return nil, err
}
return Int(n), nil
case '(':
d.depth++
return StartList{}, nil
case ')':
d.depth--
return EndList{}, nil
default:
pos := d.scan.Pos()
return nil, fmt.Errorf("unexpected token %s at L%d:C%d", scanner.TokenString(t), pos.Line, pos.Column)
}
}
示例2: parseDefinitions
func (p *parser) parseDefinitions() (defs []Definition) {
for {
switch p.tok {
case scanner.Ident:
ident := p.scanner.TokenText()
pos := p.scanner.Position
p.accept(scanner.Ident)
switch p.tok {
case '+':
p.accept('+')
defs = append(defs, p.parseAssignment(ident, pos, "+="))
case '=':
defs = append(defs, p.parseAssignment(ident, pos, "="))
case '{', '(':
defs = append(defs, p.parseModule(ident, pos))
default:
p.errorf("expected \"=\" or \"+=\" or \"{\" or \"(\", found %s",
scanner.TokenString(p.tok))
}
case scanner.EOF:
return
default:
p.errorf("expected assignment or module definition, found %s",
scanner.TokenString(p.tok))
return
}
}
}
示例3: expect
func (p *parser) expect(tok rune) string {
lit := p.lit
if p.tok != tok {
p.errorf("expected %s, got %s (%s)", scanner.TokenString(tok), scanner.TokenString(p.tok), lit)
}
p.next()
return lit
}
示例4: expect
func (p *gcParser) expect(tok int) string {
lit := p.lit
if p.tok != tok {
p.errorf("expected %q, got %q (%q)", scanner.TokenString(tok), scanner.TokenString(p.tok), lit)
}
p.next()
return lit
}
示例5: accept
func (p *parser) accept(toks ...rune) bool {
for _, tok := range toks {
if p.tok != tok {
p.errorf("expected %s, found %s", scanner.TokenString(tok),
scanner.TokenString(p.tok))
return false
}
p.next()
}
return true
}
示例6: read_int
func (this *import_data_parser) read_int() string {
val := ""
if this.toktype == '-' {
this.next()
val += "-"
}
if this.toktype != scanner.Int {
this.errorf("expected: %s, got: %s", scanner.TokenString(scanner.Int), scanner.TokenString(this.toktype))
}
val += this.token()
this.next()
return val
}
示例7: parseExport
// Export = "PackageClause { Decl } "$$" .
// PackageClause = "package" PackageName [ "safe" ] "\n" .
//
func (p *parser) parseExport() *types.Package {
p.expectKeyword("package")
name := p.parsePackageName()
if p.tok == scanner.Ident && p.lit == "safe" {
// package was compiled with -u option - ignore
p.next()
}
p.expect('\n')
pkg := p.getPkg(p.id, name)
for p.tok != '$' && p.tok != scanner.EOF {
p.parseDecl()
}
if ch := p.scanner.Peek(); p.tok != '$' || ch != '$' {
// don't call next()/expect() since reading past the
// export data may cause scanner errors (e.g. NUL chars)
p.errorf("expected '$$', got %s %c", scanner.TokenString(p.tok), ch)
}
if n := p.scanner.ErrorCount; n != 0 {
p.errorf("expected no scanner errors, got %d", n)
}
// package was imported completely and without errors
pkg.MarkComplete()
return pkg
}
示例8: read_ident
// internal, use expect(scanner.Ident) instead
func (this *import_data_parser) read_ident() string {
id := ""
prev := rune(0)
loop:
for {
switch this.toktype {
case scanner.Ident:
if prev == scanner.Ident {
break loop
}
prev = this.toktype
id += this.token()
this.next()
case '.', '?', '$':
prev = this.toktype
id += string(this.toktype)
this.next()
default:
break loop
}
}
if id == "" {
this.errorf("identifier expected, got %s", scanner.TokenString(this.toktype))
}
return id
}
示例9: parseValue
func (p *Parser) parseValue(tok rune) error {
if tok == '{' {
return p.parseObject()
} else if tok == '[' {
return p.parseArray()
} else if tok == scanner.String {
p.c.StringValue(p.s.TokenText())
return nil
} else if tok == '-' {
tok = p.s.Scan()
if tok == scanner.Int || tok == scanner.Float {
p.c.NumberValue("-" + p.s.TokenText())
return nil
}
return p.createError("expected number, but got %s",
scanner.TokenString(tok))
} else if tok == scanner.Int || tok == scanner.Float {
p.c.NumberValue(p.s.TokenText())
return nil
} else if tok == scanner.Ident && isLiteral(p.s.TokenText()) {
p.c.LiteralValue(toLiteral(p.s.TokenText()))
return nil
}
return p.createError("expected object, array, string, number or literal, but got %s", scanner.TokenString(tok))
}
示例10: parseArray
func (p *Parser) parseArray() error {
p.c.StartArray()
for {
tok := p.s.Scan()
if tok == ']' {
p.c.EndArray()
return nil
}
p.c.StartValue()
if err := p.parseValue(tok); err != nil {
return err
}
tok = p.s.Scan()
if tok == ']' {
p.c.EndValue(false)
p.c.EndArray()
return nil
}
if tok != ',' {
return p.createError("in array, expected ',', but got %s",
scanner.TokenString(tok))
}
p.c.EndValue(true)
}
return nil
}
示例11: parseExport
// Export = "PackageClause { Decl } "$$" .
// PackageClause = "package" PackageName [ "safe" ] "\n" .
//
func (p *gcParser) parseExport() *Package {
p.expectKeyword("package")
name := p.parsePackageName()
if p.tok != '\n' {
// A package is safe if it was compiled with the -u flag,
// which disables the unsafe package.
// TODO(gri) remember "safe" package
p.expectKeyword("safe")
}
p.expect('\n')
pkg := p.getPkg(p.id, name)
for p.tok != '$' && p.tok != scanner.EOF {
p.parseDecl()
}
if ch := p.scanner.Peek(); p.tok != '$' || ch != '$' {
// don't call next()/expect() since reading past the
// export data may cause scanner errors (e.g. NUL chars)
p.errorf("expected '$$', got %s %c", scanner.TokenString(p.tok), ch)
}
if n := p.scanner.ErrorCount; n != 0 {
p.errorf("expected no scanner errors, got %d", n)
}
// package was imported completely and without errors
pkg.Complete = true
return pkg
}
示例12: parseType
// Type = "<" "type" ( "-" int | int [ TypeDefinition ] ) ">" .
func (p *parser) parseType(pkg *types.Package) (t types.Type) {
p.expect('<')
p.expectKeyword("type")
switch p.tok {
case scanner.Int:
n := p.parseInt()
if p.tok == '>' {
t = p.typeMap[int(n)]
} else {
t = p.parseTypeDefinition(pkg, int(n))
}
case '-':
p.next()
n := p.parseInt()
t = lookupBuiltinType(int(n))
default:
p.errorf("expected type number, got %s (%q)", scanner.TokenString(p.tok), p.lit)
return nil
}
p.expect('>')
return
}
示例13: expect
func (p *parser) expect(tok rune) scanner.Position {
pos := p.pos
if p.tok != tok {
p.errorExpected(pos, scanner.TokenString(tok))
}
p.next() // make progress in any case
return pos
}
示例14: main
func main() {
d := freenect.NewFreenectDevice(StartWithDevice)
var s scanner.Scanner
s.Init(os.Stdin)
var cmd string
var arg int = OutOfRange
fmt.Print("gonect> ")
tok := s.Scan()
for tok != scanner.EOF {
if scanner.TokenString(tok) == "Ident" {
cmd = s.TokenText()
}
switch {
case cmd == "help":
showHelpMessage()
break
case cmd == "tilt":
s.Scan()
arg, _ = strconv.Atoi(s.TokenText())
if arg == OutOfRange { break }
d.SetTiltDegs(arg)
cmd = ""
arg = OutOfRange
case cmd == "led":
s.Scan()
led_string := s.TokenText()
flash_led(d, led_string)
break
case cmd == "rgb_frame":
s.Scan()
filename := s.TokenText()
freenect.SaveRGBAFrame(d, filename)
break
case cmd == "ir_frame":
s.Scan()
filename := s.TokenText()
freenect.SaveIRFrame(d, filename)
break
case cmd == "depth_frame":
s.Scan()
filename := s.TokenText()
freenect.SaveDepthFrame(d, filename)
break
case cmd == "quit":
return
}
fmt.Printf("gonect> ")
s.Scan()
}
}
示例15: expect
// makes sure that the current token is 'x', returns it and reads the next one
func (this *import_data_parser) expect(x rune) string {
if x == scanner.Ident {
// special case, in gccgo import data identifier is not exactly a scanner.Ident
return this.read_ident()
}
if x == scanner.Int {
// another special case, handle negative ints as well
return this.read_int()
}
if this.toktype != x {
this.errorf("expected: %s, got: %s", scanner.TokenString(x), scanner.TokenString(this.toktype))
}
tok := this.token()
this.next()
return tok
}