Welcome to participle Discussions! #126
Replies: 1 comment 2 replies
-
Hi, I am Thomas. Have recently seen your parser implemented with go. Actually I have to cope with ldap based search filters and found your parser interesting. I am going to use this parser to interpret search filters but I think I haven't fully got how to approach this. LDAP FilterAn ldap filter may look like this but could also be more complex using
Approach to ParseI have created the struct below to parse the filter but I immediately recognized that this is not working because
Struct // Ldapfilters
type Ldapfilters struct {
Filter []*Ldapfilter `@@*`
}
// Ldapfilter
type Ldapfilter struct {
Filtertype string `"(" @("&" | "|")`
Properties []*LdapProperty `@@* ")"`
}
// LdapProperty
type LdapProperty struct {
Key string `"(" @Ident "="`
Val string `@Ident")"`
} Using Custom Lexer DefinitionI tried to use a custom lever but I am not able to set it up properly. I am getting a compile error if I am getting a compile error when I do call participle.Build() or participle.MustBuild() - lever is not accepted. I think it is a type mismatch. // LdapProperty
type EmailProperty struct {
Key string `"(" @Ident "="`
Val string `@MyVal")"`
}
func TestEmailToken(t *testing.T) {
basicLexer, err := stateful.NewSimple([]stateful.Rule{
{"Comment", `(?i)rem[^\n]*`, nil},
{"String", `"(\\"|[^"])*"`, nil},
{"Number", `[-+]?(\d*\.)?\d+`, nil},
{"Ident", `[a-zA-Z_.@]\w*`, nil},
{"MyVal", `[a-zA-Z_][a-zA-Z0-9_.@]{0,}`, nil},
{"Punct", `[-[!@#$%^&*()+_={}\|:;"'<,>.?/]|]`, nil},
{"EOL", `[\n\r]+`, nil},
{"whitespace", `[ \t]+`, nil},
})
p3 := "(mail=a.b@cde.fg)"
parser, err := participle.Build(
&EmailProperty{},
participle.Lexer(basicLexer),
)
ep := &EmailProperty{}
err = parser.ParseString(p3, ep) Probably someone can enlighten me :) |
Beta Was this translation helpful? Give feedback.
-
👋 Welcome!
We’re using Discussions as a place to connect with other members of our community. We hope that you:
build together 💪.
To get started, comment below with an introduction of yourself and tell us about what you do with this community.
Beta Was this translation helpful? Give feedback.
All reactions