Exemplo n.º 1
0
func TestParsecBasic(t *testing.T) {
	g := NewGispWith(
		map[string]Toolbox{
			"axiom": Axiom, "props": Propositions, "time": Time},
		map[string]Toolbox{"time": Time, "p": Parsec})

	digit := p.Bind(p.Many1(p.Digit), p.ReturnString)
	data := "344932454094325"
	state := p.MemoryParseState(data)
	pre, err := digit(state)
	if err != nil {
		t.Fatalf("except \"%v\" pass test many1 digit but error:%v", data, err)
	}

	src := "(let ((st (p.state \"" + data + `")))
    (var data ((p.many1 p.digit) st))
    (p.s2str data))
    `
	gre, err := g.Parse(src)
	if err != nil {
		t.Fatalf("except \"%v\" pass gisp many1 digit but error:%v", src, err)
	}
	t.Logf("from gisp: %v", gre)
	t.Logf("from parsec: %v", pre)
	if !reflect.DeepEqual(pre, gre) {
		t.Fatalf("except got \"%v\" from gisp equal \"%v\" from parsec", gre, pre)
	}
}
Exemplo n.º 2
0
func TestParsecRune2(t *testing.T) {
	g := NewGispWith(
		map[string]Toolbox{
			"axiom": Axiom, "props": Propositions, "time": Time},
		map[string]Toolbox{"time": Time, "p": Parsec})
	//data := "Here is a Rune : 'a' and a is't a rune. It is a word in sentence."
	data := "'a' and a is't a rune. It is a word in sentence."
	state := p.MemoryParseState(data)
	pre, err := p.Between(p.Rune('\''), p.Rune('\''), p.AnyRune)(state)
	if err != nil {
		t.Fatalf("except found rune expr from \"%v\" but error:%v", data, err)
	}
	src := `
	(let ((st (p.state "` + data + `")))
		((p.rune '\'') st)
		(var data (p.anyone st))
		((p.rune '\'') st)
		data)
	`

	//fmt.Println(src)
	gre, err := g.Parse(src)
	if err != nil {
		t.Fatalf("except \"%v\" pass gisp '<rune>' but error:%v", src, err)
	}
	t.Logf("from gisp: %v", gre)
	t.Logf("from parsec: %v", pre)
	if !reflect.DeepEqual(pre, gre) {
		t.Fatalf("except got \"%v\" from gisp equal \"%v\" from parsec", gre, pre)
	}
}
Exemplo n.º 3
0
func TestIntParser3(t *testing.T) {
	data := "3.14"
	st := p.MemoryParseState(data)
	o, err := p.Bind_(IntParser, p.Eof)(st)
	if err == nil {
		t.Fatalf("except a Float parse error but got %v", o)
	}
}
Exemplo n.º 4
0
func TestFloatParser5(t *testing.T) {
	data := "234"
	st := p.MemoryParseState(data)
	o, err := FloatParser(st)
	if err == nil {
		t.Fatalf("except a Float parse error but got %v", o)
	}
}
Exemplo n.º 5
0
func TestIntParser1(t *testing.T) {
	data := "i234"
	st := p.MemoryParseState(data)
	o, err := IntParser(st)
	if err == nil {
		t.Fatalf("except a Int parse error but got %v", o)
	}
}
Exemplo n.º 6
0
func TestDotParser(t *testing.T) {
	data := "now.Year"
	st := p.MemoryParseState(data)
	re, err := p.Bind(AtomParser, DotSuffixParser)(st)
	if err != nil {
		t.Fatalf("except a Dot but error %v", err)
	}
	t.Log(re)
}
Exemplo n.º 7
0
Arquivo: atom.go Projeto: gale320/gisp
func atomNameParser(st p.ParseState) (interface{}, error) {
	ret, err := p.Bind(p.Many1(p.NoneOf("'[]() \t\r\n\".:")),
		p.ReturnString)(st)
	if err != nil {
		return nil, err
	}
	test := p.MemoryParseState(ret.(string))
	_, err = p.Bind_(p.Many1(p.Digit), p.Eof)(test)
	if err == nil {
		return nil, fmt.Errorf("atom name can't be a int like %s", ret.(string))
	}
	return ret, nil
}
Exemplo n.º 8
0
func TestAtomParse0(t *testing.T) {
	data := "x"
	state := p.MemoryParseState(data)
	a, err := AtomParser(state)
	if err == nil {
		test := Atom{"x", Type{ANY, false}}
		if !reflect.DeepEqual(test, a) {
			t.Fatalf("except Atom{\"x\", ANY} but %v", a)
		}
	} else {
		t.Fatalf("except Atom{\"x\", ANY} but %v", err)
	}
}
Exemplo n.º 9
0
func TestingBoolParse0(t *testing.T) {
	data := "true"
	st := p.MemoryParseState(data)
	o, err := BoolParser(st)
	if err != nil {
		t.Fatalf("except bool but error %v", err)
	}
	if b, ok := o.(Bool); ok {
		if !b {
			t.Fatalf("except bool true but %v", b)
		}
	} else {
		t.Fatalf("excpet bool but %v", o)
	}
}
Exemplo n.º 10
0
func TestFloatParser0(t *testing.T) {
	data := "0.012"
	st := p.MemoryParseState(data)
	o, err := FloatParser(st)
	if err != nil {
		t.Fatalf("except a Float but error %v", err)
	}
	if f, ok := o.(Float); ok {
		if f != Float(0.012) {
			t.Fatalf("except a Float 0.012 but %v", f)
		}
	} else {
		t.Fatalf("except Float but %v", o)
	}
}
Exemplo n.º 11
0
func TestFloatParser1(t *testing.T) {
	data := "3.1415926"
	st := p.MemoryParseState(data)
	o, err := FloatParser(st)
	if err != nil {
		t.Fatalf("except a Float but error %v", err)
	}
	if f, ok := o.(Float); ok {
		if f != Float(3.1415926) {
			t.Fatalf("except a Float 3.1415926 but %v", f)
		}
	} else {
		t.Fatalf("except Float but %v", o)
	}
}
Exemplo n.º 12
0
func TestIntParser0(t *testing.T) {
	data := "12"
	st := p.MemoryParseState(data)
	o, err := IntParser(st)
	if err != nil {
		t.Fatalf("except a Int but error %v", err)
	}
	if i, ok := o.(Int); ok {
		if i != Int(12) {
			t.Fatalf("except a Int 12 but %v", i)
		}
	} else {
		t.Fatalf("except Int but %v", o)
	}
}
Exemplo n.º 13
0
// Parse 解释执行一段文本
func (gisp *Gisp) Parse(code string) (interface{}, error) {
	st := p.MemoryParseState(code)
	var v interface{}
	var e error
	for {
		Skip(st)
		_, err := p.Eof(st)
		if err == nil {
			break
		}
		value, err := ValueParserExt(gisp)(st)
		if err != nil {
			return nil, err
		}
		switch lisp := value.(type) {
		case Lisp:
			v, e = lisp.Eval(gisp)
		default:
			v = lisp
			e = nil
		}
	}
	return v, e
}
Exemplo n.º 14
0
	Meta: map[string]interface{}{
		"name":     "parsex",
		"category": "package",
	},
	Content: map[string]interface{}{
		"state": func(env Env, args ...interface{}) (Lisp, error) {
			if len(args) != 1 {
				return nil, ParsexSignErrorf("Parsex Arg Error:except args has 1 arg.")
			}
			param, err := Eval(env, args[0])
			if err != nil {
				return nil, err
			}
			switch data := param.(type) {
			case string:
				return Q(p.MemoryParseState(data)), nil
			default:
				return nil, fmt.Errorf("Parsex Error: Except create a state from a string or List but %v", data)
			}
		},
		"s2str": func(env Env, args ...interface{}) (Lisp, error) {
			if len(args) != 1 {
				return nil, ParsexSignErrorf("Slice to string Arg Error:except args has 1 arg.")
			}
			param, err := Eval(env, args[0])
			if err != nil {
				return nil, err
			}
			var (
				slice []interface{}
				ok    bool