Beispiel #1
0
				return nil, fmt.Errorf(
					"Manybe Arg Error:expect 1 parser arg but %v.",
					reflect.TypeOf(param))
			}
		},
		"skip": func(env Env, args ...interface{}) (Lisp, error) {
			if len(args) != 1 {
				return nil, fmt.Errorf("Parsex Parser Skip Error: only accept one parsex 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, fmt.Errorf(
					"Skip Arg Error:expect 1 parser arg but %v.",
					reflect.TypeOf(param))
			}
		},
	},
}

// NewStringState 构造一个新的基于字符串的 state
func NewStringState(data string) p.State {
	re := p.BasicStateFromText(data)
	return &re
}
Beispiel #2
0
package gisp

import (
	"fmt"
	"reflect"
	"strconv"

	p "github.com/Dwarfartisan/goparsec2"
)

// Ext 扩展表示扩展环境

// Skip 忽略匹配指定算子的内容
var Skip = p.Skip(p.Space)

// IntParser 解析整数
func IntParser(st p.State) (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.Do(func(st p.State) interface{} {
Beispiel #3
0
package pjson

import (
	"fmt"
	"reflect"

	p "github.com/Dwarfartisan/goparsec2"
)

// Skip 忽略匹配指定算子的内容
var Skip = p.Skip(p.Space)

// Comma 逗号分隔符
var Comma = p.Skip(p.Str(","))

// Colon 冒号分隔符
var Colon = p.Skip(p.Str(":"))

func listBodyParser(st p.State) (interface{}, error) {
	value, err := p.SepBy1(ValueParser(), Comma)(st)
	fmt.Printf("type :%v, value :%v, err :%v\n", reflect.TypeOf(value), value, err)
	return value, err
}

// 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)