feat: [python] change grammer for better fetch value

上级 83e06482
......@@ -144,7 +144,7 @@ simple_stmt
small_stmt
: testlist_star_expr assign_part? #expr_stmt
| {p.CheckVersion(2)}? PRINT ((test (COMMA test)* COMMA?)
| RIGHT_SHIFT test ((COMMA test)+ COMMA?)) {p.SetVersion(2);} #print_stmt // Python 2
| RIGHT_SHIFT test ((COMMA test)+ COMMA?)) {p.SetVersion(2);} #print_stmt // Python 2
| DEL exprlist #del_stmt
| PASS #pass_stmt
| BREAK #break_stmt
......@@ -153,14 +153,16 @@ small_stmt
| RAISE (test (COMMA test (COMMA test)?)?)? (FROM test)? #raise_stmt
| yield_expr #yield_stmt
| IMPORT dotted_as_names #import_stmt
| FROM ((DOT | ELLIPSIS)* dotted_name | (DOT | ELLIPSIS)+)
IMPORT (STAR | OPEN_PAREN import_as_names CLOSE_PAREN | import_as_names) #from_stmt
| FROM from_stmt_source IMPORT from_stmt_as_names #from_stmt
| GLOBAL name (COMMA name)* #global_stmt
| {p.CheckVersion(2)}? EXEC expr (IN test (COMMA test)?)? {p.SetVersion(2);} #exec_stmt // Python 2
| {p.CheckVersion(2)}? EXEC expr (IN test (COMMA test)?)? {p.SetVersion(2);} #exec_stmt // Python 2
| ASSERT test (COMMA test)? #assert_stmt
| {p.CheckVersion(3)}? NONLOCAL name (COMMA name)* {p.SetVersion(3);} #nonlocal_stmt // Python 3
| {p.CheckVersion(3)}? NONLOCAL name (COMMA name)* {p.SetVersion(3);} #nonlocal_stmt // Python 3
;
from_stmt_as_names : (STAR | OPEN_PAREN import_as_names CLOSE_PAREN | import_as_names);
from_stmt_source : ((DOT | ELLIPSIS)* dotted_name | (DOT | ELLIPSIS)+) ;
testlist_star_expr
: ((test | star_expr) COMMA)+ (test | star_expr)?
| testlist
......
此差异已折叠。
......@@ -273,6 +273,18 @@ func (s *BasePythonParserListener) EnterNonlocal_stmt(ctx *Nonlocal_stmtContext)
// ExitNonlocal_stmt is called when production nonlocal_stmt is exited.
func (s *BasePythonParserListener) ExitNonlocal_stmt(ctx *Nonlocal_stmtContext) {}
// EnterFrom_stmt_as_names is called when production from_stmt_as_names is entered.
func (s *BasePythonParserListener) EnterFrom_stmt_as_names(ctx *From_stmt_as_namesContext) {}
// ExitFrom_stmt_as_names is called when production from_stmt_as_names is exited.
func (s *BasePythonParserListener) ExitFrom_stmt_as_names(ctx *From_stmt_as_namesContext) {}
// EnterFrom_stmt_source is called when production from_stmt_source is entered.
func (s *BasePythonParserListener) EnterFrom_stmt_source(ctx *From_stmt_sourceContext) {}
// ExitFrom_stmt_source is called when production from_stmt_source is exited.
func (s *BasePythonParserListener) ExitFrom_stmt_source(ctx *From_stmt_sourceContext) {}
// EnterTestlist_star_expr is called when production testlist_star_expr is entered.
func (s *BasePythonParserListener) EnterTestlist_star_expr(ctx *Testlist_star_exprContext) {}
......
......@@ -134,6 +134,12 @@ type PythonParserListener interface {
// EnterNonlocal_stmt is called when entering the nonlocal_stmt production.
EnterNonlocal_stmt(c *Nonlocal_stmtContext)
// EnterFrom_stmt_as_names is called when entering the from_stmt_as_names production.
EnterFrom_stmt_as_names(c *From_stmt_as_namesContext)
// EnterFrom_stmt_source is called when entering the from_stmt_source production.
EnterFrom_stmt_source(c *From_stmt_sourceContext)
// EnterTestlist_star_expr is called when entering the testlist_star_expr production.
EnterTestlist_star_expr(c *Testlist_star_exprContext)
......@@ -368,6 +374,12 @@ type PythonParserListener interface {
// ExitNonlocal_stmt is called when exiting the nonlocal_stmt production.
ExitNonlocal_stmt(c *Nonlocal_stmtContext)
// ExitFrom_stmt_as_names is called when exiting the from_stmt_as_names production.
ExitFrom_stmt_as_names(c *From_stmt_as_namesContext)
// ExitFrom_stmt_source is called when exiting the from_stmt_source production.
ExitFrom_stmt_source(c *From_stmt_sourceContext)
// ExitTestlist_star_expr is called when exiting the testlist_star_expr production.
ExitTestlist_star_expr(c *Testlist_star_exprContext)
......
......@@ -99,6 +99,7 @@ func Test_PythonImport(t *testing.T) {
file, _ := ioutil.ReadFile("testdata/grammar/import_stmt.py")
codeFile := app.Analysis(string(file), "import_stmt")
fmt.Println(codeFile.Imports)
g.Expect(len(codeFile.Imports)).To(Equal(8))
g.Expect(len(codeFile.Imports[2].UsageName)).To(Equal(2))
g.Expect(len(codeFile.Imports[2].UsageName)).To(Equal(10))
}
......@@ -53,15 +53,11 @@ func (s *PythonIdentListener) EnterImport_stmt(ctx *parser.Import_stmtContext) {
}
func (s *PythonIdentListener) EnterFrom_stmt(ctx *parser.From_stmtContext) {
if ctx.Dotted_name() != nil {
asNameText := ctx.Dotted_name().GetText()
codeImport := &trial.CodeImport{
Source: asNameText,
AsName: "",
}
codeImport := &trial.CodeImport{}
codeImport.Source = ctx.From_stmt_source().GetText()
codeImport.UsageName = append(codeImport.UsageName, ctx.From_stmt_as_names().GetText())
currentCodeFile.Imports = append(currentCodeFile.Imports, *codeImport)
}
currentCodeFile.Imports = append(currentCodeFile.Imports, *codeImport)
}
func (s *PythonIdentListener) EnterClassdef(ctx *parser.ClassdefContext) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册