Skip to content

Latest commit

 

History

History
106 lines (77 loc) · 3.39 KB

File metadata and controls

106 lines (77 loc) · 3.39 KB

Parsec library

CPARSEC3 Parsec library provides functionality of parser-combinators.


  • NOTE : Parsec library is UNDER CONSTRUCTION.

Generic Data Type

Generic Data Type is a parametric typed struct/union.

Generic Data Type is a similar concept as known as parameterized type, polymorphic type, template class (C++), and so on.

The below sections show Generic Data Types provided by CPARSEC3 Parsec library.

Parsec(S, T) - a parser object

Parsec(S, T)
A parser object that tries to take token(s) from a stream of type S and returns a corresponding value of type T when it was applied to a stream successfully.
See also ParsecT(S, T) trait.

Token(S) - a token object

Token(S)
A token, which was taken from a stream of type S.
See also Stream(S) trait.

Tokens(S) - a chunk of tokens

Tokens(S)
A chunk of tokens, which was taken from a stream of type S.
See also Stream(S) trait.

Trait

Trait provides a set of functions against a particular concrete type.

Trait is a similar concept as known as type class (Haskell), trait (Rust), interface (Java), and so on.

To use Trait, calling to trait(M) creates a concrete trait object.

The below sections show Traits provided by CPARSEC3 Parsec library.

ParsecT(S, T) - trait to construct, combinate, or apply parser

  • NOTE : Not implemented yet.

Stream(S) - trait to take tokens from a stream

/* String is an instance of Stream */
Stream(String) s = trait(Stream(String));
Maybe(Tuple(Token(String), String)) r = s.take1("abc");
assert(!r.none);
assert(g_eq(r.value.e1, 'a'));  /* a token */
assert(g_eq(r.value.e2, "bc")); /* rest of the stream */
Stream(S)
Type of a trait that provides a set of functions to take token of Token(S) type and/or chunk of tokens of Tokens(S) type from a stream of S type.
trait(Stream(S))
Constructs a new trait of Stream(S) type.
For example, trait(Stream(String)) creates an object of Stream(String) type.

A Stream(S) trait provides the following member functions :

bool empty(S s)
Returns true if the stream s was empty, false otherwise.
Maybe(Tuple(Token(S), S)) take1(S s)
Try to take a token from the stream s.
  • If the stream s was empty, returns m that satisfies the following :
    • m.none = true.
  • Otherwise returns m that satisfies the following :
    • m.none = false.
    • m.value.e1 is the 1st token of Token(S) type.
    • m.value.e2 is the rest of the stream s.
Maybe(Tuple(Tokens(S), S)) takeN(int n, S s)
Try to take a chunk of tokens from the stream s.
  • If n > 0 and the stream s was empty, returns m that satisfies the following :
    • m.none = true.
  • If n > 0 and the stream s was NOT empty, returns m that satisfies the following :
    • m.none = false.
    • m.value.e1 is a chunk of tokens of Tokens(S) type, of n length at most.
    • m.value.e2 is the rest of the stream s.
  • If n ≤ 0, returns m that satisfies the following :
    • m.none = false
    • m.value.e1 is a chunk of tokens of Tokens(S) type, of 0 length.
    • m.value.e2 is the stream s.