pmacs3/mode/ocaml.py

80 lines
3.7 KiB
Python

import subprocess, os.path, string, sys, traceback
import color, completer, default, mode, method, regex, tab
from point import Point
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}|\\.)*")?'),
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'[!?~][-!$%&*+\./:<=>?@^|~]*'),
]
class Ocaml(mode.Fundamental):
name = 'ocaml'
extensions = ['.mli', '.ml']
grammar = OcamlGrammar
opentokens = ('delimiter',)
opentags = {'(': ')'}
closetokens = ('delimiter',)
closetags = {')': '('}
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'),
}
_bindings = {
'close-paren': (')',),
'close-brace': ('}',),
'close-bracket': (']',),
}
install = Ocaml.install