1
0
Fork 0
kifflom/lexer/lexer.go

312 lines
6.0 KiB
Go
Raw Normal View History

2015-02-16 17:50:16 +00:00
// This lexer is based on ideas and code presented in the talk by Rob Pike
// called "Lexical Scanning in Go". More info could be found in Golang's Blog:
// http://blog.golang.org/two-go-talks-lexical-scanning-in-go-and
2015-02-12 11:12:19 +00:00
package lexer
import (
"fmt"
2015-02-18 13:26:53 +00:00
2015-02-26 11:02:50 +00:00
"github.com/localhots/kifflom/buffer"
2015-02-12 11:12:19 +00:00
)
type (
2015-02-16 17:50:16 +00:00
// Holds the state of the scanner
2015-02-12 11:12:19 +00:00
Lexer struct {
2015-02-23 13:07:19 +00:00
input *buffer.Buffer
2015-02-18 13:50:55 +00:00
stack []rune // Lexer stack
pos int // Current stack position
lineNum int // Line number
colNum int // Column number
startLine int // Start line of this item
startCol int // Start column of this item
items chan Item // Channel of scanned items
2015-02-12 11:12:19 +00:00
}
2015-02-16 17:50:16 +00:00
// Represents a token returned from the scanner
2015-02-16 08:54:53 +00:00
Item struct {
2015-02-17 17:12:40 +00:00
Token Token // The type of this item
Val string // The value of this item
Line int // Line number
Column int // Column number
2015-02-12 11:12:19 +00:00
}
2015-02-16 17:50:16 +00:00
// Identifies the type of the item
2015-02-18 09:01:18 +00:00
Token byte
2015-02-16 11:18:14 +00:00
2015-02-16 17:50:16 +00:00
// Represents the state of the scanner as a function that returns the next state
2015-02-16 11:18:14 +00:00
stateFn func(*Lexer) stateFn
2015-02-12 11:12:19 +00:00
)
const (
// Special
2015-02-16 17:50:16 +00:00
Error Token = iota
2015-02-16 11:18:14 +00:00
EOF
2015-02-12 11:12:19 +00:00
// Symbols
2015-02-16 11:18:14 +00:00
BraceOpen // {
BraceClose // }
BracketOpen // [
BracketClose // [
Quote // "
Colon // :
Comma // ,
2015-02-12 11:12:19 +00:00
// Types
2015-02-16 17:50:16 +00:00
Null
Bool
Number
String
2015-02-12 11:12:19 +00:00
)
2015-02-18 13:50:55 +00:00
// Creates a new scanner for the input buffer
2015-02-23 13:07:19 +00:00
func New(input *buffer.Buffer) *Lexer {
2015-02-16 17:50:16 +00:00
return &Lexer{
2015-02-17 17:12:23 +00:00
input: input,
2015-02-23 13:07:19 +00:00
items: make(chan Item, 100),
2015-02-17 17:12:23 +00:00
lineNum: 1,
colNum: 0,
2015-02-12 11:12:19 +00:00
}
}
2015-02-16 17:50:16 +00:00
// Starts the state machine for the lexer
2015-02-12 11:12:19 +00:00
func (l *Lexer) Run() {
2015-02-16 17:23:27 +00:00
for state := lexInitial; state != nil; {
state = state(l)
2015-02-12 11:12:19 +00:00
}
2015-02-17 17:26:19 +00:00
close(l.items)
2015-02-12 11:12:19 +00:00
}
2015-02-16 17:50:16 +00:00
// Returns the next scanned item and a boolean, which is false on EOF
2015-02-16 11:18:14 +00:00
func (l *Lexer) NextItem() (item Item, ok bool) {
item, ok = <-l.items
return
2015-02-16 08:54:53 +00:00
}
2015-02-18 13:50:55 +00:00
// Returns the next rune in the stack
2015-02-12 11:12:19 +00:00
func (l *Lexer) next() rune {
2015-02-18 13:26:53 +00:00
var r rune
2015-02-18 13:50:55 +00:00
// Reading next rune from buffer
2015-02-18 13:26:53 +00:00
if l.pos > len(l.stack)-1 {
l.stack = append(l.stack, l.input.Next())
2015-02-12 11:12:19 +00:00
}
2015-02-18 13:26:53 +00:00
r = l.stack[l.pos]
l.pos++
2015-02-17 17:12:23 +00:00
// Counting lines and columns - token coordinates
if r == '\n' {
l.lineNum++
l.colNum = 0
} else {
l.colNum++
}
2015-02-12 11:12:19 +00:00
return r
}
2015-02-16 19:26:16 +00:00
// Returns the value for the next token
func (l *Lexer) val() string {
2015-02-18 13:26:53 +00:00
return string(l.stack[:l.pos])
2015-02-16 19:26:16 +00:00
}
2015-02-18 13:50:55 +00:00
// Returns but does not consume the next rune in the stack
2015-02-12 11:12:19 +00:00
func (l *Lexer) peek() rune {
r := l.next()
2015-02-18 13:26:53 +00:00
l.backup(1)
2015-02-12 11:12:19 +00:00
return r
}
2015-02-18 13:50:55 +00:00
// Tells if the following stack matches the given string
2015-02-16 17:50:16 +00:00
func (l *Lexer) acceptString(s string) (ok bool) {
2015-02-18 13:26:53 +00:00
for i, c := range s {
if l.next() != c {
l.backup(i + 1)
return false
}
2015-02-16 17:50:16 +00:00
}
2015-02-18 13:26:53 +00:00
return true
2015-02-16 17:50:16 +00:00
}
// Steps back one rune
2015-02-17 17:12:40 +00:00
// Backup is never called right after a new line char so we don't care
// about the line number. This is also true for the ignore function
2015-02-18 13:26:53 +00:00
func (l *Lexer) backup(n int) {
l.pos -= n
l.colNum -= n
2015-02-12 11:12:19 +00:00
}
2015-02-18 13:50:55 +00:00
// Clears the stack items preceding the current position
2015-02-16 17:50:16 +00:00
func (l *Lexer) ignore() {
2015-02-18 13:26:53 +00:00
if l.pos < len(l.stack) {
l.stack = l.stack[l.pos:]
} else {
l.stack = []rune{}
}
l.pos = 0
l.startLine = l.lineNum
2015-02-17 17:12:40 +00:00
l.startCol = l.colNum
2015-02-16 17:50:16 +00:00
}
// Passes an item back to the client
2015-02-16 11:18:14 +00:00
func (l *Lexer) emit(t Token) {
2015-02-17 17:49:28 +00:00
// Single-character tokens never backup
if len(l.val()) == 1 {
l.startCol++
}
2015-02-16 19:04:17 +00:00
l.items <- Item{
2015-02-17 17:12:40 +00:00
Token: t,
Val: l.val(),
Line: l.startLine,
2015-02-17 17:12:40 +00:00
Column: l.startCol,
2015-02-16 19:04:17 +00:00
}
2015-02-18 13:50:55 +00:00
l.ignore() // Cleaning up stack
2015-02-12 11:12:19 +00:00
}
2015-02-16 17:50:16 +00:00
// Emits an error token with given string as a value and stops lexing
2015-02-12 11:12:19 +00:00
func (l *Lexer) errorf(format string, args ...interface{}) stateFn {
2015-02-16 19:04:17 +00:00
l.items <- Item{
2015-02-17 17:12:40 +00:00
Token: Error,
Val: fmt.Sprintf(format, args...),
Line: l.startLine,
2015-02-17 17:12:40 +00:00
Column: l.startCol,
2015-02-16 19:04:17 +00:00
}
2015-02-16 17:50:16 +00:00
return nil
2015-02-16 11:18:14 +00:00
}
//
// States
//
func lexInitial(l *Lexer) stateFn {
for {
switch r := l.next(); r {
2015-02-17 17:12:23 +00:00
case ' ', '\t', '\n':
2015-02-16 11:25:33 +00:00
l.ignore()
2015-02-16 11:18:14 +00:00
case 'n':
2015-02-18 13:26:53 +00:00
l.backup(1)
2015-02-16 11:18:14 +00:00
return lexNull(l)
case 't', 'f':
2015-02-18 13:26:53 +00:00
l.backup(1)
2015-02-16 11:18:14 +00:00
return lexBool(l)
case '1', '2', '3', '4', '5', '6', '7', '8', '9', '0':
2015-02-18 13:26:53 +00:00
l.backup(1)
2015-02-16 11:18:14 +00:00
return lexNumber(l)
case '"':
return lexString(l)
case '[':
l.emit(BracketOpen)
case ']':
l.emit(BracketClose)
case '{':
l.emit(BraceOpen)
case '}':
l.emit(BraceClose)
case ':':
l.emit(Colon)
case ',':
l.emit(Comma)
case 0:
2015-02-16 11:25:33 +00:00
l.emit(EOF)
return nil
2015-02-16 11:18:14 +00:00
default:
2015-02-18 15:05:16 +00:00
return l.errorf("Unexpected symbol: %q", r)
2015-02-16 11:18:14 +00:00
}
}
2015-02-12 11:12:19 +00:00
}
2015-02-16 11:18:14 +00:00
func lexNull(l *Lexer) stateFn {
if l.acceptString("null") {
l.emit(Null)
} else {
2015-02-18 15:05:16 +00:00
return l.errorf("Unexpected (null) symbol: %q", l.val())
2015-02-16 11:18:14 +00:00
}
return lexInitial
}
func lexBool(l *Lexer) stateFn {
if l.acceptString("true") || l.acceptString("false") {
l.emit(Bool)
2015-02-16 19:26:16 +00:00
} else {
2015-02-18 15:05:16 +00:00
return l.errorf("Unexpected (bool) symbol: %q", l.val())
2015-02-16 11:18:14 +00:00
}
return lexInitial
}
func lexNumber(l *Lexer) stateFn {
2015-02-18 15:33:57 +00:00
var (
numDots = 0
prev rune
cur rune
)
2015-02-16 11:18:14 +00:00
for {
2015-02-18 15:33:57 +00:00
switch cur = l.next(); cur {
2015-02-16 11:18:14 +00:00
case '1', '2', '3', '4', '5', '6', '7', '8', '9', '0':
case '.':
2015-02-16 17:50:16 +00:00
numDots++
default:
2015-02-18 13:26:53 +00:00
l.backup(1)
2015-02-18 15:33:57 +00:00
if numDots > 1 || prev == '.' {
2015-02-16 19:26:16 +00:00
return l.errorf("Invalid number: %q", l.val())
2015-02-16 11:18:14 +00:00
}
2015-02-16 11:25:33 +00:00
l.emit(Number)
return lexInitial
2015-02-16 11:18:14 +00:00
}
2015-02-18 15:33:57 +00:00
prev = cur
2015-02-16 11:18:14 +00:00
}
}
func lexString(l *Lexer) stateFn {
2015-02-16 19:04:17 +00:00
// Skipping opening quote
l.ignore()
2015-02-16 11:18:14 +00:00
escaped := false
for {
switch r := l.next(); r {
case '\\':
2015-02-16 11:25:33 +00:00
escaped = !escaped
2015-02-16 11:18:14 +00:00
case '"':
if escaped {
escaped = false
} else {
2015-02-16 19:04:17 +00:00
// Going before closing quote and emitting
2015-02-18 13:26:53 +00:00
l.backup(1)
2015-02-16 11:18:14 +00:00
l.emit(String)
2015-02-16 19:04:17 +00:00
// Skipping closing quote
l.next()
l.ignore()
2015-02-16 11:25:33 +00:00
return lexInitial
2015-02-16 11:18:14 +00:00
}
case 0:
return l.errorf("Unterminated string")
default:
escaped = false
}
}
}
2015-02-16 17:50:16 +00:00
2015-02-16 19:26:16 +00:00
//
// Debug
//
2015-02-16 17:50:16 +00:00
func (i Item) String() string {
2015-02-17 17:12:40 +00:00
var label string
2015-02-16 17:50:16 +00:00
switch i.Token {
2015-02-17 17:49:12 +00:00
case BraceOpen, BraceClose, BracketOpen, BracketClose, Quote, Colon, Comma:
label = i.Val
2015-02-16 17:50:16 +00:00
case EOF:
2015-02-17 17:12:40 +00:00
label = "EOF"
2015-02-16 17:50:16 +00:00
case Error:
2015-02-18 15:05:16 +00:00
label = fmt.Sprintf("(Error: %s)", i.Val)
2015-02-16 17:50:16 +00:00
case Null:
2015-02-17 17:12:40 +00:00
label = fmt.Sprintf("(NULL: %q)", i.Val)
2015-02-16 17:50:16 +00:00
case Bool:
2015-02-17 17:12:40 +00:00
label = fmt.Sprintf("(Bool: %q)", i.Val)
2015-02-16 17:50:16 +00:00
case Number:
2015-02-17 17:12:40 +00:00
label = fmt.Sprintf("(Number: %q)", i.Val)
2015-02-16 17:50:16 +00:00
case String:
2015-02-17 17:12:40 +00:00
label = fmt.Sprintf("(String: %q)", i.Val)
2015-02-16 17:50:16 +00:00
default:
panic("Unreachable")
}
2015-02-17 17:12:40 +00:00
return fmt.Sprintf("[%.3d:%.3d] %s", i.Line, i.Column, label)
2015-02-16 17:50:16 +00:00
}