mirror of
https://github.com/GayPizzaSpecifications/pork.git
synced 2025-08-03 05:10:55 +00:00
Add support for block comments.
This commit is contained in:
parent
5f4d74008e
commit
5455846d7a
@ -1,3 +1,4 @@
|
||||
/* fibonacci sequence */
|
||||
fib = { n in
|
||||
if n == 0
|
||||
then 0
|
||||
|
@ -1,3 +1,4 @@
|
||||
/* main function */
|
||||
main = { in
|
||||
three = 3
|
||||
two = 2
|
||||
|
@ -3,7 +3,7 @@ package gay.pizza.pork.parse
|
||||
import gay.pizza.pork.ast.nodes.*
|
||||
|
||||
class PorkParser(source: PeekableSource<Token>) {
|
||||
private val whitespaceIncludedSource = source
|
||||
private val unsanitizedSource = source
|
||||
|
||||
private fun readIntLiteral(): IntLiteral {
|
||||
val token = expect(TokenType.IntLiteral)
|
||||
@ -176,8 +176,8 @@ class PorkParser(source: PeekableSource<Token>) {
|
||||
|
||||
private fun next(): Token {
|
||||
while (true) {
|
||||
val token = whitespaceIncludedSource.next()
|
||||
if (token.type == TokenType.Whitespace) {
|
||||
val token = unsanitizedSource.next()
|
||||
if (ignoredByParser(token.type)) {
|
||||
continue
|
||||
}
|
||||
return token
|
||||
@ -186,12 +186,18 @@ class PorkParser(source: PeekableSource<Token>) {
|
||||
|
||||
private fun peek(): Token {
|
||||
while (true) {
|
||||
val token = whitespaceIncludedSource.peek()
|
||||
if (token.type == TokenType.Whitespace) {
|
||||
whitespaceIncludedSource.next()
|
||||
val token = unsanitizedSource.peek()
|
||||
if (ignoredByParser(token.type)) {
|
||||
unsanitizedSource.next()
|
||||
continue
|
||||
}
|
||||
return token
|
||||
}
|
||||
}
|
||||
|
||||
private fun ignoredByParser(type: TokenType): Boolean = when (type) {
|
||||
TokenType.BlockComment -> true
|
||||
TokenType.Whitespace -> true
|
||||
else -> false
|
||||
}
|
||||
}
|
||||
|
@ -51,11 +51,39 @@ class PorkTokenizer(val source: CharSource) {
|
||||
return Token(TokenType.Whitespace, tokenStart, whitespace)
|
||||
}
|
||||
|
||||
private fun readBlockComment(firstChar: Char): Token {
|
||||
val comment = buildString {
|
||||
append(firstChar)
|
||||
var endOfComment = false
|
||||
while (true) {
|
||||
val char = source.next()
|
||||
append(char)
|
||||
|
||||
if (endOfComment) {
|
||||
if (char != '/') {
|
||||
endOfComment = false
|
||||
continue
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
if (char == '*') {
|
||||
endOfComment = true
|
||||
}
|
||||
}
|
||||
}
|
||||
return Token(TokenType.BlockComment, tokenStart, comment)
|
||||
}
|
||||
|
||||
fun next(): Token {
|
||||
while (source.peek() != CharSource.NullChar) {
|
||||
tokenStart = source.currentIndex
|
||||
val char = source.next()
|
||||
|
||||
if (char == '/' && source.peek() == '*') {
|
||||
return readBlockComment(char)
|
||||
}
|
||||
|
||||
for (item in TokenType.SingleChars) {
|
||||
val itemChar = item.singleChar!!.char
|
||||
if (itemChar != char) {
|
||||
|
@ -26,6 +26,7 @@ enum class TokenType(vararg properties: TokenTypeProperty) {
|
||||
Then(Keyword("then")),
|
||||
Else(Keyword("else")),
|
||||
Whitespace,
|
||||
BlockComment,
|
||||
EndOfFile;
|
||||
|
||||
val promotions: List<Promotion> = properties.filterIsInstance<Promotion>()
|
||||
|
Loading…
Reference in New Issue
Block a user