diff --git a/languages/python/python_base_lexer.go b/languages/python/python_base_lexer.go index a9438e4635a6e7fef49951f4602a3a67b6fc06ff..108215f39ba5ace54fb9867a93fa1c51904abbc5 100644 --- a/languages/python/python_base_lexer.go +++ b/languages/python/python_base_lexer.go @@ -1,6 +1,16 @@ package parser -import "github.com/antlr/antlr4/runtime/Go/antlr" +import ( + "github.com/antlr/antlr4/runtime/Go/antlr" + "github.com/phodal/coca/pkg/infrastructure/container" +) + +var TabSize = 8 +var indents *container.Stack + +func init() { + indents = container.NewStack() +} type PythonBaseLexer struct { *antlr.BaseLexer @@ -12,20 +22,43 @@ type PythonBaseLexer struct { lastToken antlr.Token useStrictDefault bool useStrictCurrent bool + + _opened int + buffer antlr.Token } -func (l *PythonBaseLexer) IncIndentLevel() { +func (l *PythonBaseLexer) Emit() antlr.Token { + emit := l.BaseLexer.Emit() + return emit +} +func (l *PythonBaseLexer) IncIndentLevel() { + l._opened++ } func (l *PythonBaseLexer) DecIndentLevel() { - + if l._opened > 0 { + l._opened-- + } } func (l *PythonBaseLexer) HandleNewLine() { } +func (l *PythonBaseLexer) NextToken() antlr.Token { + if l.GetInputStream().LA(1) == antlr.TokenEOF && indents.Len() > 0 { + + } + + next := l.BaseLexer.NextToken() // Get next token + if next.GetChannel() == antlr.TokenDefaultChannel { + // Keep track of the last token on default channel + l.lastToken = next + } + return next +} + func (l *PythonBaseLexer) HandleSpaces() { } diff --git a/pkg/infrastructure/xmlparse/coll_stack.go b/pkg/infrastructure/container/coll_stack.go similarity index 61% rename from pkg/infrastructure/xmlparse/coll_stack.go rename to pkg/infrastructure/container/coll_stack.go index bf81bb0e92fb941be4f57c469133eb85c1b12ca8..d2d604bc3b355f26cc50fab6c29d62024b8cb16f 100644 --- a/pkg/infrastructure/xmlparse/coll_stack.go +++ b/pkg/infrastructure/container/coll_stack.go @@ -1,27 +1,27 @@ -package xmlparse +package container import ( l "container/list" "sync" ) -type stack struct { +type Stack struct { list *l.List mu sync.Mutex } -func NewStack() *stack { +func NewStack() *Stack { list := l.New() - return &stack{list: list,} + return &Stack{list: list,} } -func (s *stack) Push(t interface{}){ +func (s *Stack) Push(t interface{}){ s.mu.Lock() defer s.mu.Unlock() s.list.PushFront(t) } -func (s *stack) Pop() interface{} { +func (s *Stack) Pop() interface{} { s.mu.Lock() defer s.mu.Unlock() ele := s.list.Front() @@ -33,17 +33,17 @@ func (s *stack) Pop() interface{} { return nil } -func (s *stack) Peak() interface{} { +func (s *Stack) Peak() interface{} { s.mu.Lock() defer s.mu.Unlock() ele := s.list.Front() return ele.Value } -func (s *stack) Len() int { +func (s *Stack) Len() int { return s.list.Len() } -func (s *stack) IsEmpty() bool { +func (s *Stack) IsEmpty() bool { return s.list.Len() == 0 } diff --git a/pkg/infrastructure/xmlparse/xml_parser.go b/pkg/infrastructure/xmlparse/xml_parser.go index 7bd26d521f899fe8becf76a5e2c447743493e66f..bf835f5b867aade8649f16fded02fdf17317dc60 100644 --- a/pkg/infrastructure/xmlparse/xml_parser.go +++ b/pkg/infrastructure/xmlparse/xml_parser.go @@ -2,6 +2,7 @@ package xmlparse import ( "encoding/xml" + "github.com/phodal/coca/pkg/infrastructure/container" "io" "strings" ) @@ -29,7 +30,7 @@ func ParseXML(r io.Reader) *XMLNode { parser := xml.NewDecoder(r) var root XMLNode - st := NewStack() + st := container.NewStack() for { token, err := parser.Token() if err != nil { diff --git a/trial/pkg/application/pyapp/py_ident_app_test.go b/trial/pkg/application/pyapp/py_ident_app_test.go index 5fd2e27c1a107ad089c4a94c10ec1302fde77c7d..2d011275bcbea9ba486bb34f99c01af85cc0fe65 100644 --- a/trial/pkg/application/pyapp/py_ident_app_test.go +++ b/trial/pkg/application/pyapp/py_ident_app_test.go @@ -2,6 +2,7 @@ package pyapp import ( . "github.com/onsi/gomega" + "io/ioutil" "testing" ) @@ -10,7 +11,9 @@ func Test_TypeScriptConsoleLog(t *testing.T) { g := NewGomegaWithT(t) app := new(PythonApiApp) - app.Analysis("print('console.log')", "") + + file, _ := ioutil.ReadFile("testdata/grammar/class_or_func_def_stmt.py") + app.Analysis(string(file), "") g.Expect(1).To(Equal(1)) } diff --git a/trial/pkg/application/pyapp/testdata/grammar/class_or_func_def_stmt.py b/trial/pkg/application/pyapp/testdata/grammar/class_or_func_def_stmt.py new file mode 100755 index 0000000000000000000000000000000000000000..9d066a3b46b73cac29b706d91e009bc63818d85a --- /dev/null +++ b/trial/pkg/application/pyapp/testdata/grammar/class_or_func_def_stmt.py @@ -0,0 +1,12 @@ +# class_or_func_def_stmt: decorator+ (classdef | funcdef); + +# decorator classdef +@decorator +class foo: + pass + +# decorator decorator funcdef +@accepts(int,int) +@returns(float) +def bar(low,high): + pass diff --git a/trial/pkg/ast/pyast/python_ident_listener.go b/trial/pkg/ast/pyast/python_ident_listener.go index 8015565010d1deb557af4c8f8e30fd64fa9ad614..4e450a1a31df431f4262e58e9f395822640ef106 100644 --- a/trial/pkg/ast/pyast/python_ident_listener.go +++ b/trial/pkg/ast/pyast/python_ident_listener.go @@ -17,3 +17,8 @@ func (s *PythonIdentListener) EnterRoot(ctx *parser.RootContext) { fmt.Println(ctx) } +func (s *PythonIdentListener) EnterSingle_input(ctx *parser.Single_inputContext) { + +} + +