Skip to content

Commit

Permalink
Merge pull request #10 from yuuki/fix/cannot-open-include
Browse files Browse the repository at this point in the history
Fix wrong include file tracking raised by #9
  • Loading branch information
yuuki authored Mar 30, 2017
2 parents 87f7b6f + 9680e59 commit 111da7e
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions parser/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,12 @@ var SYMBOL_TABLES = map[string]int{
}

type Lexer struct {
scanner scanner.Scanner
tokens []int
pos int
filename string
e error
scanner scanner.Scanner
tokens []int
pos int
filename string
curFilename string
e error
}

type Error struct {
Expand All @@ -198,6 +199,7 @@ func NewLexer(src io.Reader, filename string) *Lexer {
lex.scanner.IsIdentRune = isIdentRune
lex.tokens = []int{}
lex.filename = filename
lex.curFilename = filename
return &lex
}

Expand Down Expand Up @@ -234,7 +236,7 @@ func (l *Lexer) scanInclude(rawfilename string) error {
return err
}

baseDir := filepath.Dir(l.filename)
baseDir := filepath.Dir(l.curFilename)
os.Chdir(baseDir)
defer os.Chdir(curDir)

Expand All @@ -247,16 +249,23 @@ func (l *Lexer) scanInclude(rawfilename string) error {
log.Infof("warning: %s: No such file or directory", rawfilename)
}

prevScanner := l.scanner
defer func() { l.scanner = prevScanner }()
prevFilename := l.curFilename
defer func() { l.curFilename = prevFilename }()

for _, rawpath := range rawpaths {
relpath := filepath.Join(baseDir, rawpath)
l.filename = relpath
log.Verbosef("--> Parsing ... %s\n", relpath)
l.curFilename = rawpath
log.Verbosef("--> Parsing ... %s\n", rawpath)

f, err := os.Open(rawpath)
if err != nil {
return err
}

l.scanner.Init(f)
l.scanner.Mode &^= scanner.ScanInts | scanner.ScanFloats | scanner.ScanChars | scanner.ScanRawStrings | scanner.ScanComments | scanner.SkipComments
l.scanner.IsIdentRune = isIdentRune
l.tokenize()

f.Close()
Expand Down Expand Up @@ -345,7 +354,7 @@ func (l *Lexer) tokenize() {

func (l *Lexer) Error(msg string) {
l.e = &Error{
Filename: l.filename,
Filename: l.curFilename,
Line: l.scanner.Line,
Column: l.scanner.Column,
Message: msg,
Expand Down

0 comments on commit 111da7e

Please sign in to comment.