Exemple #1
0
func TestParsexBasic(t *testing.T) {
	g := NewGispWith(
		map[string]Toolbox{
			"axiom": Axiom, "props": Propositions, "time": Time},
		map[string]Toolbox{"time": Time, "px": Parsex})

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

	src := "(let ((st (px.state \"" + data + `")))
	(var data ((px.many1 px.digit) st))
	(px.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 parsex: %v", pxre)
	if !reflect.DeepEqual(pxre, gre) {
		t.Fatalf("except got \"%v\" from gisp equal \"%v\" from parsex", gre, pxre)
	}
}
Exemple #2
0
func mrmul() mrMul {
	mchecker := func(st px.ParsexState) (interface{}, error) {
		x, err := st.Next(px.Always)
		if err != nil {
			return nil, err
		}
		switch m := x.(type) {
		case money:
			return m, nil
		default:
			return nil, moneySignError(x)
		}
	}
	fchecker := func(st px.ParsexState) (interface{}, error) {
		x, err := st.Next(px.Always)
		if err != nil {
			return nil, err
		}
		switch f := x.(type) {
		case Float:
			return f, nil
		default:
			return nil, floatSignError(x)
		}
	}
	return mrMul{px.Union(mchecker, px.Many1(fchecker), px.Eof)}
}
Exemple #3
0
				return nil, ParsexSignErrorf(
					"Many Arg Error:except 1 parser arg but %v.",
					reflect.TypeOf(param))
			}
		},
		"many1": func(env Env, args ...interface{}) (Lisp, error) {
			if len(args) != 1 {
				return nil, ParsexSignErrorf("Parsex Parser Many1 Erroparserr: only accept one parsex parser as arg but %v", args)
			}
			param, err := Eval(env, args[0])
			if err != nil {
				return nil, err
			}
			switch parser := param.(type) {
			case Parsexer:
				return ParsexBox(px.Many1(parser.Parser)), nil
			default:
				return nil, ParsexSignErrorf(
					"Many1 Arg Error:except 1 parser arg but %v.",
					reflect.TypeOf(param))
			}
		},
		"many": func(env Env, args ...interface{}) (Lisp, error) {
			if len(args) != 1 {
				return nil, ParsexSignErrorf("Parsex Parser Many Error: only accept one parsex parser as arg but %v", args)
			}
			param, err := Eval(env, args[0])
			if err != nil {
				return nil, err
			}
			switch parser := param.(type) {