pmacs3/mode_sql.py

61 lines
3.4 KiB
Python

import color, mode2
from lex2 import Grammar, PatternRule, NocasePatternRule, RegionRule
class StringGrammar(Grammar):
rules = [
PatternRule(
name=r'octal',
pattern=r'\\[0-7]{3}',
),
PatternRule(
name=r'escaped',
pattern=r'\\.',
),
]
class SqlGrammar(Grammar):
rules = [
PatternRule(name=r'comment', pattern=r'--.*$'),
RegionRule(name=r'comment', start='/\*', grammar=Grammar(), end='\*/'),
PatternRule(name=r'delimiter', pattern=r'[();,\.:\$\[\]]'),
NocasePatternRule(name=r'attribute', pattern=r'(?:check|exists|unique|not null|default|primary key|minvalue|foreign key|references)(?![A-Za-z0-9_])'),
NocasePatternRule(name=r'operator', pattern=r'(?:case|when|then|else|end|not|and|or|is not|is|in|not in)(?![A-Za-z0-9_])'),
NocasePatternRule(name=r'keyword', pattern=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(name=r'pseudokeyword', pattern=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(name=r'type', pattern=r'(?:void|row|serial|varchar|float|integer|int|text|timestamptz|timestamp|datetz|date|timetz|time|boolean|bool)(?![A-Za-z0-9_])'),
PatternRule(name=r'function', pattern=r'(?:nextval|current_timestamp|current_time|current_date)(?![A-Za-z0-9_])'),
RegionRule(name=r'string', start="'", grammar=StringGrammar(), end="'"),
RegionRule(name=r'quoted', start='"', grammar=StringGrammar(), end='"'),
PatternRule(name=r'bareword', pattern=r'[A-Za-z0-9_]+'),
]
class Sql(mode2.Fundamental):
grammar = SqlGrammar()
opentoken = 'delimiter'
opentags = {'(': ')', '[': ']', '{': '}'}
closetoken = 'delimiter'
closetags = {')': '(', ']': '[', '}': '{'}
def __init__(self, w):
mode2.Fundamental.__init__(self, w)
self.add_bindings('close-paren', (')',))
self.add_bindings('close-brace', ('}',))
self.add_bindings('close-bracket', (']',))
self.colors = {
'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'),
'function': color.build('yellow', 'default'),
'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'),
}
def name(self):
return "Sql"