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) } }
func compare(st px.ParsexState) (interface{}, error) { l, err := px.Bind(px.Choice( px.Bind(IntValue, LessThanNumber), px.Bind(NumberValue, LessThanFloat), px.Bind(px.StringVal, LessThanString), px.Bind(TimeValue, LessThanTime), ), func(l interface{}) px.Parser { return func(st px.ParsexState) (interface{}, error) { _, err := px.Eof(st) if err != nil { return nil, ParsexSignErrorf("less args sign error: except eof") } return l, nil } })(st) if err == nil { return l, nil } return nil, ParsexSignErrorf("Except two lessable values compare but error %v", err) }
// return false, true or nil func lessOption(env Env) px.Parser { return func(st px.ParsexState) (interface{}, error) { l, err := px.Bind(px.Choice( px.Try(px.Bind(IntValue, LessThanNumber)), px.Try(px.Bind(NumberValue, LessThanFloat)), px.Try(px.Bind(px.StringVal, LessThanString)), px.Try(px.Bind(TimeValue, LessThanTime)), px.Try(px.Bind(ListValue, LessThanListOption(env))), px.Bind(px.AnyOne, LessThanNil), ), func(l interface{}) px.Parser { return func(st px.ParsexState) (interface{}, error) { _, err := px.Eof(st) if err != nil { return nil, ParsexSignErrorf("less args sign error: except eof") } return l, nil } })(st) if err == nil { return l, nil } return nil, ParsexSignErrorf("Except two lessable values or nil compare but error: %v", err) } }
func notEquals(st px.ParsexState) (interface{}, error) { return px.Bind(px.AnyOne, neqs)(st) }
func equalsOption(st px.ParsexState) (interface{}, error) { return px.Bind(px.AnyOne, eqsOption)(st) }
return nil, err } if y == nil { return false, nil } if !reflect.DeepEqual(x, y) { return true, nil } } } var String2Values = px.Bind(StringValue, func(x interface{}) px.Parser { return func(st px.ParsexState) (interface{}, error) { y, err := StringValue(st) if err != nil { return nil, err } return []interface{}{x, y}, nil } }) var Time2Values = px.Bind(TimeValue, func(x interface{}) px.Parser { return func(st px.ParsexState) (interface{}, error) { y, err := TimeValue(st) if err != nil { return nil, err } return []interface{}{x, y}, nil } })
case float32: return Float(val), nil case float64: return Float(val), nil case Float: return val, nil default: return nil, NotNumberError{v} } } var Int2Values = px.Bind(IntValue, func(x interface{}) px.Parser { return func(st px.ParsexState) (interface{}, error) { y, err := IntValue(st) if err != nil { return nil, err } return []interface{}{x, y}, nil } }) var Num2Values = px.Bind(NumberValue, func(x interface{}) px.Parser { return func(st px.ParsexState) (interface{}, error) { y, err := NumberValue(st) if err != nil { return nil, err } return []interface{}{x, y}, nil } })
prs, err := Eval(env, args[0]) if err != nil { return nil, err } var parser Parsexer var ok bool if parser, ok = prs.(Parsexer); !ok { return nil, ParsexSignErrorf("Bind Args Error:except first arg is a parsexer.") } f, err := Eval(env, args[1]) if err != nil { return nil, err } switch fun := f.(type) { case func(interface{}) px.Parser: return ParsexBox(px.Bind(parser.Parser, fun)), nil case Functor: return ParsexBox(px.Bind(parser.Parser, func(x interface{}) px.Parser { tasker, err := fun.Task(env, x) if err != nil { return func(st px.ParsexState) (interface{}, error) { return nil, err } } pr, err := tasker.Eval(env) if err != nil { return func(st px.ParsexState) (interface{}, error) { return nil, err } } switch parser := pr.(type) {