Skip to content

Commit

Permalink
Harmony: Parse inline module declaration.
Browse files Browse the repository at this point in the history
Example code: 'module Universe {}'

Syntax tree:
    {
        type: 'ModuleDeclaration',
        id: {
            type: 'Identifier',
            name: 'Universe'
        },
        body: {
            type: 'BlockStatement',
            body: []
        }
    }

It's "extrapolated" because Mozilla Parser API still does not support
module yet.

http://code.google.com/p/esprima/issues/detail?id=188
  • Loading branch information
ariya committed Feb 26, 2012
1 parent 0222f9d commit 975f036
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
24 changes: 24 additions & 0 deletions esprima.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ parseStatement: true, parseSourceElement: true */
LabeledStatement: 'LabeledStatement',
LogicalExpression: 'LogicalExpression',
MemberExpression: 'MemberExpression',
ModuleDeclaration: 'ModuleDeclaration',
NewExpression: 'NewExpression',
ObjectExpression: 'ObjectExpression',
Program: 'Program',
Expand Down Expand Up @@ -323,6 +324,11 @@ parseStatement: true, parseSourceElement: true */
return true;
}

// Harmony
if (id === 'module') {
return true;
}

return isFutureReservedWord(id);
}

Expand Down Expand Up @@ -2143,6 +2149,22 @@ parseStatement: true, parseSourceElement: true */
};
}

// http://wiki.ecmascript.org/doku.php?id=harmony:modules

function parseModuleStatement() {
var id;

expectKeyword('module');

id = parseVariableIdentifier();

return {
type: Syntax.ModuleDeclaration,
id: id,
body: parseBlock()
};
}

// 12.3 Empty Statement

function parseEmptyStatement() {
Expand Down Expand Up @@ -2667,6 +2689,8 @@ parseStatement: true, parseSourceElement: true */
return parseFunctionDeclaration();
case 'if':
return parseIfStatement();
case 'module':
return parseModuleStatement();
case 'return':
return parseReturnStatement();
case 'switch':
Expand Down
83 changes: 83 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13589,6 +13589,89 @@ data = {

},

'Harmony Module': {

'module Universe {}': {
type: 'ModuleDeclaration',
id: {
type: 'Identifier',
name: 'Universe',
range: [7, 14],
loc: {
start: { line: 1, column: 7 },
end: { line: 1, column: 15 }
}
},
body: {
type: 'BlockStatement',
body: [],
range: [16, 17],
loc: {
start: { line: 1, column: 16 },
end: { line: 1, column: 18 }
}
},
range: [0, 17],
loc: {
start: { line: 1, column: 0 },
end: { line: 1, column: 18 }
}
},

'module Universe { module MilkyWay {} }': {
type: 'ModuleDeclaration',
id: {
type: 'Identifier',
name: 'Universe',
range: [7, 14],
loc: {
start: { line: 1, column: 7 },
end: { line: 1, column: 15 }
}
},
body: {
type: 'BlockStatement',
body: [{
type: 'ModuleDeclaration',
id: {
type: 'Identifier',
name: 'MilkyWay',
range: [25, 32],
loc: {
start: { line: 1, column: 25 },
end: { line: 1, column: 33 }
}
},
body: {
type: 'BlockStatement',
body: [],
range: [34, 35],
loc: {
start: { line: 1, column: 34 },
end: { line: 1, column: 36 }
}
},
range: [18, 35],
loc: {
start: { line: 1, column: 18 },
end: { line: 1, column: 36 }
}
}],
range: [16, 37],
loc: {
start: { line: 1, column: 16 },
end: { line: 1, column: 38 }
}
},
range: [0, 37],
loc: {
start: { line: 1, column: 0 },
end: { line: 1, column: 38 }
}
}

},

'Trace Function Entrance': {

'function hello() {}': {
Expand Down

0 comments on commit 975f036

Please sign in to comment.