Exemple #1
0
func Example_completeGeneratedProgram() { // main
	fset := token.NewFileSet()
	file := fset.AddFile("example.go", -1, len(src))
	src := bytes.NewBufferString(src)
	lx, err := lex.New(file, src, lex.RuneClass(rune2Class))
	if err != nil {
		panic(err)
	}

	l := &lexer{lx}
	for {
		c := l.scan()
		fmt.Printf("%v: %v %q\n", file.Position(c.Pos()), str(c.Rune), l.TokenBytes(nil))
		if c.Rune == lex.RuneEOF {
			return
		}
	}
	// Output:
	// example.go:3:1: FUNC "func"
	// example.go:3:6: IDENT "Xφ42"
	// example.go:3:11: '(' "("
	// example.go:3:12: ')' ")"
	// example.go:3:14: IDENT "int"
	// example.go:3:18: '{' "{"
	// example.go:3:20: IDENT "return"
	// example.go:3:27: INT "314"
	// example.go:3:31: '}' "}"
	// example.go:4:2: EOF "\xff"
}
Exemple #2
0
func newLexer(nm string, sz int, r io.RuneReader, report *xc.Report) (*lexer, error) {
	file := xc.FileSet.AddFile(nm, -1, sz+2)
	lx, err := lex.New(
		file,
		r,
		lex.ErrorFunc(func(pos token.Pos, msg string) {
			report.Err(pos, msg)
		}),
		lex.RuneClass(func(r rune) int { return int(r) }),
	)
	if err != nil {
		return nil, err
	}

	l := &lexer{
		Lexer:  lx,
		report: report,
	}
	l.emit(&Call{Target: 2})
	l.emit(&Halt{})
	l.enter = &Enter{NVars: -1}
	l.jmp = &Jmp{Target: -1}
	l.emit(l.enter)
	l.emit(l.jmp)
	return l, nil
}
Exemple #3
0
func newLexer(file *token.File, src io.RuneReader) (_ *lexer, err error) {
	l := &lexer{}
	if l.Lexer, err = lex.New(
		file,
		src,
		lex.ErrorFunc(func(pos token.Pos, msg string) {
			l.errors.Add(l.File.Position(pos), msg)
		}),
		lex.RuneClass(runeClass),
	); err != nil {
		return nil, err
	}

	return l, nil
}