-
Notifications
You must be signed in to change notification settings - Fork 9
/
document-parser.js
39 lines (34 loc) · 1.33 KB
/
document-parser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
"use strict";
var Parser = require("./parser");
// produces a document parser from a word parser in an arbitrary mode
module.exports = makeDocumentParser;
function makeDocumentParser(parseWord, makeOptions) {
var parseLine = Parser.makeDelimitedParser(parseWord, parseSomeSpaces);
var parseParagraph = Parser.makeDelimitedParser(parseLine, parseNewlineSpace);
var parseSection = Parser.makeDelimitedParser(parseParagraph, parseNewlineSpace);
var parseDocument = Parser.makeDelimitedParser(parseSection, parseNewlineSpaces);
return Parser.makeParser(function (callback, options) {
options = makeOptions(options);
var state = parseDocument(callback, options);
return state;
});
}
var parseSpace = Parser.makeExpect(" ");
var parseAnySpaces = Parser.makeParseAny(parseSpace);
var parseSomeSpaces = Parser.makeParseSome(parseSpace);
var parseNewline = Parser.makeExpect("\n");
var parseNewlines = Parser.makeParseSome(parseNewline);
function parseNewlineSpace(callback) {
return parseAnySpaces(function () {
return parseNewline(function () {
return parseAnySpaces(callback);
});
});
}
function parseNewlineSpaces(callback) {
return parseAnySpaces(function () {
return parseNewlines(function () {
return parseAnySpaces(callback);
});
});
}