Skip to content

Instantly share code, notes, and snippets.

@gbluma
Created November 28, 2014 20:00
Show Gist options
  • Save gbluma/2df91241c24d2255b61f to your computer and use it in GitHub Desktop.
Save gbluma/2df91241c24d2255b61f to your computer and use it in GitHub Desktop.
Felix Dictionary parser
class DictionaryParser {
requires package "re2";
open Regdef;
struct KeyValue {
key : string;
value : string;
}
// define some language primitives
regdef colon = ':';
regdef ws = ' '*;
regdef newline = '\n';
regdef anything = perl ("([^:\n]*)");
// define the line structure " property : value\n"
regdef line_def = ws anything ws colon ws anything ws;
// build a parser for the line structure
val parser_re2 = RE2 (render line_def);
// input is text
// output is captured parameters
fun parse(inputLine:string) =>
Match(parser_re2, inputLine);
// input is text
// output is only key/value params
fun parseLine (inputLine:string) =>
match (parse inputLine) with
| Some ?v => Some(KeyValue(v.1, v.2))
| None => None[KeyValue]
endmatch;
// used to filter out results
fun discardNones (a:opt[KeyValue]) : bool =>
match a with
| Some ?v => true
| None => false
endmatch;
// used to filter out results
fun unwrapOption (a:opt[KeyValue]) : KeyValue =>
match a with
| Some ?v => v
endmatch;
// iterate over lines and create a list of key/value pairs
fun parseText (doc:string) =>
split(doc, '\n') // split into lines
|> map parseLine // parse each line into key/value
|> filter discardNones // eliminate any mismatches
|> map unwrapOption // since all remaining are 'Some(a)', unwrap to just 'a'
;
// returns a list of keys from a list of KeyValues
fun keys( inputLines:list[KeyValue] ) : list[string] =>
inputLines |> map (fun (x:KeyValue) => x.key);
// returns a list of values from a list of KeyValues
fun values( inputLines:list[KeyValue] ) : list[string] =>
inputLines |> map (fun (x:KeyValue) => x.value);
}
open DictionaryParser;
val data = (parseText ("""
Name: Garrett
Job: Programmer
Ref: @*#@! Special characters and spaces
"""));
println$ data.keys;
println$ data.values;
// Output:
// list('Name', 'Job', 'Ref')
// list('Garrett', 'Programmer', '@*#@! Special characters and spaces')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment