now lexing comments

master
Jordan Orelli 12 years ago
parent 6dd75298e5
commit 0de1044dda

@ -1,16 +1,22 @@
(+ 1 (+ 1 1) (dave (sam 1))) (+ 1 (+ 1 1) (dave (sam 1)))
; alright I have comments now!!!!
; woooohooo!!!!
; nice nice nice
(+ 1 (+ 1
22.3 22.3
a a
;here comes a comment
3. 3.
(one two three)
"this is a string" "this is a string"
4.0 4.0
(dave (dave
1 1
"here's an escaped quote: \" how neat!!!" "here's an escaped quote: \" how neat!!!"
2 2
"and here's an escaped \\, sweet!" "and here;'s an escaped \\, sweet!"
albert-camus albert-camus
3 3
(sam 3 2 2))) (sam 3 2 2)))

@ -87,6 +87,8 @@ func lexRoot(l *lexer) (stateFn, error) {
return nil, err return nil, err
} }
switch r { switch r {
case ';':
return lexComment, nil
case '(': case '(':
return lexOpenParen, nil return lexOpenParen, nil
case ' ', '\t', '\n': case ' ', '\t', '\n':
@ -104,10 +106,12 @@ func lexOpenParen(l *lexer) (stateFn, error) {
return nil, err return nil, err
} }
switch r { switch r {
case ' ', '\t', '\n': case ' ', '\t', '\n', '\r':
return lexRoot, nil return lexWhitespace, nil
case '(': case '(':
return nil, fmt.Errorf("the whole (( thing isn't supported yet") return nil, fmt.Errorf("the whole (( thing isn't supported yet")
case ';':
return lexComment, nil
} }
if isDigit(r) { if isDigit(r) {
l.append(r) l.append(r)
@ -132,6 +136,8 @@ func lexWhitespace(l *lexer) (stateFn, error) {
return lexString, nil return lexString, nil
case '(': case '(':
return lexOpenParen, nil return lexOpenParen, nil
case ';':
return lexComment, nil
} }
if isDigit(r) { if isDigit(r) {
l.append(r) l.append(r)
@ -157,6 +163,7 @@ func lexString(l *lexer) (stateFn, error) {
return lexString, nil return lexString, nil
} }
// lex the character *after* the string escape character \
func lexStringEsc(l *lexer) (stateFn, error) { func lexStringEsc(l *lexer) (stateFn, error) {
r, err := l.next() r, err := l.next()
if err != nil { if err != nil {
@ -184,6 +191,9 @@ func lexInt(l *lexer) (stateFn, error) {
case ')': case ')':
l.emit(int3ger) l.emit(int3ger)
return lexCloseParen, nil return lexCloseParen, nil
case ';':
l.emit(int3ger)
return lexComment, nil
} }
if isDigit(r) { if isDigit(r) {
l.append(r) l.append(r)
@ -207,6 +217,9 @@ func lexFloat(l *lexer) (stateFn, error) {
case ')': case ')':
l.emit(fl0at) l.emit(fl0at)
return lexCloseParen, nil return lexCloseParen, nil
case ';':
l.emit(fl0at)
return lexComment, nil
} }
if isDigit(r) { if isDigit(r) {
l.append(r) l.append(r)
@ -228,6 +241,9 @@ func lexSymbol(l *lexer) (stateFn, error) {
case ')': case ')':
l.emit(symbol) l.emit(symbol)
return lexCloseParen, nil return lexCloseParen, nil
case ';':
l.emit(symbol)
return lexComment, nil
default: default:
l.append(r) l.append(r)
return lexSymbol, nil return lexSymbol, nil
@ -252,10 +268,29 @@ func lexCloseParen(l *lexer) (stateFn, error) {
} }
case ')': case ')':
return lexCloseParen, nil return lexCloseParen, nil
case ';':
return lexComment, nil
} }
return nil, fmt.Errorf("unimplemented") return nil, fmt.Errorf("unimplemented")
} }
// lexes a comment
func lexComment(l *lexer) (stateFn, error) {
r, err := l.next()
if err != nil {
return nil, err
}
switch r {
case '\n', '\r':
if l.depth == 0 {
return lexRoot, nil
} else {
return lexWhitespace, nil
}
}
return lexComment, nil
}
// lexes some lispy input from an io.Reader, emiting tokens on chan c. The // lexes some lispy input from an io.Reader, emiting tokens on chan c. The
// channel is closed when the input reaches EOF, signaling that there are no // channel is closed when the input reaches EOF, signaling that there are no
// new tokens. // new tokens.

Loading…
Cancel
Save