summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorgbrandl <devnull@localhost>2007-02-03 13:43:59 +0100
committergbrandl <devnull@localhost>2007-02-03 13:43:59 +0100
commitf69cefcad75e7859cbb090883d94ae57a841c095 (patch)
tree7c7cd07d5b5563a4328c76c4bd0edf0e9e1a0be6
parent8983594621e29f99631414e430c3d4d1fa344d5a (diff)
downloadpygments-f69cefcad75e7859cbb090883d94ae57a841c095.tar.gz
[svn] Add OCaml lexer.
-rw-r--r--CHANGES2
-rw-r--r--TODO1
-rw-r--r--pygments/lexers/_mapping.py1
-rw-r--r--pygments/lexers/compiled.py83
4 files changed, 85 insertions, 2 deletions
diff --git a/CHANGES b/CHANGES
index c8175a13..49327c38 100644
--- a/CHANGES
+++ b/CHANGES
@@ -5,6 +5,8 @@ Version 0.7 (in development)
----------------------------
(codename to be selected, released Feb XX, 2007)
+- Added an OCaml lexer, thanks to Adam Blinkinsop.
+
- Made the HTML formatter more flexible, and easily subclassable in order
to make it easy to implement custom wrappers, e.g. alternate line
number markup.
diff --git a/TODO b/TODO
index b67950cf..7f97c12f 100644
--- a/TODO
+++ b/TODO
@@ -10,7 +10,6 @@ for 0.7
* IPython sessions
* HTML with special formatting?
* LaTeX special formatting?
- * OCaml
* Nemerle
* Assembler
* Objective C
diff --git a/pygments/lexers/_mapping.py b/pygments/lexers/_mapping.py
index bf2dad76..d637de02 100644
--- a/pygments/lexers/_mapping.py
+++ b/pygments/lexers/_mapping.py
@@ -66,6 +66,7 @@ LEXERS = {
'MyghtyJavascriptLexer': ('pygments.lexers.templates', 'JavaScript+Myghty', ('js+myghty', 'javascript+myghty'), (), ('application/x-javascript+myghty', 'text/x-javascript+myghty', 'text/javascript+mygthy')),
'MyghtyLexer': ('pygments.lexers.templates', 'Myghty', ('myghty',), ('*.myt', 'autodelegate'), ('application/x-myghty',)),
'MyghtyXmlLexer': ('pygments.lexers.templates', 'XML+Myghty', ('xml+myghty',), (), ('application/xml+myghty',)),
+ 'OcamlLexer': ('pygments.lexers.compiled', 'OCaml', ('ocaml',), ('*.ml', '*.mli'), ('text/x-ocaml',)),
'PerlLexer': ('pygments.lexers.agile', 'Perl', ('perl', 'pl'), ('*.pl', '*.pm'), ('text/x-perl', 'application/x-perl')),
'PhpLexer': ('pygments.lexers.web', 'PHP', ('php', 'php3', 'php4', 'php5'), ('*.php', '*.php[345]'), ('text/x-php',)),
'PythonConsoleLexer': ('pygments.lexers.agile', 'Python console session', ('pycon',), (), ('text/x-python-doctest',)),
diff --git a/pygments/lexers/compiled.py b/pygments/lexers/compiled.py
index 0a499446..868b6ee3 100644
--- a/pygments/lexers/compiled.py
+++ b/pygments/lexers/compiled.py
@@ -24,7 +24,8 @@ from pygments.token import \
Error
-__all__ = ['CLexer', 'CppLexer', 'DelphiLexer', 'JavaLexer', 'DylanLexer']
+__all__ = ['CLexer', 'CppLexer', 'DelphiLexer', 'JavaLexer', 'DylanLexer',
+ 'OcamlLexer']
class CLexer(RegexLexer):
@@ -783,3 +784,83 @@ class DylanLexer(RegexLexer):
(r'[a-zA-Z0-9-]+', Name.Variable),
],
}
+
+
+class OcamlLexer(RegexLexer):
+ """
+ For the OCaml language.
+
+ *New in Pygments 0.7.*
+ """
+
+ name = 'OCaml'
+ aliases = ['ocaml']
+ filenames = ['*.ml', '*.mli']
+ mimetypes = ['text/x-ocaml']
+
+ keywords = [
+ 'and', 'as', 'assert', 'asr', 'begin', 'class',
+ 'constraint', 'do', 'done', 'downto', 'else', 'end',
+ 'exception', 'external', 'false', 'for', 'fun', 'function',
+ 'functor', 'if', 'in', 'include', 'inherit', 'initializer',
+ 'land', 'lazy', 'let', 'lor', 'lsl', 'lsr',
+ 'lxor', 'match', 'method', 'mod', 'module', 'mutable',
+ 'new', 'object', 'of', 'open', 'or', 'private',
+ 'rec', 'sig', 'struct', 'then', 'to', 'true',
+ 'try', 'type', 'val', 'virtual', 'when', 'while', 'with'
+ ]
+ keyopts = [
+ '!=','#','&','&&','\(','\)','\*','\+',',','-',
+ '-\.','->','\.','\.\.',':','::',':=',':>',';',';;','<',
+ '<-','=','>','>]','>}','\?','\?\?','\[','\[<','\[>','\[\|',
+ ']','_','`','{','{<','\|','\|]','}','~'
+ ]
+
+ operators = r'[!$%&*+\./:<=>?@^|~-]'
+ prefix_syms = r'[!?~]'
+ infix_syms = r'[=<>@^|&+\*/$%-]'
+
+ tokens = {
+ 'escape-sequence': [
+ (r'\\[\"\'ntbr]', String.Escape),
+ (r'\\[0-9]{3}', String.Escape),
+ (r'\\x[0-9a-fA-F]{2}', String.Escape),
+ ],
+ 'root': [
+ (r'\s+', Text),
+ (r'\(\*', Comment, 'comment'),
+ (r'\b(%s)\b' % '|'.join(keywords), Keyword.Reserved),
+ (r'(%s)' % '|'.join(keyopts), Keyword),
+ (r'false|true|\(\)|\[\]', Name.Constant),
+ (r'(%s|%s)?%s' % (infix_syms, prefix_syms, operators), Operator),
+
+ (r"[^\W\d][\w']*", Name),
+
+ (r'\d[\d_]*', Number.Integer),
+ (r'0[xX][\da-fA-F][\da-fA-F_]*', Number.Hex),
+ (r'0[oO][0-7][0-7_]*', Number.Oct),
+ (r'0[bB][01][01_]*', Number),
+ (r'-?\d[\d_]*(.[\d_]*)?([eE][+\-]?\d[\d_]*)', Number.Float),
+
+ (r"'(?:(\\[\\\"'ntbr ])|(\\[0-9]{3})|(\\x[0-9a-fA-F]{2}))'",
+ String.Char),
+ (r"'.'", String.Char),
+ (r"'", Keyword), # a stray quote is another syntax element
+
+ (r'"', String.Double, 'string'),
+
+ (r'[~?][a-z][\w\']*:', Name.Variable),
+ ],
+ 'comment': [
+ (r'[^(*)]', Comment),
+ (r'\(\*', Comment, '#push'),
+ (r'\*\)', Comment, '#pop'),
+ (r'[(*)]', Comment),
+ ],
+ 'string': [
+ (r'[^\\"]', String.Double),
+ include('escape-sequence'),
+ (r'\\\n', String.Double),
+ (r'"', String.Double, '#pop'),
+ ]
+ }