pmacs3/mode_sql.py

136 lines
7.6 KiB
Python
Raw Normal View History

2007-07-12 19:34:03 -04:00
import color, mode2, tab2
from lex3 import Grammar, PatternRule, NocasePatternRule, RegionRule, NocaseRegionRule, DualRegionRule, NocaseDualRegionRule
from mode_python import StringGrammar
2007-03-06 10:05:38 -05:00
2007-07-13 08:17:01 -04:00
class PlPgSqlGrammar(Grammar):
rules = [
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'keyword', r'(?:declare|begin|end|raise notice|return)'),
NocasePatternRule(r'operator', r'(?:case|when|then|else|end|not|and|or|is not|is|in|not in)(?![A-Za-z0-9_])'),
NocasePatternRule(r'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|null|true|false|into|values|as|from|order by|asc|desc|limit|distinct|cascade|using|on)(?![A-Za-z0-9_])'),
NocasePatternRule(r'type', r'(?:void|row|serial|varchar|float|integer|int|text|timestamptz|timestamp|datetz|date|timetz|time|boolean|bool)(?![A-Za-z0-9_])'),
PatternRule(r'builtin', r'(?:nextval|current_timestamp|current_time|current_date)(?![A-Za-z0-9_])'),
RegionRule(r'string', "''", StringGrammar, "''"),
RegionRule(r'quoted', '"', StringGrammar, '"'),
PatternRule(r'bareword', r'[A-Za-z0-9_]+'),
PatternRule(r'empty', r'^ *\n$'),
PatternRule(r'eol', r'\n'),
]
class FunctionGrammar(Grammar):
rules = [
PatternRule(r'comment', r'--.*\n$'),
RegionRule(r'comment', '/\*', Grammar, '\*/'),
PatternRule(r'delimiter', r':=|[():;,\.\$\[\]]'),
PatternRule(r'name', r'[a-zA-Z_][a-zA-Z0-9_]*(?=\()'),
NocasePatternRule(r'keyword', r'(?:as|returns|language)'),
NocasePatternRule(r'type', r'(?:void|row|serial|varchar|float|integer|int|text|timestamptz|timestamp|datetz|date|timetz|time|boolean|bool)(?![A-Za-z0-9_])'),
NocasePatternRule(r'language', r'(?<=language ) *[a-zA-Z_][a-zA-Z0-9_]+'),
RegionRule(r'definition', "'", PlPgSqlGrammar, "'(?!')"),
PatternRule(r'bareword', r'[A-Za-z0-9_]+'),
PatternRule(r'empty', r'^ *\n$'),
PatternRule(r'eol', r'\n'),
]
2007-06-25 11:49:04 -04:00
class SqlGrammar(Grammar):
rules = [
2007-07-12 19:34:03 -04:00
PatternRule(r'comment', r'--.*\n$'),
RegionRule(r'comment', '/\*', Grammar, '\*/'),
2007-07-13 08:17:01 -04:00
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'operator', r'(?:case|when|then|else|end|not|and|or|is not|is|in|not in)(?![A-Za-z0-9_])'),
NocasePatternRule(r'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|null|true|false|into|values|as|from|order by|asc|desc|limit|distinct|cascade|using|on)(?![A-Za-z0-9_])'),
NocasePatternRule(r'type', r'(?:void|row|serial|varchar|float|integer|int|text|timestamptz|timestamp|datetz|date|timetz|time|boolean|bool)(?![A-Za-z0-9_])'),
2007-07-13 08:17:01 -04:00
PatternRule(r'builtin', r'(?:nextval|current_timestamp|current_time|current_date)(?![A-Za-z0-9_])'),
RegionRule(r'string', "'", StringGrammar, "'"),
RegionRule(r'quoted', '"', StringGrammar, '"'),
PatternRule(r'bareword', r'[A-Za-z0-9_]+'),
2007-07-12 19:34:03 -04:00
PatternRule(r'empty', r'^ *\n$'),
PatternRule(r'eol', r'\n'),
2007-06-25 11:49:04 -04:00
]
2007-03-06 10:05:38 -05:00
2007-07-12 19:34:03 -04:00
class SqlTabber(tab2.StackTabber):
wst = ('null', 'eol',)
2007-07-12 19:34:03 -04:00
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):
token = self.get_token(y, i)
if token.name == 'delimiter' and token.string == ';':
self._opt_pop('cont')
2007-07-13 08:17:01 -04:00
elif token.name == 'keyword':
if token.string == 'declare':
self._opt_append('declare', currlvl + 4)
elif token.string == 'begin':
currlvl -= 4
elif token.string == 'end':
self._opt_pop('declare')
currlvl = self.get_curr_level()
2007-07-12 19:34:03 -04:00
if self.is_rightmost_token(y, i):
if not self._empty() and token.name == 'continuation':
self._opt_append('cont', currlvl + 4)
elif token.name == 'eol' and not self.markers:
2007-07-12 19:34:03 -04:00
self._opt_pop("cont")
return currlvl
2007-06-25 11:49:04 -04:00
class Sql(mode2.Fundamental):
grammar = SqlGrammar
2007-07-12 19:34:03 -04:00
tabbercls = SqlTabber
opentokens = ('delimiter',)
opentags = {'(': ')', '[': ']', '{': '}'}
closetokens = ('delimiter',)
closetags = {')': '(', ']': '[', '}': '{'}
2007-06-25 11:49:04 -04:00
def __init__(self, w):
mode2.Fundamental.__init__(self, w)
self.add_bindings('close-paren', (')',))
self.add_bindings('close-brace', ('}',))
self.add_bindings('close-bracket', (']',))
2007-03-06 10:05:38 -05:00
self.colors = {
2007-06-25 11:49:04 -04:00
'comment': color.build('red', 'default'),
'operator': color.build('yellow', 'default'),
'attribute': color.build('magenta', 'default'),
'keyword': color.build('cyan', 'default'),
'pseudokeyword': color.build('cyan', 'default'),
'type': color.build('green', 'default'),
2007-07-13 08:17:01 -04:00
'builtin': color.build('yellow', 'default'),
2007-06-25 11:49:04 -04:00
'quoted': color.build('yellow', 'default'),
'string.start': color.build('green', 'default'),
'string.null': color.build('green', 'default'),
'string.escaped': color.build('magenta', 'default'),
'string.octal': color.build('magenta', 'default'),
'string.end': color.build('green', 'default'),
'bareword': color.build('default', 'default'),
2007-07-13 08:17:01 -04:00
'function.start': color.build('cyan', 'default'),
'function.null': color.build('default', 'default'),
'function.name': color.build('magenta', 'default'),
'function.language': color.build('magenta', 'default'),
'function.end': color.build('default', 'default'),
'function.definition.start': color.build('magenta', 'default'),
'function.definition.bareword': color.build('magenta', 'default'),
'function.definition.null': color.build('magenta', 'default'),
'function.definition.end': color.build('magenta', 'default'),
2007-03-06 10:05:38 -05:00
}
def name(self):
return "Sql"