Skip to content

Commit 682ff59

Browse files
committed
Fixup + basic example
1 parent e6cf45e commit 682ff59

10 files changed

Lines changed: 172 additions & 25 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Tests/Experimental

README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,17 @@
11

2-
**Work In Progress**
2+
# HJSON   [![Badge License]][License]
3+
4+
*A parser / stringifier for @hjson.*
5+
6+
<br>
7+
8+
9+
10+
<!----------------------------------------------------------------------------->
11+
12+
[License]: LICENSE
13+
14+
15+
<!----------------------------------[ Badges ]--------------------------------->
16+
17+
[Badge License]: https://img.shields.io/badge/License-AGPL3-015d93.svg?style=for-the-badge&labelColor=blue

Source/Decode.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const { log } = console;
99

1010
export default function decode(string){
1111

12-
log([ ... normalize(new Tokenizer(string).iterator()) ]);
12+
// log([ ... normalize(new Tokenizer(string).iterator()) ]);
1313

1414
const tokens = normalize(new Tokenizer(string).iterator());
1515

@@ -28,7 +28,7 @@ export default function decode(string){
2828

2929
function object(state){
3030

31-
log('\nObject\n')
31+
// log('\nObject\n')
3232

3333
const { object , tokens } = state;
3434

@@ -58,7 +58,7 @@ function object(state){
5858

5959
function member(state){
6060

61-
log('\nMember\n')
61+
// log('\nMember\n')
6262

6363
const { tokens , parent , key } = state;
6464

@@ -158,7 +158,7 @@ function member(state){
158158

159159
function array(state){
160160

161-
log('\nArray\n')
161+
// log('\nArray\n')
162162

163163
const { array , tokens } = state;
164164

@@ -218,6 +218,8 @@ function array(state){
218218
return;
219219
case 'Newline' :
220220
break;
221+
case 'Comma' :
222+
break;
221223
default:
222224
throw `Invalid Array Value Token ${ token.value.type }`;
223225
}
@@ -232,14 +234,14 @@ function array(state){
232234

233235
const { type } = token.value;
234236

235-
if(type === 'Comma')
236-
break;
237+
if(
238+
type === 'Comma' ||
239+
type === 'Newline'
240+
) break;
237241

238242
switch(type){
239243
case 'ArrayEnd' :
240244
return;
241-
case 'Newline' :
242-
break;
243245
default:
244246
throw `Invalid Array Seperator Token ${ token.value.type }`;
245247
}

Source/Normalize.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,16 @@ function * forwardCombine(tokens){
164164

165165
if(
166166
nowType === 'Newline' ||
167-
nowType === 'Space' ||
168-
nowType === 'Comma'
167+
nowType === 'Space'
169168
) continue;
170169

170+
if(
171+
nowType === 'Comma'
172+
){
173+
before = { type : 'Comma' }
174+
continue;
175+
}
176+
171177
break;
172178
case 'Space' :
173179

Source/Parse.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export default function parse(object){
1414
object[key] = string(value);
1515
continue;
1616
case 'array' :
17-
array(value);
17+
parse(value);
1818
continue;
1919
case 'object' :
2020
parse(value);
@@ -36,7 +36,7 @@ function string(value){
3636

3737
switch(true){
3838
case is_bool.test(value):
39-
return Boolean(value);
39+
return value === 'true';
4040
case is_number.test(value):
4141
return parseFloat(value);
4242
default:
@@ -48,8 +48,3 @@ function string(value){
4848
return value;
4949
}
5050
}
51-
52-
function array(value){
53-
54-
55-
}

Source/README.md

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,66 @@
11

2-
**Work In Progress**
2+
# HJSON   [![Badge License]][License]
3+
4+
*A parser / stringifier for **[HJSON]**.*
5+
6+
<br>
7+
8+
## Import
9+
10+
```JavaScript
11+
import HJson from 'https://deno.land/x/hjson/mod.ts'
12+
```
13+
14+
<br>
15+
<br>
16+
17+
## Parsing
18+
19+
```JavaScript
20+
import { parse } from 'https://deno.land/x/hjson/mod.ts'
21+
22+
const { log } = console;
23+
24+
const hjson = `{
25+
some : 3
26+
variables : [
27+
with ,
28+
a
29+
lot
30+
]
31+
inside : {
32+
them : 9
33+
}
34+
}`
35+
36+
log(parse(hjson));
37+
38+
39+
/*
40+
* {
41+
* some : 3 ,
42+
* variables : [ 'with' , 'a' , 'lot' ] ,
43+
* inside : { them : 9 }
44+
* }
45+
*/
46+
```
47+
48+
<br>
49+
<br>
50+
51+
## Stringification
52+
53+
**Work In Progress**
54+
55+
<br>
56+
57+
58+
<!----------------------------------------------------------------------------->
59+
60+
[License]: LICENSE
61+
[HJSON]: https://hjson.github.io/
62+
63+
64+
<!----------------------------------[ Badges ]--------------------------------->
65+
66+
[Badge License]: https://img.shields.io/badge/License-AGPL3-015d93.svg?style=for-the-badge&labelColor=blue

Source/mod.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ import decode from './Decode.js'
44

55
export function parse ( text : string ) : object {
66
return decode(text);
7-
}
7+
}
8+
9+
export default { parse }

Tests/Array.test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
const raw = `{
3+
4+
AnArray : [
5+
10,
6+
2
7+
8+
3
9+
10+
4,
11+
12+
5
13+
]
14+
}
15+
16+
17+
`
18+
19+
const model = {
20+
AnArray : [ 10 , 2 , 3 , 4 , 5 ]
21+
}
22+
23+
24+
import { assertEquals } from 'Assert';
25+
import { parse } from '../Source/mod.ts'
26+
27+
28+
Deno.test('Array Parsing Test',() => {
29+
30+
const parsed = parse(raw);
31+
32+
assertEquals(parsed,model);
33+
});

Tests/Basic.test.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,14 @@ const model = {
4949
}
5050

5151

52-
import { assertEquals } from 'https://deno.land/std@0.149.0/testing/asserts.ts';
53-
import { parse } from '../Source/mod.ts'
52+
import { assertEquals } from 'Assert';
53+
// import { parse } from '../Source/mod.ts'
54+
import HJSON from '../Source/mod.ts'
5455

5556

5657
Deno.test('Generic Parsing Test',() => {
5758

58-
const parsed = parse(raw);
59-
60-
console.log(parsed);
59+
const parsed = HJSON.parse(raw);
6160

6261
assertEquals(parsed,model);
6362
});

Tests/Example.test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
const raw = `{
3+
some : 3
4+
variables : [
5+
with ,
6+
a
7+
lot
8+
]
9+
inside : {
10+
them : 9
11+
}
12+
}`
13+
14+
const model = {
15+
some : 3 ,
16+
variables : [ 'with' , 'a' , 'lot' ] ,
17+
inside : { them : 9 }
18+
}
19+
20+
21+
import { assertEquals } from 'Assert';
22+
import { parse } from '../Source/mod.ts'
23+
24+
25+
Deno.test('Example Parsing Test',() => {
26+
27+
const parsed = parse(raw);
28+
29+
assertEquals(parsed,model);
30+
});

0 commit comments

Comments
 (0)