Beispiel #1
0
// Parse 分析JSON源码
func Parse(code string) (interface{}, error) {
	st := p.BasicStateFromText(code)
	var v interface{}
	var e error

	for {
		Skip(&st)
		_, err := p.Try(p.EOF)(&st)
		if err == nil {
			break
		}

		value, err := ValueParser()(&st)
		if err != nil {
			return nil, err
		}

		switch vv := value.(type) {
		default:
			v = vv
			e = nil
		}
	}
	return v, e
}
Beispiel #2
0
func TestFloatParser5(t *testing.T) {
	data := "234"
	st := p.BasicStateFromText(data)
	o, err := FloatParser(&st)
	if err == nil {
		t.Fatalf("expect a Float parse error but got %v", o)
	}
}
Beispiel #3
0
func TestIntParser3(t *testing.T) {
	data := "3.14"
	st := p.BasicStateFromText(data)
	o, err := p.P(IntParser).Then(p.EOF)(&st)
	if err == nil {
		t.Fatalf("expect a Float parse error but got %v", o)
	}
}
Beispiel #4
0
func TestIntParser1(t *testing.T) {
	data := "i234"
	st := p.BasicStateFromText(data)
	o, err := IntParser(&st)
	if err == nil {
		t.Fatalf("expect a Int parse error but got %v", o)
	}
}
Beispiel #5
0
func TestBracketParser(t *testing.T) {
	data := "dict[\"meta\"]"
	st := p.BasicStateFromText(data)
	re, err := p.P(AtomParser).Bind(BracketSuffixParser)(&st)
	if err != nil {
		t.Fatalf("expect a Dot but error %v", err)
	}
	t.Log(re)
}
Beispiel #6
0
func TestDotParser(t *testing.T) {
	data := "now.Year"
	st := p.BasicStateFromText(data)
	re, err := p.P(AtomParser).Bind(DotSuffixParser)(&st)
	if err != nil {
		t.Fatalf("expect a Dot but error %v", err)
	}
	t.Log(re)
}
Beispiel #7
0
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
	})
}
Beispiel #8
0
func TestAtomParse0(t *testing.T) {
	data := "x"
	state := p.BasicStateFromText(data)
	a, err := AtomParser(&state)
	if err == nil {
		test := Atom{"x", Type{ANY, false}}
		if !reflect.DeepEqual(test, a) {
			t.Fatalf("expect Atom{\"x\", ANY} but %v", a)
		}
	} else {
		t.Fatalf("expect Atom{\"x\", ANY} but %v", err)
	}
}
Beispiel #9
0
func TestAtomParse1(t *testing.T) {
	data := "x::atom"
	state := p.BasicStateFromText(data)
	a, err := AtomParser(&state)
	if err == nil {
		test := Atom{"x", Type{ATOM, false}}
		d := a.(Atom)
		if !reflect.DeepEqual(test, d) {
			t.Fatalf("expect Atom{\"x\", ATOM} but {Name:%v, Type:%v}", d.Name, d.Type)
		}
	} else {
		t.Fatalf("expect Atom{\"x\", ATOM} but error %v", err)
	}
}
Beispiel #10
0
func TestBoolParse0(t *testing.T) {
	data := "true"
	st := p.BasicStateFromText(data)
	o, err := BoolParser(&st)
	if err != nil {
		t.Fatalf("expect bool but error %v", err)
	}
	if b, ok := o.(Bool); ok {
		if !b {
			t.Fatalf("expect bool true but %v", b)
		}
	} else {
		t.Fatalf("excpet bool but %v", o)
	}
}
Beispiel #11
0
func TestFloatParser0(t *testing.T) {
	data := "0.012"
	st := p.BasicStateFromText(data)
	o, err := FloatParser(&st)
	if err != nil {
		t.Fatalf("expect a Float but error %v", err)
	}
	if f, ok := o.(Float); ok {
		if f != Float(0.012) {
			t.Fatalf("expect a Float 0.012 but %v", f)
		}
	} else {
		t.Fatalf("expect Float but %v", o)
	}
}
Beispiel #12
0
func TestFloatParser1(t *testing.T) {
	data := "3.1415926"
	st := p.BasicStateFromText(data)
	o, err := FloatParser(&st)
	if err != nil {
		t.Fatalf("expect a Float but error %v", err)
	}
	if f, ok := o.(Float); ok {
		if f != Float(3.1415926) {
			t.Fatalf("expect a Float 3.1415926 but %v", f)
		}
	} else {
		t.Fatalf("expect Float but %v", o)
	}
}
Beispiel #13
0
func TestIntParser0(t *testing.T) {
	data := "12"
	st := p.BasicStateFromText(data)
	o, err := IntParser(&st)
	if err != nil {
		t.Fatalf("expect a Int but error %v", err)
	}
	if i, ok := o.(Int); ok {
		if i != Int(12) {
			t.Fatalf("expect a Int 12 but %v", i)
		}
	} else {
		t.Fatalf("expect Int but %v", o)
	}
}
Beispiel #14
0
// Parse 解释执行一段文本
func (gisp *Gisp) Parse(code string) (interface{}, error) {
	st := p.BasicStateFromText(code)
	var v interface{}
	var e error
	for {
		Skip(&st)
		_, err := p.Try(p.EOF)(&st)
		if err == nil {
			break
		}
		value, err := ValueParserExt(gisp)(&st)
		if err != nil {
			return nil, err
		}
		switch lisp := value.(type) {
		case Lisp:
			v, e = lisp.Eval(gisp)
		default:
			v = lisp
			e = nil
		}
	}
	return v, e
}
Beispiel #15
0
// NewStringState 构造一个新的基于字符串的 state
func NewStringState(data string) p.State {
	re := p.BasicStateFromText(data)
	return &re
}