Beispiel #1
0
func fmtFile(fname string) (bool, error) {
	input, e := ioutil.ReadFile(fname)
	if e != nil {
		return false, e
	}

	f, rec, es := parse.File(fname, bytes.NewBuffer(input), false)
	if es != nil {
		return false, fmt.Errorf("%d errors found at parsing", len(es))
	}

	var output bytes.Buffer
	gfmt.FprintFile(&output, f, rec)
	if bytes.Compare(input, output.Bytes()) == 0 {
		return false, nil
	}

	tempfile, e := ioutil.TempFile(tempDir, "e8fmt")
	if e != nil {
		return false, e
	}
	if _, e := tempfile.Write(output.Bytes()); e != nil {
		return false, e
	}
	if e := tempfile.Close(); e != nil {
		return false, e
	}
	return true, os.Rename(tempfile.Name(), fname)
}
Beispiel #2
0
// parse all files
func (l *lang) parsePkg(pinfo *build8.PkgInfo) (
	map[string]*ast.File, []*lex8.Error,
) {
	var parseErrs []*lex8.Error
	asts := make(map[string]*ast.File)
	for name, src := range pinfo.Src {
		if filepath.Base(src.Path) != name {
			panic("basename in path is different from the file name")
		}

		f, _, es := parse.File(src.Path, src, l.golike)
		if es != nil {
			parseErrs = append(parseErrs, es...)
		}

		if err := src.Close(); err != nil {
			parseErrs = append(parseErrs, &lex8.Error{Err: err})
		}

		asts[name] = f
	}
	if len(parseErrs) > 0 {
		return nil, parseErrs
	}

	return asts, nil
}
Beispiel #3
0
func TestFormatFile(t *testing.T) {
	gfmt := func(s string) string {
		r := strings.NewReader(s)
		ast, rec, es := parse.File("", r, false)
		if len(es) > 0 {
			t.Errorf("parsing %q failed with errors", s)
			for _, err := range es {
				t.Log(err)
			}
		}

		out := new(bytes.Buffer)
		FprintFile(out, ast, rec)
		return out.String()
	}

	o := func(s, exp string) {
		got := gfmt(s)
		if exp != got {
			t.Errorf("gfmt %q: expect %q, got %q", s, exp, got)
		}
	}

	o("func main() {}", "func main() {}\n")         // tailing end line
	o("func main  () {  }", "func main() {}\n")     // remove spaces
	o("\n\nfunc main  () {  }", "func main() {}\n") // remove lines
	o("func main(){}", "func main() {}\n")          // add spaces
	o("func main() {\n}", "func main() {}\n")       // merge oneliner

	o("// some comment", "// some comment\n") // comment
}
Beispiel #4
0
func listImport(
	f string, rc io.ReadCloser, imp build8.Importer, golike bool,
) []*lex8.Error {
	fast, _, es := parse.File(f, rc, golike)
	if es != nil {
		return es
	}

	if fast.Imports == nil {
		return nil
	}

	m := make(map[string]*importDecl)
	log := lex8.NewErrorList()

	for _, d := range fast.Imports.Decls {
		p, as, e := ast.ImportPathAs(d)
		if e != nil {
			log.Errorf(d.Path.Pos, "invalid path string %s", d.Path.Lit)
			continue
		}

		pos := ast.ImportPos(d)
		if other, found := m[as]; found {
			log.Errorf(pos, "%s already imported", as)
			log.Errorf(other.pos, "  previously imported here")
			continue
		}

		m[as] = &importDecl{as: as, path: p, pos: pos}
	}

	if errs := log.Errs(); errs != nil {
		return errs
	}

	for as, d := range m {
		imp.Import(as, d.path, d.pos)
	}

	return nil
}
Beispiel #5
0
func main() {
	flag.Parse()

	args := flag.Args()
	if len(args) != 1 {
		exit(errors.New("need exactly one input input file"))
	}
	fname := args[0]
	input, e := ioutil.ReadFile(fname)
	if e != nil {
		exit(e)
	}

	if *bare {
		if *parseAST {
			stmts, es := parse.Stmts(fname, bytes.NewBuffer(input))
			printErrs(es)
			gfmt.FprintStmts(os.Stdout, stmts)
		} else {
			bs, es, irLog := g8.CompileBareFunc(fname, string(input))
			printErrs(es)
			printIRLog(irLog, *ir)
			runImage(bs, *dasm, *ncycle)
		}
	} else {
		if *parseAST {
			f, rec, es := parse.File(fname, bytes.NewBuffer(input), true)
			printErrs(es)
			gfmt.FprintFile(os.Stdout, f, rec)
		} else {
			bs, es, irLog := g8.CompileSingle(fname, string(input), *golike)
			printErrs(es)
			printIRLog(irLog, *ir)
			runImage(bs, *dasm, *ncycle)
		}
	}
}