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


Golang parser.ParseFile函数代码示例

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


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

示例1: parse

func parse(fset *token.FileSet, src []byte) (interface{}, error) {
	// Try as a complete source file.
	file, err := parser.ParseFile(fset, "", src, parser.ParseComments)
	if err == nil {
		return file, nil
	}
	// If the source is missing a package clause, try as a source fragment; otherwise fail.
	if !strings.Contains(err.Error(), "expected 'package'") {
		return nil, err
	}

	// Try as a declaration list by prepending a package clause in front of src.
	// Use ';' not '\n' to keep line numbers intact.
	psrc := append([]byte("package p;"), src...)
	file, err = parser.ParseFile(fset, "", psrc, parser.ParseComments)
	if err == nil {
		return file.Decls, nil
	}
	// If the source is missing a declaration, try as a statement list; otherwise fail.
	if !strings.Contains(err.Error(), "expected declaration") {
		return nil, err
	}

	// Try as statement list by wrapping a function around src.
	fsrc := append(append([]byte("package p; func _() {"), src...), '}')
	file, err = parser.ParseFile(fset, "", fsrc, parser.ParseComments)
	if err == nil {
		return file.Decls[0].(*ast.FuncDecl).Body.List, nil
	}

	// Failed, and out of options.
	return nil, err
}
开发者ID:cgravill,项目名称:antha,代码行数:33,代码来源:format.go

示例2: format

// format parses src, prints the corresponding AST, verifies the resulting
// src is syntactically correct, and returns the resulting src or an error
// if any.
func format(src []byte, mode checkMode) ([]byte, error) {
	// parse src
	f, err := parser.ParseFile(fset, "", src, parser.ParseComments)
	if err != nil {
		return nil, fmt.Errorf("parse: %s\n%s", err, src)
	}

	// filter exports if necessary
	if mode&export != 0 {
		ast.FileExports(f) // ignore result
		f.Comments = nil   // don't print comments that are not in AST
	}

	// determine printer configuration
	cfg := Config{Tabwidth: tabwidth}
	if mode&rawFormat != 0 {
		cfg.Mode |= RawFormat
	}

	// print AST
	var buf bytes.Buffer
	if err := cfg.Fprint(&buf, fset, f); err != nil {
		return nil, fmt.Errorf("print: %s", err)
	}

	// make sure formatted output is syntactically correct
	res := buf.Bytes()
	if _, err := parser.ParseFile(fset, "", res, 0); err != nil {
		return nil, fmt.Errorf("re-parse: %s\n%s", err, buf.Bytes())
	}

	return res, nil
}
开发者ID:cgravill,项目名称:antha,代码行数:36,代码来源:printer_test.go

示例3: parse

// parse parses src, which was read from filename,
// as a Antha source file or statement list.
func parse(fset *token.FileSet, filename string, src []byte, stdin bool) (*ast.File, func(orig, src []byte) []byte, error) {
	// Try as whole source file.
	file, err := parser.ParseFile(fset, filename, src, parserMode)
	if err == nil {
		return file, nil, nil
	}
	// If the error is that the source file didn't begin with a
	// package line and this is standard input, fall through to
	// try as a source fragment.  Stop and return on any other error.
	if !stdin || !strings.Contains(err.Error(), "expected 'package'") {
		return nil, nil, err
	}

	// If this is a declaration list, make it a source file
	// by inserting a package clause.
	// Insert using a ;, not a newline, so that the line numbers
	// in psrc match the ones in src.
	psrc := append([]byte("protocol p;"), src...)
	file, err = parser.ParseFile(fset, filename, psrc, parserMode)
	if err == nil {
		adjust := func(orig, src []byte) []byte {
			// Remove the package clause.
			// Anthafmt has turned the ; into a \n.
			src = src[len("protocol p\n"):]
			return matchSpace(orig, src)
		}
		return file, adjust, nil
	}
	// If the error is that the source file didn't begin with a
	// declaration, fall through to try as a statement list.
	// Stop and return on any other error.
	if !strings.Contains(err.Error(), "expected declaration") {
		return nil, nil, err
	}

	// If this is a statement list, make it a source file
	// by inserting a package clause and turning the list
	// into a function body.  This handles expressions too.
	// Insert using a ;, not a newline, so that the line numbers
	// in fsrc match the ones in src.
	fsrc := append(append([]byte("protocol p; func _() {"), src...), '}')
	file, err = parser.ParseFile(fset, filename, fsrc, parserMode)
	if err == nil {
		adjust := func(orig, src []byte) []byte {
			// Remove the wrapping.
			// Anthafmt has turned the ; into a \n\n.
			src = src[len("protocol p\n\nfunc _() {"):]
			src = src[:len(src)-len("}\n")]
			// Anthafmt has also indented the function body one level.
			// Remove that indent.
			src = bytes.Replace(src, []byte("\n\t"), []byte("\n"), -1)
			return matchSpace(orig, src)
		}
		return file, adjust, nil
	}

	// Failed, and out of options.
	return nil, nil, err
}
开发者ID:cgravill,项目名称:antha,代码行数:61,代码来源:antha.go

示例4: TestBadComments

// Verify that the printer produces always produces a correct program
// even if the position information of comments introducing newlines
// is incorrect.
func TestBadComments(t *testing.T) {
	t.Skip("not supported in antha")
	const src = `
// first comment - text and position changed by test
package p
import "fmt"
const pi = 3.14 // rough circle
var (
	x, y, z int = 1, 2, 3
	u, v float64
)
func fibo(n int) {
	if n < 2 {
		return n /* seed values */
	}
	return fibo(n-1) + fibo(n-2)
}
`

	f, err := parser.ParseFile(fset, "", src, parser.ParseComments)
	if err != nil {
		t.Error(err) // error in test
	}

	comment := f.Comments[0].List[0]
	pos := comment.Pos()
	if fset.Position(pos).Offset != 1 {
		t.Error("expected offset 1") // error in test
	}

	testComment(t, f, len(src), &ast.Comment{Slash: pos, Text: "//-style comment"})
	testComment(t, f, len(src), &ast.Comment{Slash: pos, Text: "/*-style comment */"})
	testComment(t, f, len(src), &ast.Comment{Slash: pos, Text: "/*-style \n comment */"})
	testComment(t, f, len(src), &ast.Comment{Slash: pos, Text: "/*-style comment \n\n\n */"})
}
开发者ID:cgravill,项目名称:antha,代码行数:38,代码来源:nodes_test.go

示例5: TestLineComments

// TestLineComments, using a simple test case, checks that consequtive line
// comments are properly terminated with a newline even if the AST position
// information is incorrect.
//
func TestLineComments(t *testing.T) {
	const src = `// comment 1
	// comment 2
	// comment 3
	package main
	`

	fset := token.NewFileSet()
	f, err := parser.ParseFile(fset, "", src, parser.ParseComments)
	if err != nil {
		panic(err) // error in test
	}

	var buf bytes.Buffer
	fset = token.NewFileSet() // use the wrong file set
	Fprint(&buf, fset, f)

	nlines := 0
	for _, ch := range buf.Bytes() {
		if ch == '\n' {
			nlines++
		}
	}

	const expected = 3
	if nlines < expected {
		t.Errorf("got %d, expected %d\n", nlines, expected)
		t.Errorf("result:\n%s", buf.Bytes())
	}
}
开发者ID:cgravill,项目名称:antha,代码行数:34,代码来源:nodes_test.go

示例6: TestBadNodes

// Verify that the printer doesn't crash if the AST contains BadXXX nodes.
func TestBadNodes(t *testing.T) {
	const src = "package p\n("
	_, err := parser.ParseFile(fset, "", src, parser.ParseComments)
	if err == nil {
		t.Error("expected illegal program") // error in test
	}
}
开发者ID:cgravill,项目名称:antha,代码行数:8,代码来源:nodes_test.go

示例7: TestFilterDuplicates

func TestFilterDuplicates(t *testing.T) {
	// parse input
	fset := token.NewFileSet()
	file, err := parser.ParseFile(fset, "", input, 0)
	if err != nil {
		t.Fatal(err)
	}

	// create package
	files := map[string]*ast.File{"": file}
	pkg, err := ast.NewPackage(fset, files, nil, nil)
	if err != nil {
		t.Fatal(err)
	}

	// filter
	merged := ast.MergePackageFiles(pkg, ast.FilterFuncDuplicates)

	// pretty-print
	var buf bytes.Buffer
	if err := format.Node(&buf, fset, merged); err != nil {
		t.Fatal(err)
	}
	output := buf.String()

	if output != golden {
		t.Errorf("incorrect output:\n%s", output)
	}
}
开发者ID:cgravill,项目名称:antha,代码行数:29,代码来源:filter_test.go

示例8: TestExamples

func TestExamples(t *testing.T) {
	fset := token.NewFileSet()
	file, err := parser.ParseFile(fset, "test.go", strings.NewReader(exampleTestFile), parser.ParseComments)
	if err != nil {
		t.Fatal(err)
	}
	for i, e := range doc.Examples(file) {
		c := exampleTestCases[i]
		if e.Name != c.Name {
			t.Errorf("got Name == %q, want %q", e.Name, c.Name)
		}
		if w := c.Play; w != "" {
			var g string // hah
			if e.Play == nil {
				g = "<nil>"
			} else {
				var buf bytes.Buffer
				if err := format.Node(&buf, fset, e.Play); err != nil {
					t.Fatal(err)
				}
				g = buf.String()
			}
			if g != w {
				t.Errorf("%s: got Play == %q, want %q", c.Name, g, w)
			}
		}
		if g, w := e.Output, c.Output; g != w {
			t.Errorf("%s: got Output == %q, want %q", c.Name, g, w)
		}
	}
}
开发者ID:cgravill,项目名称:antha,代码行数:31,代码来源:_example_test.go

示例9: ExampleCommentMap

// This example illustrates how to remove a variable declaration
// in a Go/Antha program while maintaining correct comment association
// using an ast.CommentMap.
func ExampleCommentMap() {
	// src is the input for which we create the AST that we
	// are going to manipulate.
	src := `
// This is the package comment.
package main

// This comment is associated with the hello constant.
const hello = "Hello, World!" // line comment 1

// This comment is associated with the foo variable.
var foo = hello // line comment 2 

// This comment is associated with the main function.
func main() {
	fmt.Println(hello) // line comment 3
}
`

	// Create the AST by parsing src.
	fset := token.NewFileSet() // positions are relative to fset
	f, err := parser.ParseFile(fset, "src.go", src, parser.ParseComments)
	if err != nil {
		panic(err)
	}

	// Create an ast.CommentMap from the ast.File's comments.
	// This helps keeping the association between comments
	// and AST nodes.
	cmap := ast.NewCommentMap(fset, f, f.Comments)

	// Remove the first variable declaration from the list of declarations.
	f.Decls = removeFirstVarDecl(f.Decls)

	// Use the comment map to filter comments that don't belong anymore
	// (the comments associated with the variable declaration), and create
	// the new comments list.
	f.Comments = cmap.Filter(f).Comments()

	// Print the modified AST.
	var buf bytes.Buffer
	if err := format.Node(&buf, fset, f); err != nil {
		panic(err)
	}
	fmt.Printf("%s", buf.Bytes())

	// output:
	// // This is the package comment.
	// package main
	//
	// // This comment is associated with the hello constant.
	// const hello = "Hello, World!" // line comment 1
	//
	// // This comment is associated with the main function.
	// func main() {
	// 	fmt.Println(hello) // line comment 3
	// }
}
开发者ID:cgravill,项目名称:antha,代码行数:61,代码来源:example_test.go

示例10: TestGenerateLibOfEmptyComponent

// Verify that generation of an empty component produces a valid go file
func TestGenerateLibOfEmptyComponent(t *testing.T) {
	comp := execute.ComponentInfo{Name: "Test", Description: "", Icon: "", Subgraph: false, InPorts: nil, OutPorts: nil}
	componentLibrary := []execute.ComponentInfo{comp}
	var buf bytes.Buffer
	GenerateComponentLib(&buf, componentLibrary, "", "main")
	_, err := parser.ParseFile(fset, "", buf.String(), parser.AllErrors)
	if err != nil {
		t.Error(err)
	}
}
开发者ID:cgravill,项目名称:antha,代码行数:11,代码来源:antha_test.go

示例11: TestBadGenerateLibOfEmptyComponent

// Verify that parser does fail sometimes
func TestBadGenerateLibOfEmptyComponent(t *testing.T) {
	comp := execute.ComponentInfo{Name: "Test", Description: "", Icon: "", Subgraph: false, InPorts: nil, OutPorts: nil}
	componentLibrary := []execute.ComponentInfo{comp}
	var buf bytes.Buffer
	GenerateComponentLib(&buf, componentLibrary, "", "main")
	buf.WriteString("invalid tokens at end of program")
	_, err := parser.ParseFile(fset, "", buf.String(), parser.AllErrors)
	if err == nil {
		t.Error("expected illegal program")
	}
}
开发者ID:cgravill,项目名称:antha,代码行数:12,代码来源:antha_test.go

示例12: parseFunc

func parseFunc(filename, functionname string) (fun *ast.FuncDecl, fset *token.FileSet) {
	fset = token.NewFileSet()
	if file, err := parser.ParseFile(fset, filename, nil, 0); err == nil {
		for _, d := range file.Decls {
			if f, ok := d.(*ast.FuncDecl); ok && f.Name.Name == functionname {
				fun = f
				return
			}
		}
	}
	panic("function not found")
}
开发者ID:cgravill,项目名称:antha,代码行数:12,代码来源:example_test.go

示例13: TestBadNodes

// Verify that the printer doesn't crash if the AST contains BadXXX nodes.
func TestBadNodes(t *testing.T) {
	const src = "package p\n("
	const res = "package p\nBadDecl\n"
	f, err := parser.ParseFile(fset, "", src, parser.ParseComments)
	if err == nil {
		t.Error("expected illegal program") // error in test
	}
	var buf bytes.Buffer
	Fprint(&buf, fset, f)
	if buf.String() != res {
		t.Errorf("got %q, expected %q", buf.String(), res)
	}
}
开发者ID:cgravill,项目名称:antha,代码行数:14,代码来源:printer_test.go

示例14: TestX

// TextX is a skeleton test that can be filled in for debugging one-off cases.
// Do not remove.
func TestX(t *testing.T) {
	const src = `
package p
func _() {}
`
	// parse original
	f, err := parser.ParseFile(fset, "src", src, parser.ParseComments)
	if err != nil {
		t.Fatal(err)
	}

	// pretty-print original
	var buf bytes.Buffer
	if err = (&Config{Mode: UseSpaces, Tabwidth: 8}).Fprint(&buf, fset, f); err != nil {
		t.Fatal(err)
	}

	// parse pretty printed original
	if _, err := parser.ParseFile(fset, "", buf.Bytes(), 0); err != nil {
		t.Fatalf("%s\n%s", err, buf.Bytes())
	}

}
开发者ID:cgravill,项目名称:antha,代码行数:25,代码来源:nodes_test.go

示例15: xTestBadNodes

// Verify that the printer doesn't crash if the AST contains BadXXX nodes.
// XXX(ddn): test disabled because additional boilerplate throws off
// comparisons
func xTestBadNodes(t *testing.T) {
	const src = "package p\n("
	//	const res = "package p\nBadDecl\n"
	const res = "package p\n\nimport \"github.com/antha-lang/antha/antha/execute\"\nimport \"github.com/Synthace/goflow\"\nimport \"sync\"\nimport \"encoding/json\"\n//import \"log\"\n//import \"bytes\"\n//import \"io\"\n\n\n\nBadDecl\n// AsyncBag functions\nfunc (e *P) Complete(params interface{}) {\n\tp := params.(PParamBlock)\n\tif p.Error {\n\n\t\treturn\n\t}\n\tr := new(PResultBlock)\n\te.startup.Do(func() { e.setup(p) })\n\te.steps(p, r)\n\tif r.Error {\n\n\t\treturn\n\t}\n\n\te.analysis(p, r)\n\t\tif r.Error {\n\n\n\t\treturn\n\t}\n\n\te.validation(p, r)\n\t\tif r.Error {\n\n\t\treturn\n\t}\n\n}\n\n// empty function for interface support\nfunc (e *P) anthaElement() {}\n\n// init function, read characterization info from seperate file to validate ranges?\nfunc (e *P) init() {\n\te.params = make(map[execute.ThreadID]*execute.AsyncBag)\n}\n\nfunc NewP() interface{} {//*P {\n\te := new(P)\n\te.init()\n\treturn e\n}\n\n// Mapper function\nfunc (e *P) Map(m map[string]interface{}) interface{} {\n\tvar res PParamBlock\n\tres.Error = false \n\n\n\treturn res\n}\n\n\ntype P struct {\n\tflow.Component                    // component \"superclass\" embedded\n\tlock           sync.Mutex\n\tstartup        sync.Once\n\tparams         map[execute.ThreadID]*execute.AsyncBag\n}\n\ntype PParamBlock struct{\n\tID\t\texecute.ThreadID\n\tError\tbool\n}\ntype PResultBlock struct{\n\tID\t\texecute.ThreadID\n\tError\tbool\n}\ntype PJSONBlock struct{\n\tID\t\t\t*execute.ThreadID\n\tError\t\t*bool\n}\n" //TODO review the intention of this test
	f, err := parser.ParseFile(fset, "", src, parser.ParseComments)
	if err == nil {
		t.Error("expected illegal program") // error in test
	}
	var buf bytes.Buffer
	Fprint(&buf, fset, f)
	if buf.String() != res {
		t.Errorf("got %q, expected %q", buf.String(), res)
	}
}
开发者ID:cgravill,项目名称:antha,代码行数:17,代码来源:nodes_test.go


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