pmacs3/mode/roy.py

160 lines
5.1 KiB
Python

import os.path
import time
import tab
from mode.javascript import RegexGrammar, StringGrammar1, StringGrammar2
from mode import Fundamental
from method.shell import Interact
from lex import Grammar, PatternRule, RegionRule, PatternMatchRule
chr1 = '[a-zA-Z_]'
chr2 = '[a-zA-Z_0-9]'
word = chr1 + chr2 + '*'
typ = '[A-Z]' + chr2 + '*'
class TypeDefGrammar(Grammar): pass
class StructDefGrammar(Grammar): pass
class RoyGrammar(Grammar): pass
RoyGrammar.rules = [
PatternRule('comment', r'//.*$'),
PatternRule('spaces', r'[ \t]+'),
PatternRule('eol', r'\n'),
RegionRule('string', "'", StringGrammar1, "'"),
RegionRule('string', '"', StringGrammar2, '"'),
PatternRule('roy.number', r'-?[1-9][0-9]*(?:\.[0-9]+)?(?:e-?[0-9]+)?'),
PatternMatchRule('', '(' + word + ')(:)',
'roy.member', 'delimiter'),
PatternMatchRule('', '(' + word + ')( +)(:)',
'roy.member', 'spaces', 'delimiter'),
PatternMatchRule('', r'(\\)(' + word + ')', 'delimiter', 'roy.member'),
PatternMatchRule('', r'(' + word + ')( +)(<-)',
'roy.member', 'spaces', 'delimiter'),
PatternRule('roy.type', typ),
#PatternMatchRule('', '(type)( +)(' + typ + ')',
# 'roy.keyword', 'spaces', 'roy.type'),
PatternMatchRule('', '(let)( +)(' + word + ')',
'roy.keyword', 'spaces', 'roy.variable'),
PatternRule('delimiter', r'(?:->|<-|:|=|,|\(|\)|{|}|\[|\]|\+|\|)'),
PatternRule('roy.keyword', '(?:do|let|type)(?!' + chr2 + ')'),
PatternRule('roy.bareword', word),
]
#class JavascriptTabber2(tab.StackTabber2):
# fixed_indent = True
# open_tokens = {'delimiter': {'{': '}', '(': ')', '[': ']'}}
# close_tokens = {'delimiter': {'}': '{', ')': '(', ']': '['}}
# control_tokens = {'roy.keyword': {'if': 1, 'else': 1, 'while': 1,
# 'do': 1, 'for': 1}}
# end_at_eof = False
# end_at_tokens = {'delimiter': {';': 1}}
# nocontinue_tokens = {'delimiter': {';': 1, ',': 1},
# 'comment': 1,
# 'comment.start': 1,
# 'comment.data': 1,
# 'comment.end': 1}
# start_free_tokens = {'string.start': 'string.end'}
# end_free_tokens = {'string.end': 'string.start'}
# 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 == 'js.reserved' and t.string == 'function'
# def _is_indent(self, t):
# return t.name == 'spaces'
# def _is_ignored(self, t):
# return t.fqname() in ('spaces', 'eol', 'comment', 'comment.start',
# 'comment.data', 'comment.null', 'comment.end')
#class RoyStart(Interact):
# args = []
# reuse = True
# def _execute(self, w, **vargs):
# cmd = w.application.config.get('roy.cmd', './roy')
# Interact._execute(self, w, bname='*Roy*', cmd=cmd)
#
#class RhinoLoadFile(RhinoStart):
# args = []
# reuse = True
# def _execute(self, w, **vargs):
# RhinoStart._execute(self, w, **vargs)
# b = w.application.get_buffer_by_name('*Rhino*')
# path = os.path.realpath(w.buffer.path)
# time.sleep(0.5)
# b.pipe_write('load("env.js");\n')
# time.sleep(0.5)
# b.pipe_write('load("%s");\n' % path)
#class JavascriptTagManager(TagManager):
# lang = 'Javascript'
# exts = set(('.js', '.json'))
# white is for delimiters, operators, numbers
default = ('default', 'default')
# magenta is for reserved words
lo_magenta = ('magenta202', 'default')
hi_magenta = ('magenta505', 'default')
# red is for comments
lo_red = ('red300', 'default')
hi_red = ('red511', 'default')
# orange is unused
hi_orange = ('yellow531', 'default')
lo_orange = ('yellow520', 'default')
# yellow is for class names
hi_yellow = ('yellow551', 'default')
lo_yellow = ('yellow330', 'default')
# green is for strings
lo_green = ('green030', 'default')
hi_green = ('green050', 'default')
# cyan is for keywords and some operators
lo_cyan = ('cyan033', 'default')
hi_cyan = ('cyan155', 'default')
# blue is for functions and methods
lo_blue = ('blue113', 'default')
hi_blue = ('blue225', 'default')
class Roy(Fundamental):
name = 'Roy'
extensions = ['.roy']
tabwidth = 2
grammar = RoyGrammar
#tabbercls = JavascriptTabber2
#tagcls = JavascriptTagManager
commentc = '//'
opentokens = ('delimiter',)
opentags = {'(': ')', '[': ']', '{': '}'}
closetokens = ('delimiter',)
closetags = {')': '(', ']': '[', '}': '{'}
colors = {
'roy.member': hi_blue,
'roy.number': hi_orange,
'roy.type': hi_magenta,
'roy.keyword': hi_cyan,
}
#config = {'rhino.cmd': 'rhino'}
#actions = [RhinoStart, RhinoLoadFile]
_bindings = {
'close-paren': (')',),
'close-brace': ('}',),
'close-bracket': (']',),
}
install = Roy.install