Skip to content

Commit

Permalink
Fixup + basic example
Browse files Browse the repository at this point in the history
  • Loading branch information
IHateYourCode committed Jul 23, 2022
1 parent e6cf45e commit 682ff59
Show file tree
Hide file tree
Showing 10 changed files with 172 additions and 25 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Tests/Experimental
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,17 @@

**Work In Progress**
# HJSON   [![Badge License]][License]

*A parser / stringifier for @hjson.*

<br>



<!----------------------------------------------------------------------------->

[License]: LICENSE


<!----------------------------------[ Badges ]--------------------------------->

[Badge License]: https://img.shields.io/badge/License-AGPL3-015d93.svg?style=for-the-badge&labelColor=blue
18 changes: 10 additions & 8 deletions Source/Decode.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const { log } = console;

export default function decode(string){

log([ ... normalize(new Tokenizer(string).iterator()) ]);
// log([ ... normalize(new Tokenizer(string).iterator()) ]);

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

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

function object(state){

log('\nObject\n')
// log('\nObject\n')

const { object , tokens } = state;

Expand Down Expand Up @@ -58,7 +58,7 @@ function object(state){

function member(state){

log('\nMember\n')
// log('\nMember\n')

const { tokens , parent , key } = state;

Expand Down Expand Up @@ -158,7 +158,7 @@ function member(state){

function array(state){

log('\nArray\n')
// log('\nArray\n')

const { array , tokens } = state;

Expand Down Expand Up @@ -218,6 +218,8 @@ function array(state){
return;
case 'Newline' :
break;
case 'Comma' :
break;
default:
throw `Invalid Array Value Token ${ token.value.type }`;
}
Expand All @@ -232,14 +234,14 @@ function array(state){

const { type } = token.value;

if(type === 'Comma')
break;
if(
type === 'Comma' ||
type === 'Newline'
) break;

switch(type){
case 'ArrayEnd' :
return;
case 'Newline' :
break;
default:
throw `Invalid Array Seperator Token ${ token.value.type }`;
}
Expand Down
10 changes: 8 additions & 2 deletions Source/Normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,16 @@ function * forwardCombine(tokens){

if(
nowType === 'Newline' ||
nowType === 'Space' ||
nowType === 'Comma'
nowType === 'Space'
) continue;

if(
nowType === 'Comma'
){
before = { type : 'Comma' }
continue;
}

break;
case 'Space' :

Expand Down
9 changes: 2 additions & 7 deletions Source/Parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default function parse(object){
object[key] = string(value);
continue;
case 'array' :
array(value);
parse(value);
continue;
case 'object' :
parse(value);
Expand All @@ -36,7 +36,7 @@ function string(value){

switch(true){
case is_bool.test(value):
return Boolean(value);
return value === 'true';
case is_number.test(value):
return parseFloat(value);
default:
Expand All @@ -48,8 +48,3 @@ function string(value){
return value;
}
}

function array(value){


}
66 changes: 65 additions & 1 deletion Source/README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,66 @@

**Work In Progress**
# HJSON   [![Badge License]][License]

*A parser / stringifier for **[HJSON]**.*

<br>

## Import

```JavaScript
import HJson from 'https://deno.land/x/hjson/mod.ts'
```

<br>
<br>

## Parsing

```JavaScript
import { parse } from 'https://deno.land/x/hjson/mod.ts'

const { log } = console;

const hjson = `{
some : 3
variables : [
with ,
a
lot
]
inside : {
them : 9
}
}`

log(parse(hjson));


/*
* {
* some : 3 ,
* variables : [ 'with' , 'a' , 'lot' ] ,
* inside : { them : 9 }
* }
*/
```

<br>
<br>

## Stringification

**Work In Progress**

<br>


<!----------------------------------------------------------------------------->

[License]: LICENSE
[HJSON]: https://hjson.github.io/


<!----------------------------------[ Badges ]--------------------------------->

[Badge License]: https://img.shields.io/badge/License-AGPL3-015d93.svg?style=for-the-badge&labelColor=blue
4 changes: 3 additions & 1 deletion Source/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ import decode from './Decode.js'

export function parse ( text : string ) : object {
return decode(text);
}
}

export default { parse }
33 changes: 33 additions & 0 deletions Tests/Array.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

const raw = `{
AnArray : [
10,
2
3
4,
5
]
}
`

const model = {
AnArray : [ 10 , 2 , 3 , 4 , 5 ]
}


import { assertEquals } from 'Assert';
import { parse } from '../Source/mod.ts'


Deno.test('Array Parsing Test',() => {

const parsed = parse(raw);

assertEquals(parsed,model);
});
9 changes: 4 additions & 5 deletions Tests/Basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,14 @@ const model = {
}


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


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

const parsed = parse(raw);

console.log(parsed);
const parsed = HJSON.parse(raw);

assertEquals(parsed,model);
});
30 changes: 30 additions & 0 deletions Tests/Example.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

const raw = `{
some : 3
variables : [
with ,
a
lot
]
inside : {
them : 9
}
}`

const model = {
some : 3 ,
variables : [ 'with' , 'a' , 'lot' ] ,
inside : { them : 9 }
}


import { assertEquals } from 'Assert';
import { parse } from '../Source/mod.ts'


Deno.test('Example Parsing Test',() => {

const parsed = parse(raw);

assertEquals(parsed,model);
});

0 comments on commit 682ff59

Please sign in to comment.