Beispiel #1
0
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
}
Beispiel #2
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)
	}
}
Beispiel #3
0
	)(st)
	if err != nil {
		return nil, err
	}
	return r, nil
}

func stopWord(x interface{}) p.Parser {
	return p.Bind_(stop, p.Return(x))
}

func typeName(word string) p.Parser {
	return p.Bind(p.String(word), stopWord)
}

var anyType = p.Bind(p.Bind(p.Many1(p.Either(p.Try(p.Digit), p.Letter)), stopWord), p.ReturnString)

func SliceTypeParserExt(env Env) p.Parser {
	return func(st p.ParseState) (interface{}, error) {
		t, err := p.Bind_(p.String("[]"), ExtTypeParser(env))(st)
		if err != nil {
			return nil, err
		}
		return reflect.SliceOf(t.(Type).Type), nil
	}
}

func MapTypeParserExt(env Env) p.Parser {
	return func(st p.ParseState) (interface{}, error) {
		key, err := p.Between(p.String("map["), p.Rune(']'), ExtTypeParser(env))(st)
		if err != nil {
Beispiel #4
0
func bodyParser(st p.ParseState) (interface{}, error) {
	value, err := p.SepBy(ValueParser, p.Many1(Space))(st)
	return value, err
}
Beispiel #5
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("Parsec Parser Many1 Erroparserr: 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.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("Parsec Parser Many 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) {