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

Allow preserving parenthesis. #113

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ The available options are:
appearing in parser input and raw bytes in source code, and how Lua escape
sequences in JavaScript strings should be interpreted. See the
[Encoding modes](#encoding-modes) section below for more information.
- `preserveParens: false` Preserve parenthesis in the resulting tree.

The default options are also exposed through `luaparse.defaultOptions` where
they can be overriden globally.
Expand Down
5 changes: 5 additions & 0 deletions bin/luaparse
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var fs = require('fs')
, beautify = false
, quiet = false
, verbose = false
, parens = false
, input = ''
, options = {}
, snippets = [];
Expand All @@ -23,6 +24,7 @@ function usage() {
, " -c|--code [code] parse code snippet"
, " -f|--file [file] parse from file"
, " -b|--beautify output an indenteted AST"
, " -p|--parens preserve parenthesis in the source code"
, " --[no]-comments store comments. defaults to true"
, " --[no]-scope store variable scope. defaults to false"
, " --[no]-locations store location data on syntax nodes. defaults to false"
Expand Down Expand Up @@ -52,6 +54,9 @@ for (var i = 0, l = args.length; i < l; i++) {
case 'b': case 'beautify':
beautify = true;
continue;
case 'p': case 'parens':
options.preserveParens = true;
continue;
case 'q': case 'quiet':
quiet = true;
continue;
Expand Down
11 changes: 11 additions & 0 deletions luaparse.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@
, luaVersion: '5.1'
// Encoding mode: how to interpret code units higher than U+007F in input
, encodingMode: 'none'
// Preserve parenthesis in the resulting tree.
, preserveParens: false
};

function encodeUTF8(codepoint, highMask) {
Expand Down Expand Up @@ -232,6 +234,13 @@
, label: label
};
}

, parenthesizedExpression: function(expression) {
return {
type: 'ParenthesizedExpression'
, expression: expression
};
}

, breakStatement: function() {
return {
Expand Down Expand Up @@ -2517,8 +2526,10 @@
// Set the parent scope.
if (options.scope) attachScope(base, scopeHasName(name));
} else if (consume('(')) {
if (options.preserveParens) pushLocation(marker);
base = parseExpectedExpression(flowContext);
expect(')');
if (options.preserveParens) base = finishNode(ast.parenthesizedExpression(base));
} else {
return null;
}
Expand Down