RecordParser

Usage

use RecordParser;

or

import RecordParser;

Support for reading records using regular expressions.

A general purpose record reader/parser for channels. Uses a regular expression to capture portions of the input, and then assigns each capture to each record field.

This module makes the following assumptions:

  1. The file has been opened for reading and a reader channel has been created

  2. The programmer has created a record (NOT a class) in which all fields can be cast from strings (i.e no subrecords, arrays etc).

  3. The number of captures in the regex string provided MUST match the number of fields in the record.

  4. The order in which the fields in the record appear must be in the same order as the record being parsed.

Example 1

use RecordParser;

record Bar {
  var beer: string;
  var Name: string;
}

var f = open("input1.txt", ioMode.rw);
var fr = f.reader();

var M = new RecordReader(Bar, fr);

var c = M.get();
writeln("The first record read is ", c);

writeln("The rest of the records are:");
for r in M.stream() do
  writeln(r);

Example 2

use RecordParser;

record Beer {
  var name:  string;
  var beerId: int;
  var brewerId: int;
  var ABV: real;
  var style: string;
  var appearance: real;
  var aroma: real;
  var palate: real;
  var taste: real;
  var overall: real;
  var time:  int;
  var profileName:  string;
  var text: string;
}


var strt = "\\s*beer/name: (.*)\\s*beer/beerId: (.*)\\s*beer/brewerId: (.*)\\s*beer/ABV: (.*)\\s*beer/style: (.*)\\s*review/appearance: (.*)\\s*review/aroma: (.*)\\s*review/palate: (.*)\\s*review/taste: (.*)\\s*review/overall: (.*)\\s*review/time: (.*)\\s*review/profileName: (.*)\\s*review/text: (.*)";

var N = new RecordReader(Beer, ffr, strt);
writeln("========== test of stream() ==============");
for r in N.stream() do
  writeln(r);

RecordParser Types and Functions

class RecordReader

A class providing the ability to read records matching a regular expression.

type t

The record type to populate

var myReader

The channel to read from

var matchRegex : regex(string)

The regular expression to read (using match on the channel)

proc init(type t, myReader)

Create a RecordReader to match an auto-generated regular expression for a record created by the createRegex routine.

Arguments:
  • t – the record type to read

  • myReader – the channel to read from

proc init(type t, myReader, mRegex)

Create a RecordReader to read using a passed regular expression.

Arguments:
  • t – the record type to read

  • myReader – the channel to read from

  • mRegex – the regular expression to read. This argument currently must be a string, but in the future might be a compiled regular expression.

proc createRegex()

Create a string regular expression for the record type t attached to this RecordReader.

The created regular expression will search for <fieldName1> <spaces> <fieldValue1> <spaces>

iter stream_num(offst: int(64), len: int(64))

Yield records for the range offst..offst+len, but assumes that the channel is already at offst.

Arguments:
  • offst – the current position of the channel

  • len – the number of bytes to read

proc get() throws

Read the next record

iter stream()

Yield the records read