Skip to content

Commit

Permalink
feat: add quote and multilineQuote methods
Browse files Browse the repository at this point in the history
  • Loading branch information
BeksOmega committed Aug 15, 2023
1 parent b79c582 commit 04cd2fe
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 15 deletions.
6 changes: 1 addition & 5 deletions core/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export type BlockGenerator = (
/**
* Class for a code generator that translates the blocks into a language.
*/
export abstract class CodeGenerator {
export class CodeGenerator {
name_: string;

/** A dictionary of block generator functions keyed by block type. */
Expand Down Expand Up @@ -437,10 +437,6 @@ export abstract class CodeGenerator {
return msg.replace(/%1/g, "'" + id + "'");
}

abstract quote_(toQuote: string): string;

abstract multiline_quote_(toQuote: string): string;

/**
* Add one or more words to the list of reserved words for this language.
*
Expand Down
10 changes: 8 additions & 2 deletions generators/dart/dart_generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ export class DartGenerator extends CodeGenerator {
* @param {string} string Text to encode.
* @return {string} Dart string.
*/
quote_(string) {
quote(string) {
// Can't use goog.string.quote since $ must also be escaped.
string = string.replace(/\\/g, '\\\\')
.replace(/\n/g, '\\\n')
Expand All @@ -186,19 +186,25 @@ export class DartGenerator extends CodeGenerator {
return '\'' + string + '\'';
}

/** Alias for the `quote` method. Use `quote` instead. */
quote_ = this.quote;

/**
* Encode a string as a properly escaped multiline Dart string, complete with
* quotes.
* @param {string} string Text to encode.
* @return {string} Dart string.
*/
multiline_quote_(string) {
multilineQuote(string) {
const lines = string.split(/\n/g).map(this.quote_);
// Join with the following, plus a newline:
// + '\n' +
return lines.join(' + \'\\n\' + \n');
}

/** Alias for the `multilineQuote` method. Use `multilineQuote` instead. */
multiline_quote_ = this.multilineQuote;

/**
* Common tasks for generating Dart from blocks.
* Handles comments for the specified block and any connected value blocks.
Expand Down
10 changes: 8 additions & 2 deletions generators/javascript/javascript_generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ export class JavascriptGenerator extends CodeGenerator {
* @param {string} string Text to encode.
* @return {string} JavaScript string.
*/
quote_(string) {
quote(string) {
// Can't use goog.string.quote since Google's style guide recommends
// JS string literals use single quotes.
string = string.replace(/\\/g, '\\\\')
Expand All @@ -216,19 +216,25 @@ export class JavascriptGenerator extends CodeGenerator {
return '\'' + string + '\'';
}

/** Alias for the `quote` method. Use `quote` instead. */
quote_ = this.quote;

/**
* Encode a string as a properly escaped multiline JavaScript string, complete
* with quotes.
* @param {string} string Text to encode.
* @return {string} JavaScript string.
*/
multiline_quote_(string) {
multilineQuote(string) {
// Can't use goog.string.quote since Google's style guide recommends
// JS string literals use single quotes.
const lines = string.split(/\n/g).map(this.quote_);
return lines.join(' + \'\\n\' +\n');
}

/** Alias for the `multilineQuote` method. Use `multilineQuote` instead. */
multiline_quote_ = this.multilineQuote;

/**
* Common tasks for generating JavaScript from blocks.
* Handles comments for the specified block and any connected value blocks.
Expand Down
10 changes: 8 additions & 2 deletions generators/lua/lua_generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,26 +150,32 @@ export class LuaGenerator extends CodeGenerator {
* @param {string} string Text to encode.
* @return {string} Lua string.
*/
quote_(string) {
quote(string) {
string = string.replace(/\\/g, '\\\\')
.replace(/\n/g, '\\\n')
.replace(/'/g, '\\\'');
return '\'' + string + '\'';
};

/** Alias for the `quote` method. Use `quote` instead. */
quote_ = this.quote;

/**
* Encode a string as a properly escaped multiline Lua string, complete with
* quotes.
* @param {string} string Text to encode.
* @return {string} Lua string.
*/
multiline_quote_(string) {
multilineQuote(string) {
const lines = string.split(/\n/g).map(this.quote_);
// Join with the following, plus a newline:
// .. '\n' ..
return lines.join(' .. \'\\n\' ..\n');
};

/** Alias for the `multilineQuote` method. Use `multilineQuote` instead. */
multiline_quote_ = this.multilineQuote;

/**
* Common tasks for generating Lua from blocks.
* Handles comments for the specified block and any connected value blocks.
Expand Down
10 changes: 8 additions & 2 deletions generators/php/php_generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,27 +185,33 @@ export class PhpGenerator extends CodeGenerator {
* @param {string} string Text to encode.
* @return {string} PHP string.
*/
quote_(string) {
quote(string) {
string = string.replace(/\\/g, '\\\\')
.replace(/\n/g, '\\\n')
.replace(/'/g, '\\\'');
return '\'' + string + '\'';
};

/** Alias for the `quote` method. Use `quote` instead. */
quote_ = this.quote;

/**
* Encode a string as a properly escaped multiline PHP string, complete with
* quotes.
* @param {string} string Text to encode.
* @return {string} PHP string.
*/
multiline_quote_(string) {
multilineQuote(string) {
const lines = string.split(/\n/g).map(this.quote_);
// Join with the following, plus a newline:
// . "\n" .
// Newline escaping only works in double-quoted strings.
return lines.join(' . \"\\n\" .\n');
};

/** Alias for the `multilineQuote` method. Use `multilineQuote` instead. */
multiline_quote_ = this.multilineQuote;

/**
* Common tasks for generating PHP from blocks.
* Handles comments for the specified block and any connected value blocks.
Expand Down
10 changes: 8 additions & 2 deletions generators/python/python_generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ export class PythonGenerator extends CodeGenerator {
* @param {string} string Text to encode.
* @return {string} Python string.
*/
quote_(string) {
quote(string) {
string = string.replace(/\\/g, '\\\\').replace(/\n/g, '\\\n');

// Follow the CPython behaviour of repr() for a non-byte string.
Expand All @@ -244,19 +244,25 @@ export class PythonGenerator extends CodeGenerator {
return quote + string + quote;
}

/** Alias for the `quote` method. Use `quote` instead. */
quote_ = this.quote;

/**
* Encode a string as a properly escaped multiline Python string, complete
* with quotes.
* @param {string} string Text to encode.
* @return {string} Python string.
*/
multiline_quote_(string) {
multilineQuote(string) {
const lines = string.split(/\n/g).map(this.quote_);
// Join with the following, plus a newline:
// + '\n' +
return lines.join(' + \'\\n\' + \n');
}

/** Alias for the `multilineQuote` method. Use `multilineQuote` instead. */
multiline_quote_ = this.multilineQuote;

/**
* Common tasks for generating Python from blocks.
* Handles comments for the specified block and any connected value blocks.
Expand Down

0 comments on commit 04cd2fe

Please sign in to comment.