Commits

gbrandl  committed eb4e684

Add Google Go language lexer (#448).

  • Participants
  • Parent commits 3a314e7
  • Branches trunk

Comments (0)

Files changed (3)

 
 - Lexers added:
 
+  * Go
   * CMake
   * OOC
 

File pygments/lexers/_mapping.py

     'GenshiTextLexer': ('pygments.lexers.templates', 'Genshi Text', ('genshitext',), (), ('application/x-genshi-text', 'text/x-genshi')),
     'GettextLexer': ('pygments.lexers.text', 'Gettext Catalog', ('pot', 'po'), ('*.pot', '*.po'), ('application/x-gettext', 'text/x-gettext', 'text/gettext')),
     'GnuplotLexer': ('pygments.lexers.other', 'Gnuplot', ('gnuplot',), ('*.plot', '*.plt'), ('text/x-gnuplot',)),
+    'GoLexer': ('pygments.lexers.compiled', 'Go', ('go',), ('*.go',), ('text/x-gosrc',)),
     'GroffLexer': ('pygments.lexers.text', 'Groff', ('groff', 'nroff', 'man'), ('*.[1234567]', '*.man'), ('application/x-troff', 'text/troff')),
     'HaskellLexer': ('pygments.lexers.functional', 'Haskell', ('haskell', 'hs'), ('*.hs',), ('text/x-haskell',)),
     'HtmlDjangoLexer': ('pygments.lexers.templates', 'HTML+Django/Jinja', ('html+django', 'html+jinja'), (), ('text/html+django', 'text/html+jinja')),

File pygments/lexers/compiled.py

 __all__ = ['CLexer', 'CppLexer', 'DLexer', 'DelphiLexer', 'JavaLexer',
            'ScalaLexer', 'DylanLexer', 'OcamlLexer', 'ObjectiveCLexer',
            'FortranLexer', 'GLShaderLexer', 'PrologLexer', 'CythonLexer',
-           'ValaLexer', 'OocLexer']
+           'ValaLexer', 'OocLexer', 'GoLexer']
 
 
 class CLexer(RegexLexer):
 class DLexer(RegexLexer):
     """
     For D source.
+
+    *New in Pygments 1.2.*
     """
     name = 'D'
     filenames = ['*.d', '*.di']
             # -- DoubleQuotedString
             (r'"(\\\\|\\"|[^"])*"[cwd]?', String),
             # -- EscapeSequence
-            (r"""\\(['"?\\abfnrtv]|x[0-9a-fA-F]{2}|[0-7]{1,3}"""
-             r"""|u[0-9a-fA-F]{4}|U[0-9a-fA-F]{8}|&\w+;)""",
+            (r"\\(['\"?\\abfnrtv]|x[0-9a-fA-F]{2}|[0-7]{1,3}"
+             r"|u[0-9a-fA-F]{4}|U[0-9a-fA-F]{8}|&\w+;)",
              String
             ),
             # -- HexString
             (r'[;\n]', Text, '#pop'),
         ],
     }
+
+
+class GoLexer(RegexLexer):
+    """
+    For `Go <http://golang.org>`_ source.
+    """
+    name = 'Go'
+    filenames = ['*.go']
+    aliases = ['go']
+    mimetypes = ['text/x-gosrc']
+
+    tokens = {
+        'root': [
+            (r'\n', Text),
+            (r'\s+', Text),
+            (r'\\\n', Text), # line continuations
+            (r'//(.*?)\n', Comment.Single),
+            (r'/(\\\n)?[*](.|\n)*?[*](\\\n)?/', Comment.Multiline),
+            (r'(break|default|func|interface|select'
+             r'|case|defer|go|map|struct'
+             r'|chan|else|goto|package|switch'
+             r'|const|fallthrough|if|range|type'
+             r'|continue|for|import|return|var)\b', Keyword
+            ),
+            # It seems the builtin types aren't actually keywords.
+            (r'(uint8|uint16|uint32|uint64'
+             r'|int8|int16|int32|int64'
+             r'|float32|float64|byte'
+             r'|uint|int|float|uintptr'
+             r'|string|close|closed|len|cap|new|make)\b', Name.Builtin
+            ),
+            # float_lit
+            (r'\d+(\.\d+[eE][+\-]?\d+|'
+             r'\.\d*|[eE][+\-]?\d+)', Number.Float),
+            (r'\.\d+([eE][+\-]?\d+)?', Number.Float),
+            # int_lit
+            # -- octal_lit
+            (r'0[0-7]+', Number.Oct),
+            # -- hex_lit
+            (r'0[xX][0-9a-fA-F]+', Number.Hex),
+            # -- decimal_lit
+            (r'(0|[1-9][0-9]*)', Number.Integer),
+            # char_lit
+            (r"""'(\\['"\\abfnrtv]|\\x[0-9a-fA-F]{2}|\\[0-7]{1,3}"""
+             r"""|\\u[0-9a-fA-F]{4}|\\U[0-9a-fA-F]{8}|[^\\])'""",
+             String.Char
+            ),
+            # StringLiteral
+            # -- raw_string_lit
+            (r'`[^`]*`', String),
+            # -- interpreted_string_lit
+            (r'"(\\\\|\\"|[^"])*"', String),
+            # Tokens
+            (r'(<<=|>>=|<<|>>|<=|>=|&\^=|&\^|\+=|-=|\*=|/=|%=|&=|\|=|&&|\|\|'
+             r'|<-|\+\+|--|==|!=|:=|\.\.\.)|[+\-*/%&|^<>=!()\[\]{}.,;:]',
+             Punctuation
+            ),
+            # identifier
+            (r'[a-zA-Z_]\w*', Name),
+        ]
+    }