Skip to content

Instantly share code, notes, and snippets.

@eventualbuddha
Created April 2, 2010 22:49
Show Gist options
  • Save eventualbuddha/353829 to your computer and use it in GitHub Desktop.
Save eventualbuddha/353829 to your computer and use it in GitHub Desktop.
pygments lexer for fish scripts
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