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

Adds support for 'QuotedLiteral'. #79

Closed
wants to merge 3 commits into from
Closed
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
4 changes: 3 additions & 1 deletion browser/scripts/expander.js
Original file line number Diff line number Diff line change
Expand Up @@ -1114,7 +1114,8 @@
head.token.type === parser.Token.StringLiteral ||
head.token.type === parser.Token.BooleanLiteral ||
head.token.type === parser.Token.RegexLiteral ||
head.token.type === parser.Token.NullLiteral) {
head.token.type === parser.Token.NullLiteral ||
head.token.type === parser.Token.QuotedLiteral) {

return step(Lit.create(head), rest);
// identifier
Expand Down Expand Up @@ -1610,6 +1611,7 @@
};
}
}

throwError("Could not match any cases for macro: " + macroNameStx.token.value);
};
}
Expand Down
38 changes: 25 additions & 13 deletions browser/scripts/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ to decide on the correct name for identifiers.
StringLiteral: 8,
Delimiter: 9,
Pattern: 10,
RegexLiteral: 11
RegexLiteral: 11,
QuotedLiteral: 12
};

TokenName = {};
Expand All @@ -109,6 +110,7 @@ to decide on the correct name for identifiers.
TokenName[Token.Punctuator] = 'Punctuator';
TokenName[Token.StringLiteral] = 'String';
TokenName[Token.Delimiter] = 'Delimiter';
TokenName[Token.QuotedLiteral] = 'Quote';

Syntax = {
AssignmentExpression: 'AssignmentExpression',
Expand Down Expand Up @@ -900,12 +902,17 @@ to decide on the correct name for identifiers.

// 7.8.4 String Literals

function scanStringLiteral() {
function scanStringLiteral(sought, type) {
var str = '', quote, start, ch, code, unescaped, restore, octal = false;

if (type == null) {
assert(sought == null, "sought must not be supplied unless type is");
type = Token.StringLiteral;
}

quote = source[index];
assert((quote === '\'' || quote === '"'),
'String literal must starts with a quote');
assert((quote === '\'' || quote === '"' || (sought != null && quote == sought)),
'String literal must start with a quote');

start = index;
++index;
Expand Down Expand Up @@ -995,7 +1002,7 @@ to decide on the correct name for identifiers.
}

return {
type: Token.StringLiteral,
type: type,
value: str,
octal: octal,
lineNumber: lineNumber,
Expand All @@ -1004,6 +1011,10 @@ to decide on the correct name for identifiers.
};
}

function scanQuotedLiteral() {
return scanStringLiteral('`', Token.QuotedLiteral);
}

function scanRegExp() {
var str = '', ch, start, pattern, flags, value, classMarker = false, restore;

Expand Down Expand Up @@ -1103,9 +1114,7 @@ to decide on the correct name for identifiers.
token.type === Token.BooleanLiteral ||
token.type === Token.NullLiteral;
}



// only used by the reader
function advance() {
var ch, token;
Expand All @@ -1128,6 +1137,9 @@ to decide on the correct name for identifiers.
return token;
}

if (ch === '`') {
return scanQuotedLiteral();
}

if (ch === '\'' || ch === '"') {
return scanStringLiteral();
Expand Down Expand Up @@ -1548,11 +1560,15 @@ to decide on the correct name for identifiers.
};
}

if (type === Token.StringLiteral || type === Token.NumericLiteral) {
if (type === Token.StringLiteral || type === Token.NumericLiteral || type === Token.QuotedLiteral) {
if (strict && token.octal) {
throwErrorTolerant(token, Messages.StrictOctalLiteral);
}
return createLiteral(lex().token);
var res = createLiteral(lex().token);
if (type === Token.QuotedLiteral) {
res['verbatim'] = res['value'];
}
return res;
}

if (type === Token.Keyword) {
Expand Down Expand Up @@ -3906,7 +3922,6 @@ to decide on the correct name for identifiers.

return program;
}


// Sync with package.json.
// exports.version = '1.0.0-dev';
Expand All @@ -3916,7 +3931,6 @@ to decide on the correct name for identifiers.
exports.Token = Token;
exports.assert = assert;


// Deep copy.
exports.Syntax = (function () {
var name, types = {};
Expand All @@ -3936,8 +3950,6 @@ to decide on the correct name for identifiers.
}

return types;


}());

}));
Expand Down
55 changes: 55 additions & 0 deletions browser/scripts/sjs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
var fs = require("fs");

var sweet = require("./sweet.js");

var argv = require("optimist")
.usage("Usage: sjs [options] [path/to/file.js]")
.alias('o', 'output')
.describe('o', 'Output file path')
.alias('m', 'module')
.describe('m', 'use a module file for loading macro definitions')
.alias('w', 'watch')
.describe('w', 'watch a file')
.boolean('watch')
.alias('t', 'tokens')
.describe('t', 'just emit the expanded tokens without parsing an AST')
.argv;

exports.run = function() {
var infile = argv._[0];
var outfile = argv.output;
var watch = argv.watch;
var tokens = argv.tokens;

var file;
if (infile) {
file = fs.readFileSync(infile, "utf8");
} else {
file = fs.readFileSync('/dev/stdin').toString();
}

var module = argv.module;
var modulefile;

if(module) {
modulefile = fs.readFileSync(module, "utf8");
file = modulefile + "\n" + file;
}

if (watch && outfile){
fs.watch(infile, function(){
file = fs.readFileSync(infile, "utf8");
try {
fs.writeFileSync(outfile, sweet.compile(file), "utf8");
} catch (e) {
console.log(e);
}
});
} else if(outfile) {
fs.writeFileSync(outfile, sweet.compile(file), "utf8");
} else if(tokens) {
console.log(sweet.expand(file))
} else {
console.log(sweet.compile(file));
}
};
4 changes: 2 additions & 2 deletions browser/scripts/sweet.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@
var expanded = expander.expand(readTree);
return expanded;
};

exports.compile = function compile(code, options) {
return codegen.generate(exports.parse(code, options));
return codegen.generate(exports.parse(code, options), {verbatim: 'verbatim'});
}
}));
4 changes: 3 additions & 1 deletion lib/expander.js
Original file line number Diff line number Diff line change
Expand Up @@ -1114,7 +1114,8 @@
head.token.type === parser.Token.StringLiteral ||
head.token.type === parser.Token.BooleanLiteral ||
head.token.type === parser.Token.RegexLiteral ||
head.token.type === parser.Token.NullLiteral) {
head.token.type === parser.Token.NullLiteral ||
head.token.type === parser.Token.QuotedLiteral) {

return step(Lit.create(head), rest);
// identifier
Expand Down Expand Up @@ -1610,6 +1611,7 @@
};
}
}

throwError("Could not match any cases for macro: " + macroNameStx.token.value);
};
}
Expand Down
38 changes: 25 additions & 13 deletions lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ to decide on the correct name for identifiers.
StringLiteral: 8,
Delimiter: 9,
Pattern: 10,
RegexLiteral: 11
RegexLiteral: 11,
QuotedLiteral: 12
};

TokenName = {};
Expand All @@ -109,6 +110,7 @@ to decide on the correct name for identifiers.
TokenName[Token.Punctuator] = 'Punctuator';
TokenName[Token.StringLiteral] = 'String';
TokenName[Token.Delimiter] = 'Delimiter';
TokenName[Token.QuotedLiteral] = 'Quote';

Syntax = {
AssignmentExpression: 'AssignmentExpression',
Expand Down Expand Up @@ -900,12 +902,17 @@ to decide on the correct name for identifiers.

// 7.8.4 String Literals

function scanStringLiteral() {
function scanStringLiteral(sought, type) {
var str = '', quote, start, ch, code, unescaped, restore, octal = false;

if (type == null) {
assert(sought == null, "sought must not be supplied unless type is");
type = Token.StringLiteral;
}

quote = source[index];
assert((quote === '\'' || quote === '"'),
'String literal must starts with a quote');
assert((quote === '\'' || quote === '"' || (sought != null && quote == sought)),
'String literal must start with a quote');

start = index;
++index;
Expand Down Expand Up @@ -995,7 +1002,7 @@ to decide on the correct name for identifiers.
}

return {
type: Token.StringLiteral,
type: type,
value: str,
octal: octal,
lineNumber: lineNumber,
Expand All @@ -1004,6 +1011,10 @@ to decide on the correct name for identifiers.
};
}

function scanQuotedLiteral() {
return scanStringLiteral('`', Token.QuotedLiteral);
}

function scanRegExp() {
var str = '', ch, start, pattern, flags, value, classMarker = false, restore;

Expand Down Expand Up @@ -1103,9 +1114,7 @@ to decide on the correct name for identifiers.
token.type === Token.BooleanLiteral ||
token.type === Token.NullLiteral;
}



// only used by the reader
function advance() {
var ch, token;
Expand All @@ -1128,6 +1137,9 @@ to decide on the correct name for identifiers.
return token;
}

if (ch === '`') {
return scanQuotedLiteral();
}

if (ch === '\'' || ch === '"') {
return scanStringLiteral();
Expand Down Expand Up @@ -1548,11 +1560,15 @@ to decide on the correct name for identifiers.
};
}

if (type === Token.StringLiteral || type === Token.NumericLiteral) {
if (type === Token.StringLiteral || type === Token.NumericLiteral || type === Token.QuotedLiteral) {
if (strict && token.octal) {
throwErrorTolerant(token, Messages.StrictOctalLiteral);
}
return createLiteral(lex().token);
var res = createLiteral(lex().token);
if (type === Token.QuotedLiteral) {
res['verbatim'] = res['value'];
}
return res;
}

if (type === Token.Keyword) {
Expand Down Expand Up @@ -3906,7 +3922,6 @@ to decide on the correct name for identifiers.

return program;
}


// Sync with package.json.
// exports.version = '1.0.0-dev';
Expand All @@ -3916,7 +3931,6 @@ to decide on the correct name for identifiers.
exports.Token = Token;
exports.assert = assert;


// Deep copy.
exports.Syntax = (function () {
var name, types = {};
Expand All @@ -3936,8 +3950,6 @@ to decide on the correct name for identifiers.
}

return types;


}());

}));
Expand Down
14 changes: 10 additions & 4 deletions lib/sjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ var fs = require("fs");
var sweet = require("./sweet.js");

var argv = require("optimist")
.usage("Usage: sjs [options] path/to/file.js")
.demand(1)
.usage("Usage: sjs [options] [path/to/file.js]")
.alias('o', 'output')
.describe('o', 'Output file path')
.alias('m', 'module')
Expand All @@ -21,15 +20,22 @@ exports.run = function() {
var outfile = argv.output;
var watch = argv.watch;
var tokens = argv.tokens;
var file = fs.readFileSync(infile, "utf8");

var file;
if (infile) {
file = fs.readFileSync(infile, "utf8");
} else {
file = fs.readFileSync('/dev/stdin').toString();
}

var module = argv.module;
var modulefile;

if(module) {
modulefile = fs.readFileSync(module, "utf8");
file = modulefile + "\n" + file;
}

if (watch && outfile){
fs.watch(infile, function(){
file = fs.readFileSync(infile, "utf8");
Expand Down
4 changes: 2 additions & 2 deletions lib/sweet.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@
var expanded = expander.expand(readTree);
return expanded;
};

exports.compile = function compile(code, options) {
return codegen.generate(exports.parse(code, options));
return codegen.generate(exports.parse(code, options), {verbatim: 'verbatim'});
}
}));
Loading