Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2.0.0 #20

Merged
merged 46 commits into from
Oct 25, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
8153361
Changed node type name from lower to capital case. Changed names for …
vtrushin Oct 9, 2017
519b753
Cleaned up
vtrushin Oct 14, 2017
a2485d1
let -> const
vtrushin Oct 14, 2017
cb898a3
Updated travis
vtrushin Oct 14, 2017
3dadda6
Updated travis
vtrushin Oct 14, 2017
773f796
Delete .DS_Store
vtrushin Oct 14, 2017
e6679d6
Fixed package.json
vtrushin Oct 14, 2017
4e2a45a
Merge remote-tracking branch 'origin/2.1.0' into 2.1.0
vtrushin Oct 14, 2017
632fe3c
Changed version
vtrushin Oct 14, 2017
3875623
Changed rollup-plugin-babel version
vtrushin Oct 16, 2017
e537c3e
Added rollup-external-helpers bug fixing
vtrushin Oct 17, 2017
6921b84
Added code-error-fragment
vtrushin Oct 18, 2017
b3b26b5
Removed messages in error tests
vtrushin Oct 18, 2017
3844bdb
Update code-error-fragment
vtrushin Oct 18, 2017
40be860
Updated code-error-fragment version
vtrushin Oct 19, 2017
5af3130
prepublish -> prepublishOnly
vtrushin Oct 19, 2017
7a26569
Added pretest
vtrushin Oct 19, 2017
718cfd3
Delete .DS_Store
vtrushin Oct 19, 2017
9fb39a2
Delete .DS_Store
vtrushin Oct 19, 2017
3b1548f
Changing value and raw
vtrushin Oct 20, 2017
f05d467
Merge remote-tracking branch 'origin/2.0.0' into 2.0.0
vtrushin Oct 20, 2017
62852d5
Fixed literal values
vtrushin Oct 22, 2017
1a544c4
Cleand up console.log
vtrushin Oct 22, 2017
039f181
Adds -> Appends in readme.md
vtrushin Oct 22, 2017
3266cf4
Updated code-error-fragment
vtrushin Oct 22, 2017
b2c22a2
Changed string parsing
vtrushin Oct 24, 2017
93baa96
Merge branch '2.0.0' of /Users/vvtrushin/WebProjects/json-to-ast with…
vtrushin Oct 24, 2017
bcd914b
Merge remote-tracking branch 'origin/2.0.0' into 2.0.0
vtrushin Oct 24, 2017
18ac0e7
Changed `reduce` method to `for` operator in parseHexEscape fn
vtrushin Oct 24, 2017
ca546a9
Changed tests
vtrushin Oct 24, 2017
21c0e75
Changed md
vtrushin Oct 24, 2017
7a45f94
readme.md -> README.md
vtrushin Oct 24, 2017
398f33a
changelog.md -> CHANGELOG.md
vtrushin Oct 24, 2017
db84263
Update README.md
vtrushin Oct 24, 2017
72e5500
Update README.md
vtrushin Oct 24, 2017
9f4548c
Changed readme
vtrushin Oct 24, 2017
b687fc1
Removed unused files
vtrushin Oct 24, 2017
9547baf
Cleaned README
vtrushin Oct 24, 2017
8131da2
Changed README
vtrushin Oct 24, 2017
f5a2ce9
Changed README
vtrushin Oct 24, 2017
7e18952
Changed Array node descr
vtrushin Oct 24, 2017
de1e962
Changed Property node descr
vtrushin Oct 24, 2017
c31c7ea
Changed Property node descr
vtrushin Oct 24, 2017
a6b1886
Release preparing
vtrushin Oct 25, 2017
cba37b3
Merge branch 'master' into 2.0.0
vtrushin Oct 25, 2017
3659768
Resolved conflicts
vtrushin Oct 25, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions _index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
var parse = require('./build');
var fs = require('fs');

function readFile(file) {
var src = fs.readFileSync(file, 'utf8');
// normalize line endings
src = src.replace(/\r\n/, '\n');
// remove trailing newline
src = src.replace(/\n$/, '');

return src;
}

var a = parse(readFile('./_index.json'));

console.log(a.children[0].key);
2 changes: 1 addition & 1 deletion _index.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
"x\""
"\"\/"
19 changes: 19 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="build.js"></script>
</head>
<body>

<script>
fetch('_index.json').then(body => body.text()).then(text => {
// console.log(text);
console.log(jsonToAst(text));
// console.log(JSON.parse(text));
})
</script>

</body>
</html>
47 changes: 45 additions & 2 deletions lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,49 @@ function errorEof(input, tokenList, settings) {
);
}

function parseHexEscape(hexCode) {
const charCode = [...hexCode].reduce((acc, current) => (
acc * 16 + parseInt(current, 16)
), 0);

return String.fromCharCode(charCode);
}

var escapee = {
'"': '"',
'\\': '\\',
'/': '/',
b: '\b',
f: '\f',
n: '\n',
r: '\r',
t: '\t'
}

/** @param {String} string */
function parseString(string) {
let result = '';

for (let i = 0; i < String(string).length; i ++) {
let char = string.charAt(i);

if (char === '\\') {
if (string.charAt(i + 1) === 'u') {
result += parseHexEscape(string.substr(i + 2, 4));
i += 5;
} else {
result += escapee[string.charAt(i + 1)];
i++;
}
} else {
result += char;
}
}

return result;

}

function parseObject(input, tokenList, index, settings) {
// object: LEFT_BRACE (property (COMMA property)*)? RIGHT_BRACE
let startToken;
Expand Down Expand Up @@ -175,7 +218,7 @@ function parseProperty(input, tokenList, index, settings) {
if (token.type === tokenTypes.STRING) {
const key = {
type: 'Identifier',
value: input.slice(token.loc.start.offset + 1, token.loc.end.offset - 1),
value: parseString(input.slice(token.loc.start.offset + 1, token.loc.end.offset - 1)),
raw: token.value
};
if (settings.loc) {
Expand Down Expand Up @@ -344,7 +387,7 @@ function parseLiteral(input, tokenList, index, settings) {

switch (token.type) {
case tokenTypes.STRING: {
value = input.slice(token.loc.start.offset + 1, token.loc.end.offset - 1);
value = parseString(input.slice(token.loc.start.offset + 1, token.loc.end.offset - 1));
break;
}
case tokenTypes.NUMBER: {
Expand Down
124 changes: 62 additions & 62 deletions test/fixtures/valid/big-non-verbosed.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var types = require('../../types');
var object = types.createObject;
var key = types.createObjectKey;
var prop = types.createObjectProperty;
var id = types.createIdentifier;
var prop = types.createProperty();
var array = types.createArray;
var string = types.createString;
var number = types.createNumber;
Expand All @@ -10,34 +10,34 @@ var _false = types.createFalse;

var ast = array([
object([
prop(key('_id'), string('574d7238062156c6d9e6de99')),
prop(key('index'), number(0)),
prop(key('guid'), string('c99bf348-0345-49fd-be52-d0da82bdd47f')),
prop(key('isActive'), _true()),
prop(key('balance'), string('$1,087.03')),
prop(key('picture'), string('http://placehold.it/32x32')),
prop(key('age'), number(25)),
prop(key('eyeColor'), string('brown')),
prop(key('name'), object([
prop(key('first'), string('Stacie')),
prop(key('last'), string('Sargent'))
prop(id('_id'), string('574d7238062156c6d9e6de99')),
prop(id('index'), number(0)),
prop(id('guid'), string('c99bf348-0345-49fd-be52-d0da82bdd47f')),
prop(id('isActive'), _true()),
prop(id('balance'), string('$1,087.03')),
prop(id('picture'), string('http://placehold.it/32x32')),
prop(id('age'), number(25)),
prop(id('eyeColor'), string('brown')),
prop(id('name'), object([
prop(id('first'), string('Stacie')),
prop(id('last'), string('Sargent'))
])),
prop(key('company'), string('DAISU')),
prop(key('email'), string('stacie.sargent@daisu.com')),
prop(key('phone'), string('+1 (830) 537-3936')),
prop(key('address'), string('547 Charles Place, Weogufka, Marshall Islands, 6627')),
prop(key('about'), string('Exercitation nisi incididunt exercitation sit Lorem nostrud commodo incididunt cillum amet. Laboris proident non nostrud dolor esse exercitation enim sit culpa Lorem qui. Laborum aliquip pariatur mollit aute. Et consequat Lorem in cillum sunt dolore aute voluptate anim commodo. Excepteur labore proident consequat nulla occaecat in consequat minim.')),
prop(key('registered'), string('Sunday, October 4, 2015 3:58 PM')),
prop(key('latitude'), string('45.437159')),
prop(key('longitude'), string('-77.052972')),
prop(key('tags'), array([
prop(id('company'), string('DAISU')),
prop(id('email'), string('stacie.sargent@daisu.com')),
prop(id('phone'), string('+1 (830) 537-3936')),
prop(id('address'), string('547 Charles Place, Weogufka, Marshall Islands, 6627')),
prop(id('about'), string('Exercitation nisi incididunt exercitation sit Lorem nostrud commodo incididunt cillum amet. Laboris proident non nostrud dolor esse exercitation enim sit culpa Lorem qui. Laborum aliquip pariatur mollit aute. Et consequat Lorem in cillum sunt dolore aute voluptate anim commodo. Excepteur labore proident consequat nulla occaecat in consequat minim.')),
prop(id('registered'), string('Sunday, October 4, 2015 3:58 PM')),
prop(id('latitude'), string('45.437159')),
prop(id('longitude'), string('-77.052972')),
prop(id('tags'), array([
string('veniam'),
string('et'),
string('cillum'),
string('ex'),
string('nisi')
])),
prop(key('range'), array([
prop(id('range'), array([
number(0),
number(1),
number(2),
Expand All @@ -49,52 +49,52 @@ var ast = array([
number(8),
number(9)
])),
prop(key('friends'), array([
prop(id('friends'), array([
object([
prop(key('id'), number(0)),
prop(key('name'), string('Juliana Valentine'))
prop(id('id'), number(0)),
prop(id('name'), string('Juliana Valentine'))
]),
object([
prop(key('id'), number(1)),
prop(key('name'), string('Robert Eaton'))
prop(id('id'), number(1)),
prop(id('name'), string('Robert Eaton'))
]),
object([
prop(key('id'), number(2)),
prop(key('name'), string('Socorro Herrera'))
prop(id('id'), number(2)),
prop(id('name'), string('Socorro Herrera'))
])
])),
prop(key('greeting'), string('Hello, Stacie! You have 6 unread messages.')),
prop(key('favoriteFruit'), string('banana'))
prop(id('greeting'), string('Hello, Stacie! You have 6 unread messages.')),
prop(id('favoriteFruit'), string('banana'))
]),
object([
prop(key('_id'), string('574d7238bd4c01db9e4a4d5b')),
prop(key('index'), number(1)),
prop(key('guid'), string('5fd3fc48-e39e-4ee4-bc3a-6eb12bed2653')),
prop(key('isActive'), _false()),
prop(key('balance'), string('$1,696.52')),
prop(key('picture'), string('http://placehold.it/32x32')),
prop(key('age'), number(32)),
prop(key('eyeColor'), string('blue')),
prop(key('name'), object([
prop(key('first'), string('Ada')),
prop(key('last'), string('Stokes'))
prop(id('_id'), string('574d7238bd4c01db9e4a4d5b')),
prop(id('index'), number(1)),
prop(id('guid'), string('5fd3fc48-e39e-4ee4-bc3a-6eb12bed2653')),
prop(id('isActive'), _false()),
prop(id('balance'), string('$1,696.52')),
prop(id('picture'), string('http://placehold.it/32x32')),
prop(id('age'), number(32)),
prop(id('eyeColor'), string('blue')),
prop(id('name'), object([
prop(id('first'), string('Ada')),
prop(id('last'), string('Stokes'))
])),
prop(key('company'), string('FARMAGE')),
prop(key('email'), string('ada.stokes@farmage.biz')),
prop(key('phone'), string('+1 (875) 486-3569')),
prop(key('address'), string('361 Howard Place, Wyano, Michigan, 346')),
prop(key('about'), string('Culpa esse laboris enim occaecat voluptate non reprehenderit officia amet eu ad laboris officia. Exercitation qui occaecat veniam ea tempor. Reprehenderit laborum magna occaecat sit tempor eiusmod est quis ea. Sunt minim labore et eu ex. Pariatur do proident nisi sunt commodo. Deserunt est ad pariatur laboris officia. Pariatur anim deserunt excepteur voluptate amet.')),
prop(key('registered'), string('Wednesday, April 16, 2014 7:23 PM')),
prop(key('latitude'), string('-45.133396')),
prop(key('longitude'), string('43.593917')),
prop(key('tags'), array([
prop(id('company'), string('FARMAGE')),
prop(id('email'), string('ada.stokes@farmage.biz')),
prop(id('phone'), string('+1 (875) 486-3569')),
prop(id('address'), string('361 Howard Place, Wyano, Michigan, 346')),
prop(id('about'), string('Culpa esse laboris enim occaecat voluptate non reprehenderit officia amet eu ad laboris officia. Exercitation qui occaecat veniam ea tempor. Reprehenderit laborum magna occaecat sit tempor eiusmod est quis ea. Sunt minim labore et eu ex. Pariatur do proident nisi sunt commodo. Deserunt est ad pariatur laboris officia. Pariatur anim deserunt excepteur voluptate amet.')),
prop(id('registered'), string('Wednesday, April 16, 2014 7:23 PM')),
prop(id('latitude'), string('-45.133396')),
prop(id('longitude'), string('43.593917')),
prop(id('tags'), array([
string('qui'),
string('eiusmod'),
string('nisi'),
string('officia'),
string('in')
])),
prop(key('range'), array([
prop(id('range'), array([
number(0),
number(1),
number(2),
Expand All @@ -106,22 +106,22 @@ var ast = array([
number(8),
number(9)
])),
prop(key('friends'), array([
prop(id('friends'), array([
object([
prop(key('id'), number(0)),
prop(key('name'), string('Campos Pruitt'))
prop(id('id'), number(0)),
prop(id('name'), string('Campos Pruitt'))
]),
object([
prop(key('id'), number(1)),
prop(key('name'), string('Barnett Sykes'))
prop(id('id'), number(1)),
prop(id('name'), string('Barnett Sykes'))
]),
object([
prop(key('id'), number(2)),
prop(key('name'), string('Trudy Collier'))
prop(id('id'), number(2)),
prop(id('name'), string('Trudy Collier'))
])
])),
prop(key('greeting'), string('Hello, Ada! You have 8 unread messages.')),
prop(key('favoriteFruit'), string('banana'))
prop(id('greeting'), string('Hello, Ada! You have 8 unread messages.')),
prop(id('favoriteFruit'), string('banana'))
])
]);

Expand Down
35 changes: 17 additions & 18 deletions test/fixtures/valid/deep.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
var types = require('../../types');
var key = types.createObjectKey;
var location = types.location;
var id = types.createIdentifier;
var object = types.createObject;
var prop = types.createObjectProperty;
var prop = types.createProperty;
var array = types.createArray;
var string = types.createString;
var literal = types.createLiteral;

var n = array([string('n')]/*, location()*/);
var m = array([string('m'), n]/*, location()*/);
var l = array([string('l'), m]/*, location()*/);
var k = array([string('k'), l]/*, location()*/);
var j = array([string('j'), k]/*, location()*/);
var i = array([string('i'), j]/*, location()*/);
var h = array([string('h'), i]/*, location()*/);
var g = object([prop(key('g'), h)]/*, location()*/);
var f = object([prop(key('f'), g)]/*, location()*/);
var e = object([prop(key('e'), f)]/*, location()*/);
var d = object([prop(key('d'), e)]/*, location()*/);
var c = object([prop(key('c'), d)]/*, location()*/);
var b = object([prop(key('b'/*, location(3, 5, 15, 3, 8, 18)*/), c)]/*, location()*/);
var a = object([prop(key('a'/*, location(2, 3, 4, 2, 6, 7)*/), b)]/*, location(1, 1, 0, 29, 2, 516)*/);
var n = array([literal('n', '"n"')]);
var m = array([literal('m', '"m"'), n]);
var l = array([literal('l', '"l"'), m]);
var k = array([literal('k', '"k"'), l]);
var j = array([literal('j', '"j"'), k]);
var i = array([literal('i', '"i"'), j]);
var h = array([literal('h', '"h"'), i]);
var g = object([prop(id('g', '"g"'), h)]);
var f = object([prop(id('f', '"f"'), g)]);
var e = object([prop(id('e', '"e"'), f)]);
var d = object([prop(id('d', '"d"'), e)]);
var c = object([prop(id('c', '"c"'), d)]);
var b = object([prop(id('b', '"b"'), c)]);
var a = object([prop(id('a', '"a"'), b)]);

module.exports = {
ast: a,
Expand Down
17 changes: 17 additions & 0 deletions test/fixtures/valid/exponential-numbers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
var types = require('../../types');
var array = types.createArray;
var literal = types.createLiteral;

var ast = array([
literal(1, '1'),
number(1.2, '1.2'),
literal(1200, '1.2e3'),
literal(0.0012, '1.2e-3')
]);

module.exports = {
ast: ast,
options: {
loc: false
}
};
6 changes: 6 additions & 0 deletions test/fixtures/valid/exponential-numbers.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
1,
1.2,
1.2e3,
1.2e-3
]
Loading