alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Scala example source code file (MainTokenMetric.scala)

This example Scala source code file (MainTokenMetric.scala) is included in my "Source Code Warehouse" project. The intent of this project is to help you more easily find Scala source code examples by using tags.

All credit for the original source code belongs to scala-lang.org; I'm just trying to make examples easier to find. (For my Scala work, see my Scala examples and tutorials.)

Scala tags/keywords

array, compiler, consolereporter, eof, fatalerror, global, list, maintokenmetric, nsc, unitscanner

The MainTokenMetric.scala Scala example source code

/* NSC -- new Scala compiler
 * Copyright 2005-2013 LAMP/EPFL
 * @author  Martin Odersky
 */

package scala
package tools.nsc

import scala.tools.nsc.reporters.ConsoleReporter

/** The main class for NSC, a compiler for the programming
 *  language Scala.
 */
object MainTokenMetric {

  private var reporter: ConsoleReporter = _

  def tokenMetric(compiler: Global, fnames: List[String]) {
    import compiler.CompilationUnit
    import compiler.syntaxAnalyzer.UnitScanner
    import ast.parser.Tokens.EOF
    var totale = 0
    for (source <- fnames) {
      val s = new UnitScanner(new CompilationUnit(compiler.getSourceFile(source)))
      s.nextToken()
      var i = 0
      while (s.token != EOF) {
        i += 1
        s.nextToken()
      }
      Console.println(i.toString + " " + source.toString())
      totale += i
    }
    Console.println(totale.toString()+" total")
  }

  def process(args: Array[String]) {
    val settings = new Settings(sys.error)
    reporter = new ConsoleReporter(settings)
    val command = new CompilerCommand(args.toList, settings)
    try {
      val compiler = new Global(command.settings, reporter)
      tokenMetric(compiler, command.files)
    } catch {
      case ex @ FatalError(msg) =>
        if (command.settings.debug)
          ex.printStackTrace()
        reporter.error(null, "fatal error: " + msg)
    }
  }

  def main(args: Array[String]) {
    process(args)
    sys.exit(if (reporter.hasErrors) 1 else 0)
  }

}

Other Scala source code examples

Here is a short list of links related to this Scala MainTokenMetric.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.