solidity-antlr4
Version:
Solidity Lang Lexer and Parser by official ANTLR4 grammar
69 lines (68 loc) • 2.19 kB
JavaScript
import { doc, util } from "prettier";
export {};
export const lineComments = [
"LINE_COMMENT",
"AssemblyBlockLINE_COMMENT",
"YulLINE_COMMENT",
"PragmaLINE_COMMENT"
];
export const blockComments = [
"COMMENT",
"AssemblyBlockCOMMENT",
"YulCOMMENT",
"PragmaCOMMENT"
];
export const comments = [...lineComments, ...blockComments];
export class BasePrinter {
constructor(options, print) {
this.options = options;
this.print = print;
}
space = " ";
dot = ".";
comma = ",";
semi = ";";
quote = `"`;
singleQuote = `'`;
builders = doc.builders;
// The pangu space
pangu = (path) => {
return util.isNextLineEmpty(this.options.originalText, this.options.locEnd(path.node)) ? this.builders.hardline : "";
};
// value => "value"
literal = (value) => {
return this.options.singleQuote ? [this.singleQuote, value, this.singleQuote] : [this.quote, value, this.quote];
};
// value = { value }
block = (value, empty = false) => {
if (empty)
return ["{", "}"];
const groupId = Symbol("block");
const line = this.options.bracketSpacing ? this.builders.line : this.builders.softline;
const beforeLine = this.builders.indentIfBreak(line, { groupId });
const content = this.builders.indentIfBreak(value, { groupId });
return this.builders.group(["{", beforeLine, content, line, "}"], { id: groupId });
};
// value[] => value1, value2, value3
paramater = (value, sep = [this.comma, this.builders.line]) => {
return this.builders.join(sep, value);
};
// value => (value)
tuple = (value, groupId = Symbol("tuple")) => {
const content = this.builders.indentIfBreak(value, { groupId });
const line = this.builders.softline;
return this.builders.group(
["(", this.builders.indentIfBreak(line, { groupId }), content, line, ")"],
{ id: groupId }
);
};
// value => [value]
list = (value, groupId = Symbol("list")) => {
const content = this.builders.indentIfBreak(value, { groupId });
const line = this.builders.softline;
return this.builders.group(
["[", this.builders.indentIfBreak(line, { groupId }), content, line, "]"],
{ id: groupId }
);
};
}