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


Golang scanner.PrintError函数代码示例

本文整理汇总了Golang中go/scanner.PrintError函数的典型用法代码示例。如果您正苦于以下问题:Golang PrintError函数的具体用法?Golang PrintError怎么用?Golang PrintError使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


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

示例1: main

func main() {
	flag.Parse()

	var filename string
	switch flag.NArg() {
	case 0:
		filename = "/dev/stdin"
	case 1:
		filename = flag.Arg(0)
	default:
		usage()
	}

	src, err := ioutil.ReadFile(filename)
	if err != nil {
		scanner.PrintError(os.Stderr, err)
	}

	if path.Ext(filename) == ".html" {
		src = extractEBNF(src)
	}

	grammar, err := ebnf.Parse(filename, src)
	if err != nil {
		scanner.PrintError(os.Stderr, err)
	}

	if err = ebnf.Verify(grammar, *start); err != nil {
		scanner.PrintError(os.Stderr, err)
	}
}
开发者ID:ivanwyc,项目名称:google-go,代码行数:31,代码来源:ebnflint.go

示例2: main

func main() {
	flag.Parse()
	log.SetFlags(0)

	// If no paths are provided then use the present working directory.
	roots := flag.Args()
	if len(roots) == 0 {
		roots = []string{"."}
	}

	// Recursively retrieve all ego templates
	var v visitor
	for _, root := range roots {
		if err := filepath.Walk(root, v.visit); err != nil {
			scanner.PrintError(os.Stderr, err)
			os.Exit(1)
		}
	}

	// Parse every *.ego file.
	for _, path := range v.paths {
		template, err := egon.ParseFile(path)
		if err != nil {
			log.Fatal("parse file: ", err)
		}

		pkg := &egon.Package{Template: template}
		err = pkg.Write()
		if err != nil {
			log.Fatal("write: ", err)
		}
	}
}
开发者ID:commondream,项目名称:egon,代码行数:33,代码来源:main.go

示例3: FormatCode

// FormatCode runs "goimports -w" on the source file.
func (f *SourceFile) FormatCode() error {
	if NoFormat {
		return nil
	}
	// Parse file into AST
	fset := token.NewFileSet()
	file, err := parser.ParseFile(fset, f.Abs(), nil, parser.ParseComments)
	if err != nil {
		content, _ := ioutil.ReadFile(f.Abs())
		var buf bytes.Buffer
		scanner.PrintError(&buf, err)
		return fmt.Errorf("%s\n========\nContent:\n%s", buf.String(), content)
	}
	// Clean unused imports
	imports := astutil.Imports(fset, file)
	for _, group := range imports {
		for _, imp := range group {
			path := strings.Trim(imp.Path.Value, `"`)
			if !astutil.UsesImport(file, path) {
				astutil.DeleteImport(fset, file, path)
			}
		}
	}
	ast.SortImports(fset, file)
	// Open file to be written
	w, err := os.OpenFile(f.Abs(), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, os.ModePerm)
	if err != nil {
		return err
	}
	defer w.Close()
	// Write formatted code without unused imports
	return format.Node(w, fset, file)
}
开发者ID:jim-slattery-rs,项目名称:goa,代码行数:34,代码来源:workspace.go

示例4: ErrPosition

// ErrPosition reports an error at position.
func (c *Report) ErrPosition(position token.Position, format string, arg ...interface{}) {
	if c.TraceErrors {
		fmt.Fprintf(os.Stderr, "%v: %s\n", position, fmt.Sprintf(format, arg...))
	}

	if c.IgnoreErrors {
		return
	}

	c.errorsMu.Lock()         // X+
	defer c.errorsMu.Unlock() // X-

	if c.PanicOnError {
		panic(fmt.Errorf("%s: %v", position, fmt.Sprintf(format, arg...)))
	}

	c.errors.Add(position, fmt.Sprintf(format, arg...))

	if c.ErrLimit > 0 {
		c.ErrLimit--
		if c.ErrLimit == 0 {
			scanner.PrintError(os.Stderr, c.errors)
			log.Fatalf("too many errors")
		}
	}

}
开发者ID:cznic,项目名称:xc,代码行数:28,代码来源:global.go

示例5: main

func main() {
	log.SetFlags(0)
	kingpin.CommandLine.Help = "Generate native Go code from ERB-style Templates"
	kingpin.Parse()

	if len(egon.Config.Folders) == 0 {
		egon.Config.Folders = []string{"."}
	}

	// Recursively retrieve all templates
	var v visitor
	for _, root := range egon.Config.Folders {
		log.Printf("scanning folder [%s]", root)
		if err := filepath.Walk(root, v.visit); err != nil {
			scanner.PrintError(os.Stderr, err)
			os.Exit(1)
		}
	}

	// Parse every *.ego file.
	for _, path := range v.paths {
		template, err := egon.ParseFile(path)
		if err != nil {
			log.Fatal("parse file: ", err)
		}

		pkg := &egon.Package{Template: template}
		err = pkg.Write()
		if err != nil {
			log.Fatal("write: ", err)
		}
	}
}
开发者ID:SlinSo,项目名称:egon,代码行数:33,代码来源:main.go

示例6: main

func main() {
	log.SetFlags(0)
	list := flag.Bool("l", false, "List assembly code and exit.")
	trace := flag.Bool("t", false, "Trace execution.")
	stack := flag.Bool("s", false, "Trace execution and stack.")
	flag.Parse()
	if n := flag.NArg(); n != 1 {
		log.Fatalf("expected 1 argument, got %v", n)
	}

	prog, err := pl0.Parse(flag.Arg(0))
	if err != nil {
		scanner.PrintError(os.Stderr, err)
		os.Stderr.Sync()
		os.Exit(1)
	}

	if *list {
		prog.List(os.Stdout)
		return
	}

	switch {
	case *stack:
		err = prog.Run(pl0.TraceStack())
	case *trace:
		err = prog.Run(pl0.Trace())
	default:
		err = prog.Run()
	}
	if err != nil {
		log.Fatal(err)
	}
}
开发者ID:JamesLinus,项目名称:pl0,代码行数:34,代码来源:pl0.go

示例7: Example_undeclared

func Example_undeclared() {
	_, err := ParseString("",
		`
	CONST a = 42;
	
	VAR b;
	
	PROCEDURE c;
	BEGIN
		CALL aa;
		CALL c;
	END;
	
	BEGIN
		b := a;
		b := d;
		e := 10;
		CALL c;
		CALL f;
		? b;
		? g;
	END.
`)

	scanner.PrintError(os.Stdout, err)
	// Output:
	// 8:8: undeclared identifier aa
	// 14:8: undeclared identifier d
	// 15:3: undeclared identifier e
	// 17:8: undeclared identifier f
	// 19:5: undeclared identifier g
}
开发者ID:JamesLinus,项目名称:pl0,代码行数:32,代码来源:all_test.go

示例8: Example_redeclaration

func Example_redeclaration() {
	_, err := ParseString("",
		`
	CONST a = 42, b = 24, a = 314;
	
	VAR b, c, d;
	
	PROCEDURE c;
	BEGIN
	END;
	
	PROCEDURE d;
	BEGIN
	END;
	
	PROCEDURE e;
	BEGIN
	END;
	
	PROCEDURE e;
	BEGIN
	END;
	
	BEGIN
	END.
`)

	scanner.PrintError(os.Stdout, err)
	// Output:
	// 2:24: redeclaration of a at 2:8
	// 4:6: redeclaration of b at 2:16
	// 6:12: redeclaration of c at 4:9
	// 10:12: redeclaration of d at 4:12
	// 18:12: redeclaration of e at 14:12
}
开发者ID:JamesLinus,项目名称:pl0,代码行数:35,代码来源:all_test.go

示例9: report

func report(err error) {
	scanner.PrintError(os.Stderr, err)
	if list, ok := err.(scanner.ErrorList); ok {
		errorCount += len(list)
		return
	}
	errorCount++
}
开发者ID:4honor,项目名称:obdi,代码行数:8,代码来源:gotype.go

示例10: TestTypeCheck

func TestTypeCheck(t *testing.T) {
	flag.Parse()
	pkgRx, err := regexp.Compile(*pkgPat)
	if err != nil {
		t.Fatalf("illegal flag value %q: %s", *pkgPat, err)
	}

	pkgs, err := parser.ParseDir(fset, testDir, testFilter, 0)
	if err != nil {
		scanner.PrintError(os.Stderr, err)
		t.Fatalf("packages in %s contain syntax errors", testDir)
	}

	for _, pkg := range pkgs {
		if !pkgRx.MatchString(pkg.Name) {
			continue // only test selected packages
		}

		if *trace {
			fmt.Println(pkg.Name)
		}

		xlist := expectedErrors(t, pkg)
		err := CheckPackage(fset, pkg, nil)
		if err != nil {
			if elist, ok := err.(scanner.ErrorList); ok {
				// verify that errors match
				for i := 0; i < len(xlist) && i < len(elist); i++ {
					checkError(t, xlist[i], elist[i])
				}
				// the correct number or errors must have been found
				if len(xlist) != len(elist) {
					fmt.Fprintf(os.Stderr, "%s\n", pkg.Name)
					scanner.PrintError(os.Stderr, elist)
					fmt.Fprintln(os.Stderr)
					t.Errorf("TypeCheck(%s): %d errors expected but %d reported", pkg.Name, len(xlist), len(elist))
				}
			} else {
				t.Errorf("TypeCheck(%s): %v", pkg.Name, err)
			}
		} else if len(xlist) > 0 {
			t.Errorf("TypeCheck(%s): %d errors expected but 0 reported", pkg.Name, len(xlist))
		}
	}
}
开发者ID:WXB506,项目名称:golang,代码行数:45,代码来源:typechecker_test.go

示例11: Test

func Test(t *testing.T) {
	err := processFile("testdata/data.go", NewPrototype())
	e, ok := err.(scanner.ErrorList)
	if !ok {
		t.Fatal(err)
	}

	if len(e) != NUM_ERRORS {
		t.Errorf("expected %d errors", NUM_ERRORS)
		scanner.PrintError(os.Stderr, err)
	}
}
开发者ID:kless,项目名称:goutil,代码行数:12,代码来源:mkdef_test.go

示例12: MustCompileLexer

// MustCompileLexer is like CompileLexer but panics if the definitions cannot be compiled.
// It simplifies safe initialization of global variables holding compiled Lexers.
func MustCompileLexer(starts [][]int, tokdefs map[string]int, grammar, start string) (lexer *Lexer) {
	var err error
	if lexer, err = CompileLexer(starts, tokdefs, grammar, start); err != nil {
		if list, ok := err.(scanner.ErrorList); ok {
			scanner.PrintError(os.Stderr, list)
		} else {
			log.Fatal(err)
		}
		panic(err)
	}
	return
}
开发者ID:ericfode,项目名称:lexer,代码行数:14,代码来源:lexer.go

示例13: Main

func Main() {
	world = eval.NewWorld()
	defineFuncs()
	r := bufio.NewReader(os.Stdin)
	for {
		print("; ")
		line, err := r.ReadSlice('\n')
		if err != nil {
			break
		}

		// Try line as a command
		cmd, rest := getCmd(line)
		if cmd != nil {
			err := cmd.handler(rest)
			if err != nil {
				scanner.PrintError(os.Stderr, err)
			}
			continue
		}

		// Try line as code
		code, err := world.Compile(string(line))
		if err != nil {
			scanner.PrintError(os.Stderr, err)
			continue
		}
		v, err := code.Run()
		if err != nil {
			fmt.Fprintf(os.Stderr, err.String())
			continue
		}
		if v != nil {
			println(v.String())
		}
	}
}
开发者ID:edisonwsk,项目名称:golang-on-cygwin,代码行数:37,代码来源:cmd.go

示例14: generate

func generate(arch, header, out, test, predefined string) error {
	m, err := ccgo.NewModel(arch)
	if err != nil {
		return err
	}

	var cpplog *os.File
	var s []string
	opts := []cc.Opt{cc.SysIncludePaths([]string{ccgo.LibcIncludePath})}
	if *cpp {
		var err error
		if cpplog, err = os.Create("log-cpp-" + arch); err != nil {
			return err
		}

		defer cpplog.Close()

		opts = append(opts, cc.Cpp(func(a []xc.Token) {
			if len(a) == 0 {
				return
			}

			s = s[:0]
			for _, v := range a {
				s = append(s, cc.TokSrc(v))
			}
			fmt.Fprintf(cpplog, "%s: %s\n", a[0].Position(), strings.Join(s, " "))
		}))
	}

	predefined += fmt.Sprintf("\n#define __MODEL%v\n", ccgo.Archs[arch])
	tu, err := cc.Parse(predefined, paths, m, opts...)
	if err != nil {
		var buf bytes.Buffer
		scanner.PrintError(&buf, err)
		return fmt.Errorf("%s", buf.Bytes())
	}

	return (&ccgo.Job{
		DeclarationComments: *pos,
		Header:              header,
		ImportPath:          importPath,
		In:                  tu,
		Model:               m,
		Out:                 out,
		Root:                root,
		Test:                test,
	}).Run()
}
开发者ID:cznic,项目名称:c,代码行数:49,代码来源:generator.go

示例15: checkFiles

func checkFiles(t *testing.T, testname string, testfiles []string) {
	// TODO(gri) Eventually all these different phases should be
	//           subsumed into a single function call that takes
	//           a set of files and creates a fully resolved and
	//           type-checked AST.

	files, err := parseFiles(t, testname, testfiles)

	// we are expecting the following errors
	// (collect these after parsing the files so that
	// they are found in the file set)
	errors := expectedErrors(t, testname, files)

	// verify errors returned by the parser
	eliminate(t, errors, err)

	// verify errors returned after resolving identifiers
	pkg, err := ast.NewPackage(fset, files, GcImport, Universe)
	eliminate(t, errors, err)

	// verify errors returned by the typechecker
	var list scanner.ErrorList
	errh := func(pos token.Pos, msg string) {
		list.Add(fset.Position(pos), msg)
	}
	err = Check(fset, pkg, errh, nil)
	eliminate(t, errors, list)

	if *listErrors {
		scanner.PrintError(os.Stdout, err)
		return
	}

	// there should be no expected errors left
	if len(errors) > 0 {
		t.Errorf("%s: %d errors not reported:", testname, len(errors))
		for pos, msg := range errors {
			t.Errorf("%s: %s\n", fset.Position(pos), msg)
		}
	}
}
开发者ID:mm120,项目名称:gcc,代码行数:41,代码来源:check_test.go


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