position

parsley.position$
object position

This module contains parsers that provide a way to extract position information during a parse.

Position parsers can be important for when the final result of the parser needs to encode position information for later consumption: this is particularly useful for abstract syntax trees. Offset is also exposed by this interface, which may be useful for establishing a caret size in specialised error messages.

Attributes

Since

4.2.0

Source
position.scala
Graph
Supertypes
class Object
trait Matchable
class Any
Self type
position.type

Members list

Grouped members

pos

val col: Parsley[Int]

This parser returns the current column number (starting at 1) of the input without having any other effect.

This parser returns the current column number (starting at 1) of the input without having any other effect.

When this combinator is ran, no input is required, nor consumed, and the current column number will always be successfully returned. It has no other effect on the state of the parser.

Attributes

Returns

a parser that returns the column number the parser is currently at.

Note

in the presence of wide unicode characters, the value returned may be inaccurate.

Example

scala> import parsley.position.col, parsley.character.char
scala> col.parse("")
val res0 = Success(1)
scala> (char('a') *> col).parse("a")
val res0 = Success(2)
scala> (char('\n') *> col).parse("\n")
val res0 = Success(1)
Source
position.scala
val line: Parsley[Int]

This parser returns the current line number (starting at 1) of the input without having any other effect.

This parser returns the current line number (starting at 1) of the input without having any other effect.

When this combinator is ran, no input is required, nor consumed, and the current line number will always be successfully returned. It has no other effect on the state of the parser.

Attributes

Returns

a parser that returns the line number the parser is currently at.

Example

scala> import parsley.position.line, parsley.character.char
scala> line.parse("")
val res0 = Success(1)
scala> (char('a') *> line).parse("a")
val res0 = Success(1)
scala> (char('\n') *> line).parse("\n")
val res0 = Success(2)
Source
position.scala
val pos: Parsley[(Int, Int)]

This parser returns the current line and column numbers (starting at 1) of the input without having any other effect.

This parser returns the current line and column numbers (starting at 1) of the input without having any other effect.

When this combinator is ran, no input is required, nor consumed, and the current line and column number will always be successfully returned. It has no other effect on the state of the parser.

Attributes

Returns

a parser that returns the line and column number the parser is currently at.

Note

in the presence of wide unicode characters, the column value returned may be inaccurate.

Example

scala> import parsley.position.pos, parsley.character.char
scala> pos.parse("")
val res0 = Success((1, 1))
scala> (char('a') *> pos).parse("a")
val res0 = Success((1, 2))
scala> (char('\n') *> pos).parse("\n")
val res0 = Success((2, 1))
Source
position.scala

Value members

Concrete methods

def withWidth[A](p: Parsley[A]): Parsley[(A, Int)]

This combinator returns the result of a given parser and the number of characters it consumed.

This combinator returns the result of a given parser and the number of characters it consumed.

First records the initial offset on entry to given parser p, then executes p. If p succeeds, then the offset is taken again, and the two values are subtracted to give width w. The result of p, x is returned along with w as (x, w). If p fails, this combinator will also fail.

Value parameters

p

the parser to compute the width for

Attributes

Returns

a parser that pairs the result of the parser p with the number of characters it consumed

Since

4.4.0

Note

the value returned is the number of 16-bit characters consumed, not unicode codepoints.

Example

scala> import parsley.position.withWidth, parsley.character.string
scala> withWidth(string("abc")).parse("abc")
val res0 = Success(("abc", 3))
Source
position.scala

Concrete fields

This parser returns the current offset into the input (starting at 0) without having any other effect.

This parser returns the current offset into the input (starting at 0) without having any other effect.

When this combinator is ran, no input is required, nor consumed, and the current offset into the input will always be successfully returned. It has no other effect on the state of the parser.

Attributes

Returns

a parser that returns the offset the parser is currently at.

Note

offset does not take wide unicode codepoints into account.

Example

scala> import parsley.position.offset, parsley.character.char
scala> offset.parse("")
val res0 = Success(0)
scala> (char('a') *> offset).parse("a")
val res0 = Success(1)
scala> (char('\n') *> offset).parse("\n")
val res0 = Success(1)
Source
position.scala