alecthomas / participle

A parser library for Go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

structs can only be parsed with @@ or by implementing the Capture or encoding.TextUnmarshaler interfaces

ay0ks opened this issue · comments

Hello, I've tried rewriting my old ebnf grammar to participle, and I'm getting this error:

panic: Blocks: Expression: Instruction: Arguments: Value: Multiply: structs can only be parsed with @@ or by implementing the Capture or encoding.TextUnmarshaler interfaces

Code:

type Value struct {
	Register      *Register   `@@`
	Number        *Number     `| @@`
	Symbol        *Symbol     `| @@`
	String        *String     `| @@`
	Dollar        *Dollar     `| @@`
	Subexpression *Expression `| "(" @@ ")"`
}

type (
	OrExpr struct {
		AndExpr *AndExpr `@@`
		Or      *OrExpr  `| "||" @@`
	}

	AndExpr struct {
		NotExpr *NotExpr `@@`
		And     *AndExpr `| "&&" @@`
	}

	NotExpr struct {
		ComparisonExpr *ComparisonExpr `@@`
		Not            *NotExpr        `| ("!" | "not") @@`
	}

	ComparisonExpr struct {
		BitwiseExpr               *BitwiseExpr    `@@`
		ComparisonEquals          *ComparisonExpr `| @@ "==" @BitwiseExpr`
		ComparisonNotEquals       *ComparisonExpr `| @@ "!=" @BitwiseExpr`
		ComparisonLess            *ComparisonExpr `| @@ "<" @BitwiseExpr`
		ComparisonGreater         *ComparisonExpr `| @@ ">" @BitwiseExpr`
		ComparisonLessOrEquals    *ComparisonExpr `| @@ "<=" @BitwiseExpr`
		ComparisonGreaterOrEquals *ComparisonExpr `| @@ ">=" @BitwiseExpr`
		ComparisonIn              *ComparisonExpr `| @@ "in" @BitwiseExpr`
	}

	BitwiseExpr struct {
		ShiftExpr  *ShiftExpr   `@@`
		BitwiseAnd *BitwiseExpr `| @@ "&" @ShiftExpr`
		BitwiseOr  *BitwiseExpr `| @@ "|" @ShiftExpr`
		BitwiseXor *BitwiseExpr `| @@ "^" @ShiftExpr`
	}

	ShiftExpr struct {
		ArithExpr  *ArithExpr `@@`
		RightShift *ShiftExpr `| @@ ">>" @ArithExpr`
		LeftShift  *ShiftExpr `| @@ "<<" @ArithExpr`
	}

	ArithExpr struct {
		Term *Term      `@@`
		Add  *ArithExpr `| @@ "+" @Term`
		Sub  *ArithExpr `| @@ "-" @Term`
	}

	Term struct {
		Factor   *Factor `@@`
		Multiply *Term   `| @@ "*" @Factor`
		Divide   *Term   `| @@ "*" @Factor`
	}

	Factor struct {
		Atom      *Value `@@`
		UnaryAdd  *Value `| "+" @@`
		UnarySub  *Value `| "-" @@`
		UnarySwap *Value `| "~" @@`
	}
)

(Currently using only up to Term)

I assume it's because of @ in the names of structs, but it will be nice to see how to implement this Capture function.

Thanks!