Skip to content

Commit

Permalink
Export parser functions via a namespae
Browse files Browse the repository at this point in the history
  • Loading branch information
octachrome committed Jul 13, 2014
1 parent 04585e6 commit ae9cb1a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 34 deletions.
52 changes: 30 additions & 22 deletions js/lpParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,39 +53,47 @@ function mkLp(objective, constraints) {
};
}

var exports = window;

function createLpParser() {
exports.pNum = pApply(pSat(isNum), parseFloat);
exports.pSym = pApply(pSat(isAlpha), mkTerm);
exports.pCoefSym = pThen(applyCoef, pNum, pSym);
exports.pTerm = pAlt(pSym, pCoefSym);
var pNum = pApply(pSat(isNum), parseFloat);
var pSym = pApply(pSat(isAlpha), mkTerm);
var pCoefSym = pThen(applyCoef, pNum, pSym);
var pTerm = pAlt(pSym, pCoefSym);

var pPlus = pApply(pLit(Tokens.PLUS), ret(1));
var pMinus = pApply(pLit(Tokens.MINUS), ret(-1));
var pSign = pAlt(pPlus, pMinus);

exports.pPlus = pApply(pLit(Tokens.PLUS), ret(1));
exports.pMinus = pApply(pLit(Tokens.MINUS), ret(-1));
exports.pSign = pAlt(pPlus, pMinus);
var pSignedTerm = pThen(applyCoef, pSign, pTerm);
var pInitialTerm = pAlt(pTerm, pSignedTerm);
var pExpr = pThen(cons, pInitialTerm, pZeroOrMore(pSignedTerm));

exports.pSignedTerm = pThen(applyCoef, pSign, pTerm);
exports.pInitialTerm = pAlt(pTerm, pSignedTerm);
exports.pExpr = pThen(cons, pInitialTerm, pZeroOrMore(pSignedTerm));
var pSignedNum = pThen(mul, pSign, pNum);

exports.pSignedNum = pThen(mul, pSign, pNum);
var pIneq = pAlt(pLit(Tokens.LE), pLit(Tokens.GE), pLit(Tokens.EQ));
var pAnonConstraint = pThen3(mkConstraint, pExpr, pIneq, pAlt(pNum, pSignedNum));
var pNamedConstraint = pThen3(nameConstraint, pSat(isAlpha), pLit(Tokens.COLON), pAnonConstraint);
var pConstraint = pAlt(pAnonConstraint, pNamedConstraint);

exports.pIneq = pAlt(pLit(Tokens.LE), pLit(Tokens.GE), pLit(Tokens.EQ));
exports.pAnonConstraint = pThen3(mkConstraint, pExpr, pIneq, pAlt(pNum, pSignedNum));
exports.pNamedConstraint = pThen3(nameConstraint, pSat(isAlpha), pLit(Tokens.COLON), pAnonConstraint);
exports.pConstraint = pAlt(pAnonConstraint, pNamedConstraint);
var pDirection = pAlt(pLit(Tokens.MIN), pLit(Tokens.MAX));
var pObjective = pThen(mkObjective, pDirection, pExpr);

exports.pDirection = pAlt(pLit(Tokens.MIN), pLit(Tokens.MAX));
exports.pObjective = pThen(mkObjective, pDirection, pExpr);
var pConstraints = pThen(mkConstraints, pLit(Tokens.SUBJECT_TO), pOneOrMore(pConstraint));

exports.pConstraints = pThen(mkConstraints, pLit(Tokens.SUBJECT_TO), pOneOrMore(pConstraint));
var pLp = pThen3(mkLp, pObjective, pConstraints, pLit(Tokens.END));

exports.pLp = pThen3(mkLp, pObjective, pConstraints, pLit(Tokens.END));
window.lpParser = {
pNum: pNum,
pSignedTerm: pSignedTerm,
pExpr: pExpr,
pConstraint: pConstraint,
pConstraints: pConstraints,
pObjective: pObjective,
pLp: pLp
};
}

function readProb(str) {
var toks = clex(fromArray(str));
var result = pLp(toks);
var result = lpParser.pLp(toks);
return takeFirstParse(result);
}
2 changes: 1 addition & 1 deletion js/mpsParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ function createMpsParser() {
var pRhsSection = pThen(second, pLit('RHS'), pRhsSeries);
var pProb = pThen3(mkProb, pRowsSection, pColumnsSection, pRhsSection);

var mpsParser = window.mpsParser = {
window.mpsParser = {
pRow: pRow,
pColumn: pColumn,
pColumns: pColumns,
Expand Down
22 changes: 11 additions & 11 deletions test/lpParserSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ describe('lp parser', function () {
describe('pNum', function () {
it('should parse a list of numbers', function () {
var toks = clex(fromArray('1.5 44 124'));
var parser = pOneOrMore(pNum);
var parser = pOneOrMore(lpParser.pNum);
var result = parser(toks);

expect(result).toEqual(fromArray([{
Expand All @@ -25,7 +25,7 @@ describe('lp parser', function () {
describe('pExpr', function () {
it('should parse a signed term', function () {
var toks = clex(fromArray('-1.4x'));
var result = pSignedTerm(toks);
var result = lpParser.pSignedTerm(toks);

expect(result).toEqual(fromArray([{
result: {
Expand All @@ -38,7 +38,7 @@ describe('lp parser', function () {

it('should parse a large signed term', function () {
var toks = clex(fromArray('+ 99 y'));
var result = pSignedTerm(toks);
var result = lpParser.pSignedTerm(toks);

expect(result).toEqual(fromArray([{
result: {
Expand All @@ -51,7 +51,7 @@ describe('lp parser', function () {

it('should parse a sequence of signed terms', function () {
var toks = clex(fromArray('-1.4x + y - 2 z'));
var result = pExpr(toks);
var result = lpParser.pExpr(toks);

// three different ways to consume one or more terms
expect(length(result)).toBe(3);
Expand All @@ -74,7 +74,7 @@ describe('lp parser', function () {

it('should parse an expression without an initial sign', function () {
var toks = clex(fromArray('1.4x + y - 2 z'));
var result = pExpr(toks);
var result = lpParser.pExpr(toks);

// three different ways to consume one or more terms
expect(length(result)).toBe(3);
Expand All @@ -94,7 +94,7 @@ describe('lp parser', function () {

it('should parse a constraint', function () {
var toks = clex(fromArray('12 var1 - var2 >= 4'));
var result = pConstraint(toks);
var result = lpParser.pConstraint(toks);
expect(takeFirstParse(result)).toEqual({
expr: [{sym: 'var1', coef: 12}, {sym: 'var2', coef: -1}],
op: '>=',
Expand All @@ -104,7 +104,7 @@ describe('lp parser', function () {

it('should parse a constraint with a signed rhs', function () {
var toks = clex(fromArray('12 var1 - var2 >= -4'));
var result = pConstraint(toks);
var result = lpParser.pConstraint(toks);
expect(takeFirstParse(result)).toEqual({
expr: [{sym: 'var1', coef: 12}, {sym: 'var2', coef: -1}],
op: '>=',
Expand All @@ -114,7 +114,7 @@ describe('lp parser', function () {

it('should parse a named constraint', function () {
var toks = clex(fromArray('cons1: 12 var1 - var2 >= -4'));
var result = pConstraint(toks);
var result = lpParser.pConstraint(toks);
expect(takeFirstParse(result)).toEqual({
name: 'cons1',
expr: [{sym: 'var1', coef: 12}, {sym: 'var2', coef: -1}],
Expand All @@ -128,7 +128,7 @@ describe('lp parser', function () {
"minimise\n\
12 var1 - var2"
));
var result = pObjective(toks);
var result = lpParser.pObjective(toks);
expect(takeFirstParse(result)).toEqual({
dir: 'minimise',
expr: [{sym: 'var1', coef: 12}, {sym: 'var2', coef: -1}]
Expand All @@ -141,7 +141,7 @@ describe('lp parser', function () {
12 var1 - var2 <= -30\n\
c: var1 + var2 >= 3\n"
));
var result = pConstraints(toks);
var result = lpParser.pConstraints(toks);
expect(takeFirstParse(result)).toEqual([{
expr: [{sym: 'var1', coef: 12}, {sym: 'var2', coef: -1}],
op: '<=',
Expand All @@ -163,7 +163,7 @@ describe('lp parser', function () {
b: 2z + 50y <= 100\n\
end\n"
));
var result = pLp(toks);
var result = lpParser.pLp(toks);
expect(takeFirstParse(result)).toEqual({
objective: {
dir: 'maximise',
Expand Down

0 comments on commit ae9cb1a

Please sign in to comment.