Skip to content

Commit

Permalink
Fixed empty {} not handled. Added simple nested test
Browse files Browse the repository at this point in the history
  • Loading branch information
mationai committed May 13, 2022
1 parent 21b8483 commit 36cf5a2
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 4 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ For certain applications, documentation may be of much greater importance than c
## Usage

```cs
yarn add mation-spec@https://github.com/fuzzthink/mation-spec/tarball/v0.1.4 // For now
yarn add mation-spec@https://github.com/fuzzthink/mation-spec/tarball/<latest> // For now
// where <latest> is eg. v0.1.5, see Releases for latest
yarn add 'mation-spec' // When package is published
```

Expand Down
4 changes: 3 additions & 1 deletion src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,16 @@ mationGrammar {
KeyValues = (KeyValue ","?)+ //> ","? // Nonempty needed for + on parent
KeyValue = Key ":" Value
Map = "{" KeyValues "}"
EmptyMap = "{" "}" // can't be lexical rule
Key
= identifier
| stringLiteral
| numericLiteral
Value
= KeyValue
| Map
| EmptyMap
| identifier
| literal
| List
Expand Down
2 changes: 2 additions & 0 deletions src/mationGrammar.ohm
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mationGrammar {
KeyValues = (KeyValue ","?)+ //> ","? // Nonempty needed for + on parent
KeyValue = Key ":" Value
Map = "{" KeyValues "}"
EmptyMap = "{" "}" // can't be lexical rule

Key
= identifier
Expand All @@ -12,6 +13,7 @@ mationGrammar {
Value
= KeyValue
| Map
| EmptyMap
| identifier
| literal
| List
Expand Down
6 changes: 4 additions & 2 deletions src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ parse.literal = (literal={}) => {
case 'stringLiteral':
return sourceString.slice(1, -1)
}
throw new Error(`Handler missing for literal of type: ${ctorName}`)
throw new Error(`Parser missing handler for literal of type: ${ctorName}`)
}

parse.List = (iter={}) => {
Expand Down Expand Up @@ -65,8 +65,10 @@ parse.Value = (node={}) => {
return parse.KeyValue(node.child(0), node.child(2))
case 'Map':
return parse.Map(node)
case 'EmptyMap':
return {}
}
throw new Error(`Handler missing for Value of type: ${node.ctorName}`)
throw new Error(`Parser missing handler for Value of type: ${node.ctorName}`)
}

parse.Command = (Command={}) => {
Expand Down
35 changes: 35 additions & 0 deletions test/nested.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { test, expect } from 'vitest'
import { parse } from '../src/api'

test(`Nested - Mostly emtpy structures, with good mix of comma/no-comma`, () => {
expect(parse(`
/** md **
*/
model: {
dimensions: letter
creases: {
method: acrossPoints
valueType: index,
values: [
[0 1] //1
]
},
},
tweenDuration: {
}
steps: [
]
`).result)
.toStrictEqual({
model: {
dimensions: 'letter',
creases: {
method: 'acrossPoints',
valueType: 'index',
values: [[0, 1]],
},
},
tweenDuration: {},
steps: [],
})
})

0 comments on commit 36cf5a2

Please sign in to comment.