pmacs3/mode/erlang.py

121 lines
6.0 KiB
Python
Raw Normal View History

2008-11-08 10:30:04 -05:00
import commands, os.path, string, sys, traceback
2009-03-08 00:19:58 -05:00
import color, completer, default, mode, regex, tab
from point import Point
from lex import Grammar, PatternRule, RegionRule, OverridePatternRule, NocasePatternRule
2009-03-08 00:19:58 -05:00
from method.shell import Interact
2008-10-15 00:49:39 -04:00
class AtomGrammar(Grammar):
rules = [
NocasePatternRule(r'escaped', r'\\(?:[bdefnrstv\\"\']|\^[a-z]|\d{1,3})'),
NocasePatternRule(r'data', r"[^\\']+"),
]
class StringGrammar(Grammar):
rules = [
NocasePatternRule(r'escaped', r'\\(?:[bdefnrstv\\"\']|\^[a-z]|\d{1,3})'),
2008-10-15 00:49:39 -04:00
NocasePatternRule(r'data', r'[^\\"]+'),
]
class ErlangGrammar(Grammar):
rules = [
2008-05-29 19:05:15 -04:00
PatternRule(r'eol', r'\n'),
PatternRule(r'comment', r'%.*$'),
PatternRule(r'erl_attribute', r'-[a-z][a-zA-Z0-9_@]+'),
PatternRule(r'erl_reserved', r"(?:xor|when|try|rem|receive|query|orelse|or|of|not|let|if|fun|end|div|cond|catch|case|bxor|bsr|bsl|bor|bnot|begin|band|andalso|and|after)(?![a-zA-Z0-9_'])"),
2008-07-14 19:24:26 -04:00
PatternRule(r'erl_arity', r'[a-z][a-zA-Z0-9_@]*/(?:0|[1-9][0-9]*)'),
2008-05-11 23:37:57 -04:00
PatternRule(r'erl_function', r'[a-z][a-zA-Z0-9_@]*(?= *\()'),
PatternRule(r'erl_namespace', r'[a-z][a-zA-Z0-9_@]*(?=:)'),
PatternRule(r'erl_builtin', r"whereis|unregister|unlink|tuple_to_list|tuple_size|trunc|tl|time|throw|term_to_binary|term_to_binary|statistics|split_binary|spawn_opt|spawn_opt|spawn_opt|spawn_opt|spawn_monitor|spawn_monitor|spawn_link|spawn_link|spawn_link|spawn_link|spawn|spawn|spawn|spawn|size|setelement|self|round|registered|register|put|purge_module|process_info|process_info|process_flag|process_flag|processes|pre_loaded|port_control|port_connect|port_command|port_close|pid_to_list|open_port|now|nodes|nodes|node|node|monitor_node|module_loaded|make_ref|load_module|list_to_tuple|list_to_pid|list_to_integer|list_to_float|list_to_existing_atom|list_to_bitstring|list_to_binary|list_to_atom|link|length|is_tuple|is_reference|is_record|is_record|is_process_alive|is_port|is_pid|is_number|is_list|is_integer|is_function|is_function|is_float|is_boolean|is_bitstring|is_binary|is_atom|is_alive|iolist_to_binary|iolist_size|iolist|iodata|integer_to_list|hd|halt|halt|group_leader|group_leader|get_keys|get|get|garbage_collect|garbage_collect|float_to_list|float|ext_binary|exit|exit|erase|erase|ence|element|disconnect_node|delete_module|date|concat_binary|check_process_code|byte_size|bitstring_to_list|bit_size|binary_to_term|binary_to_list|binary_to_list|atom_to_list|atom_to_list|apply|apply|apply|abs"),
2008-10-15 00:49:39 -04:00
RegionRule(r'erl_atom', r"'", AtomGrammar, r"'"),
RegionRule(r'string', r'"', StringGrammar, r'"'),
PatternRule(r'erl_atom', r"[a-z][a-zA-Z0-9_@]*"),
PatternRule(r'erl_variable', r"[A-Z_][a-zA-Z0-9_@]*"),
PatternRule(r'delimiter', r'->|<<|>>|\(|\)|{|}|\[|\]|\.|;|,|\|'),
2008-05-29 19:05:15 -04:00
PatternRule(r'operator', r'\+\+|--|==|/=|=<|<|>=|>|=:=|=/=>|-|\+|\*|/|:|#|!]'),
NocasePatternRule(r'number', r'\$.|[+-]?\d+#[a-z0-9]+|[+-]?\d+\.\d+|[+-]?\.\d+|[+-]?\d+'),
]
2008-05-29 19:05:15 -04:00
class ErlangTabber(tab.StackTabber):
2008-07-14 19:24:26 -04:00
enterlvl = ('after', 'begin', 'case', 'catch', 'fun', 'if', 'query', 'receive')
def _handle_close_token(self, currlvl, y, i):
self._opt_pop(*self.enterlvl)
return tab.StackTabber._handle_close_token(self, currlvl, y, i)
2008-05-29 19:05:15 -04:00
def _handle_other_token(self, currlvl, y, i):
token = self.get_token(y, i)
rtoken = self.get_next_right_token(y, i)
ltoken = self.get_next_left_token(y, i)
2008-07-14 19:24:26 -04:00
if token.name == 'eol':
pass
elif token.name == 'string.start':
self._append('string', None)
elif token.name == 'string.end':
self._opt_pop('string')
2008-05-29 19:05:15 -04:00
elif token.name == 'delimiter':
if token.string == '->':
if rtoken is None or rtoken.name == 'comment':
level = self.get_curr_level() + self.mode.tabwidth
else:
level = rtoken.x
self._append(token.string, level)
elif token.string == '.':
self.markers = []
elif token.string == ';':
self._pop()
elif token.name == 'erl_reserved':
if token.string in self.enterlvl:
if rtoken is None or rtoken.name == 'comment':
level = self.get_curr_level() + self.mode.tabwidth
else:
level = rtoken.x
self._append(token.string, level)
2008-07-14 19:24:26 -04:00
elif token.string == 'of':
self._opt_pop('case')
2008-05-29 19:05:15 -04:00
elif token.string == 'end':
self._opt_pop('->')
self._pop_unless('->')
2008-07-14 19:24:26 -04:00
currlvl = self.get_curr_level()
2008-05-29 19:05:15 -04:00
return currlvl
2009-03-08 00:19:58 -05:00
## this shit doesn't work... hmmm
#class ErlStart(Interact):
# args = []
# def _execute(self, w, **vargs):
# Interact._execute(self, w, bname='*Erl*', cmd='erl')
class Erlang(mode.Fundamental):
name = 'Erlang'
extensions = ['.erl']
tabwidth = 4
2008-05-29 19:05:15 -04:00
tabbercls = ErlangTabber
grammar = ErlangGrammar
2009-02-15 12:06:35 -05:00
commentc = '%'
opentokens = ('delimiter',)
opentags = {'(': ')', '[': ']', '{': '}'}
closetokens = ('delimiter',)
closetags = {')': '(', ']': '[', '}': '{'}
colors = {
2008-05-11 23:37:57 -04:00
'erl_attribute': ('cyan', 'default', 'bold'),
'erl_reserved': ('cyan', 'default', 'bold'),
'erl_arity': ('magenta', 'default', 'bold'),
'erl_namespace': ('magenta', 'default', 'bold'),
'erl_function': ('blue', 'default', 'bold'),
2008-05-11 23:37:57 -04:00
'erl_builtin': ('magenta', 'default', 'bold'),
'erl_variable': ('yellow', 'default', 'bold'),
'erl_operator': ('default', 'default', 'bold'),
'erl_atom': ('magenta', 'default', 'bold'),
'erl_atom.start': ('magenta', 'default', 'bold'),
'erl_atom.null': ('magenta', 'default', 'bold'),
'erl_atom.end': ('magenta', 'default', 'bold'),
}
2009-03-08 00:19:58 -05:00
actions = [
#ErlStart,
]
2009-02-15 12:06:35 -05:00
_bindings = {
'close-paren': (')',),
'close-brace': ('}',),
'close-bracket': (']',),
}
install = Erlang.install