Skip to content

Commit

Permalink
parse null property
Browse files Browse the repository at this point in the history
  • Loading branch information
metelkin committed Jun 28, 2024
1 parent 0090cd1 commit a2f56cd
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 15 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
},
"scripts": {
"test": "npm run generate && mocha test --recursive",
"test:dev": "npm run generate && mocha ./test/parse",
"test:dev": "npm run generate && mocha ./test/syntax",
"test:cov": "nyc --reporter=lcov npm run test",
"test:bin": "npm run generate && node bin/heta.js run test/parse/input/include.heta",
"generate": "pegjs -o src/index.js src/pegjs/heta.pegjs"
Expand Down
26 changes: 19 additions & 7 deletions src/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 19 additions & 7 deletions src/pegjs/heta.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,23 @@ QuotedString "Quoted String"= (Break/Space)* "\"" s: [^"]* "\""
// all string until stop list, trim spaces
AssignString "Assignment String" = s: [^;{#@']*
{
let str = s.join('').replace(/[\s]+/g, ' ').replace(/^ +/g, '').replace(/ +$/g, '');
let str = s.join('')
.replace(/[\s]+/g, ' ') // remove multiple spaces
.replace(/^ +/g, '') // remove leading spaces
.replace(/ +$/g, ''); // remove trailing spaces
let doubleRegExpr = /^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$/;
let res = doubleRegExpr.test(str)
? parseFloat(str)
: str;

return res;

if (str === 'null') {
return null;
} else if (str === 'true') {
return true;
} else if (str === 'false') {
return false;
} else if (doubleRegExpr.test(str)) {
return parseFloat(str);
} else {
return str;
}
}

// -- BLOCKS --
Expand Down Expand Up @@ -221,7 +231,9 @@ String "String" = (Break/Space)* s: $([^,[\]{};] !"//" !"/*")+ Comment?
// XXX: alternative but bad solution, remove trailing comments
//let str = s.match(/\s*(.*?)\s*(?:\/\/|\/\*|$)/)[1] // ignores text after // or /*

if (str === 'true') {
if (str === 'null') {
res = null;
} else if (str === 'true') {
var res = true
} else if (str === 'false') {
res = false
Expand Down
1 change: 1 addition & 0 deletions test/parse/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const cases = [
{ source: './input/other-tabs.heta', target: './output/other-tabs.json' },
{ source: './input/other-strings.heta', target: './output/other-strings.json' },
{ source: './input/empty-statement.heta', target: './output/empty-statement.json' },
{ source: './input/null.heta', target: './output/null.json' },
];

describe('Check "parse"', () => {
Expand Down
4 changes: 4 additions & 0 deletions test/parse/input/null.heta
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
x1 @Record := 5;
x1 := null;

x2 @Record {assignments: {start_: null}};
5 changes: 5 additions & 0 deletions test/parse/output/null.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
{ "action": "upsert", "id": "x1", "class": "Record", "assignments": {"ode_": 5}},
{ "action": "upsert", "id": "x1", "assignments": {"ode_": null}},
{ "action": "upsert", "id": "x2", "class": "Record", "assignments": {"start_": null}}
]
46 changes: 46 additions & 0 deletions test/syntax/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,52 @@ let to_test = [
id: 'p1',
string: 'xxx/yyy'
}]
},
// null
{
description: 'null as property',
source: 'p1 {key: null};',
expectation: [{
action: 'upsert',
id: 'p1',
key: null
}]
},
{
description: 'null as deep property',
source: 'p1 {key: {subkey: null}};',
expectation: [{
action: 'upsert',
id: 'p1',
key: {subkey: null}
}]
},
{
description: 'null as array element',
source: 'p1 {key: [null]};',
expectation: [{
action: 'upsert',
id: 'p1',
key: [null]
}]
},
{
description: 'null inside assignment',
source: 'p1 .= null;',
expectation: [{
action: 'upsert',
id: 'p1',
assignments: {start_: null}
}]
},
{
description: 'null inside assignment expression',
source: 'p1 .= a + null;',
expectation: [{
action: 'upsert',
id: 'p1',
assignments: {start_: 'a + null'}
}]
}
];

Expand Down

0 comments on commit a2f56cd

Please sign in to comment.