up9inc/gopygo

Can't parse valid go file

ont opened this issue · 1 comments

ont commented

Can't parse very simple file (log from ipython session):

In [1]: import gopygo

In [2]: txt = open('/tmp/test.go').read()

In [3]: txt
Out[3]: 'package main\n\nimport (\n\t"log"\n\n\t"project/internal"\n\n\t"github.com/goava/di"\n)\n\nfunc main() {\n\tdi.SetTracer(&di.StdTracer{})\n\n\t_, err := di.New(\n\t\tinternal.ViperModule,\n\t\tinternal.BoilerModule,\n\t\tinternal.IdentModule,\n\t\tinternal.JwtModule,\n\t\tinternal.EchoModule,\n\t)\n\tif err != nil {\n\t\tlog.Fatalln("error:", err)\n\t}\n}\n'

In [4]: print(txt)
package main

import (
	"log"

	"project/internal"

	"github.com/goava/di"
)

func main() {
	di.SetTracer(&di.StdTracer{})

	_, err := di.New(
		internal.ViperModule,
		internal.BoilerModule,
		internal.IdentModule,
		internal.JwtModule,
		internal.EchoModule,
	)
	if err != nil {
		log.Fatalln("error:", err)
	}
}


In [5]: gopygo.parse(txt)
sly: Syntax error at line 1, token=STRING_LITERAL
sly: Syntax error at line 1, token=RPAREN
sly: Syntax error at line 1, token=LBRACE
sly: Syntax error at line 1, token=NEWLINE
sly: Syntax error at line 1, token=IDENT
sly: Syntax error at line 1, token=IDENT
sly: Syntax error at line 1, token=IDENT
sly: Syntax error at line 1, token=IDENT
sly: Syntax error at line 1, token=RPAREN
sly: Syntax error at line 1, token=RBRACE

@ont Hi, thank you so much for reporting this issue. The parser does not cover the whole Go grammar as of its current state. The line number tracking of the parser is also broken. Therefore it's hard to tell, exactly on what line it throws a syntax error. I suspect that, it's _, err := di.New( or the following lines.

The parser is defined in the form of Yacc grammar in this file. It might be possible to identify the issue by simplifying the test case and playing with the grammar rules in that file.