Created
May 7, 2016 13:48
-
-
Save tartakynov/87c898a9ecbc102609b6b80d2216ed5e to your computer and use it in GitHub Desktop.
Functional Tokenizer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Tokenizer = String => Seq[Token] | |
val tokenizer = new Tokenizer { | |
override def apply(input: String): Seq[Token] = input.foldRight(Seq("")) { | |
case (c, acc) if Character.isMirrored(c) => Seq("", c.toString) ++ acc | |
case (c, acc) => Seq(c + acc.head) ++ acc.tail | |
}.map(_.trim).filter(_.nonEmpty).map { | |
str => Token(str, str match { | |
case "(" | "{" | "[" | "<" => TokenType.LPAREN | |
case ")" | "}" | "]" | ">" => TokenType.RPAREN | |
case _ => TokenType.STR | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment