54 lines
1.7 KiB
Scala
54 lines
1.7 KiB
Scala
package wacc
|
|
|
|
import parsley.Parsley
|
|
import parsley.token.{Basic, Lexer}
|
|
import parsley.token.descriptions.*
|
|
|
|
object lexer {
|
|
private val desc = LexicalDesc.plain.copy(
|
|
nameDesc = NameDesc.plain.copy(
|
|
identifierStart = Basic(c => c.isLetter || c == '_'),
|
|
identifierLetter = Basic(c => c.isLetterOrDigit || c == '_')
|
|
),
|
|
symbolDesc = SymbolDesc.plain.copy(
|
|
hardKeywords = Set(
|
|
"begin", "end", "is", "skip", "if", "then", "else", "fi", "while", "do", "done", "read",
|
|
"free", "return", "exit", "print", "println", "true", "false", "int", "bool", "char",
|
|
"string", "pair", "newpair", "fst", "snd", "call", "chr", "ord", "len", "null"
|
|
),
|
|
hardOperators = Set(
|
|
"+", "-", "*", "/", "%", ">", "<", ">=", "<=", "==", "!=", "&&", "||", "!"
|
|
)
|
|
),
|
|
spaceDesc = SpaceDesc.plain.copy(
|
|
lineCommentStart = "#"
|
|
),
|
|
textDesc = TextDesc.plain.copy(
|
|
graphicCharacter = Basic(c => c >= ' ' && c != '\\' && c != '\'' && c != '"'),
|
|
escapeSequences = EscapeDesc.plain.copy(
|
|
literals = Set('\\', '"', '\''),
|
|
mapping = Map(
|
|
"0" -> '\u0000',
|
|
"b" -> '\b',
|
|
"t" -> '\t',
|
|
"n" -> '\n',
|
|
"f" -> '\f',
|
|
"r" -> '\r'
|
|
)
|
|
)
|
|
),
|
|
numericDesc = NumericDesc.plain.copy(
|
|
decimalExponentDesc = ExponentDesc.NoExponents
|
|
)
|
|
)
|
|
|
|
private val lexer = Lexer(desc)
|
|
val ident = lexer.lexeme.names.identifier
|
|
val integer = lexer.lexeme.integer.decimal32[Int]
|
|
val charLit = lexer.lexeme.character.ascii
|
|
val stringLit = lexer.lexeme.string.ascii
|
|
val implicits = lexer.lexeme.symbol.implicits
|
|
|
|
def fully[A](p: Parsley[A]): Parsley[A] = lexer.fully(p)
|
|
}
|