pmacs3/mode/ocaml.py

80 lines
3.7 KiB
Python
Raw Permalink Normal View History

import subprocess, os.path, string, sys, traceback
2007-10-21 20:55:29 -04:00
import color, completer, default, mode, method, regex, tab
from point import Point
2007-10-21 20:52:48 -04:00
from lex import Grammar, PatternRule, RegionRule, NocasePatternRule
class StringGrammar(Grammar):
rules = [
PatternRule(r'continuation', r'\\(?=\n$)'),
PatternRule(r'escaped', r"\\[\\ \"'ntbr]"),
PatternRule(r'octal', r'\\[0-9]{3}'),
PatternRule(r'hex', r'\\x[0-9A-Fa-f]{2}'),
]
#class CommentGrammar(Grammar):
# rules = []
#CommentGrammar.rules.append(RegionRule(r'comment', r'\(\*', CommentGrammar, r'\*\)'))
class CommentGrammar(Grammar):
rules = [RegionRule(r'comment', r'\(\*', None, r'\*\)')]
class OcamlGrammar(Grammar):
rules = [
PatternRule(r'spaces', r' +'),
PatternRule(r'eol', r'\n'),
RegionRule(r'comment', r'\(\*', CommentGrammar, r'\*\)'),
PatternRule(r'linenum', r'#[0-9]+ *(?:"(?:[^"\\]|\\[0-9]{3}|\\x[0-9A-Za-z]{2}|\\.)*")?'),
2007-08-16 11:20:46 -04:00
PatternRule(r'delimiter', r"[()]"),
PatternRule(r'ocaml_keyword', r"(?:with|while|when|virtual|val|type|try|true|to|then|struct|sig|rec|private|parser|or|open|of|object|new|mutable|module|mod|method|match|lxor|lsr|lsl|lor|let|lazy|land|initializer|inherti|include|in|if|functor|function|fun|for|false|external|exception|end|else|downto|done|do|constraint|class|begin|asr|assert|as|and)(?!['a-zA-Z0-9_])"),
PatternRule(r'builtin', r"(?:int|char|string|float|bool|false|true|unit|exn|array|list|option|int32|int64|nativeint|format4|lazy_t)(?!['a-zA-Z0-9_])"),
PatternRule(r'builtin_exception', r"(?:Match_failure|Assert_failure|Invalid_argument|Failure|Not_found|Out_of_memory|Stack_overflow|Sys_error|End_of_file|Division_by_zero|Sys_blocked_io|Undefined_recursive_module|Exit)(?!['a-zA-Z0-9_])"),
PatternRule(r'builtin_function', r"raise|invalid_arg|failwith"),
NocasePatternRule(r'identifier', r"[a-z_]['a-z0-9_]*"),
NocasePatternRule(r'integer', r"-?[0-9][0-9_]*"),
NocasePatternRule(r'integer', r"-?0x[0-9a-f][0-9a-f_]*"),
NocasePatternRule(r'integer', r"-?0o[0-7][0-7_]*"),
NocasePatternRule(r'integer', r"-?0b[01][01_]*"),
NocasePatternRule(r'float', r"-?[0-9][0-9_]*(?:\.[0-9_]*)?(?:e[+-]?[0-9][0-9_]*)?"),
PatternRule(r'char', r"'[^\\']'"),
PatternRule(r'char', r"'\\[\\ \"'ntbr]'"),
PatternRule(r'char', r"'\\[0-9]{3}'"),
PatternRule(r'char', r"'\\x[0-9A-Fa-f]{2}'"),
RegionRule(r'ocaml_string', r'"', StringGrammar, '"'),
PatternRule(r'label', r"~[a-z_]['a-zA-Z0-9_]*:"),
PatternRule(r'optlabel', r"\?[a-z_]['a-zA-Z0-9_]*:"),
PatternRule(r'infix', r'[-=<>@^|&+*/$%][-!$%&*+\./:<=>?@^|~]*'),
PatternRule(r'prefix', r'[!?~][-!$%&*+\./:<=>?@^|~]*'),
]
2007-10-21 20:55:29 -04:00
class Ocaml(mode.Fundamental):
name = 'ocaml'
2007-10-18 17:07:35 -04:00
extensions = ['.mli', '.ml']
grammar = OcamlGrammar
2007-08-16 11:20:46 -04:00
opentokens = ('delimiter',)
opentags = {'(': ')'}
closetokens = ('delimiter',)
closetags = {')': '('}
2007-10-18 17:07:35 -04:00
colors = {
'linenum': ('red', 'default', 'bold'),
'ocaml_keyword': ('cyan', 'default', 'bold'),
'ocaml_label': ('blue', 'default', 'bold'),
'optlabel': ('blue', 'default', 'bold'),
'ocaml_string.start': ('green', 'default', 'bold'),
'ocaml_string.null': ('green', 'default', 'bold'),
'ocaml_string.octal': ('magenta', 'default', 'bold'),
'ocaml_string.hex': ('magenta', 'default', 'bold'),
'ocaml_string.escaped': ('magenta', 'default', 'bold'),
'ocaml_string.end': ('green', 'default', 'bold'),
}
2009-02-15 12:06:35 -05:00
_bindings = {
'close-paren': (')',),
'close-brace': ('}',),
'close-bracket': (']',),
}
2007-10-19 02:41:33 -04:00
install = Ocaml.install