func atomNameParser() p.P { return p.Do(func(state p.State) interface{} { ret := p.Many1(p.RuneNone("'[]() \t\r\n\".:")).Bind(p.ReturnString).Exec(state) test := p.BasicStateFromText(ret.(string)) _, err := p.Many1(p.Digit).Then(p.EOF).Parse(&test) if err == nil { p.Fail("atom name can't be a int like %s", ret) } return ret }) }
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.Many1(p.Digit).Bind(p.ReturnString) data := "344932454094325" state := NewStringState(data) pcre, err := digit(state) if err != nil { t.Fatalf("expect \"%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("expect \"%v\" pass gisp many1 digit but error:%v", src, err) } t.Logf("from gisp: %v", gre) t.Logf("from parsec: %v", pcre) if !reflect.DeepEqual(pcre, gre) { t.Fatalf("expect got \"%v\" from gisp equal \"%v\" from parsex", gre, pcre) } }
)(st) if err != nil { return nil, err } return r, nil } func stopWord(x interface{}) p.P { return p.P(stop).Then(p.Return(x)) } func typeName(word string) p.P { return p.Str(word).Bind(stopWord) } var anyType = p.Many1(p.Choice(p.Try(p.Digit), p.Letter)).Bind(stopWord).Bind(p.ReturnString) // SliceTypeParserExt 定义了带环境的序列类型解析逻辑 func SliceTypeParserExt(env Env) p.P { return func(st p.State) (interface{}, error) { t, err := p.Str("[]").Then(ExtTypeParser(env))(st) if err != nil { return nil, err } return reflect.SliceOf(t.(Type).Type), nil } } // MapTypeParserExt 定义了带环境的映射类型解析逻辑 func MapTypeParserExt(env Env) p.P { return func(st p.State) (interface{}, error) {
return nil, fmt.Errorf( "Many Arg Error:expect 1 parser arg but %v.", reflect.TypeOf(param)) } }, "many1": func(env Env, args ...interface{}) (Lisp, error) { if len(args) != 1 { return nil, fmt.Errorf("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 Parsecer: return ParsecBox(p.Many1(parser.Parser)), nil default: return nil, fmt.Errorf( "Many1 Arg Error:expect 1 parser arg but %v.", reflect.TypeOf(param)) } }, "many": func(env Env, args ...interface{}) (Lisp, error) { if len(args) != 1 { return nil, fmt.Errorf("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) {