Created
April 2, 2010 22:49
-
-
Save eventualbuddha/353829 to your computer and use it in GitHub Desktop.
pygments lexer for fish scripts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from pygments import highlight | |
from pygments.lexer import RegexLexer, bygroups, include | |
from pygments.token import * | |
from pygments.formatters import * | |
class FishLexer(RegexLexer): | |
name = 'Fish' | |
aliases = 'fish' | |
filenames = ['*.fish'] | |
mimetypes = ['application/x-fish', 'application/x-shellscript'] | |
tokens = { | |
'root': [ | |
include('basic'), | |
(r'\(', Keyword, 'paren'), | |
include('data'), | |
], | |
'basic': [ | |
(r'\b(if|else|while|end|return|function|switch|case|continue|not|and|or|break|for)\s*\b', | |
Keyword), | |
(r'\b(alias|begin|bg|bind|block|breakpoint|break|builtin|cd|commandline|' | |
r'command|complete|contains|count|dirh|dirs|echo|emit|eval|exec|exit|fg|' | |
r'fishd|fish_indent|fish_pager|fish_prompt|fish|funced|funcsave|' | |
r'functions|help|isatty|jobs|math|mimedb|nextd|not|open|popd|prevd|' | |
r'psub|pushd|random|read|set_color|set|.|status|trap|type|ulimit|umask|' | |
r'vared)\s*\b', | |
Name.Builtin), | |
(r'#.*\n', Comment), | |
(r'\\[\w\W]', String.Escape), | |
(r'[\[\]{}=]+', Operator), | |
(r'<<\s*(\'?)\\?(\w+)[\w\W]+?\2', String), | |
], | |
'data': [ | |
(r'"(\\\\|\\[0-7]+|\\.|[^"])*"', String.Double), | |
(r"'(\\\\|\\[0-7]+|\\.|[^'])*'", String.Single), | |
(r'\s+', Text), | |
(r'[^=\s\n\[\]{}()$"\'`\\]+', Text), | |
(r'\d+(?= |\Z)', Number), | |
(r'\$#?(\w+|.)', Name.Variable), | |
], | |
'paren': [ | |
(r'\)', Keyword, '#pop'), | |
include('root'), | |
], | |
} | |
code = sys.stdin.read() | |
# for token in FishLexer().get_tokens(code): | |
# print token | |
print highlight(code, FishLexer(), TerminalFormatter()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment