diff --git a/mode/sql.py b/mode/sql.py index f8d344b..d648ae8 100644 --- a/mode/sql.py +++ b/mode/sql.py @@ -9,82 +9,56 @@ class BitStringGrammar(Grammar): class HexStringGrammar(Grammar): rules = [NocasePatternRule(r'data', r'[0-9a-f]+')] -class PlPgSqlGrammar(Grammar): - rules = [ - PatternRule(r'spaces', r' +'), +base_rules = [ + PatternRule(r'spaces', r' +'), + PatternRule(r'comment', r'--.*\n$'), + RegionRule(r'comment', '/\*', Grammar, '\*/'), + PatternRule(r'delimiter', r':=|[():;,\.\$\[\]~]'), +] - PatternRule(r'comment', r'--.*\n$'), - RegionRule(r'comment', '/\*', Grammar, '\*/'), - PatternRule(r'delimiter', r':=|[():;,\.\[\]~]'), - - NocasePatternRule(r'attribute', r'(?:check|exists|unique|not null|default|primary key|minvalue|foreign key|references)(?![A-Za-z0-9_])'), - NocasePatternRule(r'sql_keyword', r'(?:declare|begin|end if|end loop|end|raise notice|return|if|while|for|else)(?![A-Za-z0-9_])'), - NocasePatternRule(r'sql_operator', r'(?:case|when|then|else|end|not in|not|and|or|is not|is|in|between|overlaps|like|ilike|similar)(?![A-Za-z0-9_])'), - NocasePatternRule(r'sql_keyword', r'(?:create database|create index|create sequence|create table|create trigger|create view|select|insert|update|delete|drop database|drop index|drop sequence|drop table|drop trigger|drop view|create user|alter user|drop user|drop function|grant|revoke|create function|create or replace function|create or replace view|create language|create operator|create type)(?![A-Za-z0-9_])'), - NocasePatternRule(r'pseudokeyword', r'(?:returns|language|right join|left join|inner join|outer join|join|where|unknown|null|true|false|into|values|as|from|order by|next|asc|desc|limit|distinct|cascade|alias for|using|on)(?![A-Za-z0-9_])'), - NocasePatternRule(r'sql_type', r'(?:void|row|serial|varchar|float|integer|int|text|timestamptz|timestamp|datetz|date|timetz|time|boolean|bool|record|row)(?![A-Za-z0-9_])'), - PatternRule(r'sql_builtin', r'(?:nextval|current_timestamp|current_time|current_date)(?![A-Za-z0-9_])'), - PatternRule(r'number', r'[0-9]+(?:e[+-]?[0-9]+)?'), - PatternRule(r'number', r'[0-9]+\.(?:[0-9]+)?(?:e[+-]?[0-9]+)?'), - PatternRule(r'number', r'\.[0-9]+(?:e[+-]?[0-9]+)'), - RegionRule(r'string', "''", StringGrammar1, "''"), - RegionRule(r'string', "B'", BitStringGrammar, "'"), - RegionRule(r'string', "X'", HexStringGrammar, "'"), - RegionRule(r'sql_quoted', '"', StringGrammar2, '"'), - PatternRule(r'sql_variable', r'\$[1-9][0-9]*'), - PatternRule(r'sql_bareword', r'[A-Za-z0-9_]+'), - PatternRule(r'empty', r'^ *\n$'), - PatternRule(r'eol', r'\n'), - ] +function_rules = [ + NocaseRegionRule(r'function', r'create function', FunctionGrammar, r';'), + NocaseRegionRule(r'function', r'create or replace function', FunctionGrammar, r';'), +] + +sql_type_rule = NocasePatternRule(r'sql_type', r'(?:void|row|serial|varchar|float|integer|int|text|timestamptz|timestamp|datetz|date|timetz|time|boolean|bool)(?![A-Za-z0-9_])') + +sql_rules = [ + NocasePatternRule(r'attribute', r'(?:check|exists|unique|not null|default|primary key|minvalue|foreign key|references)(?![A-Za-z0-9_])'), + NocasePatternRule(r'sql_operator', r'(?:case|when|then|else|end|not in|not|and|or|is not|is|in|between|overlaps|like|ilike|similar)(?![A-Za-z0-9_])'), + NocasePatternRule(r'sql_keyword', r'(?:create database|create index|create sequence|create table|create trigger|create view|select|insert|update|delete|drop database|drop index|drop sequence|drop table|drop trigger|drop view|create user|alter user|drop user|drop function|grant|revoke|create function|create or replace function|create or replace view|create language|create operator|create type)(?![A-Za-z0-9_])'), + NocasePatternRule(r'pseudokeyword', r'(?:returns|language|right join|left join|inner join|outer join|join|where|unknown|null|true|false|into|values|as|from|order by|asc|desc|limit|distinct|cascade|using|on)(?![A-Za-z0-9_])'), + sql_type_rule, + PatternRule(r'sql_builtin', r'(?:nextval|current_timestamp|current_time|current_date)(?![A-Za-z0-9_])'), + PatternRule(r'number', r'[0-9]+(?:e[+-]?[0-9]+)?'), + PatternRule(r'number', r'[0-9]+\.(?:[0-9]+)?(?:e[+-]?[0-9]+)?'), + PatternRule(r'number', r'\.[0-9]+(?:e[+-]?[0-9]+)'), + RegionRule(r'string', "B'", BitStringGrammar, "'"), + RegionRule(r'string', "X'", HexStringGrammar, "'"), + RegionRule(r'string', "'", StringGrammar1, "'"), + RegionRule(r'sql_quoted', '"', StringGrammar2, '"'), +] + +end_rules = [ + NocasePatternRule(r'sql_bareword', r'[a-z_][a-z0-9_]*'), + PatternRule(r'empty', r'^ *\n$'), + PatternRule(r'eol', r'\n'), +] + +class PlPgSqlGrammar(Grammar): + rules = base_rules + sql_rules + end_rules class FunctionGrammar(Grammar): - rules = [ - PatternRule(r'spaces', r' +'), - - PatternRule(r'comment', r'--.*\n$'), - RegionRule(r'comment', '/\*', Grammar, '\*/'), - PatternRule(r'delimiter', r':=|[():;,\.\$\[\]~]'), - + rules = base_rules + [ PatternRule(r'name', r'[a-zA-Z_][a-zA-Z0-9_]*(?=\()'), NocasePatternRule(r'sql_keyword', r'(?:as|returns|language)'), - NocasePatternRule(r'sql_type', r'(?:void|row|serial|varchar|float|integer|int|text|timestamptz|timestamp|datetz|date|timetz|time|boolean|bool)(?![A-Za-z0-9_])'), - + sql_type_rule, NocasePatternRule(r'language', r'(?<=language ) *[a-zA-Z_][a-zA-Z0-9_]+'), - RegionRule(r'definition', "'", PlPgSqlGrammar, "'(?!')"), - PatternRule(r'sql_bareword', r'[A-Za-z0-9_]+'), - PatternRule(r'empty', r'^ *\n$'), - PatternRule(r'eol', r'\n'), - ] + ] + end_rules class SqlGrammar(Grammar): - rules = [ - PatternRule(r'spaces', r' +'), - - PatternRule(r'comment', r'--.*\n$'), - RegionRule(r'comment', '/\*', Grammar, '\*/'), - PatternRule(r'delimiter', r':=|[():;,\.\$\[\]~]'), - - NocaseRegionRule(r'function', r'create function', FunctionGrammar, r';'), - NocaseRegionRule(r'function', r'create or replace function', FunctionGrammar, r';'), - - NocasePatternRule(r'attribute', r'(?:check|exists|unique|not null|default|primary key|minvalue|foreign key|references)(?![A-Za-z0-9_])'), - NocasePatternRule(r'sql_operator', r'(?:case|when|then|else|end|not in|not|and|or|is not|is|in|between|overlaps|like|ilike|similar)(?![A-Za-z0-9_])'), - NocasePatternRule(r'sql_keyword', r'(?:create database|create index|create sequence|create table|create trigger|create view|select|insert|update|delete|drop database|drop index|drop sequence|drop table|drop trigger|drop view|create user|alter user|drop user|drop function|grant|revoke|create function|create or replace function|create or replace view|create language|create operator|create type)(?![A-Za-z0-9_])'), - NocasePatternRule(r'pseudokeyword', r'(?:returns|language|right join|left join|inner join|outer join|join|where|unknown|null|true|false|into|values|as|from|order by|asc|desc|limit|distinct|cascade|using|on)(?![A-Za-z0-9_])'), - NocasePatternRule(r'sql_type', r'(?:void|row|serial|varchar|float|integer|int|text|timestamptz|timestamp|datetz|date|timetz|time|boolean|bool)(?![A-Za-z0-9_])'), - PatternRule(r'sql_builtin', r'(?:nextval|current_timestamp|current_time|current_date)(?![A-Za-z0-9_])'), - PatternRule(r'number', r'[0-9]+(?:e[+-]?[0-9]+)?'), - PatternRule(r'number', r'[0-9]+\.(?:[0-9]+)?(?:e[+-]?[0-9]+)?'), - PatternRule(r'number', r'\.[0-9]+(?:e[+-]?[0-9]+)'), - RegionRule(r'string', "B'", BitStringGrammar, "'"), - RegionRule(r'string', "X'", HexStringGrammar, "'"), - RegionRule(r'string', "'", StringGrammar1, "'"), - RegionRule(r'sql_quoted', '"', StringGrammar2, '"'), - NocasePatternRule(r'sql_bareword', r'[a-z_][a-z0-9_]*'), - PatternRule(r'empty', r'^ *\n$'), - PatternRule(r'eol', r'\n'), - ] + rules = base_rules + function_rules + sql_rules + end_rules class SqlCommentRegion(CommentRegion): commentc = '--' @@ -146,7 +120,7 @@ class Sql(mode.Fundamental): closetags = {')': '(', ']': '[', '}': '{'} actions = [SqlCommentRegion, SqlUncommentRegion] - colors = { + colors = { 'sql_operator': ('yellow', 'default', 'bold'), 'attribute': ('magenta', 'default', 'bold'), 'sql_keyword': ('cyan', 'default', 'bold'),