Anonymous avatar Anonymous committed 62c570e

[svn] Add OCaml lexer.

Comments (0)

Files changed (4)

 ----------------------------
 (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.  
   * IPython sessions
   * HTML with special formatting?
   * LaTeX special formatting?
-  * OCaml
   * Nemerle
   * Assembler
   * Objective C

pygments/lexers/_mapping.py

     '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',)),

pygments/lexers/compiled.py

      Error
 
 
-__all__ = ['CLexer', 'CppLexer', 'DelphiLexer', 'JavaLexer', 'DylanLexer']
+__all__ = ['CLexer', 'CppLexer', 'DelphiLexer', 'JavaLexer', 'DylanLexer',
+           'OcamlLexer']
 
 
 class CLexer(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'),
+        ]
+    }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.