cznic / goyacc

github.com/cznic/goyacc has moved to modernc.org/goyacc
https://godoc.org/modernc.org/goyacc
BSD 3-Clause "New" or "Revised" License
208 stars 24 forks source link

RFE: Token dumper in the generated Go. #30

Open eric-s-raymond opened 6 years ago

eric-s-raymond commented 6 years ago

I feel as though I should not have had to write this:

// Tokname returns the string name of a token type (for testing purposes).
func Tokname(i int) string {
    if i < 128 {
        return string([]byte{byte(i & 0xff)})
    }
    return cparseTokname(i - 57342)
}

That magic number is particularly annoying.

Would you please make something equivalent a public method?

cznic commented 6 years ago

It's available as [prefix]Symname here: https://github.com/cznic/goyacc/blob/fceb47475462725e1e3e19538ba062e87ca4d902/main.go#L566

The prefix defaults to yy. Most of the time the generated parser is not imported, but part of the package where it's used. To change the prefix and make it optionally an "exporting" one, use the -p flag as in, for example, -p MyParser.

Would that handle your use case?

cznic commented 6 years ago

Also, FTR, I've started the process of migrating my repositories away from Github, please consider using the new "canonical" path modernc.org/goyacc in your project(s).

eric-s-raymond commented 6 years ago

I'm confused. Nothing with a name containing SymName exists in my generated parser code. I don't have a main.go. How do I get to that function?

eric-s-raymond commented 6 years ago

I'm fetching from golang.org/x/tools/cmd/goyacc

Should I expect that to continue to work?

cznic commented 6 years ago

golang.org/x/tools/cmd/goyacc is not the same tool as github.com/cznic/goyacc nor modernc.org/goyacc.

The tool in this repository has a lot of enhacements wrt golang.org/x/tools/cmd/goyacc.

I'm just leaving home, will get back to you later this evening (CET).

eric-s-raymond commented 6 years ago

I've installed the modernc version.

cznic commented 6 years ago

Ok, the main.go is the source of the tool per se. The earlier link into main go shows the embedded template. The produced parser goes to where the -y flag directs, the decaukt is y.go. Pretty much what the real unix yacc does.

So the yySymName function is to be found in the y.go file if you're using the defaults.

If your project is available somewhere for me to look at, I may be able to provide more useful hints.

eric-s-raymond commented 6 years ago

Here it is:

https://gitlab.com/esr/cgoparse

Note that I'm using the -o option in my go generate command; expect to find the generated grammar in src/cparse/c-grammar.go.

You will need to do "make setup" before "make".

cznic commented 6 years ago

Thanks for the link. Your project seems to at least partaally overlap in functionality with https://godoc.org/modernc.org/cc/v2 (hosted at https://gitlab.com/cznic/cc/v2). You might want to take a look at it. If you do so, please provide feedback if you may possibly use the existing cc/v2 package. I'd be happy to welcome you as another author/contributor of the project.

The core function of interest is https://godoc.org/modernc.org/cc/v2#Translate.

Currently comments are not preserved, but that's easy to add. Actually, the v1.0.0 did that, so it's only a question of porting the relevant code also to v2.0.0.

edit: s/cc/cc\/v2/

eric-s-raymond commented 6 years ago

Very interesting. Yes, cooperating with you on this looks like it would save me a lot of work.

I actually have a use in mind for the AST that I have not talked about because I don't want to imply any promises in public that I can't keep. Plesae email me at esr@thyrus.com so I can brief you and we can figure out what needs to be finished to acieve both our objectives.

cznic commented 6 years ago

Plesae email me at esr@thyrus.com ...

@eric-s-raymond

I tried about 10 minutes ago, but:

The response from the remote server was:

550 5.1.1 esr@thyrus.com Recipient not found. http://x.co/irbounce

eric-s-raymond commented 5 years ago

You emailed me while my house net was screwed up by the aftermath of a power outage. Took me a week to recover - kept getting false clues because my downstairs Ethernet switch lost its marbles.

You should be able to email me now.