Skip to content

Commit

Permalink
Fix of double custom types being misinterpreted as one (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
kraklin authored Jan 30, 2020
1 parent 537a93f commit b6b1c4c
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 33 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "elm-debug-transformer",
"version": "1.0.2",
"version": "1.0.3",
"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 All @@ -17,7 +17,6 @@
"scripts": {
"pegjs": "pegjs --plugin ./node_modules/ts-pegjs/src/tspegjs -o src/elm-debug-parser.ts src/elm-debug.pegjs",
"test": "TS_NODE_COMPILER_OPTIONS='{\"module\":\"commonjs\"}' mocha --opts mocha.opts",
"test:watch": "yarn test --watch",
"build": "yarn run pegjs && webpack",
"prepublishOnly": "yarn run test && yarn run build"
},
Expand Down
107 changes: 79 additions & 28 deletions src/elm-debug-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,56 @@ function peg$parse(input: string, options?: IParseOptions) {
function peg$parseValue(): any {
let s0;

s0 = peg$parseRecord();
if (s0 === peg$FAILED) {
s0 = peg$parseArray();
if (s0 === peg$FAILED) {
s0 = peg$parseSet();
if (s0 === peg$FAILED) {
s0 = peg$parseDict();
if (s0 === peg$FAILED) {
s0 = peg$parseList();
if (s0 === peg$FAILED) {
s0 = peg$parseCustomTypeWithParens();
if (s0 === peg$FAILED) {
s0 = peg$parseCustomType();
if (s0 === peg$FAILED) {
s0 = peg$parseTuple();
if (s0 === peg$FAILED) {
s0 = peg$parseNumber();
if (s0 === peg$FAILED) {
s0 = peg$parseBoolean();
if (s0 === peg$FAILED) {
s0 = peg$parseType();
if (s0 === peg$FAILED) {
s0 = peg$parseInternals();
if (s0 === peg$FAILED) {
s0 = peg$parseBytes();
if (s0 === peg$FAILED) {
s0 = peg$parseFile();
if (s0 === peg$FAILED) {
s0 = peg$parseString();
}
}
}
}
}
}
}
}
}
}
}
}
}
}

return s0;
}

function peg$parseCustomTypeValue(): any {
let s0;

s0 = peg$parseRecord();
if (s0 === peg$FAILED) {
s0 = peg$parseArray();
Expand Down Expand Up @@ -969,26 +1019,31 @@ function peg$parse(input: string, options?: IParseOptions) {
peg$currPos = s4;
s4 = peg$FAILED;
}
while (s4 !== peg$FAILED) {
s3.push(s4);
s4 = peg$currPos;
s5 = peg$parse_();
if (s5 !== peg$FAILED) {
if (input.charCodeAt(peg$currPos) === 44) {
s6 = peg$c32;
peg$currPos++;
} else {
s6 = peg$FAILED;
if (peg$silentFails === 0) { peg$fail(peg$c33); }
}
if (s6 !== peg$FAILED) {
s7 = peg$parse_();
if (s7 !== peg$FAILED) {
s8 = peg$parseValue();
if (s8 !== peg$FAILED) {
peg$savedPos = s4;
s5 = peg$c34(s2, s8);
s4 = s5;
if (s4 !== peg$FAILED) {
while (s4 !== peg$FAILED) {
s3.push(s4);
s4 = peg$currPos;
s5 = peg$parse_();
if (s5 !== peg$FAILED) {
if (input.charCodeAt(peg$currPos) === 44) {
s6 = peg$c32;
peg$currPos++;
} else {
s6 = peg$FAILED;
if (peg$silentFails === 0) { peg$fail(peg$c33); }
}
if (s6 !== peg$FAILED) {
s7 = peg$parse_();
if (s7 !== peg$FAILED) {
s8 = peg$parseValue();
if (s8 !== peg$FAILED) {
peg$savedPos = s4;
s5 = peg$c34(s2, s8);
s4 = s5;
} else {
peg$currPos = s4;
s4 = peg$FAILED;
}
} else {
peg$currPos = s4;
s4 = peg$FAILED;
Expand All @@ -1001,10 +1056,9 @@ function peg$parse(input: string, options?: IParseOptions) {
peg$currPos = s4;
s4 = peg$FAILED;
}
} else {
peg$currPos = s4;
s4 = peg$FAILED;
}
} else {
s3 = peg$FAILED;
}
if (s3 !== peg$FAILED) {
if (input.charCodeAt(peg$currPos) === 41) {
Expand Down Expand Up @@ -1088,9 +1142,6 @@ function peg$parse(input: string, options?: IParseOptions) {
peg$currPos = s0;
s0 = peg$FAILED;
}
if (s0 === peg$FAILED) {
s0 = peg$parseCustomType();
}

return s0;
}
Expand All @@ -1105,7 +1156,7 @@ function peg$parse(input: string, options?: IParseOptions) {
s3 = peg$currPos;
s4 = peg$parse_();
if (s4 !== peg$FAILED) {
s5 = peg$parseValue();
s5 = peg$parseCustomTypeValue();
if (s5 !== peg$FAILED) {
peg$savedPos = s3;
s4 = peg$c39(s1, s5);
Expand All @@ -1124,7 +1175,7 @@ function peg$parse(input: string, options?: IParseOptions) {
s3 = peg$currPos;
s4 = peg$parse_();
if (s4 !== peg$FAILED) {
s5 = peg$parseValue();
s5 = peg$parseCustomTypeValue();
if (s5 !== peg$FAILED) {
peg$savedPos = s3;
s4 = peg$c39(s1, s5);
Expand Down
8 changes: 5 additions & 3 deletions src/elm-debug.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ DebugString
/ ":" _ value:Value {return {type: "ElmDebug", name: "", value: value};}

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

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

Record
Expand All @@ -26,14 +29,13 @@ Array

Tuple
= "()" {return {type:"Unit"}}
/ "(" head:Value others:(_ "," _ item:Value {return item;})* ")" {return {type: "Tuple", value: [head,...others]};}
/ "(" head:Value others:(_ "," _ item:Value {return item;})+ ")" {return {type: "Tuple", value: [head,...others]};}

CustomTypeWithParens
= "(" _ customType:CustomType _ ")" {return customType;}
/ CustomType

CustomType
= mainType:Type values:(_ value:Value {return value;})+ {return {type: "Custom", name: mainType.name, value: values};}
= mainType:Type values:(_ value:CustomTypeValue {return value;})+ {return {type: "Custom", name: mainType.name, value: values};}
/ mainType:Type _ customType:CustomTypeWithParens {return {type: "Custom", name: mainType.name, value: customType};}

List
Expand Down
39 changes: 39 additions & 0 deletions test/parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,15 @@ describe('Parsing', () => {
],
});
});
it('Tuple with one item is not a tuple', (done) => {
try {
parser.parse('not tuple: (123)');
}
catch (err){
expect(err.name).to.eql('SyntaxError')
}
done();
});
});

describe('List', () => {
Expand Down Expand Up @@ -365,6 +374,36 @@ describe('Parsing', () => {
],
});
});
it('Custom type with two Nothings next to each other', () => {
expect(
parser.parse(
'custom type: CacheKey (Id "cache_id") Nothing Nothing'
).value
).to.deep.equal({
name: 'CacheKey',
type: 'Custom',
value: [
{
name: 'Id',
type: 'Custom',
value: [
{type: 'String',
value: "cache_id"
}
]
}
,{
name: 'Nothing',
type: 'Type',
}
,{
name: 'Nothing',
type: 'Type',
}
],

});
});
it('Custom type with more values in parenthesis', () => {
expect(
parser.parse(
Expand Down

0 comments on commit b6b1c4c

Please sign in to comment.