Skip to content

Commit

Permalink
Tidy up lexer; merge ScalaLexer and Lexer, pull out ModeStack trait, …
Browse files Browse the repository at this point in the history
…misc
  • Loading branch information
mdr committed Apr 22, 2012
1 parent f376665 commit c6d2179
Show file tree
Hide file tree
Showing 11 changed files with 333 additions and 211 deletions.
42 changes: 33 additions & 9 deletions perf/src/main/scala/scalariform/perf/LexerPerformanceTest.scala
Expand Up @@ -14,21 +14,45 @@ object LexerPerformanceTest {
val file = new File("/home/matt/coding/scala/src/compiler/scala/tools/nsc/typechecker/Typers.scala")
val source = Source.fromFile(file).mkString
println("Source: " + source.length + " chars")
1 to 1000 foreach { _ doIt(source) }
val tokens = ScalaLexer.rawTokenise(source)
println("Tokens: " + tokens.size)
val ITERATIONS = 3000
val WARMUP = 500
1 to WARMUP foreach { _ doIt(source) }

val its = 10000

val start = System.currentTimeMillis
1 to its foreach { _ => doIt(source) }
val durations = 1 to ITERATIONS map { _ =>
val start1 = System.nanoTime
doIt(source)
val duration = System.nanoTime - start1
duration.toDouble / 1000000.0
}
val duration = System.currentTimeMillis - start
println(duration.toDouble / its + " ms")

val meanT = duration.toDouble / ITERATIONS
println("Raw average: " + meanT)
def compute(iterable: Iterable[Double]): (Double, Double) = {
def square(x: Double) = x * x
val size = iterable.size
val mean = durations.sum / size
(mean, math.sqrt(durations.map(n => square(n - mean)).sum / size))
}
val (mean, stdDev) = compute(durations)
def isNormal(d: Double) = math.abs(d - mean) < 2 * stdDev
val durations2 = durations.filter(isNormal)
println("Original trials: " + ITERATIONS)
println("Outliers removed: " + (ITERATIONS - durations2.size))
val (mean2, stdDev2) = compute(durations2)
println("Minimum: " + durations.min)
println("Average: " + mean2 + " ms")
println("Standard deviation: " + stdDev2 + " ms")
println()
durations.foreach(println)
}

private def unicodeEscapeReader(s: String) = new UnicodeEscapeReader(s)

private def doIt(s: String) = {
UnicodeEscapeDecoder.decode(s)
// UnicodeEscapeDecoder.decode(s)
ScalaLexer.rawTokenise(s)
}

}

4 changes: 2 additions & 2 deletions scalariform/src/main/scala/scalariform/lexer/Keywords.scala
Expand Up @@ -64,5 +64,5 @@ object Keywords {
"|" -> PIPE,
"~" -> TILDE,
"!" -> EXCLAMATION)

}
}
100 changes: 0 additions & 100 deletions scalariform/src/main/scala/scalariform/lexer/Lexer.scala

This file was deleted.

3 changes: 1 addition & 2 deletions scalariform/src/main/scala/scalariform/lexer/LexerMode.scala
Expand Up @@ -32,5 +32,4 @@ class XmlMode extends LexerMode {

def nestingLevel = tagNestLevel

}

}
34 changes: 34 additions & 0 deletions scalariform/src/main/scala/scalariform/lexer/ModeStack.scala
@@ -0,0 +1,34 @@
package scalariform.lexer

import scala.collection.mutable.Stack

/**
* Keeping track of nesting level of XML within Scala.
*/
trait ModeStack { self: ScalaLexer

private val modeStack = new Stack[LexerMode]

modeStack.push(new ScalaMode)

protected def popMode() { modeStack.pop() }

protected def isRootMode = modeStack.size == 1

protected def switchToScalaModeAndFetchToken() {
modeStack.push(new ScalaMode)
fetchScalaToken()
}

protected def switchToXmlModeAndFetchToken() {
modeStack.push(new XmlMode)
fetchXmlToken()
}

protected def xmlMode: XmlMode = modeStack.head.asInstanceOf[XmlMode]

protected def isXmlMode = modeStack.head.isInstanceOf[XmlMode]

protected def scalaMode: ScalaMode = modeStack.head.asInstanceOf[ScalaMode]

}

0 comments on commit c6d2179

Please sign in to comment.