php-parser/lexer.go

107 lines
1.7 KiB
Go
Raw Normal View History

2017-11-07 06:21:38 +00:00
package main
import (
"bufio"
2017-12-03 18:49:18 +00:00
"go/token"
2017-11-07 06:21:38 +00:00
"io"
2017-12-03 07:17:05 +00:00
"unicode"
2017-11-07 06:21:38 +00:00
"github.com/cznic/golex/lex"
)
// Allocate Character classes anywhere in [0x80, 0xFF].
const (
classUnicodeLeter = iota + 0x80
classUnicodeDigit
classOther
)
type lexer struct {
*lex.Lexer
2017-12-01 14:04:53 +00:00
stateStack []int
2017-12-03 07:17:05 +00:00
lineNumber int
}
func rune2Class(r rune) int {
if r >= 0 && r < 0x80 { // Keep ASCII as it is.
return int(r)
}
if unicode.IsLetter(r) {
return classUnicodeLeter
}
if unicode.IsDigit(r) {
return classUnicodeDigit
}
// return classOther
return -1
2017-11-07 06:21:38 +00:00
}
2017-12-01 16:04:31 +00:00
func newLexer(src io.Reader, fName string) *lexer {
2017-12-03 18:49:18 +00:00
file := token.NewFileSet().AddFile(fName, -1, 1<<31-1)
2017-11-07 06:21:38 +00:00
lx, err := lex.New(file, bufio.NewReader(src), lex.RuneClass(rune2Class))
if err != nil {
panic(err)
}
2017-12-03 07:17:05 +00:00
return &lexer{lx, []int{0}, 1}
2017-11-07 06:21:38 +00:00
}
2017-12-01 14:04:53 +00:00
func (l *lexer) ungetN(n int) []byte {
2017-11-07 06:21:38 +00:00
l.Unget(l.Lookahead())
chars := l.Token()
2017-12-01 14:04:53 +00:00
for i := 1; i <= n; i++ {
char := chars[len(chars)-i]
l.Unget(char)
2017-11-07 06:21:38 +00:00
}
buf := l.TokenBytes(nil)
2017-12-01 14:04:53 +00:00
buf = buf[:len(buf)-n]
2017-11-07 06:21:38 +00:00
return buf
}
2017-12-01 14:04:53 +00:00
func (l *lexer) pushState(state int) {
l.stateStack = append(l.stateStack, state)
}
2017-11-12 11:13:31 +00:00
2017-12-01 14:04:53 +00:00
func (l *lexer) popState() {
len := len(l.stateStack)
if len <= 1 {
return
2017-11-12 11:13:31 +00:00
}
2017-12-01 14:04:53 +00:00
l.stateStack = l.stateStack[:len-1]
}
2017-11-12 11:13:31 +00:00
2017-12-01 14:04:53 +00:00
func (l *lexer) begin(state int) {
len := len(l.stateStack)
l.stateStack = l.stateStack[:len-1]
l.stateStack = append(l.stateStack, state)
}
func (l *lexer) getCurrentState() int {
return l.stateStack[len(l.stateStack)-1]
2017-11-12 11:13:31 +00:00
}
2017-12-03 07:17:05 +00:00
2017-12-03 16:36:34 +00:00
func (l *lexer) handleNewLine(tokenBytes []byte) ([]byte, int, int) {
2017-12-03 07:17:05 +00:00
startln := l.lineNumber
var prev byte
2017-12-03 16:36:34 +00:00
for _, b := range tokenBytes {
2017-12-03 07:17:05 +00:00
if b == '\n' || prev == '\r' {
l.lineNumber++
}
prev = b
}
// handle last \r
if prev == '\r' {
l.lineNumber++
}
2017-12-03 16:36:34 +00:00
return tokenBytes, startln, l.lineNumber
2017-12-03 07:17:05 +00:00
}