Exemple #1
0
// ListParser 实现数组解析器
func ListParser() p.P {
	return func(st p.State) (interface{}, error) {
		left := p.Chr('[').Then(Skip)
		right := Skip.Then(p.Chr(']'))
		empty := p.Between(left, right, Skip)

		//		list, err := p.Between(left, right, p.UnionAll(listBodyParser))(st)
		list, err := p.Between(left, right, listBodyParser)(st)

		if err != nil {
			_, e := empty(st)
			if e != nil {
				return nil, err
			}
			return List{}, nil
		}

		switch l := list.(type) {
		case List:
			return L(l), nil
		case []interface{}:
			return list.([]interface{}), nil
		default:
			return nil, fmt.Errorf("List Parser Error: %v type is unexpected: %v", list, reflect.TypeOf(list))
		}
	}
}
Exemple #2
0
// ObjectParser 实现数组解析器
func ObjectParser() p.P {
	return func(st p.State) (interface{}, error) {
		left := p.Chr('{').Then(Skip)
		right := Skip.Then(p.Chr('}'))
		empty := p.Between(left, right, Skip)

		object, err := p.Between(left, right, objectBodyParser)(st)
		if err != nil {
			_, e := empty(st)
			if e != nil {
				return nil, err
			}
			return Object{}, nil
		}

		switch o := object.(type) {
		case Object:
			return O(o), nil
		case map[string]interface{}:
			return object.(map[string]interface{}), nil
		default:
			return nil, fmt.Errorf("Object Parser Error: %v type is unexpected: %v", object, reflect.TypeOf(object))
		}

	}
}
Exemple #3
0
func TestParsecRune(t *testing.T) {
	g := NewGispWith(
		map[string]Toolbox{
			"axiom": Axiom, "props": Propositions, "time": Time},
		map[string]Toolbox{"time": Time, "p": Parsec})
	//data := "Here is a Rune : 'a' and a is't a rune. It is a word in sentence."
	data := "'a' and a is't a rune. It is a word in sentence."
	state := NewStringState(data)
	pre, err := p.Between(p.Chr('\''), p.Chr('\''), p.AsRune)(state)
	if err != nil {
		t.Fatalf("expect found rune expr from \"%v\" but error:%v", data, err)
	}
	src := `
	(let ((st (p.state "` + data + `")))
		((p.between (p.rune '\'') (p.rune '\'') p.one) st))
	`
	gre, err := g.Parse(src)
	if err != nil {
		t.Fatalf("expect \"%v\" pass gisp '<rune>' but error:%v", src, err)
	}
	t.Logf("from gisp: %v", gre)
	t.Logf("from parsec: %v", pre)
	if !reflect.DeepEqual(pre, gre) {
		t.Fatalf("expect got \"%v\" from gisp equal \"%v\" from parsec", gre, pre)
	}
}
Exemple #4
0
// MapTypeParser 定义了序列类型解析逻辑
func MapTypeParser(st p.State) (interface{}, error) {
	key, err := p.Between(p.Str("map["), p.Chr(']'), TypeParser)(st)
	if err != nil {
		return nil, err
	}
	value, err := TypeParser(st)
	if err != nil {
		return nil, err
	}
	return reflect.MapOf(key.(Type).Type, value.(Type).Type), nil
}
Exemple #5
0
// MapTypeParserExt  定义了带环境的映射类型解析逻辑
func MapTypeParserExt(env Env) p.P {
	return func(st p.State) (interface{}, error) {
		key, err := p.Between(p.Str("map["), p.Chr(']'), ExtTypeParser(env))(st)
		if err != nil {
			return nil, err
		}
		value, err := ExtTypeParser(env)(st)
		if err != nil {
			return nil, err
		}
		return reflect.MapOf(key.(Type).Type, value.(Type).Type), nil
	}
}
Exemple #6
0
// ListParserExt 实现带扩展的列表解析器
func ListParserExt(env Env) p.P {
	left := p.Chr('(').Then(Skip)
	right := Skip.Then(p.Chr(')'))
	empty := left.Then(right)
	return func(st p.State) (interface{}, error) {
		list, err := p.Try(p.Between(left, right, bodyParserExt(env)))(st)
		if err == nil {
			switch l := list.(type) {
			case List:
				return L(l), nil
			case []interface{}:
				return List(l), nil
			default:
				return nil, fmt.Errorf("List Parser(ext) Error: %v type is unexpected: %v", list, reflect.TypeOf(list))
			}
		} else {
			_, e := empty(st)
			if e == nil {
				return List{}, nil
			}
			return nil, err
		}
	}
}
Exemple #7
0
		return '\t'
	default:
		panic(st.Trap("Unknown escape sequence \\%c", r))
	}
})

// RuneParser 实现 rune 的解析
var RuneParser = p.Do(func(state p.State) interface{} {
	p.Chr('\'').Exec(state)
	c := p.Choice(p.Try(EscapeCharr), p.NChr('\'')).Exec(state)
	p.Chr('\'').Exec(state)
	return Rune(c.(rune))
})

// StringParser 实现字符串解析
var StringParser = p.Between(p.Chr('"'), p.Chr('"'),
	p.Many(p.Choice(p.Try(EscapeChars), p.NChr('"')))).Bind(p.ReturnString)

func bodyParser(st p.State) (interface{}, error) {
	value, err := p.SepBy(ValueParser(), Skip)(st)
	return value, err
}

func bodyParserExt(env Env) p.P {
	return p.Many(ValueParserExt(env).Over(Skip))
}

// ListParser 实现列表解析器
func ListParser() p.P {
	return func(st p.State) (interface{}, error) {
		left := p.Chr('(').Then(Skip)
		right := Skip.Then(p.Chr(')'))
Exemple #8
0
			return ParsecBox(p.OneOf(params...)), nil
		},
		"noneof": func(env Env, args ...interface{}) (Lisp, error) {
			params, err := Evals(env, args...)
			if err != nil {
				return nil, err
			}
			return ParsecBox(p.NoneOf(params)), nil
		},
		"between": func(env Env, args ...interface{}) (Lisp, error) {
			ptype := reflect.TypeOf((*Parsecer)(nil)).Elem()
			params, err := GetArgs(env, p.UnionAll(TypeAs(ptype), TypeAs(ptype), TypeAs(ptype), p.EOF), args)
			if err != nil {
				return nil, err
			}
			return ParsecBox(p.Between(params[0].(Parsecer).Parser, params[1].(Parsecer).Parser, params[2].(Parsecer).Parser)), nil
		},
		"bind": func(env Env, args ...interface{}) (Lisp, error) {
			if len(args) != 2 {
				return nil, fmt.Errorf("Bind Args Error:expect 2 args.")
			}
			prs, err := Eval(env, args[0])
			if err != nil {
				return nil, err
			}
			var parser Parsecer
			var ok bool
			if parser, ok = prs.(Parsecer); !ok {
				return nil, fmt.Errorf("Bind Args Error:expect first arg is a Parsecer.")
			}
			f, err := Eval(env, args[1])
Exemple #9
0
// BracketParserExt 在带有 Ext 的环境下解析中括号表达式
func BracketParserExt(env Env) p.P {
	return p.Between(p.Chr('['), p.Chr(']'),
		p.SepBy1(ValueParserExt(env), p.Chr(':')),
	)
}