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

Scala example source code file (Base.scala)

This example Scala source code file (Base.scala) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Scala tags/keywords

alt, alt, base, boolean, eps, eps, meta, regexp, regexp, sequ, sequ, star, syntaxerror

The Scala Base.scala source code

/*                     __                                               *\
**     ________ ___   / /  ___     Scala API                            **
**    / __/ __// _ | / /  / _ |    (c) 2003-2011, LAMP/EPFL             **
**  __\ \/ /__/ __ |/ /__/ __ |    http://scala-lang.org/               **
** /____/\___/_/ |_/____/_/ | |                                         **
**                          |/                                          **
\*                                                                      */



package scala.util.regexp

/** Basic regular expressions.
 *
 *  @author  Burak Emir
 *  @version 1.0
 */
abstract class Base
{
  type _regexpT <: RegExp

  abstract class RegExp {
    val isNullable: Boolean
  }
  
  object Alt {
    /** Alt( R,R,R* ) */
    def apply(rs: _regexpT*) = 
      if (rs.size < 2) throw new SyntaxError("need at least 2 branches in Alt")
      else new Alt(rs: _*)
    // Can't enforce that statically without changing the interface
    // def apply(r1: _regexpT, r2: _regexpT, rs: _regexpT*) = new Alt(Seq(r1, r2) ++ rs: _*)
    def unapplySeq(x: Alt) = Some(x.rs)
  }

  class Alt private (val rs: _regexpT*) extends RegExp {
    final val isNullable = rs exists (_.isNullable)
  }
  
  object Sequ {
    /** Sequ( R,R* ) */
    def apply(rs: _regexpT*) = if (rs.isEmpty) Eps else new Sequ(rs: _*)
    def unapplySeq(x: Sequ) = Some(x.rs)  
  }
  
  class Sequ private (val rs: _regexpT*) extends RegExp {
    final val isNullable = rs forall (_.isNullable)
  }

  case class Star(r: _regexpT) extends RegExp {
    final lazy val isNullable = true
  }

  // The empty Sequ.
  case object Eps extends RegExp {
    final lazy val isNullable = true
    override def toString() = "Eps"
  }

  /** this class can be used to add meta information to regexps */
  class Meta(r1: _regexpT) extends RegExp {
    final val isNullable = r1.isNullable
    def r = r1
  }
}

Other Scala examples (source code examples)

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

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

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 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.