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

Scala example source code file (packrat3.scala)

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

list, nosuccess, packratparser, packratparser, packratparsers, packratreader, packratreader, parser, parser, some, standardtokenparsers, success, test, unit

The Scala packrat3.scala source code

import scala.util.parsing.combinator._

import scala.util.parsing.combinator.syntactical.StandardTokenParsers
import scala.util.parsing.input._
import scala.util.parsing.syntax._

import scala.collection.mutable.HashMap

object Test {
  def main(args: Array[String]): Unit = {
    import grammars3._

    val head = phrase(AnBnCn)

    println(extractResult(head(new lexical.Scanner("a b c"))))
    println(extractResult(head(new lexical.Scanner("a a b b c c"))))
    println(extractResult(head(new lexical.Scanner("a a a b b b c c c"))))
    println(extractResult(head(new lexical.Scanner("a a a a b b b b c c c c"))))

    println(extractResult(AnBnCn(new PackratReader(new lexical.Scanner("a a a b b b b c c c c")))))
    println(extractResult(AnBnCn(new PackratReader(new lexical.Scanner("a a a a b b b c c c c")))))
    println(extractResult(AnBnCn(new PackratReader(new lexical.Scanner("a a a a b b b b c c c")))))
  }
}

object grammars3 extends StandardTokenParsers with PackratParsers {
  
  def extractResult(r: ParseResult[_]) = r match {
    case Success(a,_) => a
    case NoSuccess(a,_) => a
  }
  

  lexical.reserved ++= List("a","b", "c")
  val a: PackratParser[Any] = memo("a")
  val b: PackratParser[Any] = memo("b")
  val c: PackratParser[Any] = memo("c")

  val AnBnCn: PackratParser[Any] = 
    guard(repMany1(a,b) ~ not(b)) ~ rep1(a) ~ repMany1(b,c)// ^^{case x~y => x:::y}


  private def repMany[T](p: => Parser[T], q: => Parser[T]): Parser[List[T]] = 
  ( p~repMany(p,q)~q ^^ {case x~xs~y => x::xs:::(y::Nil)}
   | success(Nil)
  )

  def repMany1[T](p: => Parser[T], q: => Parser[T]): Parser[List[T]] = 
   p~opt(repMany(p,q))~q ^^ {case x~Some(xs)~y => x::xs:::(y::Nil)}

} 

Other Scala examples (source code examples)

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