Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expression using double quotes #417

Closed
bravbravbrav opened this issue Oct 7, 2024 · 3 comments
Closed

Expression using double quotes #417

bravbravbrav opened this issue Oct 7, 2024 · 3 comments

Comments

@bravbravbrav
Copy link

I believe that there might be some issue related to double quotes.

Code:

package main

import (
    "fmt"

    "github.com/alecthomas/participle/v2"
    "github.com/alecthomas/participle/v2/lexer"
)

type Syntax struct {
    Strings []*String `@@*`
}

type String struct {
    Value string `'"' @character '"'`
}

var rules = lexer.MustSimple([]lexer.SimpleRule{
    {`character`, `[^"]`},
})

var parser = participle.MustBuild[Syntax](
    participle.Lexer(rules),
)

func main() {
    fmt.Println(parser.String())
}

Output

panic: String:1:1: Strings: invalid syntax: "\"\"\""

goroutine 1 [running]:
github.com/alecthomas/participle/v2.MustBuild[...](...)
	/tmp/gopath1215137364/pkg/mod/github.com/alecthomas/participle/[email protected]/parser.go:55
main.init()
	/tmp/sandbox1046517504/prog.go:22 +0x112

Expected output

Syntax = String* .
String = '"' <character> '"' .
@alecthomas
Copy link
Owner

You'll need to use:

    Value string `"\"" @character "\""`

This limitation is, IIRC, because of how Participle uses the Go lexer internally.

@bravbravbrav
Copy link
Author

Cool. I recommend documenting this behavior though. Thank you for your work :)

@bravbravbrav
Copy link
Author

@alecthomas I tried to use your suggestion in my code sample, but it still results in error:

Code

package main

import (
	"fmt"
	"os"

	"github.com/alecthomas/participle/v2"
	"github.com/alecthomas/participle/v2/lexer"
)

type Syntax struct {
	Strings []*String `@@*`
}

type String struct {
	Value string `"\"" @character* "\""`
}

var rules = lexer.MustSimple([]lexer.SimpleRule{
	{`character`, `[^"]`},
})

var parser = participle.MustBuild[Syntax](
	participle.Lexer(rules),
)

func main() {
	syntax, err := parser.ParseString("code", `"a"`)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	fmt.Printf("%v\n", syntax)
}

Result

code:1:1: invalid input text "\"a\""

Repository owner locked and limited conversation to collaborators Oct 8, 2024
@alecthomas alecthomas converted this issue into discussion #418 Oct 8, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants