Browse Source

fix up indentation levels

master
Brett Langdon 10 years ago
parent
commit
7abed6705a
1 changed files with 60 additions and 8 deletions
  1. +60
    -8
      scanner/scanner.go

+ 60
- 8
scanner/scanner.go View File

@ -48,6 +48,10 @@ func NewScanner(r io.Reader) *Scanner {
} }
} }
func (scanner *Scanner) State() errorcode.ErrorCode {
return scanner.state
}
func (scanner *Scanner) nextPosition() *Position { func (scanner *Scanner) nextPosition() *Position {
if len(scanner.positionBuffer) > 0 { if len(scanner.positionBuffer) > 0 {
last := len(scanner.positionBuffer) - 1 last := len(scanner.positionBuffer) - 1
@ -213,6 +217,7 @@ func (scanner *Scanner) unreadToken(tok *token.Token) {
} }
func (scanner *Scanner) NextToken() *token.Token { func (scanner *Scanner) NextToken() *token.Token {
next_line:
if len(scanner.tokenBuffer) > 0 { if len(scanner.tokenBuffer) > 0 {
last := len(scanner.tokenBuffer) - 1 last := len(scanner.tokenBuffer) - 1
nextToken := scanner.tokenBuffer[last] nextToken := scanner.tokenBuffer[last]
@ -246,7 +251,8 @@ func (scanner *Scanner) NextToken() *token.Token {
if pos.Char == '#' || pos.Char == '\n' { if pos.Char == '#' || pos.Char == '\n' {
// Lines with only newline or comment, shouldn't affect indentation // Lines with only newline or comment, shouldn't affect indentation
if col == 0 && pos.Char == '\n' {
// TODO: Handle prompt
if col == 0 && pos.Char == '\n' && false {
blankline = false blankline = false
} else { } else {
blankline = true blankline = true
@ -255,14 +261,39 @@ func (scanner *Scanner) NextToken() *token.Token {
if !blankline && scanner.indentationLevel == 0 { if !blankline && scanner.indentationLevel == 0 {
if col == scanner.indentationStack[scanner.indentationCurrent] { if col == scanner.indentationStack[scanner.indentationCurrent] {
if altcol != scanner.indentationAltStack[scanner.indentationCurrent] { if altcol != scanner.indentationAltStack[scanner.indentationCurrent] {
return positions.AsToken(token.ERRORTOKEN)
pos = scanner.currentPosition
return &token.Token{
ID: token.ERRORTOKEN,
LineStart: pos.Line,
ColumnStart: pos.Column,
LineEnd: pos.Line,
ColumnEnd: pos.Column,
Literal: "",
}
} }
} else if col > scanner.indentationStack[scanner.indentationCurrent] { } else if col > scanner.indentationStack[scanner.indentationCurrent] {
if scanner.indentationCurrent+1 >= MAXINDENT { if scanner.indentationCurrent+1 >= MAXINDENT {
return positions.AsToken(token.ERRORTOKEN)
scanner.state = errorcode.E_TOODEEP
pos = scanner.currentPosition
return &token.Token{
ID: token.ERRORTOKEN,
LineStart: pos.Line,
ColumnStart: pos.Column,
LineEnd: pos.Line,
ColumnEnd: pos.Column,
Literal: "",
}
} }
if altcol <= scanner.indentationAltStack[scanner.indentationCurrent] { if altcol <= scanner.indentationAltStack[scanner.indentationCurrent] {
return positions.AsToken(token.ERRORTOKEN)
pos = scanner.currentPosition
return &token.Token{
ID: token.ERRORTOKEN,
LineStart: pos.Line,
ColumnStart: pos.Column,
LineEnd: pos.Line,
ColumnEnd: pos.Column,
Literal: "",
}
} }
scanner.indentationPending++ scanner.indentationPending++
scanner.indentationCurrent++ scanner.indentationCurrent++
@ -275,10 +306,28 @@ func (scanner *Scanner) NextToken() *token.Token {
scanner.indentationCurrent-- scanner.indentationCurrent--
} }
if col != scanner.indentationStack[scanner.indentationCurrent] { if col != scanner.indentationStack[scanner.indentationCurrent] {
return positions.AsToken(token.ERRORTOKEN)
scanner.state = errorcode.E_DEDENT
pos = scanner.currentPosition
return &token.Token{
ID: token.ERRORTOKEN,
LineStart: pos.Line,
ColumnStart: pos.Column,
LineEnd: pos.Line,
ColumnEnd: pos.Column,
Literal: "",
}
} }
if altcol != scanner.indentationAltStack[scanner.indentationCurrent] { if altcol != scanner.indentationAltStack[scanner.indentationCurrent] {
return positions.AsToken(token.ERRORTOKEN)
scanner.state = errorcode.E_DEDENT
pos = scanner.currentPosition
return &token.Token{
ID: token.ERRORTOKEN,
LineStart: pos.Line,
ColumnStart: pos.Column,
LineEnd: pos.Line,
ColumnEnd: pos.Column,
Literal: "",
}
} }
} }
} }
@ -387,6 +436,9 @@ func (scanner *Scanner) NextToken() *token.Token {
return positions.AsToken(token.NAME) return positions.AsToken(token.NAME)
case ch == '\n': case ch == '\n':
scanner.atBol = true scanner.atBol = true
if blankline || scanner.indentationCurrent > 0 {
goto next_line
}
return positions.AsToken(token.NEWLINE) return positions.AsToken(token.NEWLINE)
case ch == '.': case ch == '.':
pos2 := scanner.nextPosition() pos2 := scanner.nextPosition()
@ -433,11 +485,11 @@ func (scanner *Scanner) NextToken() *token.Token {
switch pos.Char { switch pos.Char {
case '(', '[', '{': case '(', '[', '{':
// Increment indentation level // Increment indentation level
// scanner.indentationLevel++
scanner.indentationLevel++
break break
case ')', ']', '}': case ')', ']', '}':
// Decrement indentation level // Decrement indentation level
// scanner.indentationLevel--
scanner.indentationLevel--
break break
} }


Loading…
Cancel
Save