org.bitbucket.inkytonik.kiama.parsing

ParsersBase

class ParsersBase extends AnyRef

Simple packrat parsing combinator suite. These combinators are largely source compatible with the Scala parser combinator library but the implementation is simpler and less general. Broadly speaking this library provides behaviour similar to the Scala library RegexParsers trait with sensible defaults and better integration with the rest of Kiama.

The parameter positions provides the position store that this suite should use to track AST node positions. Usually the value passed in is shared with the rest of a program that reports errors etc.

This library should not be used if efficiency is a concern. These combinators are essentially interpreting the grammar so the performance can't compare to a parser generator which compiles the grammar. Nevertheless, the library is perfectly fine for prototyping and for processing small to medium-sized inputs.

Some of the implementation details in this module are based on the implementation of the Scala parser combinator library but in a much simpler form.

The algorithms used here to handle left recursion are from "Packrat parsers can support left recursion" by Warth, Douglass and Millstein, ACM SIGPLAN Symposium on Partial Evaluation and Semantics-based Program Manipulation, 2008.

Source
Parsers.scala
Linear Supertypes
AnyRef, Any
Known Subclasses
Ordering
  1. Alphabetic
  2. By inheritance
Inherited
  1. ParsersBase
  2. AnyRef
  3. Any
  1. Hide All
  2. Show all
Learn more about member selection
Visibility
  1. Public
  2. All

Instance Constructors

  1. new ParsersBase(positions: Positions)

Type Members

  1. sealed abstract class Answer[T] extends AnyRef

    Parsing answers.

  2. trait CCOps[CC[_] <: Seq[_]] extends AnyRef

    Interface for operations needed for repetition collections.

  3. case class Head(rule: Rule, involvedSet: Set[Rule], evalSet: Set[Rule]) extends Product with Serializable

    Information about an active instance of left recursion.

  4. case class LR[T](seed: ParseResult[T], rule: Rule, head: Head, next: LR[T]) extends Answer[T] with Product with Serializable

    An answer that is a left recursion record.

  5. class Marker extends AnyRef

    A marker of a position.

  6. class PackratParser[T] extends Parser[T] with Rule

    A parser that is a memoising, left recursion-detecting encapsulation of a normal parser that returns a value of a particular type.

  7. abstract class Parser[+T] extends (Input) ⇒ ParseResult[T]

    A parser is a function from a string to a parser result.

  8. case class Resolution[T](result: ParseResult[T]) extends Answer[T] with Product with Serializable

    An answer that is a resolved parser result.

  9. trait Rule extends AnyRef

    Common supertype for all rules (ie regardless of result type).

  10. case class ~[+T, +U](_1: T, _2: U) extends Product with Serializable

    Special tuple class to match sequence combinator.

Value Members

  1. final def !=(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  2. final def !=(arg0: Any): Boolean

    Definition Classes
    Any
  3. final def ##(): Int

    Definition Classes
    AnyRef → Any
  4. final def ==(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  5. final def ==(arg0: Any): Boolean

    Definition Classes
    Any
  6. def Parser[T](f: (Input) ⇒ ParseResult[T]): Parser[T]

    Convenience method for making a parser out of its body function, including adding support for whitespace prefix skipping and position recording.

    Convenience method for making a parser out of its body function, including adding support for whitespace prefix skipping and position recording. All parsers should be created using this method so that they share the book-keeping.

  7. def any: Parser[Char]

    A parser that matches any character, failing if the end of input is reached.

  8. final def asInstanceOf[T0]: T0

    Definition Classes
    Any
  9. def clone(): AnyRef

    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  10. def commit[U](p: ⇒ Parser[U]): Parser[U]

    Wrap p so that its failures become errors.

    Wrap p so that its failures become errors. See also nocut.

  11. implicit def constToTupleFunction2[A, B, X](x: (A, B) ⇒ X): (~[A, B]) ⇒ X

    Convenience conversion to allow arity two functions to be used directly in tree construction actions.

  12. implicit def constToTupleFunction3[A, B, C, X](x: (A, B, C) ⇒ X): (~[~[A, B], C]) ⇒ X

    Convenience conversion to allow arity three functions to be used directly in tree construction actions.

  13. implicit def constToTupleFunction4[A, B, C, D, X](x: (A, B, C, D) ⇒ X): (~[~[~[A, B], C], D]) ⇒ X

    Convenience conversion to allow arity four functions to be used directly in tree construction actions.

  14. implicit def constToTupleFunction5[A, B, C, D, E, X](x: (A, B, C, D, E) ⇒ X): (~[~[~[~[A, B], C], D], E]) ⇒ X

    Convenience conversion to allow arity five functions to be used directly in tree construction actions.

  15. implicit def constToTupleFunction6[A, B, C, D, E, F, X](x: (A, B, C, D, E, F) ⇒ X): (~[~[~[~[~[A, B], C], D], E], F]) ⇒ X

    Convenience conversion to allow arity six functions to be used directly in tree construction actions.

  16. lazy val constrainedInt: Parser[Int]

    Parse digit strings that are constrained to fit into an Int value.

    Parse digit strings that are constrained to fit into an Int value. If the digit string is too big, a parse error results.

  17. def elem(message: String, p: (Char) ⇒ Boolean): Parser[Char]

    A parser that accepts just those characters that pass the given predicate.

    A parser that accepts just those characters that pass the given predicate. The message is used to describe what was expected if an error occurs.

  18. def elem(ch: Char): Parser[Char]

    A parser that accepts just the given character.

  19. final def eq(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  20. def equals(arg0: Any): Boolean

    Definition Classes
    AnyRef → Any
  21. def error(message: String): Parser[Nothing]

    A parser that always errors with the given message.

  22. def failure(message: String): Parser[Nothing]

    A parser that always fails with the given message.

  23. def finalize(): Unit

    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  24. final def getClass(): Class[_]

    Definition Classes
    AnyRef → Any
  25. def grep[T, CC[_] <: Seq[_]](p: ⇒ Parser[T])(ops: CCOps[CC]): Parser[CC[T]]

    Generic repetition zero or more times.

  26. def grep1[T, CC[_] <: Seq[_]](p: ⇒ Parser[T])(ops: CCOps[CC]): Parser[CC[T]]

    Generic repetition one or more times.

  27. def grep1sep[T, CC[_] <: Seq[_]](p: ⇒ Parser[T], q: ⇒ Parser[Any])(ops: CCOps[CC]): Parser[CC[T]]

    Generic repetition one or more times with separators.

  28. def grepsep[T, CC[_] <: Seq[_]](p: ⇒ Parser[T], q: ⇒ Parser[Any])(ops: CCOps[CC]): Parser[CC[T]]

    Generic repetition zero or more times with separators.

  29. def guard[T](p: ⇒ Parser[T]): Parser[T]

    A parser that succeeds iff its argument parser succeeds, but consumes no input in any circumstances.

  30. def hashCode(): Int

    Definition Classes
    AnyRef → Any
  31. var heads: HashMap[Input, Head]

    Map between left input positions and active left recursion instances.

  32. final def isInstanceOf[T0]: Boolean

    Definition Classes
    Any
  33. def keywords(ext: Regex, kws: List[String]): Parser[String]

    Parser for keywords.

    Parser for keywords. The list of string arguments gives the text of the keywords in a language. The regular expression gives the possible extension of the keyword to stop the keyword being seen as an identifier instead. For example, the keyword list might contain "begin" and "end" and the extension regular expression might be [^a-zA-Z0-9]. Thus, begin followed by something other than a letter or digit is a keyword, but beginfoo8 is an identifier. This parser succeeds if any of the keywords is present, provided that it's not immediately followed by something that extends it.

  34. lazy val latestNoSuccess: DynamicVariable[Option[NoSuccess]]

    Record lack of success so that we can nicely handle the case where a phrase doesn't parse when looking for the end of input but there was a later lack of success for some other reason.

  35. implicit def literal(s: String): Parser[String]

    A parser that matches a literal string after skipping any whitespace.

    A parser that matches a literal string after skipping any whitespace. The form of the latter is defined by the whitespace parser.

  36. def mark[T](p: ⇒ Parser[String]): Parser[Marker]

    Mark a string parser so that its value is discarded but a marker is returned instead.

    Mark a string parser so that its value is discarded but a marker is returned instead. That return value can then be used to set the position of another value. We can't use the string value itself since we are not guaranteed to have reference equality on strings.

  37. implicit def memo[T](parser: ⇒ Parser[T]): PackratParser[T]

    (Implicit) conversion of non-memoising parser into a memoising one.

  38. final def ne(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  39. def nocut[T](p: ⇒ Parser[T]): Parser[T]

    Suppress cuts in the parser p.

    Suppress cuts in the parser p. I.e., errors produced by p are propagated as failures instead. See also commit.

  40. def not[T](p: ⇒ Parser[T]): Parser[Unit]

    Invert the result of a parser without consuming any input.

  41. final def notify(): Unit

    Definition Classes
    AnyRef
  42. final def notifyAll(): Unit

    Definition Classes
    AnyRef
  43. def opt[T](p: ⇒ Parser[T]): Parser[Option[T]]

    Optional parsing.

  44. def parse[T](p: Parser[T], source: Source): ParseResult[T]

    Run a parser on a string to obtain its result.

  45. def parseAll[T](p: Parser[T], source: Source): ParseResult[T]

    Run a parser on all of a string to obtain its result.

  46. implicit def parseResultToTuple2[A, B](p: Parser[~[A, B]]): Parser[(A, B)]

    Convenience conversion to lift parsers that return 2-tilde-tuples to parsers that return regular 2-tuples.

  47. implicit def parseResultToTuple3[A, B, C](p: Parser[~[~[A, B], C]]): Parser[(A, B, C)]

    Convenience conversion to lift parsers that return 3-tilde-tuples to parsers that return regular 3-tuples.

  48. implicit def parseResultToTuple4[A, B, C, D](p: Parser[~[~[~[A, B], C], D]]): Parser[(A, B, C, D)]

    Convenience conversion to lift parsers that return 4-tilde-tuples to parsers that return regular 4-tuples.

  49. implicit def parseResultToTuple5[A, B, C, D, E](p: Parser[~[~[~[~[A, B], C], D], E]]): Parser[(A, B, C, D, E)]

    Convenience conversion to lift parsers that return 5-tilde-tuples to parsers that return regular 5-tuples.

  50. implicit def parseResultToTuple6[A, B, C, D, E, F](p: Parser[~[~[~[~[~[A, B], C], D], E], F]]): Parser[(A, B, C, D, E, F)]

    Convenience conversion to lift parsers that return 6-tilde-tuples to parsers that return regular 6-tuples.

  51. def parseWhitespace(in: Input): ParseResult[Any]

    If we are parsing whitespace already, succeed with no progress so that we don't recurse.

    If we are parsing whitespace already, succeed with no progress so that we don't recurse. If we are not already parsing whitespace, then apply the whitespace parser, swallowing any errors from it unless they occur at the end of the input. In other words, an error not at the end is treated as the absence of whitespace.

  52. var parsingWhitespace: Boolean

    Are we currently parsing whitespace?

  53. def phrase[T](p: ⇒ Parser[T]): Parser[T]

    Phrases, i.

    Phrases, i.e., parse and succeed with no input remaining, except possibly for whitespace at the end. If there is another later failure, prefer it over the failure due to end of input being expected since the later one is usually more informative.

  54. implicit def regex(r: Regex): Parser[String]

    A parser that matches a regex string after skipping any whitespace.

    A parser that matches a regex string after skipping any whitespace. The form of the latter is defined by the whitespace parser.

  55. def stringToInt(s: String): Either[Int, String]

    Convert the digit string s to an Int if it's in range, but return an error message if it's too big.

  56. def success[T](v: ⇒ T): Parser[T]

    Succeed with a given value consuming no non-whitespace input.

    Succeed with a given value consuming no non-whitespace input. The value is evaluated each time this parser is used.

  57. final def synchronized[T0](arg0: ⇒ T0): T0

    Definition Classes
    AnyRef
  58. def toString(): String

    Definition Classes
    AnyRef → Any
  59. def updateLatestNoSuccess[T](res: NoSuccess): ParseResult[T]

  60. final def wait(): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  61. final def wait(arg0: Long, arg1: Int): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  62. final def wait(arg0: Long): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  63. def whitespace: Parser[Any]

    A parser that skips whitespace (default: sequences of zero or more whitespace characters).

    A parser that skips whitespace (default: sequences of zero or more whitespace characters). This definition can be overridden as long as the new definition succeeds at the end of the input.

  64. def wrap[T, U](p: ⇒ Parser[T], f: (T) ⇒ Either[U, String]): Parser[U]

    Wrap a parser p that produces a value of type T to produce a parser returning values of type U.

    Wrap a parser p that produces a value of type T to produce a parser returning values of type U.

    The function f is responsible for converting the T value into either a U value or a string that indicates what went wrong. In the latter case, the resulting parser will error at the original position with the message, ignoring any other errors at that position. Failures or errors of p will be lifted to the returned type.

Inherited from AnyRef

Inherited from Any

Ungrouped