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

Scala example source code file (RegexTest.scala)

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

array, array, charsequencereader, ident, ident, int, number, parser, parser, regexparsers, regextest, str, string, string

The Scala RegexTest.scala source code


package scala.util.parsing.combinator.testing

import scala.util.parsing.combinator._
import scala.util.parsing.input._

case class Ident(s: String)
case class Number(n: Int)
case class Str(s: String)

object RegexTest extends RegexParsers {
  val ident: Parser[Any] = """[a-zA-Z_]\w*""".r ^^ (s => Ident(s))
  val number: Parser[Any] = """\d\d*""".r ^^ (s => Number(s.toInt))
  val string: Parser[Any] = "\".*\"".r ^^ (s => Str(s.substring(1, s.length - 1)))
  val parser = (ident | number | string)*

  def main(args: Array[String]) = {
    val in = args mkString " "
    println("\nin : "+in)
    println(phrase[Any](parser)(new CharSequenceReader(in)))
  }
}

Other Scala examples (source code examples)

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