-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from xenharmonic-devs/sw2-grammar
Formalize grammar
- Loading branch information
Showing
10 changed files
with
517 additions
and
368 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
dist/ | ||
docs/ | ||
src/sw2-ast.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -106,3 +106,7 @@ dist | |
# Typedoc | ||
|
||
docs/ | ||
|
||
# Generated grammars | ||
|
||
src/sw2-ast.js |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import {describe, it, expect} from 'vitest'; | ||
import {parse} from '../sw2-ast'; | ||
|
||
describe('Scale Workshop 2 Abstract Syntax Tree Parser', () => { | ||
it('parses plain numbers as plain literals ', () => { | ||
const ast = parse('81'); | ||
expect(ast.type).toBe('PlainLiteral'); | ||
expect(ast.value).toBe(81); | ||
}); | ||
|
||
it('parses dot-separated numbers as cents literals', () => { | ||
const ast = parse('81.80'); | ||
expect(ast.type).toBe('CentsLiteral'); | ||
expect(ast.whole).toBe(81); | ||
expect(ast.fractional).toBe('80'); | ||
}); | ||
|
||
it('parses comma-separated numbers as numeric literals', () => { | ||
const ast = parse('81,80'); | ||
expect(ast.type).toBe('NumericLiteral'); | ||
expect(ast.whole).toBe(81); | ||
expect(ast.fractional).toBe('80'); | ||
}); | ||
|
||
it('parses leading zeroes in the fractional part of cents literals', () => { | ||
const ast = parse('.00123'); | ||
expect(ast.type).toBe('CentsLiteral'); | ||
expect(ast.whole).toBe(null); | ||
expect(ast.fractional).toBe('00123'); | ||
}); | ||
|
||
it('parses leading zeroes in the fractional part of numeric literals', () => { | ||
const ast = parse(',00123'); | ||
expect(ast.type).toBe('NumericLiteral'); | ||
expect(ast.whole).toBe(null); | ||
expect(ast.fractional).toBe('00123'); | ||
}); | ||
|
||
it('parses slash-separated numbers as fraction literals', () => { | ||
const ast = parse('81/80'); | ||
expect(ast.type).toBe('FractionLiteral'); | ||
expect(ast.numerator).toBe(81); | ||
expect(ast.denominator).toBe(80); | ||
}); | ||
|
||
it('parses backslash-separated numbers as EDJI fractions', () => { | ||
const ast = parse('5\\7'); | ||
expect(ast.type).toBe('EdjiFraction'); | ||
expect(ast.numerator).toBe(5); | ||
expect(ast.denominator).toBe(7); | ||
expect(ast.equave).toBe(null); | ||
}); | ||
|
||
it('parses EDJI fractions with explicit equaves', () => { | ||
const ast = parse('6\\13<3>'); | ||
expect(ast.type).toBe('EdjiFraction'); | ||
expect(ast.numerator).toBe(6); | ||
expect(ast.denominator).toBe(13); | ||
expect(ast.equave.type).toBe('PlainLiteral'); | ||
expect(ast.equave.value).toBe(3); | ||
}); | ||
|
||
it('parses space-separated numbers between a square and an angle bracket as monzos', () => { | ||
const ast = parse('[-4 4 -1>'); | ||
expect(ast.type).toBe('Monzo'); | ||
expect(ast.components).toEqual(['-4', '4', '-1']); | ||
}); | ||
|
||
it('parses comma-separated numbers between a square and an angle bracket as monzos', () => { | ||
const ast = parse('[-4, 4, -1>'); | ||
expect(ast.type).toBe('Monzo'); | ||
expect(ast.components).toEqual(['-4', '4', '-1']); | ||
}); | ||
|
||
it('parses unary negated EDJI', () => { | ||
const ast = parse('-1\\12'); | ||
expect(ast.type).toBe('UnaryExpression'); | ||
expect(ast.operator).toBe('-'); | ||
expect(ast.operand.type).toBe('EdjiFraction'); | ||
expect(ast.operand.numerator).toBe(1); | ||
expect(ast.operand.denominator).toBe(12); | ||
expect(ast.operand.equave).toBe(null); | ||
}); | ||
|
||
it('parses binary added numbers and cents', () => { | ||
const ast = parse('2 + 1.23'); | ||
expect(ast.type).toBe('BinaryExpression'); | ||
expect(ast.operator).toBe('+'); | ||
}); | ||
|
||
it('parses binary subtracted numbers and cents', () => { | ||
const ast = parse('2 - 1.23'); | ||
expect(ast.type).toBe('BinaryExpression'); | ||
expect(ast.operator).toBe('-'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.