package pacomb

  1. Overview
  2. Docs

Module Pacomb.InputSource

A module providing efficient input buffers with preprocessing.

Type

Sourcetype buffer

The abstract type for an input buffer.

Sourcetype pos

The abstract type position in the buffer

Sourceval init_pos : pos

position at the beginning of a buffer

Reading from a buffer

Sourceval read : buffer -> pos -> char * buffer * pos

read buf pos returns the character at position pos in the buffer buf, together with the new buffer and position.

Sourceval sub : buffer -> pos -> int -> string

sub b i len returns len characters from position pos. If the end of buffer is reached, the string is filed with eof '\255'

Sourceval get : buffer -> pos -> char

get buf pos returns the character at position pos in the buffer buf.

Creating a buffer

Sourcetype context = Utf8.context
Sourceval from_file : ?utf8:context -> string -> buffer

from_file fn returns a buffer constructed using the file fn. if utf8 is Utf8.UTF8 (Utf8.ASCII is the default), positions are reported according to utf8. read is still reading bytes.

Sourceval from_channel : ?utf8:context -> ?filename:string -> in_channel -> buffer

from_channel ~filename ch returns a buffer constructed using the channel ch. The optional filename is only used as a reference to the channel in error messages.

Sourceval from_string : ?utf8:context -> ?filename:string -> string -> buffer

from_string ~filename str returns a buffer constructed using the string str. The optional filename is only used as a reference to the channel in error messages.

Sourceval from_fun : ('a -> unit) -> context -> string -> ('a -> string * bool) -> 'a -> buffer

from_fun finalise utf8 name get data returns a buffer constructed from the object data using the get function. The get function is used to obtain one line of input from data. The finalise function is applied to data when the end of file is reached. The name string is used to reference the origin of the data in error messages. Position are reported according to utf8.

Sourceexception Preprocessor_error of string * string

Exception that can be raised by a preprocessor in case of error. The first string references the name of the buffer (e.g. the name of the corresponding file) and the second string contains the message.

Sourceval pp_error : string -> string -> 'a

pp_error name msg raises Preprocessor_error(name,msg).

Sourcemodule type Preprocessor = sig ... end

Specification of a preprocessor.

Sourcemodule WithPP (PP : Preprocessor) : sig ... end

Functor for building buffers with a preprocessor.

Buffer manipulation functions

Sourceval is_empty : buffer -> int -> bool

is_empty buf test whether the buffer buf is empty.

Sourceval line_num : buffer -> int

line_num buf returns the current line number of buf.

Sourceval col_num : buffer -> pos -> int
Sourceval line_offset : buffer -> int

line_beginning buf returns the offset of the current line in the buffer buf.

Sourceval char_pos : buffer -> pos -> int

position in the file

Sourceval byte_pos : buffer -> pos -> int

position in bytes, regardless to utf8

Sourceval normalize : buffer -> pos -> buffer * pos

normalize buf pos ensures that pos is less than the length of the current line in str.

Sourceval filename : buffer -> string

filename buf returns the file name associated to the buf.

Sourceval buffer_uid : buffer -> int

buffer_uid buf returns a unique identifier for buf.

Sourceval buffer_equal : buffer -> buffer -> bool

buffer_eq b1 b2 tests the equality of b1 and b2.

Sourceval buffer_compare : buffer -> buffer -> int

buffer_compare b1 b2 compares b1 and b2.

Sourceval buffer_before : buffer -> int -> buffer -> int -> bool

buffer_before b1 i1 b2 i2 returns true if the position b1, i1 is before b2, i2. Gives meaningless result if b1 and b2 do not refer to the same file.

Sourcemodule Tbl : sig ... end

Table to associate value to positions in input buffers. The complexity of access in the table is O(ln(N)) where N is the number of tables.