Skip to content

Instantly share code, notes, and snippets.

@chris-martin
Created November 19, 2012 01:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chris-martin/4108467 to your computer and use it in GitHub Desktop.
Save chris-martin/4108467 to your computer and use it in GitHub Desktop.
Ply (scala parser combinators)
package vine
import scala.util.parsing.combinator.syntactical._
import util.parsing.input.{CharSequenceReader, Reader}
import java.io.InputStream
import collection.immutable.PagedSeq
import io.Source
import util.parsing.combinator.RegexParsers
object Ply extends RegexParsers {
/* import lexical._
override type Elem = Token*/
override def skipWhitespace = false
val eot = "\u001a"
val eol = System.getProperty("line.separator")
val t = eot | eol
val word = ("""\S*"""r)
val whitespace = ("""\w*"""r)
/*
reserved += (eot, eol,
"ply", "format ascii 1.0", "comment", "element",
"vertex", "face", "property", "end_header",
"format")
*/
//def ply = header ~ body ~ body
def ply = "ply" ~ eol ~ "ply"
def body = ((((word)*) ~ t)*)
def header = plyFormat ~ vertexHead ~ faceHead ~ "end_header" ~ t
def plyFormat = "ply" ~ t ~ "format ascii 1.0" ~ t
def property = "property" ~ ((word)*) ~ ((word)*) ~ (word) ~ t
def properties = ((property)*)
def vertexHead = "element" ~ "vertex" ~ (whitespace) ~ t ~ properties
def faceHead = "element" ~ "face" ~ (whitespace) ~ t ~ properties
def parse(r:Reader[Char]) {
var fn = phrase(ply)
//val in = new Scanner(r)
val result:ParseResult[Any] = fn(r)
println(result)
}
implicit def streamToReader(i:InputStream):Reader[Char] =
new CharSequenceReader(PagedSeq.fromSource(Source.fromInputStream(i)))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment