pmacs3/mode_sql.py

61 lines
3.4 KiB
Python
Raw Normal View History

2007-06-25 11:49:04 -04:00
import color, mode2
from lex2 import Grammar, PatternRule, NocasePatternRule, RegionRule
2007-03-06 10:05:38 -05:00
2007-06-25 11:49:04 -04:00
class StringGrammar(Grammar):
rules = [
PatternRule(
name=r'octal',
pattern=r'\\[0-7]{3}',
),
PatternRule(
name=r'escaped',
pattern=r'\\.',
),
]
2007-03-06 10:05:38 -05:00
2007-06-25 11:49:04 -04:00
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_]+'),
]
2007-03-06 10:05:38 -05:00
2007-06-25 11:49:04 -04:00
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', (']',))
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'),
'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'),
2007-03-06 10:05:38 -05:00
}
def name(self):
return "Sql"