From 682ff59a2966d369dca13d0cecfc1b4ff2a6f05c Mon Sep 17 00:00:00 2001 From: IHateYourCode <89636625+IHateYourCode@users.noreply.github.com> Date: Sat, 23 Jul 2022 05:06:21 -0400 Subject: [PATCH] Fixup + basic example --- .gitignore | 1 + README.md | 17 ++++++++++- Source/Decode.js | 18 ++++++------ Source/Normalize.js | 10 +++++-- Source/Parse.js | 9 ++---- Source/README.md | 66 ++++++++++++++++++++++++++++++++++++++++++- Source/mod.ts | 4 ++- Tests/Array.test.js | 33 ++++++++++++++++++++++ Tests/Basic.test.js | 9 +++--- Tests/Example.test.js | 30 ++++++++++++++++++++ 10 files changed, 172 insertions(+), 25 deletions(-) create mode 100644 .gitignore create mode 100644 Tests/Array.test.js create mode 100644 Tests/Example.test.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c9c636a --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +Tests/Experimental \ No newline at end of file diff --git a/README.md b/README.md index 9c78e1c..973a669 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,17 @@ -**Work In Progress** +# HJSON   [![Badge License]][License] + +*A parser / stringifier for @hjson.* + +
+ + + + + +[License]: LICENSE + + + + +[Badge License]: https://img.shields.io/badge/License-AGPL3-015d93.svg?style=for-the-badge&labelColor=blue \ No newline at end of file diff --git a/Source/Decode.js b/Source/Decode.js index 8799419..159fbe4 100644 --- a/Source/Decode.js +++ b/Source/Decode.js @@ -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()); @@ -28,7 +28,7 @@ export default function decode(string){ function object(state){ - log('\nObject\n') + // log('\nObject\n') const { object , tokens } = state; @@ -58,7 +58,7 @@ function object(state){ function member(state){ - log('\nMember\n') + // log('\nMember\n') const { tokens , parent , key } = state; @@ -158,7 +158,7 @@ function member(state){ function array(state){ - log('\nArray\n') + // log('\nArray\n') const { array , tokens } = state; @@ -218,6 +218,8 @@ function array(state){ return; case 'Newline' : break; + case 'Comma' : + break; default: throw `Invalid Array Value Token ${ token.value.type }`; } @@ -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 }`; } diff --git a/Source/Normalize.js b/Source/Normalize.js index a3a67af..92dd268 100644 --- a/Source/Normalize.js +++ b/Source/Normalize.js @@ -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' : diff --git a/Source/Parse.js b/Source/Parse.js index feef11f..39f3007 100644 --- a/Source/Parse.js +++ b/Source/Parse.js @@ -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); @@ -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: @@ -48,8 +48,3 @@ function string(value){ return value; } } - -function array(value){ - - -} \ No newline at end of file diff --git a/Source/README.md b/Source/README.md index 0372be2..ebff050 100644 --- a/Source/README.md +++ b/Source/README.md @@ -1,2 +1,66 @@ -**Work In Progress** \ No newline at end of file +# HJSON   [![Badge License]][License] + +*A parser / stringifier for **[HJSON]**.* + +
+ +## Import + +```JavaScript +import HJson from 'https://deno.land/x/hjson/mod.ts' +``` + +
+
+ +## 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 } + * } + */ +``` + +
+
+ +## Stringification + +**Work In Progress** + +
+ + + + +[License]: LICENSE +[HJSON]: https://hjson.github.io/ + + + + +[Badge License]: https://img.shields.io/badge/License-AGPL3-015d93.svg?style=for-the-badge&labelColor=blue \ No newline at end of file diff --git a/Source/mod.ts b/Source/mod.ts index 4b61b9b..886e73d 100644 --- a/Source/mod.ts +++ b/Source/mod.ts @@ -4,4 +4,6 @@ import decode from './Decode.js' export function parse ( text : string ) : object { return decode(text); -} \ No newline at end of file +} + +export default { parse } \ No newline at end of file diff --git a/Tests/Array.test.js b/Tests/Array.test.js new file mode 100644 index 0000000..3d6ea78 --- /dev/null +++ b/Tests/Array.test.js @@ -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); +}); \ No newline at end of file diff --git a/Tests/Basic.test.js b/Tests/Basic.test.js index 92e86c6..a12241f 100644 --- a/Tests/Basic.test.js +++ b/Tests/Basic.test.js @@ -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); }); \ No newline at end of file diff --git a/Tests/Example.test.js b/Tests/Example.test.js new file mode 100644 index 0000000..dd932ed --- /dev/null +++ b/Tests/Example.test.js @@ -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); +}); \ No newline at end of file