package gisp import ( "fmt" "reflect" "strconv" p "github.com/Dwarfartisan/goparsec" ) var Space = p.Space var Skip = p.Skip(Space) // IntParser 解析整数 func IntParser(st p.ParseState) (interface{}, error) { i, err := p.Int(st) if err == nil { val, err := strconv.Atoi(i.(string)) if err == nil { return Int(val), nil } return nil, err } return nil, err } // 用于string var EscapeChars = p.Bind_(p.Rune('\\'), func(st p.ParseState) (interface{}, error) { r, err := p.OneOf("nrt\"\\")(st) if err == nil {
return nil, ParsexSignErrorf( "Manybe Arg Error:except 1 parser arg but %v.", reflect.TypeOf(param)) } }, "skip": func(env Env, args ...interface{}) (Lisp, error) { if len(args) != 1 { return nil, ParsexSignErrorf("Parsec Parser Skip Error: only accept one parsec 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.Skip(parser.Parser)), nil default: return nil, ParsexSignErrorf( "Skip Arg Error:except 1 parser arg but %v.", reflect.TypeOf(param)) } }, }, } // Parsecer 定了一个对 Parsec 解释器的封装 type Parsecer struct { Parser p.Parser } // Task 实现了其求值逻辑