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

Scala example source code file (MetaParser.scala)

This example Scala source code file (MetaParser.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

exception, metaparser, none, option, some, string, stringbuffer, stringtokenizer, unit

The MetaParser.scala Scala example source code

/*     ___ ____ ___   __   ___   ___
**    / _// __// _ | / /  / _ | / _ \    Scala classfile decoder
**  __\ \/ /__/ __ |/ /__/ __ |/ ___/    (c) 2003-2013, LAMP/EPFL
** /____/\___/_/ |_/____/_/ |_/_/        http://scala-lang.org/
**
*/


package scala
package tools.scalap

import java.util._


/** a parser class for parsing meta type information in classfiles
 *  generated by pico.
 */
class MetaParser(meta: String) {
  val scanner = new StringTokenizer(meta, "()[], \t<;", true)
  var token: String = _
  val res = new StringBuffer

  private def nextToken: String = {
    do {
      token = scanner.nextToken().trim()
    } while (token.length() == 0)
    token
  }

  protected def parseType(): Unit = {
    if (token startsWith "?")
      res.append(token.substring(1))
    else
      res.append(token)
    nextToken
    if (token == "[") {
      do {
        res.append(if (token == ",") ", " else "[")
        nextToken
        parseType
      } while (token == ",")
      nextToken
      res.append("]")
    }
  }

  def parse: Option[String] =
    if (scanner.hasMoreTokens()) {
      nextToken
      try {
        if (!scanner.hasMoreTokens())
          None
        else if (token == "class")
          Some(parseMetaClass)
        else if (token == "method")
          Some(parseMetaMethod)
        else if (token == "field")
          Some(parseMetaField)
        else if (token == "constr")
          Some(parseConstrField)
        else
          None
      } catch {
        case _: Exception => None
      }
    } else
      None;

  protected def parseMetaClass: String = {
    nextToken
    if (token == "[") {
      do {
        res.append(if (token == "[") "[" else ", ")
        nextToken
        if (token == "+") {
          nextToken
          res.append('+')
        } else if (token == "-") {
          nextToken
          res.append('-')
        }
        res.append(token.substring(1))
        nextToken
        if (token == "<") {
          nextToken
          res.append(" <: ")
          parseType
        }
      } while (token == ",")
      nextToken
      res.append("]")
    }
    if (token == "extends") {
      do {
        if (token == "extends")
          res.append(" extends ")
        else
          res.append(" with ")
        nextToken
        parseType
      } while (token == "with")
    }
    res.toString();
  }

  protected def parseMetaMethod: String = {
    nextToken
    if (token == "[") {
      nextToken
      if (token == "]") {
        nextToken
      } else {
        var loop = true
        res.append("[")
        while (loop) {
          res.append(token.substring(1));
          nextToken;
          if (token == "<") {
            nextToken;
            res.append(" <: ")
            parseType
          }
          if (token == ",") {
            nextToken
            res.append(", ")
          } else
            loop = false
        }
        nextToken
        res.append("]")
      }
    }
    if (token == "(") {
      do {
        if (token == ",") {
          nextToken;
          if (token != ")")
            res.append(", ")
        } else {
          nextToken;
          res.append("(")
        }
        if (token != ")") {
          if (token == "def") {
            nextToken;
            res.append("def ")
          }
          parseType
        }
      } while (token == ",")
      nextToken
      res.append("): ")
      parseType
    } else {
      res.append(": ")
      parseType
    }
    res.toString()
  }

  protected def parseMetaField: String = {
    nextToken
    res.append(": ")
    parseType
    res.toString()
  }

  protected def parseConstrField: String = {
    nextToken
    if (token == "(") {
      do {
        res.append(if (token == "(") "(" else ", ")
        nextToken
        if (token != ")")
          parseType
      } while (token == ",")
      nextToken
      res.append(")")
    } else {
    }
    res.toString()
  }
}

Other Scala source code examples

Here is a short list of links related to this Scala MetaParser.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.