32 lines
No EOL
1.4 KiB
Kotlin
32 lines
No EOL
1.4 KiB
Kotlin
package parser.grammars
|
|
|
|
import com.github.h0tk3y.betterParse.combinators.use
|
|
import com.github.h0tk3y.betterParse.grammar.Grammar
|
|
import com.github.h0tk3y.betterParse.lexer.Token
|
|
import com.github.h0tk3y.betterParse.lexer.literalToken
|
|
import com.github.h0tk3y.betterParse.lexer.regexToken
|
|
import com.github.h0tk3y.betterParse.lexer.token
|
|
|
|
abstract class Tokens : Grammar<Any>() {
|
|
// Prolog tokens
|
|
protected val nameToken: Token by regexToken("[a-z][a-zA-Z0-9_]*")
|
|
protected val variableToken: Token by regexToken("[A-Z][a-zA-Z0-9_]*")
|
|
|
|
// Arithmetic tokens
|
|
protected val floatToken: Token by regexToken("-?[1-9][0-9]*\\.[0-9]+")
|
|
protected val integerToken: Token by regexToken("-?([1-9][0-9]*|0)")
|
|
|
|
// Special tokens
|
|
protected val neck by literalToken(":-")
|
|
protected val comma: Token by literalToken(",")
|
|
protected val leftParenthesis: Token by literalToken("(")
|
|
protected val rightParenthesis: Token by literalToken(")")
|
|
protected val dot by literalToken(".")
|
|
|
|
// Ignored tokens
|
|
protected val whitespace: Token by regexToken("\\s+", ignore = true)
|
|
protected val singleLineComment: Token by regexToken("%[^\\n]*", ignore = true)
|
|
protected val multiLineComment: Token by regexToken("/\\*.*?\\*/", ignore = true)
|
|
|
|
protected val dummy by token { _, _ -> -1 } use { throw IllegalStateException("This parser should not be used") }
|
|
} |