mirror of
https://github.com/GayPizzaSpecifications/pork.git
synced 2025-08-02 21:00:56 +00:00
Add support for line comments.
This commit is contained in:
parent
5455846d7a
commit
0b3700667d
@ -11,6 +11,8 @@ main = { in
|
||||
multiply = { a, b in
|
||||
a * b
|
||||
}
|
||||
|
||||
// calculates the result
|
||||
calculateSimpleResult = calculateSimple()
|
||||
calculateComplexResult = calculateComplex()
|
||||
multiplyResult = multiply(50, 50)
|
||||
|
@ -197,6 +197,7 @@ class PorkParser(source: PeekableSource<Token>) {
|
||||
|
||||
private fun ignoredByParser(type: TokenType): Boolean = when (type) {
|
||||
TokenType.BlockComment -> true
|
||||
TokenType.LineComment -> true
|
||||
TokenType.Whitespace -> true
|
||||
else -> false
|
||||
}
|
||||
|
@ -75,6 +75,20 @@ class PorkTokenizer(val source: CharSource) {
|
||||
return Token(TokenType.BlockComment, tokenStart, comment)
|
||||
}
|
||||
|
||||
private fun readLineComment(firstChar: Char): Token {
|
||||
val comment = buildString {
|
||||
append(firstChar)
|
||||
while (true) {
|
||||
val char = source.peek()
|
||||
if (char == CharSource.NullChar || char == '\n') {
|
||||
break
|
||||
}
|
||||
append(source.next())
|
||||
}
|
||||
}
|
||||
return Token(TokenType.LineComment, tokenStart, comment)
|
||||
}
|
||||
|
||||
fun next(): Token {
|
||||
while (source.peek() != CharSource.NullChar) {
|
||||
tokenStart = source.currentIndex
|
||||
@ -84,6 +98,10 @@ class PorkTokenizer(val source: CharSource) {
|
||||
return readBlockComment(char)
|
||||
}
|
||||
|
||||
if (char == '/' && source.peek() == '/') {
|
||||
return readLineComment(char)
|
||||
}
|
||||
|
||||
for (item in TokenType.SingleChars) {
|
||||
val itemChar = item.singleChar!!.char
|
||||
if (itemChar != char) {
|
||||
|
@ -27,6 +27,7 @@ enum class TokenType(vararg properties: TokenTypeProperty) {
|
||||
Else(Keyword("else")),
|
||||
Whitespace,
|
||||
BlockComment,
|
||||
LineComment,
|
||||
EndOfFile;
|
||||
|
||||
val promotions: List<Promotion> = properties.filterIsInstance<Promotion>()
|
||||
|
Loading…
Reference in New Issue
Block a user