Skip to content

Commit

Permalink
Add support for Vector, File and Bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
kraklin committed Jun 13, 2019
1 parent 1de2c4e commit 18956d9
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "elm-debug-transformer",
"version": "0.0.5",
"version": "0.0.6",
"description": "Transform Elm Debug.log output into nice log object with custom formatter",
"main": "dist/elm-console-debug.js",
"author": "Tomas Latal <[email protected]>",
Expand Down
15 changes: 12 additions & 3 deletions src/elm-debug-simple.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ DebugString
/ ":" _ value:Value {return value;}

Value
= Record / Array / Set / Dict / List / CustomTypeWithParens / Tuple / Number / Boolean / Type / Internals / String
= Record / Array / Set / Dict / List / CustomTypeWithParens / Tuple / Number / Boolean / Type / Internals / Bytes / File / String

Record
= "{}" {return {};}
/ "{ " chars:[a-zA-Z]+ " = " value:Value " }" {return {[toStr(chars)]: value}}
/ "{ " chars:[a-zA-Z]+ " = " value:Value values:(", " tag:[a-zA-Z]+ " = " otherVal:Value {return {[toStr(tag)]: otherVal};})* " }" { var composed = [{[toStr(chars)]: value},...values].reduce((item, obj) => {return {...item,...obj} },{}); return composed}
/ "{ " key:VariableName " = " value:Value " }" {return {[key]: value}}
/ "{ " key:VariableName " = " value:Value values:(", " tag:VariableName " = " otherVal:Value {return {[tag]: otherVal};})* " }" { var composed = [{[key]: value},...values].reduce((item, obj) => {return {...item,...obj} },{}); return composed}

Dict
= "Dict.fromList " values:ListValue {return values.map((tuple) => { return {[tuple[0]]: tuple[1]};})}
Expand Down Expand Up @@ -52,6 +52,15 @@ Internals
= "<function>" {return {type: "<function>"};}
/ "<internals>" {return {type: "<internals>"};}

Bytes
= "<" digits:[0-9]+ " bytes>" {return {type: "Bytes", value: parseInt(toStr(digits), 10)};}

File
= "<" chars:(!('"' / "\\" / "<" / ">") char:. { return char; })+ ">" {return { type: "File", value: chars.join('')}; }

VariableName =
chars:[a-zA-Z0-9_]+ {return toStr(chars);}

Type =
type:[a-zA-Z]+ {return toStr(type);}

Expand Down
14 changes: 11 additions & 3 deletions src/elm-debug.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ DebugString
/ ":" _ value:Value {return {type: "ElmDebug", tag: "", value: value};}

Value
= Record / Array / Set / Dict / List / CustomTypeWithParens / Tuple / Number / Boolean / Type / Internals / String
= Record / Array / Set / Dict / List / CustomTypeWithParens / Tuple / Number / Boolean / Type / Internals / Bytes / File / String

Record
= "{}" {return {type: "Record", value: {}};}
/ "{ " chars:[a-zA-Z]+ " = " value:Value " }" {return {type: "Record", value: {[toStr(chars)]: value}}}
/ "{ " chars:[a-zA-Z]+ " = " value:Value values:(", " tag:[a-zA-Z]+ " = " otherVal:Value {return {[toStr(tag)]: otherVal};})* " }" { var composed = [{[toStr(chars)]: value},...values].reduce((item, obj) => {return {...item,...obj} },{}); return {type: "Record", value: composed}}
/ "{ " key:VariableName " = " value:Value " }" {return {type: "Record", value: {[key]: value}}}
/ "{ " key:VariableName " = " value:Value values:(", " tag:VariableName " = " otherVal:Value {return {[tag]: otherVal};})* " }" { var composed = [{[key]: value},...values].reduce((item, obj) => {return {...item,...obj} },{}); return {type: "Record", value: composed}}

Dict
= "Dict.fromList " values:ListValue {return {type: "Dict", value: values.map((tuple) => { return {key: tuple.value[0], value: tuple.value[1]};})}}
Expand Down Expand Up @@ -52,7 +52,15 @@ Internals
= "<function>" {return {type: "Function"};}
/ "<internals>" {return {type: "Internals"};}

Bytes
= "<" digits:[0-9]+ " bytes>" {return {type: "Bytes", value: parseInt(toStr(digits), 10)};}

File
= "<" chars:(!('"' / "\\" / "<" / ">") char:. { return char; })+ ">" {return { type: "File", value: chars.join('') };}

VariableName =
chars:[a-zA-Z0-9_]+ {return toStr(chars);}

Type =
type:[a-zA-Z]+ {return {type: "Type", name: toStr(type)};}

Expand Down
15 changes: 15 additions & 0 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ describe('Parsing', () => {
it('Nested records', () => {
parser.parse("record: { name = \"Name\", warning = { name = Nothing, waves = [] } }").value.should.deep.equal({type: "Record", value: {"name": "Name", "warning": {type: "Record", value: {"name": {type: "Type", name: "Nothing"}, "waves": {type: "List", value: []}}}}});
});
it('Vector', () => {
parser.parse("vec: { 0 = 0, 1 = 0, 2 = 0 }").value.should.deep.equal({type: "Record", value: {"0": {type: "Number", value: 0}, "1": {type: "Number", value: 0}, "2": {type: "Number", value: 0}}});
});
});

describe('Custom types', () => {
Expand Down Expand Up @@ -146,5 +149,17 @@ describe('Parsing', () => {
parser.parse("custom type: <internals>").value.should.deep.equal({type: "Internals"});
});
});

describe('Bytes', () => {
it('Bytes', () => {
parser.parse("bytes: <24294 bytes>").value.should.deep.equal({type: "Bytes", value: 24294});
});
});

describe('File', () => {
it('File', () => {
parser.parse("file: <Some-name-[and]_extrachars(0000239649).extension>").value.should.deep.equal({type: "File", value: "Some-name-[and]_extrachars(0000239649).extension"});
});
});

});

0 comments on commit 18956d9

Please sign in to comment.