2007-10-21 20:55:29 -04:00
|
|
|
import mode, tab
|
2007-10-21 20:52:48 -04:00
|
|
|
from lex import Grammar, PatternRule, NocasePatternRule, RegionRule, NocaseRegionRule
|
2008-10-01 16:58:43 -04:00
|
|
|
from mode.python import StringGrammar1, StringGrammar2
|
2009-02-13 22:14:38 -05:00
|
|
|
from method import CommentRegion, UncommentRegion
|
2007-07-21 11:40:53 -04:00
|
|
|
|
|
|
|
class PlPgSqlGrammar(Grammar):
|
|
|
|
rules = [
|
2008-10-01 16:58:43 -04:00
|
|
|
PatternRule(r'spaces', r' +'),
|
|
|
|
|
2007-07-21 11:40:53 -04:00
|
|
|
PatternRule(r'comment', r'--.*\n$'),
|
|
|
|
RegionRule(r'comment', '/\*', Grammar, '\*/'),
|
2008-10-01 16:58:43 -04:00
|
|
|
PatternRule(r'delimiter', r':=|[():;,\.\[\]~]'),
|
2007-07-21 11:40:53 -04:00
|
|
|
|
|
|
|
NocasePatternRule(r'attribute', r'(?:check|exists|unique|not null|default|primary key|minvalue|foreign key|references)(?![A-Za-z0-9_])'),
|
2008-03-16 01:23:14 -04:00
|
|
|
NocasePatternRule(r'sql_keyword', r'(?:declare|begin|end if|end loop|end|raise notice|return|if|while|for|else)(?![A-Za-z0-9_])'),
|
2009-02-13 22:36:38 -05:00
|
|
|
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_])'),
|
2008-03-16 01:23:14 -04:00
|
|
|
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_])'),
|
2009-02-13 22:36:38 -05:00
|
|
|
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_])'),
|
2008-03-16 01:23:14 -04:00
|
|
|
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_])'),
|
2008-10-01 16:58:43 -04:00
|
|
|
RegionRule(r'string', "''", StringGrammar1, "''"),
|
|
|
|
RegionRule(r'sql_quoted', '"', StringGrammar2, '"'),
|
2008-03-16 01:23:14 -04:00
|
|
|
PatternRule(r'sql_variable', r'\$[1-9][0-9]*'),
|
|
|
|
PatternRule(r'sql_bareword', r'[A-Za-z0-9_]+'),
|
2007-07-21 11:40:53 -04:00
|
|
|
PatternRule(r'empty', r'^ *\n$'),
|
|
|
|
PatternRule(r'eol', r'\n'),
|
|
|
|
]
|
|
|
|
|
|
|
|
class FunctionGrammar(Grammar):
|
|
|
|
rules = [
|
2008-10-01 16:58:43 -04:00
|
|
|
PatternRule(r'spaces', r' +'),
|
|
|
|
|
2007-07-21 11:40:53 -04:00
|
|
|
PatternRule(r'comment', r'--.*\n$'),
|
|
|
|
RegionRule(r'comment', '/\*', Grammar, '\*/'),
|
2008-10-01 16:58:43 -04:00
|
|
|
PatternRule(r'delimiter', r':=|[():;,\.\$\[\]~]'),
|
2007-07-21 11:40:53 -04:00
|
|
|
|
|
|
|
PatternRule(r'name', r'[a-zA-Z_][a-zA-Z0-9_]*(?=\()'),
|
2008-03-16 01:23:14 -04:00
|
|
|
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_])'),
|
2007-07-21 11:40:53 -04:00
|
|
|
|
|
|
|
NocasePatternRule(r'language', r'(?<=language ) *[a-zA-Z_][a-zA-Z0-9_]+'),
|
|
|
|
|
|
|
|
RegionRule(r'definition', "'", PlPgSqlGrammar, "'(?!')"),
|
2008-03-16 01:23:14 -04:00
|
|
|
PatternRule(r'sql_bareword', r'[A-Za-z0-9_]+'),
|
2007-07-21 11:40:53 -04:00
|
|
|
PatternRule(r'empty', r'^ *\n$'),
|
|
|
|
PatternRule(r'eol', r'\n'),
|
|
|
|
]
|
|
|
|
|
2009-02-14 09:28:45 -05:00
|
|
|
class BitStringGrammar(Grammar):
|
|
|
|
rules = [PatternRule(r'data', r'[01]+')]
|
|
|
|
|
|
|
|
class HexStringGrammar(Grammar):
|
|
|
|
rules = [NocasePatternRule(r'data', r'[0-9a-f]+')]
|
|
|
|
|
2007-07-21 11:40:53 -04:00
|
|
|
class SqlGrammar(Grammar):
|
|
|
|
rules = [
|
2008-10-01 16:58:43 -04:00
|
|
|
PatternRule(r'spaces', r' +'),
|
|
|
|
|
2007-07-21 11:40:53 -04:00
|
|
|
PatternRule(r'comment', r'--.*\n$'),
|
|
|
|
RegionRule(r'comment', '/\*', Grammar, '\*/'),
|
2008-10-01 16:58:43 -04:00
|
|
|
PatternRule(r'delimiter', r':=|[():;,\.\$\[\]~]'),
|
2007-07-21 11:40:53 -04:00
|
|
|
|
|
|
|
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_])'),
|
2009-02-13 22:36:38 -05:00
|
|
|
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_])'),
|
2008-03-16 01:23:14 -04:00
|
|
|
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_])'),
|
2009-02-13 22:36:38 -05:00
|
|
|
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_])'),
|
2008-03-16 01:23:14 -04:00
|
|
|
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_])'),
|
2009-02-14 09:28:45 -05:00
|
|
|
RegionRule(r'string', "B'", BitStringGrammar, "'"),
|
|
|
|
RegionRule(r'string', "X'", HexStringGrammar, "'"),
|
2008-10-01 16:58:43 -04:00
|
|
|
RegionRule(r'string', "'", StringGrammar1, "'"),
|
|
|
|
RegionRule(r'sql_quoted', '"', StringGrammar2, '"'),
|
2008-03-16 01:23:14 -04:00
|
|
|
PatternRule(r'sql_bareword', r'[A-Za-z0-9_]+'),
|
2007-07-21 11:40:53 -04:00
|
|
|
PatternRule(r'empty', r'^ *\n$'),
|
|
|
|
PatternRule(r'eol', r'\n'),
|
|
|
|
]
|
|
|
|
|
2009-02-13 22:14:38 -05:00
|
|
|
class SqlCommentRegion(CommentRegion):
|
|
|
|
commentc = '--'
|
|
|
|
class SqlUncommentRegion(UncommentRegion):
|
|
|
|
commentc = '--'
|
|
|
|
|
2007-10-21 20:55:29 -04:00
|
|
|
class SqlTabber(tab.StackTabber):
|
2007-07-21 11:40:53 -04:00
|
|
|
wst = ('null', 'eol',)
|
|
|
|
def is_base(self, y):
|
|
|
|
if y == 0:
|
|
|
|
return True
|
|
|
|
highlighter = self.mode.window.buffer.highlights[self.mode.name()]
|
|
|
|
if not highlighter.tokens[y]:
|
|
|
|
return False
|
|
|
|
t = highlighter.tokens[y][0]
|
|
|
|
return t.name == 'function'
|
|
|
|
def _handle_other_token(self, currlvl, y, i):
|
2008-04-02 19:06:52 -04:00
|
|
|
w = self.mode.tabwidth
|
|
|
|
token = self.get_token(y, i)
|
|
|
|
s = token.string.lower()
|
2008-03-01 13:14:37 -05:00
|
|
|
if token.name == 'delimiter' and s == ';':
|
2007-07-21 11:40:53 -04:00
|
|
|
self._opt_pop('cont')
|
2008-03-16 01:23:14 -04:00
|
|
|
elif token.name == 'sql_keyword':
|
2008-03-01 13:14:37 -05:00
|
|
|
if s == 'declare':
|
2008-04-02 19:06:52 -04:00
|
|
|
self._append('declare', currlvl + w)
|
2008-03-01 13:14:37 -05:00
|
|
|
elif s == 'begin':
|
2008-04-02 19:06:52 -04:00
|
|
|
currlvl -= w
|
2008-03-01 13:14:37 -05:00
|
|
|
elif s == 'end':
|
2007-07-21 11:40:53 -04:00
|
|
|
self._opt_pop('declare')
|
|
|
|
currlvl = self.get_curr_level()
|
2008-03-01 13:14:37 -05:00
|
|
|
elif s == 'end if':
|
|
|
|
self._opt_pop('if')
|
|
|
|
currlvl = self.get_curr_level()
|
|
|
|
elif s == 'end loop':
|
|
|
|
self._opt_pop('loop')
|
|
|
|
currlvl = self.get_curr_level()
|
|
|
|
elif s == 'else':
|
2008-04-02 19:06:52 -04:00
|
|
|
currlvl -= w
|
2008-03-01 13:14:37 -05:00
|
|
|
elif s == 'if':
|
2008-04-02 19:06:52 -04:00
|
|
|
self._append('if', currlvl + w)
|
2008-03-01 13:14:37 -05:00
|
|
|
elif s in ('while', 'for'):
|
2008-04-02 19:06:52 -04:00
|
|
|
self._append('loop', currlvl + w)
|
2007-07-21 11:40:53 -04:00
|
|
|
|
|
|
|
if self.is_rightmost_token(y, i):
|
|
|
|
if not self._empty() and token.name == 'continuation':
|
2008-04-02 19:06:52 -04:00
|
|
|
self._append('cont', currlvl + w)
|
2007-07-21 11:40:53 -04:00
|
|
|
elif token.name == 'eol' and not self.markers:
|
|
|
|
self._opt_pop("cont")
|
|
|
|
return currlvl
|
|
|
|
|
2007-10-21 20:55:29 -04:00
|
|
|
class Sql(mode.Fundamental):
|
2007-10-18 17:07:35 -04:00
|
|
|
modename = 'Sql'
|
|
|
|
extensions = ['.sql']
|
2007-07-21 11:40:53 -04:00
|
|
|
grammar = SqlGrammar
|
|
|
|
tabbercls = SqlTabber
|
|
|
|
opentokens = ('delimiter',)
|
|
|
|
opentags = {'(': ')', '[': ']', '{': '}'}
|
|
|
|
closetokens = ('delimiter',)
|
|
|
|
closetags = {')': '(', ']': '[', '}': '{'}
|
2009-02-13 22:36:38 -05:00
|
|
|
actions = [SqlCommentRegion, SqlUncommentRegion]
|
|
|
|
|
2007-10-18 17:07:35 -04:00
|
|
|
colors = {
|
2008-05-03 13:31:30 -04:00
|
|
|
'sql_operator': ('yellow', 'default', 'bold'),
|
|
|
|
'attribute': ('magenta', 'default', 'bold'),
|
|
|
|
'sql_keyword': ('cyan', 'default', 'bold'),
|
|
|
|
'pseudokeyword': ('cyan', 'default', 'bold'),
|
|
|
|
'sql_type': ('green', 'default', 'bold'),
|
|
|
|
'sql_builtin': ('yellow', 'default', 'bold'),
|
|
|
|
'sql_quoted.start': ('yellow', 'default', 'bold'),
|
2008-11-08 13:32:32 -05:00
|
|
|
'sql_quoted.data': ('yellow', 'default', 'bold'),
|
2008-05-03 13:31:30 -04:00
|
|
|
'sql_quoted.null': ('yellow', 'default', 'bold'),
|
|
|
|
'sql_quoted.end': ('yellow', 'default', 'bold'),
|
|
|
|
'sql_variable': ('yellow', 'default', 'bold'),
|
|
|
|
'sql_bareword': ('default', 'default', 'bold'),
|
2007-07-21 11:40:53 -04:00
|
|
|
|
2008-05-03 13:31:30 -04:00
|
|
|
'function.start': ('cyan', 'default', 'bold'),
|
2008-11-08 13:32:32 -05:00
|
|
|
'function.data': ('default', 'default', 'bold'),
|
2008-05-03 13:31:30 -04:00
|
|
|
'function.null': ('default', 'default', 'bold'),
|
|
|
|
'function.name': ('magenta', 'default', 'bold'),
|
|
|
|
'function.language': ('magenta', 'default', 'bold'),
|
|
|
|
'function.end': ('default', 'default', 'bold'),
|
2007-07-21 11:40:53 -04:00
|
|
|
|
2008-05-03 13:31:30 -04:00
|
|
|
'function.definition.start': ('magenta', 'default', 'bold'),
|
|
|
|
'function.definition.sql_bareword': ('magenta', 'default', 'bold'),
|
2008-11-08 13:32:32 -05:00
|
|
|
'function.definition.data': ('magenta', 'default', 'bold'),
|
2008-05-03 13:31:30 -04:00
|
|
|
'function.definition.null': ('magenta', 'default', 'bold'),
|
|
|
|
'function.definition.end': ('magenta', 'default', 'bold'),
|
2007-07-21 11:40:53 -04:00
|
|
|
}
|
|
|
|
def __init__(self, w):
|
2007-10-21 20:55:29 -04:00
|
|
|
mode.Fundamental.__init__(self, w)
|
2007-07-21 11:40:53 -04:00
|
|
|
self.add_bindings('close-paren', (')',))
|
|
|
|
self.add_bindings('close-brace', ('}',))
|
|
|
|
self.add_bindings('close-bracket', (']',))
|
2009-02-13 22:36:38 -05:00
|
|
|
self.add_bindings('sql-comment-region', ('C-c #',))
|
|
|
|
self.add_bindings('sql-uncomment-region', ('C-u C-c #',))
|
2007-10-19 02:41:33 -04:00
|
|
|
|
|
|
|
install = Sql.install
|