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
|
multiply = { a, b in
|
||||||
a * b
|
a * b
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// calculates the result
|
||||||
calculateSimpleResult = calculateSimple()
|
calculateSimpleResult = calculateSimple()
|
||||||
calculateComplexResult = calculateComplex()
|
calculateComplexResult = calculateComplex()
|
||||||
multiplyResult = multiply(50, 50)
|
multiplyResult = multiply(50, 50)
|
||||||
|
@ -197,6 +197,7 @@ class PorkParser(source: PeekableSource<Token>) {
|
|||||||
|
|
||||||
private fun ignoredByParser(type: TokenType): Boolean = when (type) {
|
private fun ignoredByParser(type: TokenType): Boolean = when (type) {
|
||||||
TokenType.BlockComment -> true
|
TokenType.BlockComment -> true
|
||||||
|
TokenType.LineComment -> true
|
||||||
TokenType.Whitespace -> true
|
TokenType.Whitespace -> true
|
||||||
else -> false
|
else -> false
|
||||||
}
|
}
|
||||||
|
@ -75,6 +75,20 @@ class PorkTokenizer(val source: CharSource) {
|
|||||||
return Token(TokenType.BlockComment, tokenStart, comment)
|
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 {
|
fun next(): Token {
|
||||||
while (source.peek() != CharSource.NullChar) {
|
while (source.peek() != CharSource.NullChar) {
|
||||||
tokenStart = source.currentIndex
|
tokenStart = source.currentIndex
|
||||||
@ -84,6 +98,10 @@ class PorkTokenizer(val source: CharSource) {
|
|||||||
return readBlockComment(char)
|
return readBlockComment(char)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (char == '/' && source.peek() == '/') {
|
||||||
|
return readLineComment(char)
|
||||||
|
}
|
||||||
|
|
||||||
for (item in TokenType.SingleChars) {
|
for (item in TokenType.SingleChars) {
|
||||||
val itemChar = item.singleChar!!.char
|
val itemChar = item.singleChar!!.char
|
||||||
if (itemChar != char) {
|
if (itemChar != char) {
|
||||||
|
@ -27,6 +27,7 @@ enum class TokenType(vararg properties: TokenTypeProperty) {
|
|||||||
Else(Keyword("else")),
|
Else(Keyword("else")),
|
||||||
Whitespace,
|
Whitespace,
|
||||||
BlockComment,
|
BlockComment,
|
||||||
|
LineComment,
|
||||||
EndOfFile;
|
EndOfFile;
|
||||||
|
|
||||||
val promotions: List<Promotion> = properties.filterIsInstance<Promotion>()
|
val promotions: List<Promotion> = properties.filterIsInstance<Promotion>()
|
||||||
|
Loading…
Reference in New Issue
Block a user