Skip to content

Commit

Permalink
Fix parser's universal minus logic
Browse files Browse the repository at this point in the history
  • Loading branch information
frostburn committed Nov 30, 2023
1 parent 800118f commit 314f26d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
33 changes: 33 additions & 0 deletions src/__tests__/parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,39 @@ describe('Line parser', () => {
).toThrow();
});

it('does parse fractions with universal minus disabled', () => {
const result = parseLine_(
'3/2',
DEFAULT_NUMBER_OF_COMPONENTS,
undefined,
false,
false
);
expect(
result.equals(
new Interval(
ExtendedMonzo.fromFraction(
new Fraction(3, 2),
DEFAULT_NUMBER_OF_COMPONENTS
),
'ratio'
)
)
).toBeTruthy();
});

it('does parse negative cents even with minus disabled for other types', () => {
const result = parseLine_(
'-1.23',
DEFAULT_NUMBER_OF_COMPONENTS,
undefined,
false,
false
);
expect(result.type === 'cents');
expect(result.monzo.totalCents()).toBeCloseTo(-1.23);
});

it('rejects fractions without a numerator', () => {
expect(() => parseLine('/5')).toThrow();
});
Expand Down
6 changes: 4 additions & 2 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,10 @@ export function parseLine(
universalMinus = true
): Interval {
const ast = parseAst(input);
if (!universalMinus && ast.type !== 'CentsLiteral') {
throw new Error('Univeral minus violation');
if (!universalMinus && ast.type === 'UnaryExpression') {
if (ast.operand.type !== 'CentsLiteral') {
throw new Error('Univeral minus violation');
}
}
if (
!admitBareNumbers &&
Expand Down

0 comments on commit 314f26d

Please sign in to comment.