package edu.uwm.cs import scala.util.parsing.combinator.{ImplicitConversions, RegexParsers} abstract class CoolLexer extends RegexParsers with ImplicitConversions { override val whiteSpace = """\s*(/(\*([^*]|\*[^/])*\*/|/[^\n]*\n)\s*)*"""r override val skipWhitespace = true val STR_CONST = ("""["]([^"\\\n\r]|\\(.|\n|\r))*\\?["]"""r) ^^ { s => Symbol(canonicalizeStr(s.substring(1, s.length - 1))) } val INT_CONST = ("""\d+"""r) ^^ toSym val OBJECTID = ("""[a-z][\w_\d]*"""r) ^^ toSym val TYPEID = ("""[A-Z][\w_\d]*"""r) ^^ toSym val CLASS = "class" ^^ toSym val FALSE = "false" ^^^ false val SUPER = "super" ^^ toSym val WHILE = "while" ^^ toSym val NATIVE = "native" ^^ toSym val CASE = "case" ^^ toSym val ELSE = "else" ^^ toSym val TRUE = "true" ^^^ true val NEW = "new" ^^ toSym val NOT = "!" ^^ toSym val IF = "if" ^^ toSym val LE = "<=" ^^ toSym val EQ = "eq" ^^ toSym val ARROW = "=>" ^^ toSym // new keywords?? val OVERRIDE = "override" ^^ toSym val EXTENDS = "extends" ^^ toSym val DEF = "def" ^^ toSym val VAR = "var" ^^ toSym val NULL = "null" ^^ toSym val MATCH = "match" ^^ toSym val EQUALS = "==" ^^ toSym def toSym(s: String) = Symbol(s) /** * Relies on constant-time {@link java.lang.String#substring}. */ private def canonicalizeStr(str: String): String = { if (str.length == 0) str else { val first = str charAt 0 val (res, inc) = if (first == '\\') { val sub = str charAt 1 match { case 'b' => '\b' case 't' => '\t' case 'f' => '\f' case 'n' => '\n' case 'r' => '\r' case '0' => '\0' case c => c } (sub, 2) } else { (first, 1) } res + canonicalizeStr(str substring inc) } } }