Skip to content

Commit

Permalink
Cosmetic and structural updates
Browse files Browse the repository at this point in the history
  • Loading branch information
izeigerman committed Apr 20, 2018
1 parent db2bc51 commit 6533cb4
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 21 deletions.
4 changes: 2 additions & 2 deletions core/src/main/scala/parsecat/parsers/CharacterParsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ trait CharacterParsers extends Combinators {
input.char(pos.pos) match {
case Right((ch, nextInput)) =>
if (p(ch)) {
val newPos = TextPosition.getNextPos(ch, pos)
val newPos = pos.getNextPosition(ch)
ParseOutput(newPos, nextInput, context, ch).asRight
} else {
ParseError(pos, s"unexpected character '$ch'", info).asLeft
Expand All @@ -65,7 +65,7 @@ trait CharacterParsers extends Combinators {
input.stringOfLength(s.length, pos.pos) match {
case Right((actual, nextInput)) =>
if (s.contentEquals(actual)) {
ParseOutput(TextPosition.getNextPos(s, pos), nextInput, context, s).asRight
ParseOutput(pos.getNextPosition(s), nextInput, context, s).asRight
} else {
ParseError(pos, s"input doesn't match value '$s'", info).asLeft
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/scala/parsecat/parsers/RegexParsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ trait RegexParsers extends CharacterParsers {
val remainder = input.pageRemainder(pos.pos)
val regexMatch = r.findPrefixOf(remainder)
regexMatch
.map(out => ParseOutput(TextPosition.getNextPos(out, pos), input, context, out).asRight)
.map(out => ParseOutput(pos.getNextPosition(out), input, context, out).asRight)
.getOrElse(ParseError(pos, s"input doesn't match regex '$r'", info).asLeft)
} else {
ParseError(pos, "can't apply regex on a multi-page stream", info).asLeft
Expand Down
24 changes: 12 additions & 12 deletions core/src/main/scala/parsecat/parsers/TextPosition.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,23 @@
package parsecat.parsers

import cats._
import cats.instances.int._
import cats.instances.long._

final case class TextPosition(pos: Int, row: Int, col: Int)

object TextPosition {
implicit val showForTextPosition: Show[TextPosition] = Show.show(p => s"row ${p.row}, column ${p.col}")
implicit val orderForTextPosition: Order[TextPosition] = Order.by(_.pos)

def getNextPos(str: String, pos: TextPosition): TextPosition = {
str.foldLeft(pos)((p, c) => getNextPos(c, p))
final case class TextPosition(pos: Long, row: Int, col: Int) {
def getNextPosition(str: String): TextPosition = {
str.foldLeft(this)((p, c) => p.getNextPosition(c))
}

def getNextPos(char: Char, pos: TextPosition): TextPosition = {
def getNextPosition(char: Char): TextPosition = {
if (char == '\n') {
TextPosition(pos.pos + 1, pos.row + 1, 1)
TextPosition(pos + 1, row + 1, 1)
} else {
TextPosition(pos.pos + 1, pos.row, pos.col + 1)
TextPosition(pos + 1, row, col + 1)
}
}
}

object TextPosition {
implicit val showForTextPosition: Show[TextPosition] = Show.show(p => s"row ${p.row}, column ${p.col}")
implicit val orderForTextPosition: Order[TextPosition] = Order.by(_.pos)
}
10 changes: 4 additions & 6 deletions core/src/main/scala/parsecat/stream/PagedStringStream.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ import java.io.{InputStream, InputStreamReader, Reader}
import cats.implicits._
import PagedStringStream._

private[parsecat] final class PagedStringStream(stream: Stream[Array[Char]],
pageOffset: Long,
val isSinglePage: Boolean) {
private[parsecat] final case class PagedStringStream(stream: Stream[Array[Char]],
pageOffset: Long,
isSinglePage: Boolean) {

def char(offset: Long): Either[String, (Char, PagedStringStream)] = {
if (offset < pageOffset) {
Expand Down Expand Up @@ -79,7 +79,7 @@ private[parsecat] final class PagedStringStream(stream: Stream[Array[Char]],

def isEmpty: Boolean = stream.isEmpty

private def nextPage: PagedStringStream = {
def nextPage: PagedStringStream = {
PagedStringStream(stream.tail, pageOffset + stream.head.length, isSinglePage)
}
}
Expand Down Expand Up @@ -138,7 +138,6 @@ object PagedStringStream {
implicit def fromCharArrayIterable(i: Iterable[Array[Char]]): PagedStringStream = fromCharArrayIterator(i.iterator)

final case class SlicedCharSequence(original: Array[Char], startIdx: Int, endIdx: Int) extends CharSequence {

override def length(): Int = Math.min(endIdx, original.length) - startIdx

override def subSequence(start: Int, end: Int): CharSequence =
Expand All @@ -150,7 +149,6 @@ object PagedStringStream {
}

final case class CompositeCharSequence(first: CharSequence, second: CharSequence) extends CharSequence {

override def length(): Int = first.length() + second.length()

override def subSequence(start: Int, end: Int): CharSequence = {
Expand Down

0 comments on commit 6533cb4

Please sign in to comment.