Skip to content

Commit

Permalink
Merge PR #482: Replace "command" terminology with "clause"
Browse files Browse the repository at this point in the history
  • Loading branch information
nene authored Oct 1, 2022
2 parents 6070055 + d26c99a commit 3070430
Show file tree
Hide file tree
Showing 28 changed files with 55 additions and 55 deletions.
2 changes: 1 addition & 1 deletion src/formatter/Indentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const INDENT_TYPE_BLOCK_LEVEL = 'block-level';
* There are two types of indentation levels:
*
* - BLOCK_LEVEL : increased by open-parenthesis
* - TOP_LEVEL : increased by RESERVED_COMMAND words
* - TOP_LEVEL : increased by RESERVED_CLAUSE words
*/
export default class Indentation {
private indentTypes: string[] = [];
Expand Down
2 changes: 1 addition & 1 deletion src/formatter/tabularStyle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default function toTabularFormat(tokenText: string, indentStyle: IndentSt
export function isTabularToken(type: TokenType): boolean {
return (
isLogicalOperator(type) ||
type === TokenType.RESERVED_COMMAND ||
type === TokenType.RESERVED_CLAUSE ||
type === TokenType.RESERVED_SELECT ||
type === TokenType.RESERVED_SET_OPERATION ||
type === TokenType.RESERVED_JOIN ||
Expand Down
6 changes: 3 additions & 3 deletions src/languages/bigquery/bigquery.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { functions } from './bigquery.functions';

const reservedSelect = expandPhrases(['SELECT [ALL | DISTINCT] [AS STRUCT | AS VALUE]']);

const reservedCommands = expandPhrases([
const reservedClauses = expandPhrases([
// Queries: https://cloud.google.com/bigquery/docs/reference/standard-sql/query-syntax
'WITH [RECURSIVE]',
'FROM',
Expand Down Expand Up @@ -143,7 +143,7 @@ export default class BigQueryFormatter extends Formatter {
// TODO: handle trailing comma in select clause
tokenizer() {
return new Tokenizer({
reservedCommands,
reservedClauses,
reservedSelect,
reservedSetOperations,
reservedJoins,
Expand Down Expand Up @@ -175,7 +175,7 @@ function postProcess(tokens: Token[]): Token[] {
return detectArraySubscripts(combineParameterizedTypes(tokens));
}

// Converts OFFSET token inside array from RESERVED_COMMAND to RESERVED_FUNCTION_NAME
// Converts OFFSET token inside array from RESERVED_CLAUSE to RESERVED_FUNCTION_NAME
// See: https://cloud.google.com/bigquery/docs/reference/standard-sql/functions-and-operators#array_subscript_operator
function detectArraySubscripts(tokens: Token[]) {
let prevToken = EOF_TOKEN;
Expand Down
4 changes: 2 additions & 2 deletions src/languages/db2/db2.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { keywords } from './db2.keywords';

const reservedSelect = expandPhrases(['SELECT [ALL | DISTINCT]']);

const reservedCommands = expandPhrases([
const reservedClauses = expandPhrases([
// queries
'WITH',
'FROM',
Expand Down Expand Up @@ -177,7 +177,7 @@ const reservedPhrases = expandPhrases([
export default class Db2Formatter extends Formatter {
tokenizer() {
return new Tokenizer({
reservedCommands,
reservedClauses,
reservedSelect,
reservedSetOperations,
reservedJoins,
Expand Down
4 changes: 2 additions & 2 deletions src/languages/hive/hive.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { keywords } from './hive.keywords';

const reservedSelect = expandPhrases(['SELECT [ALL | DISTINCT]']);

const reservedCommands = expandPhrases([
const reservedClauses = expandPhrases([
// queries
'WITH',
'FROM',
Expand Down Expand Up @@ -84,7 +84,7 @@ const reservedPhrases = expandPhrases(['{ROWS | RANGE} BETWEEN']);
export default class HiveFormatter extends Formatter {
tokenizer() {
return new Tokenizer({
reservedCommands,
reservedClauses,
reservedSelect,
reservedSetOperations,
reservedJoins,
Expand Down
4 changes: 2 additions & 2 deletions src/languages/mariadb/mariadb.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { functions } from './mariadb.functions';

const reservedSelect = expandPhrases(['SELECT [ALL | DISTINCT | DISTINCTROW]']);

const reservedCommands = expandPhrases([
const reservedClauses = expandPhrases([
// queries
'WITH [RECURSIVE]',
'FROM',
Expand Down Expand Up @@ -263,7 +263,7 @@ const reservedPhrases = expandPhrases([
export default class MariaDbFormatter extends Formatter {
tokenizer() {
return new Tokenizer({
reservedCommands,
reservedClauses,
reservedSelect,
reservedSetOperations,
reservedJoins,
Expand Down
4 changes: 2 additions & 2 deletions src/languages/mysql/mysql.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { functions } from './mysql.functions';

const reservedSelect = expandPhrases(['SELECT [ALL | DISTINCT | DISTINCTROW]']);

const reservedCommands = expandPhrases([
const reservedClauses = expandPhrases([
// queries
'WITH [RECURSIVE]',
'FROM',
Expand Down Expand Up @@ -231,7 +231,7 @@ const reservedPhrases = expandPhrases([
export default class MySqlFormatter extends Formatter {
tokenizer() {
return new Tokenizer({
reservedCommands,
reservedClauses,
reservedSelect,
reservedSetOperations,
reservedJoins,
Expand Down
4 changes: 2 additions & 2 deletions src/languages/n1ql/n1ql.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { keywords } from './n1ql.keywords';

const reservedSelect = expandPhrases(['SELECT [ALL | DISTINCT]']);

const reservedCommands = expandPhrases([
const reservedClauses = expandPhrases([
// queries
'WITH',
'FROM',
Expand Down Expand Up @@ -81,7 +81,7 @@ const reservedPhrases = expandPhrases(['{ROWS | RANGE | GROUPS} BETWEEN']);
export default class N1qlFormatter extends Formatter {
tokenizer() {
return new Tokenizer({
reservedCommands,
reservedClauses,
reservedSelect,
reservedSetOperations,
reservedJoins,
Expand Down
4 changes: 2 additions & 2 deletions src/languages/plsql/plsql.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { functions } from './plsql.functions';

const reservedSelect = expandPhrases(['SELECT [ALL | DISTINCT | UNIQUE]']);

const reservedCommands = expandPhrases([
const reservedClauses = expandPhrases([
// queries
'WITH',
'FROM',
Expand Down Expand Up @@ -81,7 +81,7 @@ const reservedPhrases = expandPhrases([
export default class PlSqlFormatter extends Formatter {
tokenizer() {
return new Tokenizer({
reservedCommands,
reservedClauses,
reservedSelect,
reservedSetOperations,
reservedJoins,
Expand Down
4 changes: 2 additions & 2 deletions src/languages/postgresql/postgresql.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { keywords } from './postgresql.keywords';

const reservedSelect = expandPhrases(['SELECT [ALL | DISTINCT]']);

const reservedCommands = expandPhrases([
const reservedClauses = expandPhrases([
// queries
'WITH [RECURSIVE]',
'FROM',
Expand Down Expand Up @@ -249,7 +249,7 @@ const reservedPhrases = expandPhrases([
export default class PostgreSqlFormatter extends Formatter {
tokenizer() {
return new Tokenizer({
reservedCommands,
reservedClauses,
reservedSelect,
reservedSetOperations,
reservedJoins,
Expand Down
4 changes: 2 additions & 2 deletions src/languages/redshift/redshift.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { keywords } from './redshift.keywords';

const reservedSelect = expandPhrases(['SELECT [ALL | DISTINCT]']);

const reservedCommands = expandPhrases([
const reservedClauses = expandPhrases([
// queries
'WITH [RECURSIVE]',
'FROM',
Expand Down Expand Up @@ -143,7 +143,7 @@ const reservedPhrases = expandPhrases([
export default class RedshiftFormatter extends Formatter {
tokenizer() {
return new Tokenizer({
reservedCommands,
reservedClauses,
reservedSelect,
reservedSetOperations,
reservedJoins,
Expand Down
4 changes: 2 additions & 2 deletions src/languages/singlestoredb/singlestoredb.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { functions } from './singlestoredb.functions';

const reservedSelect = expandPhrases(['SELECT [ALL | DISTINCT | DISTINCTROW]']);

const reservedCommands = expandPhrases([
const reservedClauses = expandPhrases([
// queries
'WITH',
'FROM',
Expand Down Expand Up @@ -233,7 +233,7 @@ const reservedPhrases = expandPhrases([
export default class SingleStoreDbFormatter extends Formatter {
tokenizer() {
return new Tokenizer({
reservedCommands,
reservedClauses,
reservedSelect,
reservedSetOperations,
reservedJoins,
Expand Down
4 changes: 2 additions & 2 deletions src/languages/snowflake/snowflake.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const reservedSelect = expandPhrases(['SELECT [ALL | DISTINCT]']);
// Steps 1-4 can be combined by the following script in the developer console:
// $x('//tbody/tr/*[1]//a/span/text()').map(x => x.nodeValue) // Step 1
// filter(x => !x.match(/\(.*\)/) && !x.match(/…/) && !x.match(/<.*>/)) // Step 2-4
const reservedCommands = expandPhrases([
const reservedClauses = expandPhrases([
// queries
'WITH [RECURSIVE]',
'FROM',
Expand Down Expand Up @@ -315,7 +315,7 @@ const reservedPhrases = expandPhrases([
export default class SnowflakeFormatter extends Formatter {
tokenizer() {
return new Tokenizer({
reservedCommands,
reservedClauses,
reservedSelect,
reservedSetOperations,
reservedJoins,
Expand Down
4 changes: 2 additions & 2 deletions src/languages/spark/spark.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { functions } from './spark.functions';
// http://spark.apache.org/docs/latest/sql-ref-syntax.html
const reservedSelect = expandPhrases(['SELECT [ALL | DISTINCT]']);

const reservedCommands = expandPhrases([
const reservedClauses = expandPhrases([
// queries
'WITH',
'FROM',
Expand Down Expand Up @@ -119,7 +119,7 @@ const reservedPhrases = expandPhrases([
export default class SparkFormatter extends Formatter {
tokenizer() {
return new Tokenizer({
reservedCommands,
reservedClauses,
reservedSelect,
reservedSetOperations,
reservedJoins,
Expand Down
4 changes: 2 additions & 2 deletions src/languages/sql/sql.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { keywords } from './sql.keywords';

const reservedSelect = expandPhrases(['SELECT [ALL | DISTINCT]']);

const reservedCommands = expandPhrases([
const reservedClauses = expandPhrases([
// queries
'WITH [RECURSIVE]',
'FROM',
Expand Down Expand Up @@ -73,7 +73,7 @@ const reservedPhrases = expandPhrases([
export default class SqlFormatter extends Formatter {
tokenizer() {
return new Tokenizer({
reservedCommands,
reservedClauses,
reservedSelect,
reservedSetOperations,
reservedJoins,
Expand Down
4 changes: 2 additions & 2 deletions src/languages/sqlite/sqlite.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { keywords } from './sqlite.keywords';

const reservedSelect = expandPhrases(['SELECT [ALL | DISTINCT]']);

const reservedCommands = expandPhrases([
const reservedClauses = expandPhrases([
// queries
'WITH [RECURSIVE]',
'FROM',
Expand Down Expand Up @@ -62,7 +62,7 @@ const reservedPhrases = expandPhrases([
export default class SqliteFormatter extends Formatter {
tokenizer() {
return new Tokenizer({
reservedCommands,
reservedClauses,
reservedSelect,
reservedSetOperations,
reservedJoins,
Expand Down
4 changes: 2 additions & 2 deletions src/languages/transactsql/transactsql.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { keywords } from './transactsql.keywords';

const reservedSelect = expandPhrases(['SELECT [ALL | DISTINCT]']);

const reservedCommands = expandPhrases([
const reservedClauses = expandPhrases([
// queries
'WITH',
'FROM',
Expand Down Expand Up @@ -220,7 +220,7 @@ const reservedPhrases = expandPhrases([
export default class TransactSqlFormatter extends Formatter {
tokenizer() {
return new Tokenizer({
reservedCommands,
reservedClauses,
reservedSelect,
reservedSetOperations,
reservedJoins,
Expand Down
4 changes: 2 additions & 2 deletions src/languages/trino/trino.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { keywords } from './trino.keywords';
const reservedSelect = expandPhrases(['SELECT [ALL | DISTINCT]']);

// https://github.com/trinodb/trino/blob/432d2897bdef99388c1a47188743a061c4ac1f34/core/trino-parser/src/main/antlr4/io/trino/sql/parser/SqlBase.g4#L41
const reservedCommands = expandPhrases([
const reservedClauses = expandPhrases([
// queries
'WITH [RECURSIVE]',
'FROM',
Expand Down Expand Up @@ -122,7 +122,7 @@ const reservedPhrases = expandPhrases(['{ROWS | RANGE | GROUPS} BETWEEN']);
export default class TrinoFormatter extends Formatter {
tokenizer() {
return new Tokenizer({
reservedCommands,
reservedClauses,
reservedSelect,
reservedSetOperations,
reservedJoins,
Expand Down
8 changes: 4 additions & 4 deletions src/lexer/Tokenizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export default class Tokenizer {
/(?:0x[0-9a-fA-F]+|0b[01]+|(?:-\s*)?[0-9]+(?:\.[0-9]*)?(?:[eE][-+]?[0-9]+(?:\.[0-9]+)?)?)(?!\w)/uy,
},
// RESERVED_PHRASE is matched before all other keyword tokens
// to e.g. prioritize matching "TIMESTAMP WITH TIME ZONE" phrase over "WITH" command.
// to e.g. prioritize matching "TIMESTAMP WITH TIME ZONE" phrase over "WITH" clause.
{
type: TokenType.RESERVED_PHRASE,
regex: regex.reservedWord(cfg.reservedPhrases ?? [], cfg.identChars),
Expand All @@ -72,12 +72,12 @@ export default class Tokenizer {
},
{
type: TokenType.LIMIT,
regex: cfg.reservedCommands.includes('LIMIT') ? /LIMIT\b/iuy : undefined,
regex: cfg.reservedClauses.includes('LIMIT') ? /LIMIT\b/iuy : undefined,
text: toCanonical,
},
{
type: TokenType.RESERVED_COMMAND,
regex: regex.reservedWord(cfg.reservedCommands, cfg.identChars),
type: TokenType.RESERVED_CLAUSE,
regex: regex.reservedWord(cfg.reservedClauses, cfg.identChars),
text: toCanonical,
},
{
Expand Down
2 changes: 1 addition & 1 deletion src/lexer/TokenizerOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export interface ParamTypes {

export interface TokenizerOptions {
// Main clauses that start new block, like: WITH, FROM, WHERE, ORDER BY
reservedCommands: string[];
reservedClauses: string[];
// SELECT clause and its variations
reservedSelect: string[];
// True to support XOR in addition to AND and OR
Expand Down
10 changes: 5 additions & 5 deletions src/lexer/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export enum TokenType {
RESERVED_FUNCTION_NAME = 'RESERVED_FUNCTION_NAME',
RESERVED_PHRASE = 'RESERVED_PHRASE',
RESERVED_SET_OPERATION = 'RESERVED_SET_OPERATION',
RESERVED_COMMAND = 'RESERVED_COMMAND',
RESERVED_CLAUSE = 'RESERVED_CLAUSE',
RESERVED_SELECT = 'RESERVED_SELECT',
RESERVED_JOIN = 'RESERVED_JOIN',
ARRAY_IDENTIFIER = 'ARRAY_IDENTIFIER', // IDENTIFIER token in front of [
Expand Down Expand Up @@ -74,17 +74,17 @@ export const testToken =
export const isToken = {
ARRAY: testToken({ text: 'ARRAY', type: TokenType.RESERVED_KEYWORD }),
BY: testToken({ text: 'BY', type: TokenType.RESERVED_KEYWORD }),
SET: testToken({ text: 'SET', type: TokenType.RESERVED_COMMAND }),
SET: testToken({ text: 'SET', type: TokenType.RESERVED_CLAUSE }),
STRUCT: testToken({ text: 'STRUCT', type: TokenType.RESERVED_KEYWORD }),
WINDOW: testToken({ text: 'WINDOW', type: TokenType.RESERVED_COMMAND }),
WINDOW: testToken({ text: 'WINDOW', type: TokenType.RESERVED_CLAUSE }),
};

/** Checks if token is any Reserved Keyword or Command */
/** Checks if token is any Reserved Keyword or Clause */
export const isReserved = (type: TokenType): boolean =>
type === TokenType.RESERVED_KEYWORD ||
type === TokenType.RESERVED_FUNCTION_NAME ||
type === TokenType.RESERVED_PHRASE ||
type === TokenType.RESERVED_COMMAND ||
type === TokenType.RESERVED_CLAUSE ||
type === TokenType.RESERVED_SELECT ||
type === TokenType.RESERVED_SET_OPERATION ||
type === TokenType.RESERVED_JOIN ||
Expand Down
2 changes: 1 addition & 1 deletion src/parser/grammar.ne
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ all_columns_asterisk -> %ASTERISK {%
() => ({ type: NodeType.all_columns_asterisk })
%}

other_clause -> %RESERVED_COMMAND free_form_sql:* {%
other_clause -> %RESERVED_CLAUSE free_form_sql:* {%
([nameToken, children]) => ({
type: NodeType.clause,
nameKw: toKeywordNode(nameToken),
Expand Down
Loading

0 comments on commit 3070430

Please sign in to comment.