feat: add comment support to saccharine language #25
@@ -77,6 +77,23 @@ func getToken(i *iterator.Iterator[rune]) (*Token, error) {
|
|||||||
}
|
}
|
||||||
case letter == ';':
|
case letter == ';':
|
||||||
return NewHardBreak(index), nil
|
return NewHardBreak(index), nil
|
||||||
|
case letter == '#':
|
||||||
|
// Skip everything until the next newline or EOF
|
||||||
|
for {
|
||||||
|
if i.Done() {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
r, err := i.Next()
|
||||||
|
if err != nil {
|
||||||
|
return nil, trace.Wrap(err, "error while parsing comment")
|
||||||
|
}
|
||||||
|
if r == '\n' {
|
||||||
|
// Put the newline back so it can be processed as a soft break
|
||||||
|
i.Back()
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil, nil
|
||||||
case unicode.IsSpace(letter):
|
case unicode.IsSpace(letter):
|
||||||
return nil, nil
|
return nil, nil
|
||||||
case isVariable(letter):
|
case isVariable(letter):
|
||||||
|
|||||||
1
tests/comments.expected
Normal file
1
tests/comments.expected
Normal file
@@ -0,0 +1 @@
|
|||||||
|
VALUE
|
||||||
15
tests/comments.test
Normal file
15
tests/comments.test
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
# This is a full-line comment at the start
|
||||||
|
# The following defines the identity function
|
||||||
|
identity := \x.x # This is an end-of-line comment
|
||||||
|
|
||||||
|
# Define a simple function that applies a function twice
|
||||||
|
twice := \f.\x.(f (f x))
|
||||||
|
|
||||||
|
# Test that comments don't interfere with expressions
|
||||||
|
result := (twice identity VALUE) # Should just return VALUE
|
||||||
|
|
||||||
|
# Multiple comments in a row
|
||||||
|
# can appear anywhere
|
||||||
|
# without breaking the code
|
||||||
|
|
||||||
|
result # Final comment at the end
|
||||||
Reference in New Issue
Block a user