feat: [python] make override success

上级 8d616647
package parser 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 { type PythonBaseLexer struct {
*antlr.BaseLexer *antlr.BaseLexer
...@@ -12,20 +22,43 @@ type PythonBaseLexer struct { ...@@ -12,20 +22,43 @@ type PythonBaseLexer struct {
lastToken antlr.Token lastToken antlr.Token
useStrictDefault bool useStrictDefault bool
useStrictCurrent 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() { func (l *PythonBaseLexer) DecIndentLevel() {
if l._opened > 0 {
l._opened--
}
} }
func (l *PythonBaseLexer) HandleNewLine() { 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() { func (l *PythonBaseLexer) HandleSpaces() {
} }
package xmlparse package container
import ( import (
l "container/list" l "container/list"
"sync" "sync"
) )
type stack struct { type Stack struct {
list *l.List list *l.List
mu sync.Mutex mu sync.Mutex
} }
func NewStack() *stack { func NewStack() *Stack {
list := l.New() 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() s.mu.Lock()
defer s.mu.Unlock() defer s.mu.Unlock()
s.list.PushFront(t) s.list.PushFront(t)
} }
func (s *stack) Pop() interface{} { func (s *Stack) Pop() interface{} {
s.mu.Lock() s.mu.Lock()
defer s.mu.Unlock() defer s.mu.Unlock()
ele := s.list.Front() ele := s.list.Front()
...@@ -33,17 +33,17 @@ func (s *stack) Pop() interface{} { ...@@ -33,17 +33,17 @@ func (s *stack) Pop() interface{} {
return nil return nil
} }
func (s *stack) Peak() interface{} { func (s *Stack) Peak() interface{} {
s.mu.Lock() s.mu.Lock()
defer s.mu.Unlock() defer s.mu.Unlock()
ele := s.list.Front() ele := s.list.Front()
return ele.Value return ele.Value
} }
func (s *stack) Len() int { func (s *Stack) Len() int {
return s.list.Len() return s.list.Len()
} }
func (s *stack) IsEmpty() bool { func (s *Stack) IsEmpty() bool {
return s.list.Len() == 0 return s.list.Len() == 0
} }
...@@ -2,6 +2,7 @@ package xmlparse ...@@ -2,6 +2,7 @@ package xmlparse
import ( import (
"encoding/xml" "encoding/xml"
"github.com/phodal/coca/pkg/infrastructure/container"
"io" "io"
"strings" "strings"
) )
...@@ -29,7 +30,7 @@ func ParseXML(r io.Reader) *XMLNode { ...@@ -29,7 +30,7 @@ func ParseXML(r io.Reader) *XMLNode {
parser := xml.NewDecoder(r) parser := xml.NewDecoder(r)
var root XMLNode var root XMLNode
st := NewStack() st := container.NewStack()
for { for {
token, err := parser.Token() token, err := parser.Token()
if err != nil { if err != nil {
......
...@@ -2,6 +2,7 @@ package pyapp ...@@ -2,6 +2,7 @@ package pyapp
import ( import (
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
"io/ioutil"
"testing" "testing"
) )
...@@ -10,7 +11,9 @@ func Test_TypeScriptConsoleLog(t *testing.T) { ...@@ -10,7 +11,9 @@ func Test_TypeScriptConsoleLog(t *testing.T) {
g := NewGomegaWithT(t) g := NewGomegaWithT(t)
app := new(PythonApiApp) 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)) g.Expect(1).To(Equal(1))
} }
......
# 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
...@@ -17,3 +17,8 @@ func (s *PythonIdentListener) EnterRoot(ctx *parser.RootContext) { ...@@ -17,3 +17,8 @@ func (s *PythonIdentListener) EnterRoot(ctx *parser.RootContext) {
fmt.Println(ctx) fmt.Println(ctx)
} }
func (s *PythonIdentListener) EnterSingle_input(ctx *parser.Single_inputContext) {
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册