Exemple #1
0
func TestFilePosition(t *testing.T) {
	var tests = []struct {
		col, row int
		pos      token.Pos
	}{
		{1, 1, token.Pos(1)},
		{1, 2, token.Pos(8)},
		{7, 2, token.Pos(14)},
	}
	f := token.NewFile("", 1, 15)
	f.AddLine(0)
	p := f.Position(token.Pos(1))
	if p.String() != "1:1" {
		t.Fatal("Nameless file: Expected: 1:1, Got:", p.String())
	}

	f = token.NewFile("test.calc", 1, 15)
	f.AddLine(0)
	p = f.Position(token.Pos(1))
	if p.String() != "test.calc:1:1" {
		t.Fatal("Nameless file: Expected: test.calc:1:1, Got:", p.String())
	}

	f = token.NewFile("test", 1, len(test_expr))
	f.AddLine(0)
	f.AddLine(6)
	for _, v := range tests {
		p := f.Position(v.pos)
		if p.Col != v.col || p.Row != v.row {
			t.Fatal("For:", v.pos, "Expected:", v.col, "and", v.row, "Got:",
				p.Col, "and", p.Row)
		}
	}
}
Exemple #2
0
func TestBasicLit(t *testing.T) {
	b := &ast.BasicLit{
		LitPos: token.Pos(1),
		Kind:   token.INTEGER,
		Lit:    "24",
	}
	pos, end := token.Pos(1), token.Pos(3)
	if b.Pos() != pos || b.End() != end {
		t.Fatal("Expected:", pos, end, "Got:", b.Pos(), b.End())
	}
}
Exemple #3
0
func TestBinaryExpr(t *testing.T) {
	// (+ 3 5)
	x := &ast.BasicLit{
		LitPos: token.Pos(4),
		Kind:   token.INTEGER,
		Lit:    "3",
	}
	y := &ast.BasicLit{
		LitPos: token.Pos(6),
		Kind:   token.INTEGER,
		Lit:    "5",
	}
	b := &ast.BinaryExpr{
		Expression: ast.Expression{
			Opening: token.Pos(1),
			Closing: token.Pos(7),
		},
		Op:    token.ADD,
		OpPos: token.Pos(2),
		List:  []ast.Expr{x, y},
	}

	if b.Pos() != token.Pos(1) {
		t.Fatal("BinaryExpr: Expected: 1 Got:", b.Pos())
	}
	if b.End() != token.Pos(7) {
		t.Fatal("BinaryExpr: Expected: 7 Got:", b.End())
	}
}
Exemple #4
0
func (s *Scanner) next() {
	s.ch = rune(0)
	if s.roffset < len(s.src) {
		s.offset = s.roffset
		s.ch = rune(s.src[s.offset])
		if s.ch == '\n' || s.offset >= len(s.src)-1 {
			s.file.AddLine(token.Pos(s.offset))
		}
		s.roffset++
	}
}
Exemple #5
0
func (i *Ident) End() token.Pos      { return i.NamePos + token.Pos(len(i.Name)) }
Exemple #6
0
func (b *BasicLit) End() token.Pos   { return b.LitPos + token.Pos(len(b.Lit)) }