Example #1
0
// SuffixParser 定义了后缀表达式的通用判定
func SuffixParser(prefix interface{}) p.Parser {
	suffix := p.Either(p.Try(DotSuffix(prefix)), BracketSuffix(prefix))
	return func(st p.ParseState) (interface{}, error) {
		s, err := suffix(st)
		if err != nil {
			return prefix, nil
		}
		return SuffixParser(s)(st)
	}
}
Example #2
0
File: type.go Project: zy02636/gisp
func ExtTypeParser(env Env) p.Parser {
	return func(st p.ParseState) (interface{}, error) {
		_, err := p.String("::")(st)
		if err != nil {
			return nil, err
		}
		buildin := p.Choice(
			p.Try(p.Bind_(typeName("bool"), p.Return(BOOL))),
			p.Try(p.Bind_(typeName("float"), p.Return(FLOAT))),
			p.Try(p.Bind_(typeName("int"), p.Return(INT))),
			p.Try(p.Bind_(typeName("string"), p.Return(STRING))),
			p.Try(p.Bind_(typeName("time"), p.Return(TIME))),
			p.Try(p.Bind_(typeName("duration"), p.Return(DURATION))),
			p.Try(p.Bind_(typeName("any"), p.Return(ANY))),
			p.Try(p.Bind_(typeName("atom"), p.Return(ATOM))),
			p.Try(p.Bind_(p.String("list"), p.Return(LIST))),
			p.Try(p.Bind_(typeName("quote"), p.Return(QUOTE))),
			p.Try(p.Bind_(p.String("dict"), p.Return(DICT))),
			p.Try(MapTypeParserExt(env)),
		)
		ext := func(st p.ParseState) (interface{}, error) {
			n, err := anyType(st)
			if err != nil {
				return nil, err
			}
			t, ok := env.Lookup(n.(string))
			if !ok {
				return nil, st.Trap("type %v not found", n)
			}
			if typ, ok := t.(reflect.Type); ok {
				return typ, nil
			} else {
				return nil, st.Trap("var %v is't a type. It is %v", n, reflect.TypeOf(t))
			}
		}
		t, err := p.Either(buildin, ext)(st)
		if err != nil {
			return nil, err
		}
		_, err = p.Try(p.Rune('?'))(st)
		option := err == nil
		return Type{t.(reflect.Type), option}, nil
	}
}
Example #3
0
File: type.go Project: zy02636/gisp
	)(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 {
Example #4
0
			return '\'', nil
		case '\\':
			return '\\', nil
		case 't':
			return '\t', nil
		default:
			return nil, st.Trap("Unknown escape sequence \\%c", r)
		}
	} else {
		return nil, err
	}
})

var RuneParser = p.Bind(
	p.Between(p.Rune('\''), p.Rune('\''),
		p.Either(p.Try(EscapeCharr), p.NoneOf("'"))),
	func(x interface{}) p.Parser {
		return p.Return(Rune(x.(rune)))
	},
)

var StringParser = p.Bind(
	p.Between(p.Rune('"'), p.Rune('"'),
		p.Many(p.Either(p.Try(EscapeChars), p.NoneOf("\"")))),
	p.ReturnString)

func bodyParser(st p.ParseState) (interface{}, error) {
	value, err := p.SepBy(ValueParser, p.Many1(Space))(st)
	return value, err
}
Example #5
0
			case Parsecer:
				return ParsecBox(p.Try(parser.Parser)), nil
			default:
				return nil, ParsexSignErrorf(
					"Try Arg Error:except 1 parser arg but %v.",
					reflect.TypeOf(param))
			}

		},
		"either": func(env Env, args ...interface{}) (Lisp, error) {
			ptype := reflect.TypeOf((px.Parser)(nil))
			params, err := GetArgs(env, px.UnionAll(TypeAs(ptype), TypeAs(ptype), px.Eof), args)
			if err != nil {
				return nil, err
			}
			return ParsecBox(p.Either(params[0].(Parsecer).Parser, params[1].(Parsecer).Parser)), nil
		},
		"choice": func(env Env, args ...interface{}) (Lisp, error) {
			ptype := reflect.TypeOf((px.Parser)(nil))
			params, err := GetArgs(env, px.ManyTil(TypeAs(ptype), px.Eof), args)
			if err != nil {
				return nil, err
			}
			parsers := make([]p.Parser, len(params))
			for idx, prs := range params {
				if parser, ok := prs.(Parsecer); ok {
					parsers[idx] = parser.Parser
				}
				return nil, ParsexSignErrorf("Choice Args Error:except parsec parsers but %v is %v",
					prs, reflect.TypeOf(prs))
			}
Example #6
0
// BracketSuffixParser 定义 bracket 表达式判定
func BracketSuffixParser(x interface{}) p.Parser {
	return p.Either(p.Try(BracketSuffix(x)), p.Return(x))
}