This repository has been archived by the owner on Apr 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
194 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Compiled Object files, Static and Dynamic libs (Shared Objects) | ||
|
||
.idea/ | ||
|
||
*.o | ||
*.a | ||
*.so | ||
|
||
# Folders | ||
_obj | ||
_test | ||
|
||
# Architecture specific extensions/prefixes | ||
*.[568vq] | ||
[568vq].out | ||
|
||
*.cgo1.go | ||
*.cgo2.c | ||
_cgo_defun.c | ||
_cgo_gotypes.go | ||
_cgo_export.* | ||
|
||
_testmain.go | ||
|
||
*.exe | ||
*.test | ||
*.prof |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package patcher_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/lysu/go-struct-patcher" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestAll(t *testing.T) { | ||
|
||
assert := assert.New(t) | ||
|
||
testPath := "abc.r1.d" | ||
|
||
tok, err := patcher.Lex(testPath) | ||
assert.Nil(err) | ||
|
||
assert.Len(tok, 5) | ||
|
||
patcher := patcher.NewParser(tok) | ||
assert.NotNil(patcher) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
package patcher | ||
|
||
type Parser struct { | ||
idx int | ||
tokens []*Token | ||
lastToken *Token | ||
} | ||
|
||
func NewParser(tokens []*Token) *Parser { | ||
p := &Parser{tokens: tokens} | ||
if len(tokens) > 0 { | ||
p.lastToken = tokens[len(tokens)-1] | ||
} | ||
return p | ||
} | ||
|
||
func (p *Parser) Consume() { | ||
p.ConsumeN(1) | ||
} | ||
|
||
func (p *Parser) ConsumeN(count int) { | ||
p.idx += count | ||
} | ||
|
||
func (p *Parser) Current() *Token { | ||
return p.Get(p.idx) | ||
} | ||
|
||
func (p *Parser) MatchType(typ TokenType) *Token { | ||
if t := p.PeekType(typ); t != nil { | ||
p.Consume() | ||
return t | ||
} | ||
return nil | ||
} | ||
|
||
func (p *Parser) Match(typ TokenType, val string) *Token { | ||
if t := p.Peek(typ, val); t != nil { | ||
p.Consume() | ||
return t | ||
} | ||
return nil | ||
} | ||
|
||
func (p *Parser) MatchOne(typ TokenType, vals ...string) *Token { | ||
for _, val := range vals { | ||
if t := p.Peek(typ, val); t != nil { | ||
p.Consume() | ||
return t | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (p *Parser) PeekType(typ TokenType) *Token { | ||
return p.PeekTypeN(0, typ) | ||
} | ||
|
||
func (p *Parser) Peek(typ TokenType, val string) *Token { | ||
return p.PeekN(0, typ, val) | ||
} | ||
|
||
func (p *Parser) PeekOne(typ TokenType, vals ...string) *Token { | ||
for _, v := range vals { | ||
t := p.PeekN(0, typ, v) | ||
if t != nil { | ||
return t | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (p *Parser) PeekN(shift int, typ TokenType, val string) *Token { | ||
t := p.Get(p.idx + shift) | ||
if t != nil { | ||
if t.Typ == typ && t.Val == val { | ||
return t | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (p *Parser) PeekTypeN(shift int, typ TokenType) *Token { | ||
t := p.Get(p.idx + shift) | ||
if t != nil { | ||
if t.Typ == typ { | ||
return t | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (p *Parser) Remaining() int { | ||
return len(p.tokens) - p.idx | ||
} | ||
|
||
func (p *Parser) Count() int { | ||
return len(p.tokens) | ||
} | ||
|
||
func (p *Parser) Get(i int) *Token { | ||
if i < len(p.tokens) { | ||
return p.tokens[i] | ||
} | ||
return nil | ||
} | ||
|
||
func (p *Parser) GetR(shift int) *Token { | ||
i := p.idx + shift | ||
return p.Get(i) | ||
} | ||
|
||
func (p *Parser) Error(msg string, token *Token) *Error { | ||
if token == nil { | ||
// Set current token | ||
token = p.Current() | ||
if token == nil { | ||
// Set to last token | ||
if len(p.tokens) > 0 { | ||
token = p.tokens[len(p.tokens)-1] | ||
} | ||
} | ||
} | ||
var line, col int | ||
if token != nil { | ||
line = token.Line | ||
col = token.Col | ||
} | ||
return &Error{ | ||
Line: line, | ||
Column: col, | ||
Token: token, | ||
ErrorMsg: msg, | ||
} | ||
} |