Example #1
0
// 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()
	ast1, err1 := parser.ParseFile(fset, "", src, parser.ParseComments)
	if err1 != nil {
		panic(err1)
	}

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

	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)
	}
}
Example #2
0
func main() {
	/*
	     p1 := &ast.CallExpr {
	   Fun: &ast.Ident {
	     NamePos: 385,
	     Name: "Mul",
	     Obj: nil },
	   Lparen: 388,
	   Args: []ast.Expr {
	     &ast.CallExpr {
	       Fun: &ast.Ident {
	         NamePos: 389,
	         Name: "Const",
	         Obj: nil },
	       Lparen: 394,
	       Args: []ast.Expr {
	         &ast.BasicLit {
	           ValuePos: 395,
	           Kind: token.INT,
	           Value: []uint8 {
	             49,
	             48 } } },
	       Ellipsis: 0,
	       Rparen: 397 },
	     &ast.CallExpr {
	       Fun: &ast.Ident {
	         NamePos: 399,
	         Name: "Const",
	         Obj: nil },
	       Lparen: 404,
	       Args: []ast.Expr {
	         &ast.BasicLit {
	           ValuePos: 405,
	           Kind: token.INT,
	           Value: []uint8 {
	             50,
	             48 } } },
	       Ellipsis: 0,
	       Rparen: 407 } },
	   Ellipsis: 0,
	   Rparen: 408 }
	*/

	// fmt.Printf("%s \n",
	c, _ := parser.ParseExpr(token.NewFileSet(), "temp.go", "Mul(Const(1),x)")
	p := c.(*ast.CallExpr)
	fmt.Print(callToStmt("m", "e", p))
	c2, _ := parser.ParseExpr(token.NewFileSet(), "temp.go", "Mul(x,Const(1))")
	p2 := c2.(*ast.CallExpr)
	callToStmt("m", "e", p2)
	// fmt.Printf"%s \n", p2)
}
Example #3
0
func NewMethod(src string) *FuncDecl {
	fs := token.NewFileSet()
	if d, ok := parser.ParseDeclList(fs, "temp.go", src); ok == nil {
		return d[0].(*FuncDecl)
	}
	return nil
}
Example #4
0
func NewExpr(src string) Expr {
	fs := token.NewFileSet()
	if e, ok := parser.ParseExpr(fs, "temp.go", src); ok == nil {
		return e
	}
	return nil
}
Example #5
0
func NewStmtList(src string) []Stmt {
	fs := token.NewFileSet()
	if e, ok := parser.ParseStmtList(fs, "temp.go", src); ok == nil {
		return e
	}
	return nil
}
Example #6
0
func NewStruct0(src string) *GenDecl {
	fs := token.NewFileSet()
	if d, ok := parser.ParseDeclList(fs, "temp.go", src); ok == nil {
		return d[0].(*GenDecl)
	}
	return nil
}
Example #7
0
func NewStruct(structName string) *GenDecl {
	src := fmt.Sprintf("type %s struct{}", structName)
	fs := token.NewFileSet()
	if d, ok := parser.ParseDeclList(fs, "temp.go", src); ok == nil {
		return d[0].(*GenDecl)
	}
	return nil
}
Example #8
0
func TestPositions(t *testing.T) {
	const delta = 7 // a non-zero base offset increment
	fset := token.NewFileSet()
	for _, test := range tests {
		// verify consistency of test case
		if test.source != nil && len(test.source) != test.size {
			t.Errorf("%s: inconsistent test case: expected file size %d; got %d", test.filename, test.size, len(test.source))
		}

		// add file and verify name and size
		f := fset.AddFile(test.filename, fset.Base()+delta, test.size)
		if f.Name() != test.filename {
			t.Errorf("expected filename %q; got %q", test.filename, f.Name())
		}
		if f.Size() != test.size {
			t.Errorf("%s: expected file size %d; got %d", f.Name(), test.size, f.Size())
		}
		if fset.File(f.Pos(0)) != f {
			t.Errorf("%s: f.Pos(0) was not found in f", f.Name())
		}

		// add lines individually and verify all positions
		for i, offset := range test.lines {
			f.AddLine(offset)
			if f.LineCount() != i+1 {
				t.Errorf("%s, AddLine: expected line count %d; got %d", f.Name(), i+1, f.LineCount())
			}
			// adding the same offset again should be ignored
			f.AddLine(offset)
			if f.LineCount() != i+1 {
				t.Errorf("%s, AddLine: expected unchanged line count %d; got %d", f.Name(), i+1, f.LineCount())
			}
			verifyPositions(t, fset, f, test.lines[0:i+1])
		}

		// add lines with SetLines and verify all positions
		if ok := f.SetLines(test.lines); !ok {
			t.Errorf("%s: SetLines failed", f.Name())
		}
		if f.LineCount() != len(test.lines) {
			t.Errorf("%s, SetLines: expected line count %d; got %d", f.Name(), len(test.lines), f.LineCount())
		}
		verifyPositions(t, fset, f, test.lines)

		// add lines with SetLinesForContent and verify all positions
		src := test.source
		if src == nil {
			// no test source available - create one from scratch
			src = makeTestSource(test.size, test.lines)
		}
		f.SetLinesForContent(src)
		if f.LineCount() != len(test.lines) {
			t.Errorf("%s, SetLinesForContent: expected line count %d; got %d", f.Name(), len(test.lines), f.LineCount())
		}
		verifyPositions(t, fset, f, test.lines)
	}
}
Example #9
0
func TestNoPos(t *testing.T) {
	if token.NoPos.IsValid() {
		t.Errorf("NoPos should not be valid")
	}
	var fset *token.FileSet
	checkPos(t, "nil NoPos", fset.Position(token.NoPos), token.Position{})
	fset = token.NewFileSet()
	checkPos(t, "fset NoPos", fset.Position(token.NoPos), token.Position{})
}
Example #10
0
func main() {

	goopt.Version = fmt.Sprintf("%d.%d", VER_MAJOR, VER_MINOR)
	goopt.Summary = PROG_NAME
	goopt.Parse(nil)

	if *ver {
		fmt.Printf("\n%s version %d.%d", PROG_NAME, VER_MAJOR, VER_MINOR)
		fmt.Printf("\nCopyright (c) 2011 Chanwit Kaewkasi / SUT\n\n")
		return
	}

	var filename string = ""
	if len(goopt.Args) == 1 {
		filename = goopt.Args[0]
	} else {
		fmt.Print(goopt.Usage())
		return
	}

	fset := token.NewFileSet()
	astf, err := parser.ParseFile(fset, filename, nil, 0)
	if err == nil {
		v := NewVisitor(astf)
		Walk(v, astf)
		tempfile, err := os.OpenFile(filename+"k", os.O_WRONLY|os.O_CREATE, 0665)
		if err == nil {
			printer.Fprint(tempfile, fset, astf)
			tempfile.Close()
			newArgs := make([]string, len(os.Args))
			copy(newArgs, os.Args)
			for i, v := range newArgs {
				if v == filename {
					newArgs[i] = filename + "k"
				}
			}
			gc := "8g"
			switch os.Getenv("GOARCH") {
			case "386":
				gc = "8g"
			case "amd64":
				gc = "6g"
			case "arm":
				gc = "5g"
			}
			newArgs[0] = os.Getenv("GOROOT") + "/bin/" + gc
			StdExecve(newArgs, true)
			os.Remove(filename + "k")
		}
	} else {
		fmt.Printf("%s\n", err)
	}
}
Example #11
0
func TestFiles(t *testing.T) {
	fset := token.NewFileSet()
	for i, test := range tests {
		fset.AddFile(test.filename, fset.Base(), test.size)
		j := 0
		for g := range fset.Files() {
			if g.Name() != tests[j].filename {
				t.Errorf("expected filename = %s; got %s", tests[j].filename, g.Name())
			}
			j++
		}
		if j != i+1 {
			t.Errorf("expected %d files; got %d", i+1, j)
		}
	}
}
Example #12
0
func TestLineInfo(t *testing.T) {
	fset := token.NewFileSet()
	f := fset.AddFile("foo", fset.Base(), 500)
	lines := []int{0, 42, 77, 100, 210, 220, 277, 300, 333, 401}
	// add lines individually and provide alternative line information.
	for _, offs := range lines {
		f.AddLine(offs)
		f.AddLineInfo(offs, "bar", 42)
	}
	// verify positions for all offsets
	for offs := 0; offs <= f.Size(); offs++ {
		p := f.Pos(offs)
		_, col := linecol(lines, offs)
		msg := fmt.Sprintf("%s (offs = %d, p = %d)", f.Name(), offs, p)
		checkPos(t, msg, f.Position(f.Pos(offs)), token.Position{"bar", offs, 42, col})
		checkPos(t, msg, fset.Position(p), token.Position{"bar", offs, 42, col})
	}
}
Example #13
0
	"korat/ast"
	"korat/parser"
	"korat/printer"
	"korat/token"
	"path/filepath"
	"testing"
)

const (
	dataDir  = "src/korat/printer/testdata"
	tabwidth = 8
)

var update = flag.Bool("update", false, "update golden files")

var fset = token.NewFileSet()

func lineString(text []byte, i int) string {
	i0 := i
	for i < len(text) && text[i] != '\n' {
		i++
	}
	return string(text[i0:i])
}

type checkMode uint

const (
	export checkMode = 1 << iota
	rawFormat
)