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

Scala example source code file (Parsed.scala)

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

boolean, char, compiler, defaultdelimiters, delimited, int, nsc, parsed, string, todo

The Parsed.scala Scala example source code

/* NSC -- new Scala compiler
 * Copyright 2005-2013 LAMP/EPFL
 * @author Paul Phillips
 */

package scala.tools.nsc
package interpreter

import util.returning

/** One instance of a command buffer.
 */
class Parsed private (
  val buffer: String,
  val cursor: Int,
  val delimited: Char => Boolean
) extends Delimited {
  def isEmpty       = args.isEmpty
  def isUnqualified = args.size == 1
  def isAtStart     = cursor <= 0

  private var _verbosity = 0

  def verbosity = _verbosity
  def withVerbosity(v: Int): this.type = returning[this.type](this)(_ => _verbosity = v)

  def args = toArgs(buffer take cursor).toList
  def bufferHead = args.head
  def headLength = bufferHead.length + 1
  def bufferTail = new Parsed(buffer drop headLength, cursor - headLength, delimited) withVerbosity verbosity

  def prev = new Parsed(buffer, cursor - 1, delimited) withVerbosity verbosity
  def currentChar = buffer(cursor)
  def currentArg = args.last
  def position =
    if (isEmpty) 0
    else if (isLastDelimiter) cursor
    else cursor - currentArg.length

  def isFirstDelimiter  = !isEmpty && isDelimiterChar(buffer.head)
  def isLastDelimiter   = !isEmpty && isDelimiterChar(buffer.last)

  def isQuoted = false // TODO
  def isEscaped = !isAtStart && isEscapeChar(currentChar) && !isEscapeChar(prev.currentChar)
  def isDelimiter = !isQuoted && !isEscaped && isDelimiterChar(currentChar)

  override def toString = "Parsed(%s / %d)".format(buffer, cursor)
}

object Parsed {
  val DefaultDelimiters = "[]{},`; \t".toSet

  private def onull(s: String) = if (s == null) "" else s

  def apply(s: String, cursor: Int): Parsed = apply(onull(s), cursor, DefaultDelimiters)
  def apply(s: String, cursor: Int, delimited: Char => Boolean): Parsed =
    new Parsed(onull(s), cursor, delimited)

  def dotted(s: String, cursor: Int): Parsed = new Parsed(onull(s), cursor, _ == '.')
}

Other Scala source code examples

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