Permalink
Browse files

Make recursive incompleteLex tail-recursive

  • Loading branch information...
1 parent 5324a6c commit bdb29a8c05c89e7e5cec8622b2c1bcb823df26cf @brucespang committed Jan 10, 2012
Showing with 11 additions and 11 deletions.
  1. +11 −11 src/main/scala/abstract_lexer.scala
View
22 src/main/scala/abstract_lexer.scala
@@ -28,25 +28,25 @@ trait AbstractLexer {
}
def incompleteLex(text:String, tokens:List[Token]=List()):Tuple2[List[Token], String] = {
- text match {
- case "" => handleLexFinish(text, tokens)
- case _ =>
+ (text, tokens) match {
+ case ("", _) =>
+ handleLexFinish(text, tokens)
+ case (_, (token:HaltingToken) :: restTokens) =>
+ handleLexFinish(text, restTokens)
+ case (_,_) =>
val (pattern, string) = patternWithLongestMatch(text, patterns.keys.toList)
val restText = text.substring(string.length, text.length)
- patterns(pattern)(string) fold (
+ val (newText, newTokens) = patterns(pattern)(string) fold (
{ lexer =>
val (returnedTokens, returnedText) = lexer.incompleteLex(restText, tokens)
- incompleteLex(returnedText, tokens ::: returnedTokens)
+ (returnedText, tokens ::: returnedTokens)
},
- { token =>
- token match {
- case t: HaltingToken => handleLexFinish(restText, tokens)
- case _ => incompleteLex(restText, token :: tokens)
- }
- }
+ { token => (restText, (token :: tokens)) }
)
+
+ incompleteLex(newText, newTokens)
}
}

0 comments on commit bdb29a8

Please sign in to comment.